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.
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>.
20 namespace Doctrine\ORM\Query;
23 * Represents a chain of tree walkers that modify an AST and finally emit output.
24 * Only the last walker in the chain can emit output. Any previous walkers can modify
25 * the AST to influence the final output produced by the last walker.
27 * @author Roman Borschel <roman@code-factory.org>
30 class TreeWalkerChain implements TreeWalker
32 /** The tree walkers. */
33 private $_walkers = array();
34 /** The original Query. */
36 /** The ParserResult of the original query that was produced by the Parser. */
37 private $_parserResult;
38 /** The query components of the original query (the "symbol table") that was produced by the Parser. */
39 private $_queryComponents;
44 public function __construct($query, $parserResult, array $queryComponents)
46 $this->_query = $query;
47 $this->_parserResult = $parserResult;
48 $this->_queryComponents = $queryComponents;
52 * Adds a tree walker to the chain.
54 * @param string $walkerClass The class of the walker to instantiate.
56 public function addTreeWalker($walkerClass)
58 $this->_walkers[] = new $walkerClass($this->_query, $this->_parserResult, $this->_queryComponents);
62 * Walks down a SelectStatement AST node, thereby generating the appropriate SQL.
64 * @return string The SQL.
66 public function walkSelectStatement(AST\SelectStatement $AST)
68 foreach ($this->_walkers as $walker) {
69 $walker->walkSelectStatement($AST);
74 * Walks down a SelectClause AST node, thereby generating the appropriate SQL.
76 * @return string The SQL.
78 public function walkSelectClause($selectClause)
80 foreach ($this->_walkers as $walker) {
81 $walker->walkSelectClause($selectClause);
86 * Walks down a FromClause AST node, thereby generating the appropriate SQL.
88 * @return string The SQL.
90 public function walkFromClause($fromClause)
92 foreach ($this->_walkers as $walker) {
93 $walker->walkFromClause($fromClause);
98 * Walks down a FunctionNode AST node, thereby generating the appropriate SQL.
100 * @return string The SQL.
102 public function walkFunction($function)
104 foreach ($this->_walkers as $walker) {
105 $walker->walkFunction($function);
110 * Walks down an OrderByClause AST node, thereby generating the appropriate SQL.
112 * @param OrderByClause
113 * @return string The SQL.
115 public function walkOrderByClause($orderByClause)
117 foreach ($this->_walkers as $walker) {
118 $walker->walkOrderByClause($orderByClause);
123 * Walks down an OrderByItem AST node, thereby generating the appropriate SQL.
126 * @return string The SQL.
128 public function walkOrderByItem($orderByItem)
130 foreach ($this->_walkers as $walker) {
131 $walker->walkOrderByItem($orderByItem);
136 * Walks down a HavingClause AST node, thereby generating the appropriate SQL.
138 * @param HavingClause
139 * @return string The SQL.
141 public function walkHavingClause($havingClause)
143 foreach ($this->_walkers as $walker) {
144 $walker->walkHavingClause($havingClause);
149 * Walks down a Join AST node and creates the corresponding SQL.
152 * @return string The SQL.
154 public function walkJoin($join)
156 foreach ($this->_walkers as $walker) {
157 $walker->walkJoin($join);
162 * Walks down a SelectExpression AST node and generates the corresponding SQL.
164 * @param SelectExpression $selectExpression
165 * @return string The SQL.
167 public function walkSelectExpression($selectExpression)
169 foreach ($this->_walkers as $walker) {
170 $walker->walkSelectExpression($selectExpression);
175 * Walks down a QuantifiedExpression AST node, thereby generating the appropriate SQL.
177 * @param QuantifiedExpression
178 * @return string The SQL.
180 public function walkQuantifiedExpression($qExpr)
182 foreach ($this->_walkers as $walker) {
183 $walker->walkQuantifiedExpression($qExpr);
188 * Walks down a Subselect AST node, thereby generating the appropriate SQL.
191 * @return string The SQL.
193 public function walkSubselect($subselect)
195 foreach ($this->_walkers as $walker) {
196 $walker->walkSubselect($subselect);
201 * Walks down a SubselectFromClause AST node, thereby generating the appropriate SQL.
203 * @param SubselectFromClause
204 * @return string The SQL.
206 public function walkSubselectFromClause($subselectFromClause)
208 foreach ($this->_walkers as $walker) {
209 $walker->walkSubselectFromClause($subselectFromClause);
214 * Walks down a SimpleSelectClause AST node, thereby generating the appropriate SQL.
216 * @param SimpleSelectClause
217 * @return string The SQL.
219 public function walkSimpleSelectClause($simpleSelectClause)
221 foreach ($this->_walkers as $walker) {
222 $walker->walkSimpleSelectClause($simpleSelectClause);
227 * Walks down a SimpleSelectExpression AST node, thereby generating the appropriate SQL.
229 * @param SimpleSelectExpression
230 * @return string The SQL.
232 public function walkSimpleSelectExpression($simpleSelectExpression)
234 foreach ($this->_walkers as $walker) {
235 $walker->walkSimpleSelectExpression($simpleSelectExpression);
240 * Walks down an AggregateExpression AST node, thereby generating the appropriate SQL.
242 * @param AggregateExpression
243 * @return string The SQL.
245 public function walkAggregateExpression($aggExpression)
247 foreach ($this->_walkers as $walker) {
248 $walker->walkAggregateExpression($aggExpression);
253 * Walks down a GroupByClause AST node, thereby generating the appropriate SQL.
255 * @param GroupByClause
256 * @return string The SQL.
258 public function walkGroupByClause($groupByClause)
260 foreach ($this->_walkers as $walker) {
261 $walker->walkGroupByClause($groupByClause);
266 * Walks down a GroupByItem AST node, thereby generating the appropriate SQL.
269 * @return string The SQL.
271 public function walkGroupByItem($groupByItem)
273 foreach ($this->_walkers as $walker) {
274 $walker->walkGroupByItem($groupByItem);
279 * Walks down an UpdateStatement AST node, thereby generating the appropriate SQL.
281 * @param UpdateStatement
282 * @return string The SQL.
284 public function walkUpdateStatement(AST\UpdateStatement $AST)
286 foreach ($this->_walkers as $walker) {
287 $walker->walkUpdateStatement($AST);
292 * Walks down a DeleteStatement AST node, thereby generating the appropriate SQL.
294 * @param DeleteStatement
295 * @return string The SQL.
297 public function walkDeleteStatement(AST\DeleteStatement $AST)
299 foreach ($this->_walkers as $walker) {
300 $walker->walkDeleteStatement($AST);
305 * Walks down a DeleteClause AST node, thereby generating the appropriate SQL.
307 * @param DeleteClause
308 * @return string The SQL.
310 public function walkDeleteClause(AST\DeleteClause $deleteClause)
312 foreach ($this->_walkers as $walker) {
313 $walker->walkDeleteClause($deleteClause);
318 * Walks down an UpdateClause AST node, thereby generating the appropriate SQL.
320 * @param UpdateClause
321 * @return string The SQL.
323 public function walkUpdateClause($updateClause)
325 foreach ($this->_walkers as $walker) {
326 $walker->walkUpdateClause($updateClause);
331 * Walks down an UpdateItem AST node, thereby generating the appropriate SQL.
334 * @return string The SQL.
336 public function walkUpdateItem($updateItem)
338 foreach ($this->_walkers as $walker) {
339 $walker->walkUpdateItem($updateItem);
344 * Walks down a WhereClause AST node, thereby generating the appropriate SQL.
347 * @return string The SQL.
349 public function walkWhereClause($whereClause)
351 foreach ($this->_walkers as $walker) {
352 $walker->walkWhereClause($whereClause);
357 * Walks down a ConditionalExpression AST node, thereby generating the appropriate SQL.
359 * @param ConditionalExpression
360 * @return string The SQL.
362 public function walkConditionalExpression($condExpr)
364 foreach ($this->_walkers as $walker) {
365 $walker->walkConditionalExpression($condExpr);
370 * Walks down a ConditionalTerm AST node, thereby generating the appropriate SQL.
372 * @param ConditionalTerm
373 * @return string The SQL.
375 public function walkConditionalTerm($condTerm)
377 foreach ($this->_walkers as $walker) {
378 $walker->walkConditionalTerm($condTerm);
383 * Walks down a ConditionalFactor AST node, thereby generating the appropriate SQL.
385 * @param ConditionalFactor
386 * @return string The SQL.
388 public function walkConditionalFactor($factor)
390 foreach ($this->_walkers as $walker) {
391 $walker->walkConditionalFactor($factor);
396 * Walks down a ConditionalPrimary AST node, thereby generating the appropriate SQL.
398 * @param ConditionalPrimary
399 * @return string The SQL.
401 public function walkConditionalPrimary($condPrimary)
403 foreach ($this->_walkers as $walker) {
404 $walker->walkConditionalPrimary($condPrimary);
409 * Walks down an ExistsExpression AST node, thereby generating the appropriate SQL.
411 * @param ExistsExpression
412 * @return string The SQL.
414 public function walkExistsExpression($existsExpr)
416 foreach ($this->_walkers as $walker) {
417 $walker->walkExistsExpression($existsExpr);
422 * Walks down a CollectionMemberExpression AST node, thereby generating the appropriate SQL.
424 * @param CollectionMemberExpression
425 * @return string The SQL.
427 public function walkCollectionMemberExpression($collMemberExpr)
429 foreach ($this->_walkers as $walker) {
430 $walker->walkCollectionMemberExpression($collMemberExpr);
435 * Walks down an EmptyCollectionComparisonExpression AST node, thereby generating the appropriate SQL.
437 * @param EmptyCollectionComparisonExpression
438 * @return string The SQL.
440 public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
442 foreach ($this->_walkers as $walker) {
443 $walker->walkEmptyCollectionComparisonExpression($emptyCollCompExpr);
448 * Walks down a NullComparisonExpression AST node, thereby generating the appropriate SQL.
450 * @param NullComparisonExpression
451 * @return string The SQL.
453 public function walkNullComparisonExpression($nullCompExpr)
455 foreach ($this->_walkers as $walker) {
456 $walker->walkNullComparisonExpression($nullCompExpr);
461 * Walks down an InExpression AST node, thereby generating the appropriate SQL.
463 * @param InExpression
464 * @return string The SQL.
466 public function walkInExpression($inExpr)
468 foreach ($this->_walkers as $walker) {
469 $walker->walkInExpression($inExpr);
474 * Walks down an InstanceOfExpression AST node, thereby generating the appropriate SQL.
476 * @param InstanceOfExpression
477 * @return string The SQL.
479 function walkInstanceOfExpression($instanceOfExpr)
481 foreach ($this->_walkers as $walker) {
482 $walker->walkInstanceOfExpression($instanceOfExpr);
487 * Walks down a literal that represents an AST node, thereby generating the appropriate SQL.
490 * @return string The SQL.
492 public function walkLiteral($literal)
494 foreach ($this->_walkers as $walker) {
495 $walker->walkLiteral($literal);
500 * Walks down a BetweenExpression AST node, thereby generating the appropriate SQL.
502 * @param BetweenExpression
503 * @return string The SQL.
505 public function walkBetweenExpression($betweenExpr)
507 foreach ($this->_walkers as $walker) {
508 $walker->walkBetweenExpression($betweenExpr);
513 * Walks down a LikeExpression AST node, thereby generating the appropriate SQL.
515 * @param LikeExpression
516 * @return string The SQL.
518 public function walkLikeExpression($likeExpr)
520 foreach ($this->_walkers as $walker) {
521 $walker->walkLikeExpression($likeExpr);
526 * Walks down a StateFieldPathExpression AST node, thereby generating the appropriate SQL.
528 * @param StateFieldPathExpression
529 * @return string The SQL.
531 public function walkStateFieldPathExpression($stateFieldPathExpression)
533 foreach ($this->_walkers as $walker) {
534 $walker->walkStateFieldPathExpression($stateFieldPathExpression);
539 * Walks down a ComparisonExpression AST node, thereby generating the appropriate SQL.
541 * @param ComparisonExpression
542 * @return string The SQL.
544 public function walkComparisonExpression($compExpr)
546 foreach ($this->_walkers as $walker) {
547 $walker->walkComparisonExpression($compExpr);
552 * Walks down an InputParameter AST node, thereby generating the appropriate SQL.
554 * @param InputParameter
555 * @return string The SQL.
557 public function walkInputParameter($inputParam)
559 foreach ($this->_walkers as $walker) {
560 $walker->walkInputParameter($inputParam);
565 * Walks down an ArithmeticExpression AST node, thereby generating the appropriate SQL.
567 * @param ArithmeticExpression
568 * @return string The SQL.
570 public function walkArithmeticExpression($arithmeticExpr)
572 foreach ($this->_walkers as $walker) {
573 $walker->walkArithmeticExpression($arithmeticExpr);
578 * Walks down an ArithmeticTerm AST node, thereby generating the appropriate SQL.
581 * @return string The SQL.
583 public function walkArithmeticTerm($term)
585 foreach ($this->_walkers as $walker) {
586 $walker->walkArithmeticTerm($term);
591 * Walks down a StringPrimary that represents an AST node, thereby generating the appropriate SQL.
594 * @return string The SQL.
596 public function walkStringPrimary($stringPrimary)
598 foreach ($this->_walkers as $walker) {
599 $walker->walkStringPrimary($stringPrimary);
604 * Walks down an ArithmeticFactor that represents an AST node, thereby generating the appropriate SQL.
607 * @return string The SQL.
609 public function walkArithmeticFactor($factor)
611 foreach ($this->_walkers as $walker) {
612 $walker->walkArithmeticFactor($factor);
617 * Walks down an SimpleArithmeticExpression AST node, thereby generating the appropriate SQL.
619 * @param SimpleArithmeticExpression
620 * @return string The SQL.
622 public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
624 foreach ($this->_walkers as $walker) {
625 $walker->walkSimpleArithmeticExpression($simpleArithmeticExpr);
630 * Walks down an PathExpression AST node, thereby generating the appropriate SQL.
633 * @return string The SQL.
635 public function walkPathExpression($pathExpr)
637 foreach ($this->_walkers as $walker) {
638 $walker->walkPathExpression($pathExpr);
643 * Walks down an ResultVariable AST node, thereby generating the appropriate SQL.
645 * @param string $resultVariable
646 * @return string The SQL.
648 public function walkResultVariable($resultVariable)
650 foreach ($this->_walkers as $walker) {
651 $walker->walkResultVariable($resultVariable);
656 * Gets an executor that can be used to execute the result of this walker.
658 * @return AbstractExecutor
660 public function getExecutor($AST)