diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Tools/Pagination/WhereInWalker.php')
-rw-r--r-- | vendor/doctrine/orm/src/Tools/Pagination/WhereInWalker.php | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Tools/Pagination/WhereInWalker.php b/vendor/doctrine/orm/src/Tools/Pagination/WhereInWalker.php new file mode 100644 index 0000000..01741ca --- /dev/null +++ b/vendor/doctrine/orm/src/Tools/Pagination/WhereInWalker.php | |||
@@ -0,0 +1,116 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Tools\Pagination; | ||
6 | |||
7 | use Doctrine\ORM\Query\AST\ArithmeticExpression; | ||
8 | use Doctrine\ORM\Query\AST\ConditionalExpression; | ||
9 | use Doctrine\ORM\Query\AST\ConditionalPrimary; | ||
10 | use Doctrine\ORM\Query\AST\ConditionalTerm; | ||
11 | use Doctrine\ORM\Query\AST\InListExpression; | ||
12 | use Doctrine\ORM\Query\AST\InputParameter; | ||
13 | use Doctrine\ORM\Query\AST\NullComparisonExpression; | ||
14 | use Doctrine\ORM\Query\AST\PathExpression; | ||
15 | use Doctrine\ORM\Query\AST\SelectStatement; | ||
16 | use Doctrine\ORM\Query\AST\SimpleArithmeticExpression; | ||
17 | use Doctrine\ORM\Query\AST\WhereClause; | ||
18 | use Doctrine\ORM\Query\TreeWalkerAdapter; | ||
19 | use RuntimeException; | ||
20 | |||
21 | use function count; | ||
22 | use function reset; | ||
23 | |||
24 | /** | ||
25 | * Appends a condition equivalent to "WHERE IN (:dpid_1, :dpid_2, ...)" to the whereClause of the AST. | ||
26 | * | ||
27 | * The parameter namespace (dpid) is defined by | ||
28 | * the PAGINATOR_ID_ALIAS | ||
29 | * | ||
30 | * The HINT_PAGINATOR_HAS_IDS query hint indicates whether there are | ||
31 | * any ids in the parameter at all. | ||
32 | */ | ||
33 | class WhereInWalker extends TreeWalkerAdapter | ||
34 | { | ||
35 | /** | ||
36 | * ID Count hint name. | ||
37 | */ | ||
38 | public const HINT_PAGINATOR_HAS_IDS = 'doctrine.paginator_has_ids'; | ||
39 | |||
40 | /** | ||
41 | * Primary key alias for query. | ||
42 | */ | ||
43 | public const PAGINATOR_ID_ALIAS = 'dpid'; | ||
44 | |||
45 | public function walkSelectStatement(SelectStatement $selectStatement): void | ||
46 | { | ||
47 | // Get the root entity and alias from the AST fromClause | ||
48 | $from = $selectStatement->fromClause->identificationVariableDeclarations; | ||
49 | |||
50 | if (count($from) > 1) { | ||
51 | throw new RuntimeException('Cannot count query which selects two FROM components, cannot make distinction'); | ||
52 | } | ||
53 | |||
54 | $fromRoot = reset($from); | ||
55 | $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; | ||
56 | $rootClass = $this->getMetadataForDqlAlias($rootAlias); | ||
57 | $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); | ||
58 | |||
59 | $pathType = PathExpression::TYPE_STATE_FIELD; | ||
60 | if (isset($rootClass->associationMappings[$identifierFieldName])) { | ||
61 | $pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION; | ||
62 | } | ||
63 | |||
64 | $pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias, $identifierFieldName); | ||
65 | $pathExpression->type = $pathType; | ||
66 | |||
67 | $hasIds = $this->_getQuery()->getHint(self::HINT_PAGINATOR_HAS_IDS); | ||
68 | |||
69 | if ($hasIds) { | ||
70 | $arithmeticExpression = new ArithmeticExpression(); | ||
71 | $arithmeticExpression->simpleArithmeticExpression = new SimpleArithmeticExpression( | ||
72 | [$pathExpression], | ||
73 | ); | ||
74 | $expression = new InListExpression( | ||
75 | $arithmeticExpression, | ||
76 | [new InputParameter(':' . self::PAGINATOR_ID_ALIAS)], | ||
77 | ); | ||
78 | } else { | ||
79 | $expression = new NullComparisonExpression($pathExpression); | ||
80 | } | ||
81 | |||
82 | $conditionalPrimary = new ConditionalPrimary(); | ||
83 | $conditionalPrimary->simpleConditionalExpression = $expression; | ||
84 | if ($selectStatement->whereClause) { | ||
85 | if ($selectStatement->whereClause->conditionalExpression instanceof ConditionalTerm) { | ||
86 | $selectStatement->whereClause->conditionalExpression->conditionalFactors[] = $conditionalPrimary; | ||
87 | } elseif ($selectStatement->whereClause->conditionalExpression instanceof ConditionalPrimary) { | ||
88 | $selectStatement->whereClause->conditionalExpression = new ConditionalExpression( | ||
89 | [ | ||
90 | new ConditionalTerm( | ||
91 | [ | ||
92 | $selectStatement->whereClause->conditionalExpression, | ||
93 | $conditionalPrimary, | ||
94 | ], | ||
95 | ), | ||
96 | ], | ||
97 | ); | ||
98 | } else { | ||
99 | $tmpPrimary = new ConditionalPrimary(); | ||
100 | $tmpPrimary->conditionalExpression = $selectStatement->whereClause->conditionalExpression; | ||
101 | $selectStatement->whereClause->conditionalExpression = new ConditionalTerm( | ||
102 | [ | ||
103 | $tmpPrimary, | ||
104 | $conditionalPrimary, | ||
105 | ], | ||
106 | ); | ||
107 | } | ||
108 | } else { | ||
109 | $selectStatement->whereClause = new WhereClause( | ||
110 | new ConditionalExpression( | ||
111 | [new ConditionalTerm([$conditionalPrimary])], | ||
112 | ), | ||
113 | ); | ||
114 | } | ||
115 | } | ||
116 | } | ||