TYPO3 CMS  TYPO3_6-2
Query.php
Go to the documentation of this file.
1 <?php
3 
17 
23 class Query implements QueryInterface {
24 
28  const JCR_JOIN_TYPE_INNER = '{http://www.jcp.org/jcr/1.0}joinTypeInner';
29 
33  const JCR_JOIN_TYPE_LEFT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeLeftOuter';
34 
38  const JCR_JOIN_TYPE_RIGHT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeRightOuter';
39 
43  const CHARSET = 'utf-8';
44 
48  protected $type;
49 
54  protected $objectManager;
55 
60  protected $dataMapper;
61 
67 
72  protected $qomFactory;
73 
77  protected $source;
78 
82  protected $constraint;
83 
87  protected $statement;
88 
92  protected $orderings = array();
93 
97  protected $limit;
98 
102  protected $offset;
103 
109  protected $querySettings;
110 
116  public function __construct($type) {
117  $this->type = $type;
118  }
119 
129  $this->querySettings = $querySettings;
130  }
131 
139  public function getQuerySettings() {
140  if (!$this->querySettings instanceof QuerySettingsInterface) {
141  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception('Tried to get the query settings without seting them before.', 1248689115);
142  }
143  return $this->querySettings;
144  }
145 
152  public function getType() {
153  return $this->type;
154  }
155 
161  public function setSource(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source) {
162  $this->source = $source;
163  }
164 
171  protected function getSelectorName() {
172  $source = $this->getSource();
173  if ($source instanceof \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SelectorInterface) {
174  return $source->getSelectorName();
175  } else {
176  return '';
177  }
178  }
179 
185  public function getSource() {
186  if ($this->source === NULL) {
187  $this->source = $this->qomFactory->selector($this->getType(), $this->dataMapper->convertClassNameToTableName($this->getType()));
188  }
189  return $this->source;
190  }
191 
199  public function execute($returnRawQueryResult = FALSE) {
200  if ($returnRawQueryResult === TRUE || $this->getQuerySettings()->getReturnRawQueryResult() === TRUE) {
201  return $this->persistenceManager->getObjectDataByQuery($this);
202  } else {
203  return $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\QueryResultInterface', $this);
204  }
205  }
206 
219  public function setOrderings(array $orderings) {
220  $this->orderings = $orderings;
221  return $this;
222  }
223 
234  public function getOrderings() {
235  return $this->orderings;
236  }
237 
247  public function setLimit($limit) {
248  if (!is_int($limit) || $limit < 1) {
249  throw new \InvalidArgumentException('The limit must be an integer >= 1', 1245071870);
250  }
251  $this->limit = $limit;
252  return $this;
253  }
254 
262  public function unsetLimit() {
263  unset($this->limit);
264  return $this;
265  }
266 
273  public function getLimit() {
274  return $this->limit;
275  }
276 
286  public function setOffset($offset) {
287  if (!is_int($offset) || $offset < 0) {
288  throw new \InvalidArgumentException('The offset must be a positive integer', 1245071872);
289  }
290  $this->offset = $offset;
291  return $this;
292  }
293 
300  public function getOffset() {
301  return $this->offset;
302  }
303 
312  public function matching($constraint) {
313  $this->constraint = $constraint;
314  return $this;
315  }
316 
325  public function statement($statement, array $parameters = array()) {
326  $this->statement = $this->qomFactory->statement($statement, $parameters);
327  return $this;
328  }
329 
335  public function getStatement() {
336  return $this->statement;
337  }
338 
345  public function getConstraint() {
346  return $this->constraint;
347  }
348 
358  public function logicalAnd($constraint1) {
359  if (is_array($constraint1)) {
360  $resultingConstraint = array_shift($constraint1);
361  $constraints = $constraint1;
362  } else {
363  $constraints = func_get_args();
364  $resultingConstraint = array_shift($constraints);
365  }
366  if ($resultingConstraint === NULL) {
367  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);
368  }
369  foreach ($constraints as $constraint) {
370  $resultingConstraint = $this->qomFactory->_and($resultingConstraint, $constraint);
371  }
372  return $resultingConstraint;
373  }
374 
383  public function logicalOr($constraint1) {
384  if (is_array($constraint1)) {
385  $resultingConstraint = array_shift($constraint1);
386  $constraints = $constraint1;
387  } else {
388  $constraints = func_get_args();
389  $resultingConstraint = array_shift($constraints);
390  }
391  if ($resultingConstraint === NULL) {
392  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);
393  }
394  foreach ($constraints as $constraint) {
395  $resultingConstraint = $this->qomFactory->_or($resultingConstraint, $constraint);
396  }
397  return $resultingConstraint;
398  }
399 
408  public function logicalNot(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint) {
409  return $this->qomFactory->not($constraint);
410  }
411 
421  public function equals($propertyName, $operand, $caseSensitive = TRUE) {
422  if (is_object($operand) || $caseSensitive) {
423  $comparison = $this->qomFactory->comparison(
424  $this->qomFactory->propertyValue($propertyName, $this->getSelectorName()),
426  $operand
427  );
428  } else {
429  $comparison = $this->qomFactory->comparison(
430  $this->qomFactory->lowerCase($this->qomFactory->propertyValue($propertyName, $this->getSelectorName())),
432  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Charset\\CharsetConverter')->conv_case(\TYPO3\CMS\Extbase\Persistence\Generic\Query::CHARSET, $operand, 'toLower')
433  );
434  }
435  return $comparison;
436  }
437 
447  public function like($propertyName, $operand, $caseSensitive = TRUE) {
448  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LIKE, $operand);
449  }
450 
460  public function contains($propertyName, $operand) {
461  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_CONTAINS, $operand);
462  }
463 
474  public function in($propertyName, $operand) {
475  if (!is_array($operand) && !$operand instanceof \ArrayAccess && !$operand instanceof \Traversable) {
476  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException('The "in" operator must be given a multivalued operand (array, ArrayAccess, Traversable).', 1264678095);
477  }
478  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_IN, $operand);
479  }
480 
489  public function lessThan($propertyName, $operand) {
490  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LESS_THAN, $operand);
491  }
492 
501  public function lessThanOrEqual($propertyName, $operand) {
502  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO, $operand);
503  }
504 
513  public function greaterThan($propertyName, $operand) {
514  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_GREATER_THAN, $operand);
515  }
516 
525  public function greaterThanOrEqual($propertyName, $operand) {
526  return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $operand);
527  }
528 
532  public function __wakeup() {
533  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
534  $this->persistenceManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
535  $this->dataMapper = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
536  $this->qomFactory = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\QueryObjectModelFactory');
537  }
538 
542  public function __sleep() {
543  return array('type', 'source', 'constraint', 'statement', 'orderings', 'limit', 'offset', 'querySettings');
544  }
545 
552  public function count() {
553  return $this->execute()->count();
554  }
555 
566  public function isEmpty($propertyName) {
567  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
568  }
569 }
setSource(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source)
Definition: Query.php:161
equals($propertyName, $operand, $caseSensitive=TRUE)
Definition: Query.php:421
$parameters
Definition: FileDumpEID.php:15
greaterThan($propertyName, $operand)
Definition: Query.php:513
logicalNot(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint)
Definition: Query.php:408
execute($returnRawQueryResult=FALSE)
Definition: Query.php:199
lessThan($propertyName, $operand)
Definition: Query.php:489
greaterThanOrEqual($propertyName, $operand)
Definition: Query.php:525
contains($propertyName, $operand)
Definition: Query.php:460
like($propertyName, $operand, $caseSensitive=TRUE)
Definition: Query.php:447
statement($statement, array $parameters=array())
Definition: Query.php:325
setQuerySettings(QuerySettingsInterface $querySettings)
Definition: Query.php:128
lessThanOrEqual($propertyName, $operand)
Definition: Query.php:501