These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
14 | * |
||
15 | * This software consists of voluntary contributions made by many individuals |
||
16 | * and is licensed under the MIT license. For more information, see |
||
17 | * <http://www.doctrine-project.org>. |
||
18 | */ |
||
19 | |||
20 | namespace Doctrine\ORM\Tools\Pagination; |
||
21 | |||
22 | use Doctrine\ORM\Query\SqlWalker; |
||
23 | use Doctrine\ORM\Query\AST\SelectStatement; |
||
24 | |||
25 | /** |
||
26 | * Wraps the query in order to accurately count the root objects. |
||
27 | * |
||
28 | * Given a DQL like `SELECT u FROM User u` it will generate an SQL query like: |
||
29 | * SELECT COUNT(*) (SELECT DISTINCT <id> FROM (<original SQL>)) |
||
30 | * |
||
31 | * Works with composite keys but cannot deal with queries that have multiple |
||
32 | * root entities (e.g. `SELECT f, b from Foo, Bar`) |
||
33 | * |
||
34 | * @author Sander Marechal <[email protected]> |
||
35 | */ |
||
36 | class CountOutputWalker extends SqlWalker |
||
37 | { |
||
38 | /** |
||
39 | * @var \Doctrine\DBAL\Platforms\AbstractPlatform |
||
40 | */ |
||
41 | private $platform; |
||
0 ignored issues
–
show
Comprehensibility
introduced
by
![]() |
|||
42 | |||
43 | /** |
||
44 | * @var \Doctrine\ORM\Query\ResultSetMapping |
||
45 | */ |
||
46 | private $rsm; |
||
0 ignored issues
–
show
|
|||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private $queryComponents; |
||
0 ignored issues
–
show
|
|||
52 | |||
53 | /** |
||
54 | * Constructor. |
||
55 | * |
||
56 | * Stores various parameters that are otherwise unavailable |
||
57 | * because Doctrine\ORM\Query\SqlWalker keeps everything private without |
||
58 | * accessors. |
||
59 | * |
||
60 | * @param \Doctrine\ORM\Query $query |
||
61 | * @param \Doctrine\ORM\Query\ParserResult $parserResult |
||
62 | * @param array $queryComponents |
||
63 | */ |
||
64 | 12 | public function __construct($query, $parserResult, array $queryComponents) |
|
65 | { |
||
66 | 12 | $this->platform = $query->getEntityManager()->getConnection()->getDatabasePlatform(); |
|
67 | 12 | $this->rsm = $parserResult->getResultSetMapping(); |
|
68 | 12 | $this->queryComponents = $queryComponents; |
|
69 | |||
70 | 12 | parent::__construct($query, $parserResult, $queryComponents); |
|
71 | 12 | } |
|
72 | |||
73 | /** |
||
74 | * Walks down a SelectStatement AST node, wrapping it in a COUNT (SELECT DISTINCT). |
||
75 | * |
||
76 | * Note that the ORDER BY clause is not removed. Many SQL implementations (e.g. MySQL) |
||
77 | * are able to cache subqueries. By keeping the ORDER BY clause intact, the limitSubQuery |
||
78 | * that will most likely be executed next can be read from the native SQL cache. |
||
79 | * |
||
80 | * @param SelectStatement $AST |
||
81 | * |
||
82 | * @return string |
||
83 | * |
||
84 | * @throws \RuntimeException |
||
85 | */ |
||
86 | 12 | public function walkSelectStatement(SelectStatement $AST) |
|
87 | { |
||
88 | 12 | if ($this->platform->getName() === "mssql") { |
|
89 | $AST->orderByClause = null; |
||
90 | } |
||
91 | |||
92 | 12 | $sql = parent::walkSelectStatement($AST); |
|
93 | |||
94 | // Find out the SQL alias of the identifier column of the root entity |
||
95 | // It may be possible to make this work with multiple root entities but that |
||
96 | // would probably require issuing multiple queries or doing a UNION SELECT |
||
97 | // so for now, It's not supported. |
||
98 | |||
99 | // Get the root entity and alias from the AST fromClause |
||
100 | 12 | $from = $AST->fromClause->identificationVariableDeclarations; |
|
101 | 12 | if (count($from) > 1) { |
|
102 | throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); |
||
103 | } |
||
104 | |||
105 | 12 | $fromRoot = reset($from); |
|
106 | 12 | $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; |
|
107 | 12 | $rootClass = $this->queryComponents[$rootAlias]['metadata']; |
|
108 | 12 | $rootIdentifier = $rootClass->identifier; |
|
109 | |||
110 | // For every identifier, find out the SQL alias by combing through the ResultSetMapping |
||
111 | 12 | $sqlIdentifier = []; |
|
112 | 12 | foreach ($rootIdentifier as $property) { |
|
113 | 12 | if (isset($rootClass->fieldMappings[$property])) { |
|
114 | 11 | foreach (array_keys($this->rsm->fieldMappings, $property) as $alias) { |
|
115 | 11 | if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) { |
|
116 | 11 | $sqlIdentifier[$property] = $alias; |
|
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | 12 | if (isset($rootClass->associationMappings[$property])) { |
|
122 | 1 | $joinColumn = $rootClass->associationMappings[$property]['joinColumns'][0]['name']; |
|
123 | |||
124 | 1 | foreach (array_keys($this->rsm->metaMappings, $joinColumn) as $alias) { |
|
125 | 1 | if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) { |
|
126 | 12 | $sqlIdentifier[$property] = $alias; |
|
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | 12 | if (count($rootIdentifier) != count($sqlIdentifier)) { |
|
133 | throw new \RuntimeException(sprintf( |
||
134 | 'Not all identifier properties can be found in the ResultSetMapping: %s', |
||
135 | implode(', ', array_diff($rootIdentifier, array_keys($sqlIdentifier))) |
||
136 | )); |
||
137 | } |
||
138 | |||
139 | // Build the counter query |
||
140 | 12 | return sprintf('SELECT %s AS dctrn_count FROM (SELECT DISTINCT %s FROM (%s) dctrn_result) dctrn_table', |
|
141 | 12 | $this->platform->getCountExpression('*'), |
|
142 | 12 | implode(', ', $sqlIdentifier), |
|
143 | 12 | $sql |
|
144 | ); |
||
145 | } |
||
146 | } |
||
147 |