summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Tools/Pagination/WhereInWalker.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
committerpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
commitbf6655a534a6775d30cafa67bd801276bda1d98d (patch)
treec6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/doctrine/orm/src/Tools/Pagination/WhereInWalker.php
parent94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff)
downloadAppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/orm/src/Tools/Pagination/WhereInWalker.php')
-rw-r--r--vendor/doctrine/orm/src/Tools/Pagination/WhereInWalker.php116
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
3declare(strict_types=1);
4
5namespace Doctrine\ORM\Tools\Pagination;
6
7use Doctrine\ORM\Query\AST\ArithmeticExpression;
8use Doctrine\ORM\Query\AST\ConditionalExpression;
9use Doctrine\ORM\Query\AST\ConditionalPrimary;
10use Doctrine\ORM\Query\AST\ConditionalTerm;
11use Doctrine\ORM\Query\AST\InListExpression;
12use Doctrine\ORM\Query\AST\InputParameter;
13use Doctrine\ORM\Query\AST\NullComparisonExpression;
14use Doctrine\ORM\Query\AST\PathExpression;
15use Doctrine\ORM\Query\AST\SelectStatement;
16use Doctrine\ORM\Query\AST\SimpleArithmeticExpression;
17use Doctrine\ORM\Query\AST\WhereClause;
18use Doctrine\ORM\Query\TreeWalkerAdapter;
19use RuntimeException;
20
21use function count;
22use 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 */
33class 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}