‪TYPO3CMS  ‪main
Query.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Container\ContainerInterface;
32 
40 class ‪Query implements ‪QueryInterface
41 {
45  public const ‪JCR_JOIN_TYPE_INNER = '{http://www.jcp.org/jcr/1.0}joinTypeInner';
46 
50  public const ‪JCR_JOIN_TYPE_LEFT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeLeftOuter';
51 
55  public const ‪JCR_JOIN_TYPE_RIGHT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeRightOuter';
56 
60  public const ‪CHARSET = 'utf-8';
61 
66  protected ‪$type;
67 
71  protected ContainerInterface ‪$container;
72 
76  protected ‪$source;
77 
81  protected ‪$constraint;
82 
86  protected ‪$statement;
87 
91  protected $orderings = [];
92 
96  protected $limit;
97 
101  protected $offset;
102 
108  protected $querySettings;
109 
114  protected $parentQuery;
115 
116  public function __construct(
120  ContainerInterface ‪$container
121  ) {
124  $this->‪qomFactory = ‪$qomFactory;
125  $this->‪container = ‪$container;
126  }
127 
131  public function ‪setType(string ‪$type): void
132  {
133  $this->type = ‪$type;
134  }
135 
139  public function ‪getParentQuery(): ?‪QueryInterface
140  {
141  return $this->parentQuery;
142  }
143 
147  public function ‪setParentQuery(?QueryInterface $parentQuery): void
148  {
149  $this->parentQuery = $parentQuery;
150  }
151 
158  public function ‪setQuerySettings(‪QuerySettingsInterface $querySettings)
159  {
160  $this->querySettings = $querySettings;
161  }
162 
169  public function ‪getQuerySettings()
170  {
171  if (!$this->querySettings instanceof ‪QuerySettingsInterface) {
172  throw new ‪Exception('Tried to get the query settings without setting them before.', 1248689115);
173  }
174  return $this->querySettings;
175  }
176 
183  public function ‪getType()
184  {
185  return ‪$this->type;
186  }
187 
192  {
193  $this->source = ‪$source;
194  }
195 
202  protected function ‪getSelectorName()
203  {
204  ‪$source = $this->‪getSource();
205  if (‪$source instanceof ‪SelectorInterface) {
206  return ‪$source->getSelectorName();
207  }
208  return '';
209  }
210 
216  public function ‪getSource()
217  {
218  if ($this->source === null) {
219  $this->source = $this->‪qomFactory->selector($this->‪getType(), $this->‪dataMapFactory->buildDataMap($this->getType())->getTableName());
220  }
221  return ‪$this->source;
222  }
223 
231  public function ‪execute($returnRawQueryResult = false)
232  {
233  if ($returnRawQueryResult) {
234  return $this->‪persistenceManager->getObjectDataByQuery($this);
235  }
237  $queryResult = $this->‪container->get(QueryResultInterface::class);
238  $queryResult->setQuery($this);
239  return $queryResult;
240  }
241 
254  public function ‪setOrderings(array $orderings)
255  {
256  $this->‪orderings = $orderings;
257  return $this;
258  }
259 
269  public function ‪getOrderings()
270  {
271  return $this->orderings;
272  }
273 
283  public function ‪setLimit($limit)
284  {
285  if (!is_int($limit) || $limit < 1) {
286  throw new \InvalidArgumentException('The limit must be an integer >= 1', 1245071870);
287  }
288  $this->limit = $limit;
289  return $this;
290  }
291 
298  public function ‪unsetLimit()
299  {
300  $this->limit = null;
301  return $this;
302  }
303 
309  public function ‪getLimit()
310  {
311  return $this->limit;
312  }
313 
323  public function ‪setOffset($offset)
324  {
325  if (!is_int($offset) || $offset < 0) {
326  throw new \InvalidArgumentException('The offset must be a positive integer', 1245071872);
327  }
328  $this->offset = $offset;
329  return $this;
330  }
331 
337  public function ‪getOffset()
338  {
339  return $this->offset;
340  }
341 
350  public function ‪matching(‪$constraint)
351  {
352  $this->constraint = ‪$constraint;
353  return $this;
354  }
355 
364  public function ‪statement(‪$statement, array $parameters = [])
365  {
366  $this->‪statement = $this->‪qomFactory->statement(‪$statement, $parameters);
367  return $this;
368  }
369 
375  public function ‪getStatement()
376  {
377  return ‪$this->statement;
378  }
379 
385  public function ‪getConstraint()
386  {
387  return ‪$this->constraint;
388  }
389 
394  public function ‪logicalAnd(‪ConstraintInterface ...$constraints): ‪AndInterface
395  {
396  $constraints = array_filter($constraints, fn(mixed ‪$constraint): bool => ‪$constraint instanceof ‪ConstraintInterface);
397  switch (‪count($constraints)) {
398  case 0:
399  $alwaysTrue = $this->‪greaterThan('uid', 0);
400  return $this->‪qomFactory->_and($alwaysTrue, $alwaysTrue);
401  case 1:
402  $alwaysTrue = $this->‪greaterThan('uid', 0);
403  return $this->‪qomFactory->_and(array_shift($constraints), $alwaysTrue);
404  default:
405  $resultingConstraint = $this->‪qomFactory->_and(array_shift($constraints), array_shift($constraints));
406  foreach ($constraints as $furtherConstraint) {
407  $resultingConstraint = $this->‪qomFactory->_and($resultingConstraint, $furtherConstraint);
408  }
409  return $resultingConstraint;
410  }
411  }
412 
417  public function ‪logicalOr(ConstraintInterface ...$constraints): OrInterface
418  {
419  $constraints = array_filter($constraints, fn(mixed ‪$constraint): bool => ‪$constraint instanceof ConstraintInterface);
420  switch (‪count($constraints)) {
421  case 0:
422  $alwaysFalse = $this->‪equals('uid', 0);
423  return $this->‪qomFactory->_or($alwaysFalse, $alwaysFalse);
424  case 1:
425  $alwaysFalse = $this->‪equals('uid', 0);
426  return $this->‪qomFactory->_or(array_shift($constraints), $alwaysFalse);
427  default:
428  $resultingConstraint = $this->‪qomFactory->_or(array_shift($constraints), array_shift($constraints));
429  foreach ($constraints as $furtherConstraint) {
430  $resultingConstraint = $this->‪qomFactory->_or($resultingConstraint, $furtherConstraint);
431  }
432  return $resultingConstraint;
433  }
434  }
435 
444  {
445  return $this->‪qomFactory->not($constraint);
446  }
447 
456  public function ‪equals($propertyName, $operand, $caseSensitive = true)
457  {
458  if (is_object($operand) || $caseSensitive) {
459  $comparison = $this->‪qomFactory->comparison(
460  $this->‪qomFactory->propertyValue($propertyName, $this->getSelectorName()),
462  $operand
463  );
464  } else {
465  $comparison = $this->‪qomFactory->comparison(
466  $this->‪qomFactory->lowerCase($this->qomFactory->propertyValue($propertyName, $this->getSelectorName())),
468  mb_strtolower($operand, \‪TYPO3\CMS\‪Extbase\Persistence\Generic\‪Query::CHARSET)
469  );
470  }
471  return $comparison;
472  }
473 
481  public function ‪like($propertyName, $operand)
482  {
483  return $this->‪qomFactory->comparison(
484  $this->‪qomFactory->propertyValue($propertyName, $this->getSelectorName()),
486  $operand
487  );
488  }
489 
498  public function ‪contains($propertyName, $operand)
499  {
500  return $this->‪qomFactory->comparison($this->‪qomFactory->propertyValue($propertyName, $this->getSelectorName()), ‪QueryInterface::OPERATOR_CONTAINS, $operand);
501  }
502 
512  public function ‪in($propertyName, $operand)
513  {
515  throw new ‪UnexpectedTypeException('The "in" operator must be given a multivalued operand (array, ArrayAccess, Traversable).', 1264678095);
516  }
517  return $this->‪qomFactory->comparison($this->‪qomFactory->propertyValue($propertyName, $this->getSelectorName()), ‪QueryInterface::OPERATOR_IN, $operand);
518  }
519 
527  public function ‪lessThan($propertyName, $operand)
528  {
529  return $this->‪qomFactory->comparison($this->‪qomFactory->propertyValue($propertyName, $this->getSelectorName()), ‪QueryInterface::OPERATOR_LESS_THAN, $operand);
530  }
531 
539  public function ‪lessThanOrEqual($propertyName, $operand)
540  {
541  return $this->‪qomFactory->comparison($this->‪qomFactory->propertyValue($propertyName, $this->getSelectorName()), ‪QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO, $operand);
542  }
543 
551  public function ‪greaterThan($propertyName, $operand)
552  {
553  return $this->‪qomFactory->comparison($this->‪qomFactory->propertyValue($propertyName, $this->getSelectorName()), ‪QueryInterface::OPERATOR_GREATER_THAN, $operand);
554  }
555 
563  public function ‪greaterThanOrEqual($propertyName, $operand)
564  {
565  return $this->‪qomFactory->comparison($this->‪qomFactory->propertyValue($propertyName, $this->getSelectorName()), ‪QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $operand);
566  }
567 
576  public function ‪between($propertyName, $operandLower, $operandUpper)
577  {
578  return $this->‪logicalAnd(
579  $this->‪greaterThanOrEqual($propertyName, $operandLower),
580  $this->‪lessThanOrEqual($propertyName, $operandUpper)
581  );
582  }
583 
587  public function ‪__wakeup()
588  {
589  $this->‪persistenceManager = GeneralUtility::makeInstance(PersistenceManagerInterface::class);
590  $this->‪dataMapFactory = GeneralUtility::makeInstance(DataMapFactory::class);
591  $this->‪qomFactory = GeneralUtility::makeInstance(QueryObjectModelFactory::class);
592  }
593 
598  public function ‪__sleep()
599  {
600  return ['type', 'source', 'constraint', 'statement', 'orderings', 'limit', 'offset', 'querySettings'];
601  }
602 
608  public function ‪count()
609  {
610  return $this->‪execute()->count();
611  }
612 }
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\logicalOr
‪logicalOr(ConstraintInterface ... $constraints)
Definition: Query.php:408
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\OPERATOR_LIKE
‪const OPERATOR_LIKE
Definition: QueryInterface.php:74
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\setOffset
‪QueryInterface setOffset($offset)
Definition: Query.php:314
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\OPERATOR_GREATER_THAN_OR_EQUAL_TO
‪const OPERATOR_GREATER_THAN_OR_EQUAL_TO
Definition: QueryInterface.php:69
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\getStatement
‪TYPO3 CMS Extbase Persistence Generic Qom Statement getStatement()
Definition: Query.php:366
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:24
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:18
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\in
‪TYPO3 CMS Extbase Persistence Generic Qom ComparisonInterface in($propertyName, $operand)
Definition: Query.php:503
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\setLimit
‪QueryInterface setLimit($limit)
Definition: Query.php:274
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:30
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\getOffset
‪int getOffset()
Definition: Query.php:328
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\persistenceManager
‪$this persistenceManager
Definition: Query.php:114
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\lessThan
‪TYPO3 CMS Extbase Persistence Generic Qom ComparisonInterface lessThan($propertyName, $operand)
Definition: Query.php:518
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\OPERATOR_GREATER_THAN
‪const OPERATOR_GREATER_THAN
Definition: QueryInterface.php:64
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\$qomFactory
‪QueryObjectModelFactory $qomFactory
Definition: Query.php:69
‪TYPO3
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\like
‪TYPO3 CMS Extbase Persistence Generic Qom ComparisonInterface like($propertyName, $operand)
Definition: Query.php:472
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\OPERATOR_IN
‪const OPERATOR_IN
Definition: QueryInterface.php:84
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
Definition: QueryObjectModelFactory.php:30
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface
Definition: SourceInterface.php:23
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\logicalNot
‪TYPO3 CMS Extbase Persistence Generic Qom NotInterface logicalNot(ConstraintInterface $constraint)
Definition: Query.php:434
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\JCR_JOIN_TYPE_RIGHT_OUTER
‪const JCR_JOIN_TYPE_RIGHT_OUTER
Definition: Query.php:55
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\__sleep
‪array __sleep()
Definition: Query.php:589
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\qomFactory
‪$this qomFactory
Definition: Query.php:115
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\logicalAnd
‪logicalAnd(ConstraintInterface ... $constraints)
Definition: Query.php:385
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\greaterThan
‪TYPO3 CMS Extbase Persistence Generic Qom ComparisonInterface greaterThan($propertyName, $operand)
Definition: Query.php:542
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\statement
‪QueryInterface statement($statement, array $parameters=[])
Definition: Query.php:355
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\OrInterface
Definition: OrInterface.php:29
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\setType
‪setType(string $type)
Definition: Query.php:122
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\OPERATOR_LESS_THAN_OR_EQUAL_TO
‪const OPERATOR_LESS_THAN_OR_EQUAL_TO
Definition: QueryInterface.php:59
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:34
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\setParentQuery
‪setParentQuery(?QueryInterface $parentQuery)
Definition: Query.php:138
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\$persistenceManager
‪PersistenceManagerInterface $persistenceManager
Definition: Query.php:68
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\getQuerySettings
‪QuerySettingsInterface getQuerySettings()
Definition: Query.php:160
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\SelectorInterface
Definition: SelectorInterface.php:32
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility
Definition: TypeHandlingUtility.php:29
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\JCR_JOIN_TYPE_INNER
‪const JCR_JOIN_TYPE_INNER
Definition: Query.php:45
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\$dataMapFactory
‪DataMapFactory $dataMapFactory
Definition: Query.php:67
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\getLimit
‪int null getLimit()
Definition: Query.php:300
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\getSelectorName
‪string getSelectorName()
Definition: Query.php:193
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\OPERATOR_CONTAINS
‪const OPERATOR_CONTAINS
Definition: QueryInterface.php:79
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\greaterThanOrEqual
‪TYPO3 CMS Extbase Persistence Generic Qom ComparisonInterface greaterThanOrEqual($propertyName, $operand)
Definition: Query.php:554
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\$statement
‪TYPO3 CMS Extbase Persistence Generic Qom Statement $statement
Definition: Query.php:82
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\matching
‪QueryInterface matching($constraint)
Definition: Query.php:341
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\contains
‪TYPO3 CMS Extbase Persistence Generic Qom ComparisonInterface contains($propertyName, $operand)
Definition: Query.php:489
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\getOrderings
‪array< string, string > getOrderings()
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\CHARSET
‪const CHARSET
Definition: Query.php:60
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\dataMapFactory
‪array< string, $orderings=array();protected int|null $limit;protected int $offset;protected QuerySettingsInterface $querySettings;protected QueryInterface|null $parentQuery;public function __construct(DataMapFactory $dataMapFactory, PersistenceManagerInterface $persistenceManager, QueryObjectModelFactory $qomFactory, ContainerInterface $container) { $this-> dataMapFactory
Definition: Query.php:113
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\setOrderings
‪QueryInterface setOrderings(array $orderings)
Definition: Query.php:245
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\$constraint
‪TYPO3 CMS Extbase Persistence Generic Qom ConstraintInterface $constraint
Definition: Query.php:78
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\__wakeup
‪__wakeup()
Definition: Query.php:578
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\$type
‪string $type
Definition: Query.php:65
‪TYPO3\CMS\Extbase\Persistence\Generic
Definition: Backend.php:18
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\unsetLimit
‪QueryInterface unsetLimit()
Definition: Query.php:289
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\setQuerySettings
‪setQuerySettings(QuerySettingsInterface $querySettings)
Definition: Query.php:149
‪TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException
Definition: UnexpectedTypeException.php:25
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\OPERATOR_LESS_THAN
‪const OPERATOR_LESS_THAN
Definition: QueryInterface.php:54
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\getParentQuery
‪getParentQuery()
Definition: Query.php:130
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\count
‪int count()
Definition: Query.php:599
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isValidTypeForMultiValueComparison
‪static isValidTypeForMultiValueComparison($value)
Definition: TypeHandlingUtility.php:147
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\lessThanOrEqual
‪TYPO3 CMS Extbase Persistence Generic Qom ComparisonInterface lessThanOrEqual($propertyName, $operand)
Definition: Query.php:530
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\setSource
‪setSource(SourceInterface $source)
Definition: Query.php:182
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\OPERATOR_EQUAL_TO
‪const OPERATOR_EQUAL_TO
Definition: QueryInterface.php:34
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\equals
‪TYPO3 CMS Extbase Persistence Generic Qom ComparisonInterface equals($propertyName, $operand, $caseSensitive=true)
Definition: Query.php:447
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\getType
‪string getType()
Definition: Query.php:174
‪TYPO3\CMS\Extbase\Persistence\Generic\Query
Definition: Query.php:41
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface
Definition: ConstraintInterface.php:27
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\getSource
‪TYPO3 CMS Extbase Persistence Generic Qom SourceInterface getSource()
Definition: Query.php:207
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\JCR_JOIN_TYPE_LEFT_OUTER
‪const JCR_JOIN_TYPE_LEFT_OUTER
Definition: Query.php:50
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\between
‪TYPO3 CMS Extbase Persistence Generic Qom AndInterface between($propertyName, $operandLower, $operandUpper)
Definition: Query.php:567
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\execute
‪QueryResultInterface array execute($returnRawQueryResult=false)
Definition: Query.php:222
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\getConstraint
‪TYPO3 CMS Extbase Persistence Generic Qom ConstraintInterface null getConstraint()
Definition: Query.php:376
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\AndInterface
Definition: AndInterface.php:27
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\orderings
‪array< string, function getOrderings() { return $this-> orderings
Definition: Query.php:262
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\$source
‪TYPO3 CMS Extbase Persistence Generic Qom SourceInterface $source
Definition: Query.php:74
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\container
‪$this container
Definition: Query.php:116
‪TYPO3\CMS\Extbase\Persistence\Generic\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\$container
‪ContainerInterface $container
Definition: Query.php:70