TYPO3 CMS  TYPO3_8-7
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 
19 
25 class Query implements QueryInterface
26 {
30  const JCR_JOIN_TYPE_INNER = '{http://www.jcp.org/jcr/1.0}joinTypeInner';
31 
35  const JCR_JOIN_TYPE_LEFT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeLeftOuter';
36 
40  const JCR_JOIN_TYPE_RIGHT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeRightOuter';
41 
45  const CHARSET = 'utf-8';
46 
50  protected $type;
51 
55  protected $objectManager;
56 
60  protected $dataMapper;
61 
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 
159  {
160  $this->querySettings = $querySettings;
161  }
162 
170  public function getQuerySettings()
171  {
172  if (!$this->querySettings instanceof QuerySettingsInterface) {
173  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception('Tried to get the query settings without seting them before.', 1248689115);
174  }
175  return $this->querySettings;
176  }
177 
184  public function getType()
185  {
186  return $this->type;
187  }
188 
194  public function setSource(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source)
195  {
196  $this->source = $source;
197  }
198 
205  protected function getSelectorName()
206  {
207  $source = $this->getSource();
208  if ($source instanceof \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SelectorInterface) {
209  return $source->getSelectorName();
210  }
211  return '';
212  }
213 
219  public function getSource()
220  {
221  if ($this->source === null) {
222  $this->source = $this->qomFactory->selector($this->getType(), $this->dataMapper->convertClassNameToTableName($this->getType()));
223  }
224  return $this->source;
225  }
226 
234  public function execute($returnRawQueryResult = false)
235  {
236  if ($returnRawQueryResult) {
237  return $this->persistenceManager->getObjectDataByQuery($this);
238  }
239  return $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class, $this);
240  }
241 
254  public function setOrderings(array $orderings)
255  {
256  $this->orderings = $orderings;
257  return $this;
258  }
259 
270  public function getOrderings()
271  {
272  return $this->orderings;
273  }
274 
284  public function setLimit($limit)
285  {
286  if (!is_int($limit) || $limit < 1) {
287  throw new \InvalidArgumentException('The limit must be an integer >= 1', 1245071870);
288  }
289  $this->limit = $limit;
290  return $this;
291  }
292 
300  public function unsetLimit()
301  {
302  unset($this->limit);
303  return $this;
304  }
305 
312  public function getLimit()
313  {
314  return $this->limit;
315  }
316 
326  public function setOffset($offset)
327  {
328  if (!is_int($offset) || $offset < 0) {
329  throw new \InvalidArgumentException('The offset must be a positive integer', 1245071872);
330  }
331  $this->offset = $offset;
332  return $this;
333  }
334 
341  public function getOffset()
342  {
343  return $this->offset;
344  }
345 
354  public function matching($constraint)
355  {
356  $this->constraint = $constraint;
357  return $this;
358  }
359 
368  public function statement($statement, array $parameters = [])
369  {
370  $this->statement = $this->qomFactory->statement($statement, $parameters);
371  return $this;
372  }
373 
379  public function getStatement()
380  {
381  return $this->statement;
382  }
383 
390  public function getConstraint()
391  {
392  return $this->constraint;
393  }
394 
404  public function logicalAnd($constraint1)
405  {
406  if (is_array($constraint1)) {
407  $resultingConstraint = array_shift($constraint1);
408  $constraints = $constraint1;
409  } else {
410  $constraints = func_get_args();
411  $resultingConstraint = array_shift($constraints);
412  }
413  if ($resultingConstraint === null) {
414  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);
415  }
416  foreach ($constraints as $constraint) {
417  $resultingConstraint = $this->qomFactory->_and($resultingConstraint, $constraint);
418  }
419  return $resultingConstraint;
420  }
421 
430  public function logicalOr($constraint1)
431  {
432  if (is_array($constraint1)) {
433  $resultingConstraint = array_shift($constraint1);
434  $constraints = $constraint1;
435  } else {
436  $constraints = func_get_args();
437  $resultingConstraint = array_shift($constraints);
438  }
439  if ($resultingConstraint === null) {
440  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);
441  }
442  foreach ($constraints as $constraint) {
443  $resultingConstraint = $this->qomFactory->_or($resultingConstraint, $constraint);
444  }
445  return $resultingConstraint;
446  }
447 
456  public function logicalNot(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint)
457  {
458  return $this->qomFactory->not($constraint);
459  }
460 
470  public function equals($propertyName, $operand, $caseSensitive = true)
471  {
472  if (is_object($operand) || $caseSensitive) {
473  $comparison = $this->qomFactory->comparison(
474  $this->qomFactory->propertyValue($propertyName, $this->getSelectorName()),
476  $operand
477  );
478  } else {
479  $comparison = $this->qomFactory->comparison(
480  $this->qomFactory->lowerCase($this->qomFactory->propertyValue($propertyName, $this->getSelectorName())),
482  mb_strtolower($operand, \TYPO3\CMS\Extbase\Persistence\Generic\Query::CHARSET)
483  );
484  }
485  return $comparison;
486  }
487 
497  public function like($propertyName, $operand, $caseSensitive = null)
498  {
499  if ($caseSensitive !== null) {
501  'The parameter $caseSensitive for the Extbase like criterion has been deprecated.' .
502  'A case sensitive comparison cannot be reliably done as it is dependent on Database ' .
503  'Server settings. For MySQL switch the field to a case sensitive collation to achieve ' .
504  'the desired result.'
505  );
506  }
507 
508  return $this->qomFactory->comparison(
509  $this->qomFactory->propertyValue($propertyName, $this->getSelectorName()),
511  $operand
512  );
513  }
514 
524  public function contains($propertyName, $operand)
525  {
526  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_CONTAINS, $operand);
527  }
528 
539  public function in($propertyName, $operand)
540  {
541  if (!\TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::isValidTypeForMultiValueComparison($operand)) {
542  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException('The "in" operator must be given a multivalued operand (array, ArrayAccess, Traversable).', 1264678095);
543  }
544  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_IN, $operand);
545  }
546 
555  public function lessThan($propertyName, $operand)
556  {
557  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LESS_THAN, $operand);
558  }
559 
568  public function lessThanOrEqual($propertyName, $operand)
569  {
570  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO, $operand);
571  }
572 
581  public function greaterThan($propertyName, $operand)
582  {
583  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_GREATER_THAN, $operand);
584  }
585 
594  public function greaterThanOrEqual($propertyName, $operand)
595  {
596  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $operand);
597  }
598 
609  public function between($propertyName, $operandLower, $operandUpper)
610  {
611  return $this->logicalAnd(
612  $this->greaterThanOrEqual($propertyName, $operandLower),
613  $this->lessThanOrEqual($propertyName, $operandUpper)
614  );
615  }
616 
619  public function __wakeup()
620  {
621  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
622  $this->persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
623  $this->dataMapper = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
624  $this->qomFactory = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory::class);
625  }
626 
630  public function __sleep()
631  {
632  return ['type', 'source', 'constraint', 'statement', 'orderings', 'limit', 'offset', 'querySettings'];
633  }
634 
641  public function count()
642  {
643  return $this->execute()->count();
644  }
645 
656  public function isEmpty($propertyName)
657  {
658  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__, 1476122265);
659  }
660 }
setSource(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source)
Definition: Query.php:194
like($propertyName, $operand, $caseSensitive=null)
Definition: Query.php:497
between($propertyName, $operandLower, $operandUpper)
Definition: Query.php:609
equals($propertyName, $operand, $caseSensitive=true)
Definition: Query.php:470
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: Query.php:112
greaterThan($propertyName, $operand)
Definition: Query.php:581
execute($returnRawQueryResult=false)
Definition: Query.php:234
logicalNot(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint)
Definition: Query.php:456
static makeInstance($className,... $constructorArguments)
injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
Definition: Query.php:120
lessThan($propertyName, $operand)
Definition: Query.php:555
injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
Definition: Query.php:128
greaterThanOrEqual($propertyName, $operand)
Definition: Query.php:594
contains($propertyName, $operand)
Definition: Query.php:524
statement($statement, array $parameters=[])
Definition: Query.php:368
setQuerySettings(QuerySettingsInterface $querySettings)
Definition: Query.php:158
injectQomFactory(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory $qomFactory)
Definition: Query.php:136
lessThanOrEqual($propertyName, $operand)
Definition: Query.php:568