[BUGFIX] Use named parameters in Extbase IN() queries
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / Storage / Typo3DbQueryParser.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic\Storage;
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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\Connection;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression;
21 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Extbase\Persistence\Generic\Exception\InconsistentQuerySettingsException;
25 use TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidRelationConfigurationException;
26 use TYPO3\CMS\Extbase\Persistence\Generic\Exception\MissingColumnMapException;
27 use TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException;
28 use TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedOrderException;
29 use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap;
30 use TYPO3\CMS\Extbase\Persistence\Generic\Qom;
31 use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface;
32 use TYPO3\CMS\Extbase\Persistence\QueryInterface;
33 use TYPO3\CMS\Frontend\Page\PageRepository;
34
35 /**
36 * QueryParser, converting the qom to string representation
37 */
38 class Typo3DbQueryParser
39 {
40 /**
41 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
42 */
43 protected $dataMapper;
44
45 /**
46 * The TYPO3 page repository. Used for language and workspace overlay
47 *
48 * @var PageRepository
49 */
50 protected $pageRepository;
51
52 /**
53 * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
54 */
55 protected $environmentService;
56
57 /**
58 * Instance of the Doctrine query builder
59 *
60 * @var QueryBuilder
61 */
62 protected $queryBuilder;
63
64 /**
65 * Maps domain model properties to their corresponding table aliases that are used in the query, e.g.:
66 *
67 * 'property1' => 'tableName',
68 * 'property1.property2' => 'tableName1',
69 *
70 * @var array
71 */
72 protected $tablePropertyMap = [];
73
74 /**
75 * Maps tablenames to their aliases to be used in where clauses etc.
76 * Mainly used for joins on the same table etc.
77 *
78 * @var array
79 */
80 protected $tableAliasMap = [];
81
82 /**
83 * Stores all tables used in for SQL joins
84 *
85 * @var array
86 */
87 protected $unionTableAliasCache = [];
88
89 /**
90 * @var string
91 */
92 protected $tableName = '';
93
94 /**
95 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
96 */
97 public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
98 {
99 $this->dataMapper = $dataMapper;
100 }
101
102 /**
103 * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
104 */
105 public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
106 {
107 $this->environmentService = $environmentService;
108 }
109
110 /**
111 * Returns a ready to be executed QueryBuilder object, based on the query
112 *
113 * @param QueryInterface $query
114 * @return QueryBuilder
115 */
116 public function convertQueryToDoctrineQueryBuilder(QueryInterface $query)
117 {
118 // Reset all properties
119 $this->tablePropertyMap = [];
120 $this->tableAliasMap = [];
121 $this->unionTableAliasCache = [];
122 $this->tableName = '';
123 // Find the right table name
124 $source = $query->getSource();
125 $this->initializeQueryBuilder($source);
126
127 $constraint = $query->getConstraint();
128 if ($constraint instanceof Qom\ConstraintInterface) {
129 $wherePredicates = $this->parseConstraint($constraint, $source);
130 if (!empty($wherePredicates)) {
131 $this->queryBuilder->andWhere($wherePredicates);
132 }
133 }
134
135 $this->parseOrderings($query->getOrderings(), $source);
136 $this->addTypo3Constraints($query);
137
138 return $this->queryBuilder;
139 }
140
141 /**
142 * Creates the queryBuilder object whether it is a regular select or a JOIN
143 *
144 * @param Qom\SourceInterface $source The source
145 */
146 protected function initializeQueryBuilder(Qom\SourceInterface $source)
147 {
148 if ($source instanceof Qom\SelectorInterface) {
149 $className = $source->getNodeTypeName();
150 $tableName = $this->dataMapper->getDataMap($className)->getTableName();
151 $this->tableName = $tableName;
152
153 $this->queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
154 ->getQueryBuilderForTable($tableName);
155
156 $this->queryBuilder
157 ->getRestrictions()
158 ->removeAll();
159
160 $tableAlias = $this->getUniqueAlias($tableName);
161
162 $this->queryBuilder
163 ->select($tableAlias . '.*')
164 ->from($tableName, $tableAlias);
165
166 $this->addRecordTypeConstraint($className);
167 } elseif ($source instanceof Qom\JoinInterface) {
168 $leftSource = $source->getLeft();
169 $leftTableName = $leftSource->getSelectorName();
170
171 $this->queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
172 ->getQueryBuilderForTable($leftTableName);
173 $leftTableAlias = $this->getUniqueAlias($leftTableName);
174 $this->queryBuilder
175 ->select($leftTableAlias . '.*')
176 ->from($leftTableName, $leftTableAlias);
177 $this->parseJoin($source, $leftTableAlias);
178 }
179 }
180
181 /**
182 * Transforms a constraint into SQL and parameter arrays
183 *
184 * @param Qom\ConstraintInterface $constraint The constraint
185 * @param Qom\SourceInterface $source The source
186 * @return CompositeExpression|string
187 * @throws \RuntimeException
188 */
189 protected function parseConstraint(Qom\ConstraintInterface $constraint, Qom\SourceInterface $source)
190 {
191 if ($constraint instanceof Qom\AndInterface) {
192 $constraint1 = $constraint->getConstraint1();
193 $constraint2 = $constraint->getConstraint2();
194 if (($constraint1 instanceof Qom\ConstraintInterface)
195 && ($constraint2 instanceof Qom\ConstraintInterface)
196 ) {
197 return $this->queryBuilder->expr()->andX(
198 $this->parseConstraint($constraint1, $source),
199 $this->parseConstraint($constraint2, $source)
200 );
201 } else {
202 return '';
203 }
204 } elseif ($constraint instanceof Qom\OrInterface) {
205 $constraint1 = $constraint->getConstraint1();
206 $constraint2 = $constraint->getConstraint2();
207 if (($constraint1 instanceof Qom\ConstraintInterface)
208 && ($constraint2 instanceof Qom\ConstraintInterface)
209 ) {
210 return $this->queryBuilder->expr()->orX(
211 $this->parseConstraint($constraint->getConstraint1(), $source),
212 $this->parseConstraint($constraint->getConstraint2(), $source)
213 );
214 } else {
215 return '';
216 }
217 } elseif ($constraint instanceof Qom\NotInterface) {
218 return ' NOT(' . $this->parseConstraint($constraint->getConstraint(), $source) . ')';
219 } elseif ($constraint instanceof Qom\ComparisonInterface) {
220 return $this->parseComparison($constraint, $source);
221 } else {
222 throw new \RuntimeException('not implemented', 1476199898);
223 }
224 }
225
226 /**
227 * Transforms orderings into SQL.
228 *
229 * @param array $orderings An array of orderings (Qom\Ordering)
230 * @param Qom\SourceInterface $source The source
231 * @throws UnsupportedOrderException
232 */
233 protected function parseOrderings(array $orderings, Qom\SourceInterface $source)
234 {
235 foreach ($orderings as $propertyName => $order) {
236 if ($order !== QueryInterface::ORDER_ASCENDING && $order !== QueryInterface::ORDER_DESCENDING) {
237 throw new UnsupportedOrderException('Unsupported order encountered.', 1242816074);
238 }
239 $className = null;
240 $tableName = '';
241 if ($source instanceof Qom\SelectorInterface) {
242 $className = $source->getNodeTypeName();
243 $tableName = $this->dataMapper->convertClassNameToTableName($className);
244 $fullPropertyPath = '';
245 while (strpos($propertyName, '.') !== false) {
246 $this->addUnionStatement($className, $tableName, $propertyName, $fullPropertyPath);
247 }
248 } elseif ($source instanceof Qom\JoinInterface) {
249 $tableName = $source->getLeft()->getSelectorName();
250 }
251 $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
252 if ($tableName !== '') {
253 $this->queryBuilder->addOrderBy($tableName . '.' . $columnName, $order);
254 } else {
255 $this->queryBuilder->addOrderBy($columnName, $order);
256 }
257 }
258 }
259
260 /**
261 * add TYPO3 Constraints for all tables to the queryBuilder
262 *
263 * @param QueryInterface $query
264 */
265 protected function addTypo3Constraints(QueryInterface $query)
266 {
267 foreach ($this->tableAliasMap as $tableAlias => $tableName) {
268 $additionalWhereClauses = $this->getAdditionalWhereClause($query->getQuerySettings(), $tableName, $tableAlias);
269 $statement = $this->getVisibilityConstraintStatement($query->getQuerySettings(), $tableName, $tableAlias);
270 if ($statement !== '') {
271 $additionalWhereClauses[] = $statement;
272 }
273 if (!empty($additionalWhereClauses)) {
274 if (in_array($tableAlias, $this->unionTableAliasCache, true)) {
275 $this->queryBuilder->andWhere(
276 $this->queryBuilder->expr()->orX(
277 $this->queryBuilder->expr()->andX(...$additionalWhereClauses),
278 $this->queryBuilder->expr()->isNull($tableAlias . '.uid')
279 )
280 );
281 } else {
282 $this->queryBuilder->andWhere(...$additionalWhereClauses);
283 }
284 }
285 }
286 }
287
288 /**
289 * Parse a Comparison into SQL and parameter arrays.
290 *
291 * @param Qom\ComparisonInterface $comparison The comparison to parse
292 * @param Qom\SourceInterface $source The source
293 * @return string
294 * @throws \RuntimeException
295 * @throws RepositoryException
296 * @throws Exception\BadConstraintException
297 */
298 protected function parseComparison(Qom\ComparisonInterface $comparison, Qom\SourceInterface $source)
299 {
300 if ($comparison->getOperator() === QueryInterface::OPERATOR_CONTAINS) {
301 if ($comparison->getOperand2() === null) {
302 throw new Exception\BadConstraintException('The value for the CONTAINS operator must not be null.', 1484828468);
303 } else {
304 $value = $this->dataMapper->getPlainValue($comparison->getOperand2());
305 if (!$source instanceof Qom\SelectorInterface) {
306 throw new \RuntimeException('Source is not of type "SelectorInterface"', 1395362539);
307 }
308 $className = $source->getNodeTypeName();
309 $tableName = $this->dataMapper->convertClassNameToTableName($className);
310 $operand1 = $comparison->getOperand1();
311 $propertyName = $operand1->getPropertyName();
312 $fullPropertyPath = '';
313 while (strpos($propertyName, '.') !== false) {
314 $this->addUnionStatement($className, $tableName, $propertyName, $fullPropertyPath);
315 }
316 $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
317 $dataMap = $this->dataMapper->getDataMap($className);
318 $columnMap = $dataMap->getColumnMap($propertyName);
319 $typeOfRelation = $columnMap instanceof ColumnMap ? $columnMap->getTypeOfRelation() : null;
320 if ($typeOfRelation === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
321 $relationTableName = $columnMap->getRelationTableName();
322 $queryBuilderForSubselect = $this->queryBuilder->getConnection()->createQueryBuilder();
323 $queryBuilderForSubselect
324 ->select($columnMap->getParentKeyFieldName())
325 ->from($relationTableName)
326 ->where(
327 $queryBuilderForSubselect->expr()->eq(
328 $columnMap->getChildKeyFieldName(),
329 $this->queryBuilder->createNamedParameter($value)
330 )
331 );
332 $additionalWhereForMatchFields = $this->getAdditionalMatchFieldsStatement($queryBuilderForSubselect->expr(), $columnMap, $relationTableName, $relationTableName);
333 if ($additionalWhereForMatchFields) {
334 $queryBuilderForSubselect->andWhere($additionalWhereForMatchFields);
335 }
336
337 return $this->queryBuilder->expr()->comparison(
338 $this->queryBuilder->quoteIdentifier($tableName . '.uid'),
339 'IN',
340 '(' . $queryBuilderForSubselect->getSQL() . ')'
341 );
342 } elseif ($typeOfRelation === ColumnMap::RELATION_HAS_MANY) {
343 $parentKeyFieldName = $columnMap->getParentKeyFieldName();
344 if (isset($parentKeyFieldName)) {
345 $childTableName = $columnMap->getChildTableName();
346
347 // Build the SQL statement of the subselect
348 $queryBuilderForSubselect = $this->queryBuilder->getConnection()->createQueryBuilder();
349 $queryBuilderForSubselect
350 ->select($parentKeyFieldName)
351 ->from($childTableName)
352 ->where(
353 $queryBuilderForSubselect->expr()->eq(
354 'uid',
355 (int)$value
356 )
357 );
358
359 // Add it to the main query
360 return $this->queryBuilder->expr()->eq(
361 $tableName . '.uid',
362 $queryBuilderForSubselect->getSQL()
363 );
364 } else {
365 return $this->queryBuilder->expr()->inSet(
366 $tableName . '.' . $columnName,
367 $this->queryBuilder->createNamedParameter($value)
368 );
369 }
370 } else {
371 throw new RepositoryException('Unsupported or non-existing property name "' . $propertyName . '" used in relation matching.', 1327065745);
372 }
373 }
374 } else {
375 return $this->parseDynamicOperand($comparison, $source);
376 }
377 }
378
379 /**
380 * Parse a DynamicOperand into SQL and parameter arrays.
381 *
382 * @param Qom\ComparisonInterface $comparison
383 * @param Qom\SourceInterface $source The source
384 * @return string
385 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception
386 * @throws Exception\BadConstraintException
387 */
388 protected function parseDynamicOperand(Qom\ComparisonInterface $comparison, Qom\SourceInterface $source)
389 {
390 $value = $comparison->getOperand2();
391 $fieldName = $this->parseOperand($comparison->getOperand1(), $source);
392 $expr = null;
393 $exprBuilder = $this->queryBuilder->expr();
394 switch ($comparison->getOperator()) {
395 case QueryInterface::OPERATOR_IN:
396 $hasValue = false;
397 $plainValues = [];
398 foreach ($value as $singleValue) {
399 $plainValue = $this->dataMapper->getPlainValue($singleValue);
400 if ($plainValue !== null) {
401 $hasValue = true;
402 $parameterType = ctype_digit((string)$plainValue) ? \PDO::PARAM_INT : \PDO::PARAM_STR;
403 $plainValues[] = $this->queryBuilder->createNamedParameter($plainValue, $parameterType);
404 }
405 }
406 if (!$hasValue) {
407 throw new Exception\BadConstraintException('The IN operator needs a non-empty value list to compare against. The given value list is empty.', 1484828466);
408 }
409 $expr = $exprBuilder->comparison($fieldName, 'IN', '(' . implode(', ', $plainValues) . ')');
410 break;
411 case QueryInterface::OPERATOR_EQUAL_TO:
412 if ($value === null) {
413 $expr = $fieldName . ' IS NULL';
414 } else {
415 $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value));
416 $expr = $exprBuilder->comparison($fieldName, $exprBuilder::EQ, $value);
417 }
418 break;
419 case QueryInterface::OPERATOR_EQUAL_TO_NULL:
420 $expr = $fieldName . ' IS NULL';
421 break;
422 case QueryInterface::OPERATOR_NOT_EQUAL_TO:
423 if ($value === null) {
424 $expr = $fieldName . ' IS NOT NULL';
425 } else {
426 $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value));
427 $expr = $exprBuilder->comparison($fieldName, $exprBuilder::NEQ, $value);
428 }
429 break;
430 case QueryInterface::OPERATOR_NOT_EQUAL_TO_NULL:
431 $expr = $fieldName . ' IS NOT NULL';
432 break;
433 case QueryInterface::OPERATOR_LESS_THAN:
434 $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value), \PDO::PARAM_INT);
435 $expr = $exprBuilder->comparison($fieldName, $exprBuilder::LT, $value);
436 break;
437 case QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO:
438 $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value), \PDO::PARAM_INT);
439 $expr = $exprBuilder->comparison($fieldName, $exprBuilder::LTE, $value);
440 break;
441 case QueryInterface::OPERATOR_GREATER_THAN:
442 $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value), \PDO::PARAM_INT);
443 $expr = $exprBuilder->comparison($fieldName, $exprBuilder::GT, $value);
444 break;
445 case QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO:
446 $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value), \PDO::PARAM_INT);
447 $expr = $exprBuilder->comparison($fieldName, $exprBuilder::GTE, $value);
448 break;
449 case QueryInterface::OPERATOR_LIKE:
450 $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value));
451 $expr = $exprBuilder->comparison($fieldName, 'LIKE', $value);
452 break;
453 default:
454 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception('Unsupported operator encountered.', 1242816073);
455 }
456 return $expr;
457 }
458
459 /**
460 * @param Qom\DynamicOperandInterface $operand
461 * @param Qom\SourceInterface $source The source
462 * @return string
463 * @throws \InvalidArgumentException
464 */
465 protected function parseOperand(Qom\DynamicOperandInterface $operand, Qom\SourceInterface $source)
466 {
467 if ($operand instanceof Qom\LowerCaseInterface) {
468 $constraintSQL = 'LOWER(' . $this->parseOperand($operand->getOperand(), $source) . ')';
469 } elseif ($operand instanceof Qom\UpperCaseInterface) {
470 $constraintSQL = 'UPPER(' . $this->parseOperand($operand->getOperand(), $source) . ')';
471 } elseif ($operand instanceof Qom\PropertyValueInterface) {
472 $propertyName = $operand->getPropertyName();
473 $className = '';
474 if ($source instanceof Qom\SelectorInterface) {
475 $className = $source->getNodeTypeName();
476 $tableName = $this->dataMapper->convertClassNameToTableName($className);
477 $fullPropertyPath = '';
478 while (strpos($propertyName, '.') !== false) {
479 $this->addUnionStatement($className, $tableName, $propertyName, $fullPropertyPath);
480 }
481 } elseif ($source instanceof Qom\JoinInterface) {
482 $tableName = $source->getJoinCondition()->getSelector1Name();
483 }
484 $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
485 $constraintSQL = (!empty($tableName) ? $tableName . '.' : '') . $columnName;
486 $constraintSQL = $this->queryBuilder->getConnection()->quoteIdentifier($constraintSQL);
487 } else {
488 throw new \InvalidArgumentException('Given operand has invalid type "' . get_class($operand) . '".', 1395710211);
489 }
490 return $constraintSQL;
491 }
492
493 /**
494 * Add a constraint to ensure that the record type of the returned tuples is matching the data type of the repository.
495 *
496 * @param string $className The class name
497 */
498 protected function addRecordTypeConstraint($className)
499 {
500 if ($className !== null) {
501 $dataMap = $this->dataMapper->getDataMap($className);
502 if ($dataMap->getRecordTypeColumnName() !== null) {
503 $recordTypes = [];
504 if ($dataMap->getRecordType() !== null) {
505 $recordTypes[] = $dataMap->getRecordType();
506 }
507 foreach ($dataMap->getSubclasses() as $subclassName) {
508 $subclassDataMap = $this->dataMapper->getDataMap($subclassName);
509 if ($subclassDataMap->getRecordType() !== null) {
510 $recordTypes[] = $subclassDataMap->getRecordType();
511 }
512 }
513 if (!empty($recordTypes)) {
514 $recordTypeStatements = [];
515 foreach ($recordTypes as $recordType) {
516 $tableName = $dataMap->getTableName();
517 $recordTypeStatements[] = $this->queryBuilder->expr()->eq(
518 $tableName . '.' . $dataMap->getRecordTypeColumnName(),
519 $this->queryBuilder->createNamedParameter($recordType)
520 );
521 }
522 $this->queryBuilder->andWhere(
523 $this->queryBuilder->expr()->orX(...$recordTypeStatements)
524 );
525 }
526 }
527 }
528 }
529
530 /**
531 * Builds a condition for filtering records by the configured match field,
532 * e.g. MM_match_fields, foreign_match_fields or foreign_table_field.
533 *
534 * @param ExpressionBuilder $exprBuilder
535 * @param ColumnMap $columnMap The column man for which the condition should be build.
536 * @param string $childTableAlias The alias of the child record table used in the query.
537 * @param string $parentTable The real name of the parent table (used for building the foreign_table_field condition).
538 * @return string The match field conditions or an empty string.
539 */
540 protected function getAdditionalMatchFieldsStatement($exprBuilder, $columnMap, $childTableAlias, $parentTable = null)
541 {
542 $additionalWhereForMatchFields = [];
543 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
544 if (is_array($relationTableMatchFields) && !empty($relationTableMatchFields)) {
545 foreach ($relationTableMatchFields as $fieldName => $value) {
546 $additionalWhereForMatchFields[] = $exprBuilder->eq($childTableAlias . '.' . $fieldName, $this->queryBuilder->createNamedParameter($value));
547 }
548 }
549
550 if (isset($parentTable)) {
551 $parentTableFieldName = $columnMap->getParentTableFieldName();
552 if (!empty($parentTableFieldName)) {
553 $additionalWhereForMatchFields[] = $exprBuilder->eq($childTableAlias . '.' . $parentTableFieldName, $this->queryBuilder->createNamedParameter($parentTable));
554 }
555 }
556
557 if (!empty($additionalWhereForMatchFields)) {
558 return $exprBuilder->andX(...$additionalWhereForMatchFields);
559 } else {
560 return '';
561 }
562 }
563
564 /**
565 * Adds additional WHERE statements according to the query settings.
566 *
567 * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings
568 * @param string $tableName The table name to add the additional where clause for
569 * @param string $tableAlias The table alias used in the query.
570 * @return array
571 */
572 protected function getAdditionalWhereClause(QuerySettingsInterface $querySettings, $tableName, $tableAlias = null)
573 {
574 $whereClause = [];
575 if ($querySettings->getRespectSysLanguage()) {
576 $systemLanguageStatement = $this->getSysLanguageStatement($tableName, $tableAlias, $querySettings);
577 if (!empty($systemLanguageStatement)) {
578 $whereClause[] = $systemLanguageStatement;
579 }
580 }
581
582 if ($querySettings->getRespectStoragePage()) {
583 $pageIdStatement = $this->getPageIdStatement($tableName, $tableAlias, $querySettings->getStoragePageIds());
584 if (!empty($pageIdStatement)) {
585 $whereClause[] = $pageIdStatement;
586 }
587 }
588
589 return $whereClause;
590 }
591
592 /**
593 * Adds enableFields and deletedClause to the query if necessary
594 *
595 * @param QuerySettingsInterface $querySettings
596 * @param string $tableName The database table name
597 * @param string $tableAlias
598 * @return string
599 */
600 protected function getVisibilityConstraintStatement(QuerySettingsInterface $querySettings, $tableName, $tableAlias)
601 {
602 $statement = '';
603 if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) {
604 $ignoreEnableFields = $querySettings->getIgnoreEnableFields();
605 $enableFieldsToBeIgnored = $querySettings->getEnableFieldsToBeIgnored();
606 $includeDeleted = $querySettings->getIncludeDeleted();
607 if ($this->environmentService->isEnvironmentInFrontendMode()) {
608 $statement .= $this->getFrontendConstraintStatement($tableName, $ignoreEnableFields, $enableFieldsToBeIgnored, $includeDeleted);
609 } else {
610 // TYPO3_MODE === 'BE'
611 $statement .= $this->getBackendConstraintStatement($tableName, $ignoreEnableFields, $includeDeleted);
612 }
613 if (!empty($statement)) {
614 $statement = $this->replaceTableNameWithAlias($statement, $tableName, $tableAlias);
615 $statement = strtolower(substr($statement, 1, 3)) === 'and' ? substr($statement, 5) : $statement;
616 }
617 }
618 return $statement;
619 }
620
621 /**
622 * Returns constraint statement for frontend context
623 *
624 * @param string $tableName
625 * @param bool $ignoreEnableFields A flag indicating whether the enable fields should be ignored
626 * @param array $enableFieldsToBeIgnored If $ignoreEnableFields is true, this array specifies enable fields to be ignored. If it is NULL or an empty array (default) all enable fields are ignored.
627 * @param bool $includeDeleted A flag indicating whether deleted records should be included
628 * @return string
629 * @throws InconsistentQuerySettingsException
630 */
631 protected function getFrontendConstraintStatement($tableName, $ignoreEnableFields, array $enableFieldsToBeIgnored = [], $includeDeleted)
632 {
633 $statement = '';
634 if ($ignoreEnableFields && !$includeDeleted) {
635 if (!empty($enableFieldsToBeIgnored)) {
636 // array_combine() is necessary because of the way \TYPO3\CMS\Frontend\Page\PageRepository::enableFields() is implemented
637 $statement .= $this->getPageRepository()->enableFields($tableName, -1, array_combine($enableFieldsToBeIgnored, $enableFieldsToBeIgnored));
638 } else {
639 $statement .= $this->getPageRepository()->deleteClause($tableName);
640 }
641 } elseif (!$ignoreEnableFields && !$includeDeleted) {
642 $statement .= $this->getPageRepository()->enableFields($tableName);
643 } elseif (!$ignoreEnableFields && $includeDeleted) {
644 throw new InconsistentQuerySettingsException('Query setting "ignoreEnableFields=FALSE" can not be used together with "includeDeleted=TRUE" in frontend context.', 1460975922);
645 }
646 return $statement;
647 }
648
649 /**
650 * Returns constraint statement for backend context
651 *
652 * @param string $tableName
653 * @param bool $ignoreEnableFields A flag indicating whether the enable fields should be ignored
654 * @param bool $includeDeleted A flag indicating whether deleted records should be included
655 * @return string
656 */
657 protected function getBackendConstraintStatement($tableName, $ignoreEnableFields, $includeDeleted)
658 {
659 $statement = '';
660 if (!$ignoreEnableFields) {
661 $statement .= BackendUtility::BEenableFields($tableName);
662 }
663 if (!$includeDeleted) {
664 $statement .= BackendUtility::deleteClause($tableName);
665 }
666 return $statement;
667 }
668
669 /**
670 * Builds the language field statement
671 *
672 * @param string $tableName The database table name
673 * @param string $tableAlias The table alias used in the query.
674 * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings
675 * @return string
676 */
677 protected function getSysLanguageStatement($tableName, $tableAlias, $querySettings)
678 {
679 if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) {
680 if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])) {
681 // Select all entries for the current language
682 // If any language is set -> get those entries which are not translated yet
683 // They will be removed by \TYPO3\CMS\Frontend\Page\PageRepository::getRecordOverlay if not matching overlay mode
684 $languageField = $GLOBALS['TCA'][$tableName]['ctrl']['languageField'];
685
686 if (isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
687 && $querySettings->getLanguageUid() > 0
688 ) {
689 $mode = $querySettings->getLanguageMode();
690
691 if ($mode === 'strict') {
692 $queryBuilderForSubselect = $this->queryBuilder->getConnection()->createQueryBuilder();
693 $queryBuilderForSubselect
694 ->select($tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
695 ->from($tableName)
696 ->where(
697 $queryBuilderForSubselect->expr()->andX(
698 $queryBuilderForSubselect->expr()->gt($tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'], 0),
699 $queryBuilderForSubselect->expr()->eq($tableName . '.' . $languageField, (int)$querySettings->getLanguageUid())
700 )
701 );
702 return $this->queryBuilder->expr()->orX(
703 $this->queryBuilder->expr()->eq($tableAlias . '.' . $languageField, -1),
704 $this->queryBuilder->expr()->andX(
705 $this->queryBuilder->expr()->eq($tableAlias . '.' . $languageField, (int)$querySettings->getLanguageUid()),
706 $this->queryBuilder->expr()->eq($tableAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'], 0)
707 ),
708 $this->queryBuilder->expr()->andX(
709 $this->queryBuilder->expr()->eq($tableAlias . '.' . $languageField, 0),
710 $this->queryBuilder->expr()->in(
711 $tableAlias . '.uid',
712 $queryBuilderForSubselect->getSQL()
713
714 )
715 )
716 );
717 } else {
718 $queryBuilderForSubselect = $this->queryBuilder->getConnection()->createQueryBuilder();
719 $queryBuilderForSubselect
720 ->select($tableAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
721 ->from($tableName)
722 ->where(
723 $queryBuilderForSubselect->expr()->andX(
724 $queryBuilderForSubselect->expr()->gt($tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'], 0),
725 $queryBuilderForSubselect->expr()->eq($tableName . '.' . $languageField, (int)$querySettings->getLanguageUid())
726 )
727 );
728 return $this->queryBuilder->expr()->orX(
729 $this->queryBuilder->expr()->in($tableAlias . '.' . $languageField, [(int)$querySettings->getLanguageUid(), -1]),
730 $this->queryBuilder->expr()->andX(
731 $this->queryBuilder->expr()->eq($tableAlias . '.' . $languageField, 0),
732 $this->queryBuilder->expr()->notIn(
733 $tableAlias . '.uid',
734 $queryBuilderForSubselect->getSQL()
735
736 )
737 )
738 );
739 }
740 } else {
741 return $this->queryBuilder->expr()->in(
742 $tableAlias . '.' . $languageField,
743 [(int)$querySettings->getLanguageUid(), -1]
744 );
745 }
746 }
747 }
748 return '';
749 }
750
751 /**
752 * Builds the page ID checking statement
753 *
754 * @param string $tableName The database table name
755 * @param string $tableAlias The table alias used in the query.
756 * @param array $storagePageIds list of storage page ids
757 * @throws InconsistentQuerySettingsException
758 * @return string
759 */
760 protected function getPageIdStatement($tableName, $tableAlias, array $storagePageIds)
761 {
762 if (!is_array($GLOBALS['TCA'][$tableName]['ctrl'])) {
763 return '';
764 }
765
766 $rootLevel = (int)$GLOBALS['TCA'][$tableName]['ctrl']['rootLevel'];
767 switch ($rootLevel) {
768 // Only in pid 0
769 case 1:
770 $storagePageIds = [0];
771 break;
772 // Pid 0 and pagetree
773 case -1:
774 if (empty($storagePageIds)) {
775 $storagePageIds = [0];
776 } else {
777 $storagePageIds[] = 0;
778 }
779 break;
780 // Only pagetree or not set
781 case 0:
782 if (empty($storagePageIds)) {
783 throw new InconsistentQuerySettingsException('Missing storage page ids.', 1365779762);
784 }
785 break;
786 // Invalid configuration
787 default:
788 return '';
789 }
790 $storagePageIds = array_map('intval', $storagePageIds);
791 if (count($storagePageIds) === 1) {
792 return $this->queryBuilder->expr()->eq($tableAlias . '.pid', reset($storagePageIds));
793 } else {
794 return $this->queryBuilder->expr()->in($tableAlias . '.pid', $storagePageIds);
795 }
796 }
797
798 /**
799 * Transforms a Join into SQL and parameter arrays
800 *
801 * @param Qom\JoinInterface $join The join
802 * @param string $leftTableAlias The alias from the table to main
803 */
804 protected function parseJoin(Qom\JoinInterface $join, $leftTableAlias)
805 {
806 $leftSource = $join->getLeft();
807 $leftClassName = $leftSource->getNodeTypeName();
808 $this->addRecordTypeConstraint($leftClassName);
809 $rightSource = $join->getRight();
810 if ($rightSource instanceof Qom\JoinInterface) {
811 $left = $rightSource->getLeft();
812 $rightClassName = $left->getNodeTypeName();
813 $rightTableName = $left->getSelectorName();
814 } else {
815 $rightClassName = $rightSource->getNodeTypeName();
816 $rightTableName = $rightSource->getSelectorName();
817 $this->queryBuilder->addSelect($rightTableName . '.*');
818 }
819 $this->addRecordTypeConstraint($rightClassName);
820 $rightTableAlias = $this->getUniqueAlias($rightTableName);
821 $joinCondition = $join->getJoinCondition();
822 $joinConditionExpression = null;
823 $this->unionTableAliasCache[] = $rightTableAlias;
824 if ($joinCondition instanceof Qom\EquiJoinCondition) {
825 $column1Name = $this->dataMapper->convertPropertyNameToColumnName($joinCondition->getProperty1Name(), $leftClassName);
826 $column2Name = $this->dataMapper->convertPropertyNameToColumnName($joinCondition->getProperty2Name(), $rightClassName);
827
828 $joinConditionExpression = $this->queryBuilder->expr()->eq(
829 $leftTableAlias . '.' . $column1Name,
830 $rightTableAlias . '.' . $column2Name
831 );
832 }
833 $this->queryBuilder->leftJoin($leftTableAlias, $rightTableName, $rightTableAlias, $joinConditionExpression);
834 if ($rightSource instanceof Qom\JoinInterface) {
835 $this->parseJoin($rightSource, $rightTableAlias);
836 }
837 }
838
839 /**
840 * Generates a unique alias for the given table and the given property path.
841 * The property path will be mapped to the generated alias in the tablePropertyMap.
842 *
843 * @param string $tableName The name of the table for which the alias should be generated.
844 * @param string $fullPropertyPath The full property path that is related to the given table.
845 * @return string The generated table alias.
846 */
847 protected function getUniqueAlias($tableName, $fullPropertyPath = null)
848 {
849 if (isset($fullPropertyPath) && isset($this->tablePropertyMap[$fullPropertyPath])) {
850 return $this->tablePropertyMap[$fullPropertyPath];
851 }
852
853 $alias = $tableName;
854 $i = 0;
855 while (isset($this->tableAliasMap[$alias])) {
856 $alias = $tableName . $i;
857 $i++;
858 }
859
860 $this->tableAliasMap[$alias] = $tableName;
861
862 if (isset($fullPropertyPath)) {
863 $this->tablePropertyMap[$fullPropertyPath] = $alias;
864 }
865
866 return $alias;
867 }
868
869 /**
870 * adds a union statement to the query, mostly for tables referenced in the where condition.
871 * The property for which the union statement is generated will be appended.
872 *
873 * @param string &$className The name of the parent class, will be set to the child class after processing.
874 * @param string &$tableName The name of the parent table, will be set to the table alias that is used in the union statement.
875 * @param string &$propertyPath The remaining property path, will be cut of by one part during the process.
876 * @param string $fullPropertyPath The full path the the current property, will be used to make table names unique.
877 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception
878 * @throws InvalidRelationConfigurationException
879 * @throws MissingColumnMapException
880 */
881 protected function addUnionStatement(&$className, &$tableName, &$propertyPath, &$fullPropertyPath)
882 {
883 $explodedPropertyPath = explode('.', $propertyPath, 2);
884 $propertyName = $explodedPropertyPath[0];
885 $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
886 $realTableName = $this->dataMapper->convertClassNameToTableName($className);
887 $tableName = isset($this->tablePropertyMap[$fullPropertyPath]) ? $this->tablePropertyMap[$fullPropertyPath] : $realTableName;
888 $columnMap = $this->dataMapper->getDataMap($className)->getColumnMap($propertyName);
889
890 if ($columnMap === null) {
891 throw new MissingColumnMapException('The ColumnMap for property "' . $propertyName . '" of class "' . $className . '" is missing.', 1355142232);
892 }
893
894 $parentKeyFieldName = $columnMap->getParentKeyFieldName();
895 $childTableName = $columnMap->getChildTableName();
896
897 if ($childTableName === null) {
898 throw new InvalidRelationConfigurationException('The relation information for property "' . $propertyName . '" of class "' . $className . '" is missing.', 1353170925);
899 }
900
901 $fullPropertyPath .= ($fullPropertyPath === '') ? $propertyName : '.' . $propertyName;
902 $childTableAlias = $this->getUniqueAlias($childTableName, $fullPropertyPath);
903
904 // If there is already a union with the current identifier we do not need to build it again and exit early.
905 if (in_array($childTableAlias, $this->unionTableAliasCache, true)) {
906 $propertyPath = $explodedPropertyPath[1];
907 $tableName = $childTableAlias;
908 $className = $this->dataMapper->getType($className, $propertyName);
909 return;
910 }
911
912 if ($columnMap->getTypeOfRelation() === ColumnMap::RELATION_HAS_ONE) {
913 if (isset($parentKeyFieldName)) {
914 // @todo: no test for this part yet
915 $joinConditionExpression = $this->queryBuilder->expr()->eq(
916 $tableName . '.uid',
917 $childTableAlias . '.' . $parentKeyFieldName
918 );
919 } else {
920 $joinConditionExpression = $this->queryBuilder->expr()->eq(
921 $tableName . '.' . $columnName,
922 $childTableAlias . '.uid'
923 );
924 }
925 $this->queryBuilder->leftJoin($tableName, $childTableName, $childTableAlias, $joinConditionExpression);
926 $this->unionTableAliasCache[] = $childTableAlias;
927 $this->queryBuilder->andWhere(
928 $this->getAdditionalMatchFieldsStatement($this->queryBuilder->expr(), $columnMap, $childTableAlias, $realTableName)
929 );
930 } elseif ($columnMap->getTypeOfRelation() === ColumnMap::RELATION_HAS_MANY) {
931 // @todo: no tests for this part yet
932 if (isset($parentKeyFieldName)) {
933 $joinConditionExpression = $this->queryBuilder->expr()->eq(
934 $tableName . '.uid',
935 $childTableAlias . '.' . $parentKeyFieldName
936 );
937 } else {
938 $joinConditionExpression = $this->queryBuilder->expr()->inSet(
939 $tableName . '.' . $columnName,
940 $this->queryBuilder->quoteIdentifier($childTableAlias . '.uid'),
941 true
942 );
943 }
944 $this->queryBuilder->leftJoin($tableName, $childTableName, $childTableAlias, $joinConditionExpression);
945 $this->unionTableAliasCache[] = $childTableAlias;
946 $this->queryBuilder->andWhere(
947 $this->getAdditionalMatchFieldsStatement($this->queryBuilder->expr(), $columnMap, $childTableAlias, $realTableName)
948 );
949 } elseif ($columnMap->getTypeOfRelation() === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
950 $relationTableName = $columnMap->getRelationTableName();
951 $relationTableAlias = $relationTableAlias = $this->getUniqueAlias($relationTableName, $fullPropertyPath . '_mm');
952
953 $joinConditionExpression = $this->queryBuilder->expr()->andX(
954 $this->queryBuilder->expr()->eq(
955 $tableName . '.uid',
956 $relationTableAlias . '.' . $columnMap->getParentKeyFieldName()
957 ),
958 $this->getAdditionalMatchFieldsStatement($this->queryBuilder->expr(), $columnMap, $relationTableAlias, $realTableName)
959 );
960 $this->queryBuilder->leftJoin($tableName, $relationTableName, $relationTableAlias, $joinConditionExpression);
961 $joinConditionExpression = $this->queryBuilder->expr()->eq(
962 $relationTableAlias . '.' . $columnMap->getChildKeyFieldName(),
963 $childTableAlias . '.uid'
964 );
965 $this->queryBuilder->leftJoin($relationTableAlias, $childTableName, $childTableAlias, $joinConditionExpression);
966 $this->unionTableAliasCache[] = $childTableAlias;
967 $this->queryBuilder->addGroupBy($this->tableName . '.uid');
968 } else {
969 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception('Could not determine type of relation.', 1252502725);
970 }
971 $propertyPath = $explodedPropertyPath[1];
972 $tableName = $childTableAlias;
973 $className = $this->dataMapper->getType($className, $propertyName);
974 }
975
976 /**
977 * If the table name does not match the table alias all occurrences of
978 * "tableName." are replaced with "tableAlias." in the given SQL statement.
979 *
980 * @param string $statement The SQL statement in which the values are replaced.
981 * @param string $tableName The table name that is replaced.
982 * @param string $tableAlias The table alias that replaced the table name.
983 * @return string The modified SQL statement.
984 */
985 protected function replaceTableNameWithAlias($statement, $tableName, $tableAlias)
986 {
987 if ($tableAlias !== $tableName) {
988 /** @var Connection $connection */
989 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
990 $quotedTableName = $connection->quoteIdentifier($tableName);
991 $quotedTableAlias = $connection->quoteIdentifier($tableAlias);
992 $statement = str_replace(
993 [$tableName . '.', $quotedTableName . '.'],
994 [$tableAlias . '.', $quotedTableAlias . '.'],
995 $statement
996 );
997 }
998
999 return $statement;
1000 }
1001
1002 /**
1003 * @return PageRepository
1004 */
1005 protected function getPageRepository()
1006 {
1007 if (!$this->pageRepository instanceof PageRepository) {
1008 if ($this->environmentService->isEnvironmentInFrontendMode() && is_object($GLOBALS['TSFE'])) {
1009 $this->pageRepository = $GLOBALS['TSFE']->sys_page;
1010 } else {
1011 $this->pageRepository = GeneralUtility::makeInstance(PageRepository::class);
1012 }
1013 }
1014
1015 return $this->pageRepository;
1016 }
1017 }