TYPO3 CMS  TYPO3_7-6
Query.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
24 class Query implements QueryInterface
25 {
29  const JCR_JOIN_TYPE_INNER = '{http://www.jcp.org/jcr/1.0}joinTypeInner';
30 
34  const JCR_JOIN_TYPE_LEFT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeLeftOuter';
35 
39  const JCR_JOIN_TYPE_RIGHT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeRightOuter';
40 
44  const CHARSET = 'utf-8';
45 
49  protected $type;
50 
54  protected $objectManager;
55 
59  protected $dataMapper;
60 
66 
70  protected $qomFactory;
71 
75  protected $source;
76 
80  protected $constraint;
81 
85  protected $statement;
86 
90  protected $orderings = [];
91 
95  protected $limit;
96 
100  protected $offset;
101 
107  protected $querySettings;
108 
112  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
113  {
114  $this->objectManager = $objectManager;
115  }
116 
120  public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
121  {
122  $this->dataMapper = $dataMapper;
123  }
124 
129  {
130  $this->persistenceManager = $persistenceManager;
131  }
132 
136  public function injectQomFactory(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory $qomFactory)
137  {
138  $this->qomFactory = $qomFactory;
139  }
140 
146  public function __construct($type)
147  {
148  $this->type = $type;
149  }
150 
160  {
161  $this->querySettings = $querySettings;
162  }
163 
171  public function getQuerySettings()
172  {
173  if (!$this->querySettings instanceof QuerySettingsInterface) {
174  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception('Tried to get the query settings without seting them before.', 1248689115);
175  }
176  return $this->querySettings;
177  }
178 
185  public function getType()
186  {
187  return $this->type;
188  }
189 
195  public function setSource(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source)
196  {
197  $this->source = $source;
198  }
199 
206  protected function getSelectorName()
207  {
208  $source = $this->getSource();
209  if ($source instanceof \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SelectorInterface) {
210  return $source->getSelectorName();
211  } else {
212  return '';
213  }
214  }
215 
221  public function getSource()
222  {
223  if ($this->source === null) {
224  $this->source = $this->qomFactory->selector($this->getType(), $this->dataMapper->convertClassNameToTableName($this->getType()));
225  }
226  return $this->source;
227  }
228 
236  public function execute($returnRawQueryResult = false)
237  {
238  if ($returnRawQueryResult) {
239  return $this->persistenceManager->getObjectDataByQuery($this);
240  } else {
241  return $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class, $this);
242  }
243  }
244 
257  public function setOrderings(array $orderings)
258  {
259  $this->orderings = $orderings;
260  return $this;
261  }
262 
273  public function getOrderings()
274  {
275  return $this->orderings;
276  }
277 
287  public function setLimit($limit)
288  {
289  if (!is_int($limit) || $limit < 1) {
290  throw new \InvalidArgumentException('The limit must be an integer >= 1', 1245071870);
291  }
292  $this->limit = $limit;
293  return $this;
294  }
295 
303  public function unsetLimit()
304  {
305  unset($this->limit);
306  return $this;
307  }
308 
315  public function getLimit()
316  {
317  return $this->limit;
318  }
319 
329  public function setOffset($offset)
330  {
331  if (!is_int($offset) || $offset < 0) {
332  throw new \InvalidArgumentException('The offset must be a positive integer', 1245071872);
333  }
334  $this->offset = $offset;
335  return $this;
336  }
337 
344  public function getOffset()
345  {
346  return $this->offset;
347  }
348 
357  public function matching($constraint)
358  {
359  $this->constraint = $constraint;
360  return $this;
361  }
362 
371  public function statement($statement, array $parameters = [])
372  {
373  $this->statement = $this->qomFactory->statement($statement, $parameters);
374  return $this;
375  }
376 
382  public function getStatement()
383  {
384  return $this->statement;
385  }
386 
393  public function getConstraint()
394  {
395  return $this->constraint;
396  }
397 
407  public function logicalAnd($constraint1)
408  {
409  if (is_array($constraint1)) {
410  $resultingConstraint = array_shift($constraint1);
411  $constraints = $constraint1;
412  } else {
413  $constraints = func_get_args();
414  $resultingConstraint = array_shift($constraints);
415  }
416  if ($resultingConstraint === null) {
417  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidNumberOfConstraintsException('There must be at least one constraint or a non-empty array of constraints given.', 1268056288);
418  }
419  foreach ($constraints as $constraint) {
420  $resultingConstraint = $this->qomFactory->_and($resultingConstraint, $constraint);
421  }
422  return $resultingConstraint;
423  }
424 
433  public function logicalOr($constraint1)
434  {
435  if (is_array($constraint1)) {
436  $resultingConstraint = array_shift($constraint1);
437  $constraints = $constraint1;
438  } else {
439  $constraints = func_get_args();
440  $resultingConstraint = array_shift($constraints);
441  }
442  if ($resultingConstraint === null) {
443  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidNumberOfConstraintsException('There must be at least one constraint or a non-empty array of constraints given.', 1268056289);
444  }
445  foreach ($constraints as $constraint) {
446  $resultingConstraint = $this->qomFactory->_or($resultingConstraint, $constraint);
447  }
448  return $resultingConstraint;
449  }
450 
459  public function logicalNot(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint)
460  {
461  return $this->qomFactory->not($constraint);
462  }
463 
473  public function equals($propertyName, $operand, $caseSensitive = true)
474  {
475  if (is_object($operand) || $caseSensitive) {
476  $comparison = $this->qomFactory->comparison(
477  $this->qomFactory->propertyValue($propertyName, $this->getSelectorName()),
479  $operand
480  );
481  } else {
482  $comparison = $this->qomFactory->comparison(
483  $this->qomFactory->lowerCase($this->qomFactory->propertyValue($propertyName, $this->getSelectorName())),
485  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Charset\CharsetConverter::class)->conv_case(\TYPO3\CMS\Extbase\Persistence\Generic\Query::CHARSET, $operand, 'toLower')
486  );
487  }
488  return $comparison;
489  }
490 
500  public function like($propertyName, $operand, $caseSensitive = true)
501  {
502  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LIKE, $operand);
503  }
504 
514  public function contains($propertyName, $operand)
515  {
516  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_CONTAINS, $operand);
517  }
518 
529  public function in($propertyName, $operand)
530  {
531  if (!\TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::isValidTypeForMultiValueComparison($operand)) {
532  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException('The "in" operator must be given a multivalued operand (array, ArrayAccess, Traversable).', 1264678095);
533  }
534  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_IN, $operand);
535  }
536 
545  public function lessThan($propertyName, $operand)
546  {
547  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LESS_THAN, $operand);
548  }
549 
558  public function lessThanOrEqual($propertyName, $operand)
559  {
560  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO, $operand);
561  }
562 
571  public function greaterThan($propertyName, $operand)
572  {
573  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_GREATER_THAN, $operand);
574  }
575 
584  public function greaterThanOrEqual($propertyName, $operand)
585  {
586  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $operand);
587  }
588 
599  public function between($propertyName, $operandLower, $operandUpper)
600  {
601  return $this->logicalAnd(
602  $this->greaterThanOrEqual($propertyName, $operandLower),
603  $this->lessThanOrEqual($propertyName, $operandUpper)
604  );
605  }
606 
610  public function __wakeup()
611  {
612  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
613  $this->persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
614  $this->dataMapper = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
615  $this->qomFactory = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory::class);
616  }
617 
621  public function __sleep()
622  {
623  return ['type', 'source', 'constraint', 'statement', 'orderings', 'limit', 'offset', 'querySettings'];
624  }
625 
632  public function count()
633  {
634  return $this->execute()->count();
635  }
636 
647  public function isEmpty($propertyName)
648  {
649  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
650  }
651 }
setSource(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source)
Definition: Query.php:195
between($propertyName, $operandLower, $operandUpper)
Definition: Query.php:599
equals($propertyName, $operand, $caseSensitive=true)
Definition: Query.php:473
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: Query.php:112
greaterThan($propertyName, $operand)
Definition: Query.php:571
execute($returnRawQueryResult=false)
Definition: Query.php:236
like($propertyName, $operand, $caseSensitive=true)
Definition: Query.php:500
logicalNot(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint)
Definition: Query.php:459
injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
Definition: Query.php:120
lessThan($propertyName, $operand)
Definition: Query.php:545
injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
Definition: Query.php:128
greaterThanOrEqual($propertyName, $operand)
Definition: Query.php:584
contains($propertyName, $operand)
Definition: Query.php:514
statement($statement, array $parameters=[])
Definition: Query.php:371
setQuerySettings(QuerySettingsInterface $querySettings)
Definition: Query.php:159
injectQomFactory(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory $qomFactory)
Definition: Query.php:136
lessThanOrEqual($propertyName, $operand)
Definition: Query.php:558