Exception
9 months ago
CountOutputWalker.php
9 months ago
CountWalker.php
9 months ago
LimitSubqueryOutputWalker.php
9 months ago
LimitSubqueryWalker.php
9 months ago
Paginator.php
9 months ago
RootTypeWalker.php
9 months ago
RowNumberOverFunction.php
9 months ago
WhereInWalker.php
9 months ago
index.php
9 months ago
LimitSubqueryWalker.php
93 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Tools\Pagination; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\DBAL\Types\Type; |
| 6 | use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadata; |
| 7 | use MailPoetVendor\Doctrine\ORM\Query; |
| 8 | use MailPoetVendor\Doctrine\ORM\Query\AST\Functions\IdentityFunction; |
| 9 | use MailPoetVendor\Doctrine\ORM\Query\AST\Node; |
| 10 | use MailPoetVendor\Doctrine\ORM\Query\AST\PathExpression; |
| 11 | use MailPoetVendor\Doctrine\ORM\Query\AST\SelectExpression; |
| 12 | use MailPoetVendor\Doctrine\ORM\Query\AST\SelectStatement; |
| 13 | use MailPoetVendor\Doctrine\ORM\Query\TreeWalkerAdapter; |
| 14 | use RuntimeException; |
| 15 | use function count; |
| 16 | use function is_string; |
| 17 | use function reset; |
| 18 | class LimitSubqueryWalker extends TreeWalkerAdapter |
| 19 | { |
| 20 | public const IDENTIFIER_TYPE = 'doctrine_paginator.id.type'; |
| 21 | public const FORCE_DBAL_TYPE_CONVERSION = 'doctrine_paginator.scalar_result.force_dbal_type_conversion'; |
| 22 | private $aliasCounter = 0; |
| 23 | public function walkSelectStatement(SelectStatement $AST) |
| 24 | { |
| 25 | // Get the root entity and alias from the AST fromClause |
| 26 | $from = $AST->fromClause->identificationVariableDeclarations; |
| 27 | $fromRoot = reset($from); |
| 28 | $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; |
| 29 | $rootClass = $this->getMetadataForDqlAlias($rootAlias); |
| 30 | $this->validate($AST); |
| 31 | $identifier = $rootClass->getSingleIdentifierFieldName(); |
| 32 | if (isset($rootClass->associationMappings[$identifier])) { |
| 33 | throw new RuntimeException('Paginating an entity with foreign key as identifier only works when using the Output Walkers. Call Paginator#setUseOutputWalkers(true) before iterating the paginator.'); |
| 34 | } |
| 35 | $query = $this->_getQuery(); |
| 36 | $query->setHint(self::IDENTIFIER_TYPE, Type::getType($rootClass->fieldMappings[$identifier]['type'])); |
| 37 | $query->setHint(self::FORCE_DBAL_TYPE_CONVERSION, \true); |
| 38 | $pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias, $identifier); |
| 39 | $pathExpression->type = PathExpression::TYPE_STATE_FIELD; |
| 40 | $AST->selectClause->selectExpressions = [new SelectExpression($pathExpression, '_dctrn_id')]; |
| 41 | $AST->selectClause->isDistinct = ($query->getHints()[Paginator::HINT_ENABLE_DISTINCT] ?? \true) === \true; |
| 42 | if (!isset($AST->orderByClause)) { |
| 43 | return; |
| 44 | } |
| 45 | // @phpstan-ignore method.deprecated |
| 46 | $queryComponents = $this->_getQueryComponents(); |
| 47 | foreach ($AST->orderByClause->orderByItems as $item) { |
| 48 | if ($item->expression instanceof PathExpression) { |
| 49 | $AST->selectClause->selectExpressions[] = new SelectExpression($this->createSelectExpressionItem($item->expression), '_dctrn_ord' . $this->aliasCounter++); |
| 50 | continue; |
| 51 | } |
| 52 | if (is_string($item->expression) && isset($queryComponents[$item->expression])) { |
| 53 | $qComp = $queryComponents[$item->expression]; |
| 54 | if (isset($qComp['resultVariable'])) { |
| 55 | $AST->selectClause->selectExpressions[] = new SelectExpression($qComp['resultVariable'], $item->expression); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | private function validate(SelectStatement $AST) : void |
| 61 | { |
| 62 | // Prevent LimitSubqueryWalker from being used with queries that include |
| 63 | // a limit, a fetched to-many join, and an order by condition that |
| 64 | // references a column from the fetch joined table. |
| 65 | $queryComponents = $this->getQueryComponents(); |
| 66 | $query = $this->_getQuery(); |
| 67 | $from = $AST->fromClause->identificationVariableDeclarations; |
| 68 | $fromRoot = reset($from); |
| 69 | if ($query instanceof Query && $query->getMaxResults() !== null && $AST->orderByClause && count($fromRoot->joins)) { |
| 70 | // Check each orderby item. |
| 71 | // TODO: check complex orderby items too... |
| 72 | foreach ($AST->orderByClause->orderByItems as $orderByItem) { |
| 73 | $expression = $orderByItem->expression; |
| 74 | if ($orderByItem->expression instanceof PathExpression && isset($queryComponents[$expression->identificationVariable])) { |
| 75 | $queryComponent = $queryComponents[$expression->identificationVariable]; |
| 76 | if (isset($queryComponent['parent']) && isset($queryComponent['relation']) && $queryComponent['relation']['type'] & ClassMetadata::TO_MANY) { |
| 77 | throw new RuntimeException('Cannot select distinct identifiers from query with LIMIT and ORDER BY on a column from a fetch joined to-many association. Use output walkers.'); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | private function createSelectExpressionItem(PathExpression $pathExpression) : Node |
| 84 | { |
| 85 | if ($pathExpression->type === PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION) { |
| 86 | $identity = new IdentityFunction('identity'); |
| 87 | $identity->pathExpression = clone $pathExpression; |
| 88 | return $identity; |
| 89 | } |
| 90 | return clone $pathExpression; |
| 91 | } |
| 92 | } |
| 93 |