PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Doctrine/ORM/Query/TreeWalkerChain.php

https://bitbucket.org/chives/doctrine2
PHP | 691 lines | 310 code | 56 blank | 325 comment | 1 complexity | 3459b3b100ddc9fb871981b43d28f0b2 MD5 | raw file
  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
 20namespace Doctrine\ORM\Query;
 21
 22/**
 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.
 26 *
 27 * @author Roman Borschel <roman@code-factory.org>
 28 * @since 2.0
 29 */
 30class TreeWalkerChain implements TreeWalker
 31{
 32    /** The tree walkers. */
 33    private $_walkers = array();
 34    /** The original Query. */
 35    private $_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;
 40
 41    /**
 42     * Return internal queryComponents array
 43     *
 44     * @return array
 45     */
 46    public function getQueryComponents()
 47    {
 48        return $this->_queryComponents;
 49    }
 50
 51    /**
 52     * Set or override a query component for a given dql alias.
 53     *
 54     * @param string $dqlAlias The DQL alias.
 55     * @param array $queryComponent
 56     */
 57    public function setQueryComponent($dqlAlias, array $queryComponent)
 58    {
 59        $requiredKeys = array('metadata', 'parent', 'relation', 'map', 'nestingLevel', 'token');
 60
 61        if (array_diff($requiredKeys, array_keys($queryComponent))) {
 62            throw QueryException::invalidQueryComponent($dqlAlias);
 63        }
 64
 65        $this->_queryComponents[$dqlAlias] = $queryComponent;
 66    }
 67
 68    /**
 69     * @inheritdoc
 70     */
 71    public function __construct($query, $parserResult, array $queryComponents)
 72    {
 73        $this->_query = $query;
 74        $this->_parserResult = $parserResult;
 75        $this->_queryComponents = $queryComponents;
 76    }
 77
 78    /**
 79     * Adds a tree walker to the chain.
 80     *
 81     * @param string $walkerClass The class of the walker to instantiate.
 82     */
 83    public function addTreeWalker($walkerClass)
 84    {
 85        $this->_walkers[] = new $walkerClass($this->_query, $this->_parserResult, $this->_queryComponents);
 86    }
 87
 88    /**
 89     * Walks down a SelectStatement AST node, thereby generating the appropriate SQL.
 90     *
 91     * @return string The SQL.
 92     */
 93    public function walkSelectStatement(AST\SelectStatement $AST)
 94    {
 95        foreach ($this->_walkers as $walker) {
 96            $walker->walkSelectStatement($AST);
 97
 98            $this->_queryComponents = $walker->getQueryComponents();
 99        }
100    }
101
102    /**
103     * Walks down a SelectClause AST node, thereby generating the appropriate SQL.
104     *
105     * @return string The SQL.
106     */
107    public function walkSelectClause($selectClause)
108    {
109        foreach ($this->_walkers as $walker) {
110            $walker->walkSelectClause($selectClause);
111        }
112    }
113
114    /**
115     * Walks down a FromClause AST node, thereby generating the appropriate SQL.
116     *
117     * @return string The SQL.
118     */
119    public function walkFromClause($fromClause)
120    {
121        foreach ($this->_walkers as $walker) {
122            $walker->walkFromClause($fromClause);
123        }
124    }
125
126    /**
127     * Walks down a FunctionNode AST node, thereby generating the appropriate SQL.
128     *
129     * @return string The SQL.
130     */
131    public function walkFunction($function)
132    {
133        foreach ($this->_walkers as $walker) {
134            $walker->walkFunction($function);
135        }
136    }
137
138    /**
139     * Walks down an OrderByClause AST node, thereby generating the appropriate SQL.
140     *
141     * @param OrderByClause
142     * @return string The SQL.
143     */
144    public function walkOrderByClause($orderByClause)
145    {
146        foreach ($this->_walkers as $walker) {
147            $walker->walkOrderByClause($orderByClause);
148        }
149    }
150
151    /**
152     * Walks down an OrderByItem AST node, thereby generating the appropriate SQL.
153     *
154     * @param OrderByItem
155     * @return string The SQL.
156     */
157    public function walkOrderByItem($orderByItem)
158    {
159        foreach ($this->_walkers as $walker) {
160            $walker->walkOrderByItem($orderByItem);
161        }
162    }
163
164    /**
165     * Walks down a HavingClause AST node, thereby generating the appropriate SQL.
166     *
167     * @param HavingClause
168     * @return string The SQL.
169     */
170    public function walkHavingClause($havingClause)
171    {
172        foreach ($this->_walkers as $walker) {
173            $walker->walkHavingClause($havingClause);
174        }
175    }
176
177    /**
178     * Walks down a Join AST node and creates the corresponding SQL.
179     *
180     * @param Join $join
181     * @return string The SQL.
182     */
183    public function walkJoin($join)
184    {
185        foreach ($this->_walkers as $walker) {
186            $walker->walkJoin($join);
187        }
188    }
189
190    /**
191     * Walks down a SelectExpression AST node and generates the corresponding SQL.
192     *
193     * @param SelectExpression $selectExpression
194     * @return string The SQL.
195     */
196    public function walkSelectExpression($selectExpression)
197    {
198        foreach ($this->_walkers as $walker) {
199            $walker->walkSelectExpression($selectExpression);
200        }
201    }
202
203    /**
204     * Walks down a QuantifiedExpression AST node, thereby generating the appropriate SQL.
205     *
206     * @param QuantifiedExpression
207     * @return string The SQL.
208     */
209    public function walkQuantifiedExpression($qExpr)
210    {
211        foreach ($this->_walkers as $walker) {
212            $walker->walkQuantifiedExpression($qExpr);
213        }
214    }
215
216    /**
217     * Walks down a Subselect AST node, thereby generating the appropriate SQL.
218     *
219     * @param Subselect
220     * @return string The SQL.
221     */
222    public function walkSubselect($subselect)
223    {
224        foreach ($this->_walkers as $walker) {
225            $walker->walkSubselect($subselect);
226        }
227    }
228
229    /**
230     * Walks down a SubselectFromClause AST node, thereby generating the appropriate SQL.
231     *
232     * @param SubselectFromClause
233     * @return string The SQL.
234     */
235    public function walkSubselectFromClause($subselectFromClause)
236    {
237        foreach ($this->_walkers as $walker) {
238            $walker->walkSubselectFromClause($subselectFromClause);
239        }
240    }
241
242    /**
243     * Walks down a SimpleSelectClause AST node, thereby generating the appropriate SQL.
244     *
245     * @param SimpleSelectClause
246     * @return string The SQL.
247     */
248    public function walkSimpleSelectClause($simpleSelectClause)
249    {
250        foreach ($this->_walkers as $walker) {
251            $walker->walkSimpleSelectClause($simpleSelectClause);
252        }
253    }
254
255    /**
256     * Walks down a SimpleSelectExpression AST node, thereby generating the appropriate SQL.
257     *
258     * @param SimpleSelectExpression
259     * @return string The SQL.
260     */
261    public function walkSimpleSelectExpression($simpleSelectExpression)
262    {
263        foreach ($this->_walkers as $walker) {
264            $walker->walkSimpleSelectExpression($simpleSelectExpression);
265        }
266    }
267
268    /**
269     * Walks down an AggregateExpression AST node, thereby generating the appropriate SQL.
270     *
271     * @param AggregateExpression
272     * @return string The SQL.
273     */
274    public function walkAggregateExpression($aggExpression)
275    {
276        foreach ($this->_walkers as $walker) {
277            $walker->walkAggregateExpression($aggExpression);
278        }
279    }
280
281    /**
282     * Walks down a GroupByClause AST node, thereby generating the appropriate SQL.
283     *
284     * @param GroupByClause
285     * @return string The SQL.
286     */
287    public function walkGroupByClause($groupByClause)
288    {
289        foreach ($this->_walkers as $walker) {
290            $walker->walkGroupByClause($groupByClause);
291        }
292    }
293
294    /**
295     * Walks down a GroupByItem AST node, thereby generating the appropriate SQL.
296     *
297     * @param GroupByItem
298     * @return string The SQL.
299     */
300    public function walkGroupByItem($groupByItem)
301    {
302        foreach ($this->_walkers as $walker) {
303            $walker->walkGroupByItem($groupByItem);
304        }
305    }
306
307    /**
308     * Walks down an UpdateStatement AST node, thereby generating the appropriate SQL.
309     *
310     * @param UpdateStatement
311     * @return string The SQL.
312     */
313    public function walkUpdateStatement(AST\UpdateStatement $AST)
314    {
315        foreach ($this->_walkers as $walker) {
316            $walker->walkUpdateStatement($AST);
317        }
318    }
319
320    /**
321     * Walks down a DeleteStatement AST node, thereby generating the appropriate SQL.
322     *
323     * @param DeleteStatement
324     * @return string The SQL.
325     */
326    public function walkDeleteStatement(AST\DeleteStatement $AST)
327    {
328        foreach ($this->_walkers as $walker) {
329            $walker->walkDeleteStatement($AST);
330        }
331    }
332
333    /**
334     * Walks down a DeleteClause AST node, thereby generating the appropriate SQL.
335     *
336     * @param DeleteClause
337     * @return string The SQL.
338     */
339    public function walkDeleteClause(AST\DeleteClause $deleteClause)
340    {
341        foreach ($this->_walkers as $walker) {
342            $walker->walkDeleteClause($deleteClause);
343        }
344    }
345
346    /**
347     * Walks down an UpdateClause AST node, thereby generating the appropriate SQL.
348     *
349     * @param UpdateClause
350     * @return string The SQL.
351     */
352    public function walkUpdateClause($updateClause)
353    {
354        foreach ($this->_walkers as $walker) {
355            $walker->walkUpdateClause($updateClause);
356        }
357    }
358
359    /**
360     * Walks down an UpdateItem AST node, thereby generating the appropriate SQL.
361     *
362     * @param UpdateItem
363     * @return string The SQL.
364     */
365    public function walkUpdateItem($updateItem)
366    {
367        foreach ($this->_walkers as $walker) {
368            $walker->walkUpdateItem($updateItem);
369        }
370    }
371
372    /**
373     * Walks down a WhereClause AST node, thereby generating the appropriate SQL.
374     *
375     * @param WhereClause
376     * @return string The SQL.
377     */
378    public function walkWhereClause($whereClause)
379    {
380        foreach ($this->_walkers as $walker) {
381            $walker->walkWhereClause($whereClause);
382        }
383    }
384
385    /**
386     * Walks down a ConditionalExpression AST node, thereby generating the appropriate SQL.
387     *
388     * @param ConditionalExpression
389     * @return string The SQL.
390     */
391    public function walkConditionalExpression($condExpr)
392    {
393        foreach ($this->_walkers as $walker) {
394            $walker->walkConditionalExpression($condExpr);
395        }
396    }
397
398    /**
399     * Walks down a ConditionalTerm AST node, thereby generating the appropriate SQL.
400     *
401     * @param ConditionalTerm
402     * @return string The SQL.
403     */
404    public function walkConditionalTerm($condTerm)
405    {
406        foreach ($this->_walkers as $walker) {
407            $walker->walkConditionalTerm($condTerm);
408        }
409    }
410
411    /**
412     * Walks down a ConditionalFactor AST node, thereby generating the appropriate SQL.
413     *
414     * @param ConditionalFactor
415     * @return string The SQL.
416     */
417    public function walkConditionalFactor($factor)
418    {
419        foreach ($this->_walkers as $walker) {
420            $walker->walkConditionalFactor($factor);
421        }
422    }
423
424    /**
425     * Walks down a ConditionalPrimary AST node, thereby generating the appropriate SQL.
426     *
427     * @param ConditionalPrimary
428     * @return string The SQL.
429     */
430    public function walkConditionalPrimary($condPrimary)
431    {
432        foreach ($this->_walkers as $walker) {
433            $walker->walkConditionalPrimary($condPrimary);
434        }
435    }
436
437    /**
438     * Walks down an ExistsExpression AST node, thereby generating the appropriate SQL.
439     *
440     * @param ExistsExpression
441     * @return string The SQL.
442     */
443    public function walkExistsExpression($existsExpr)
444    {
445        foreach ($this->_walkers as $walker) {
446            $walker->walkExistsExpression($existsExpr);
447        }
448    }
449
450    /**
451     * Walks down a CollectionMemberExpression AST node, thereby generating the appropriate SQL.
452     *
453     * @param CollectionMemberExpression
454     * @return string The SQL.
455     */
456    public function walkCollectionMemberExpression($collMemberExpr)
457    {
458        foreach ($this->_walkers as $walker) {
459            $walker->walkCollectionMemberExpression($collMemberExpr);
460        }
461    }
462
463    /**
464     * Walks down an EmptyCollectionComparisonExpression AST node, thereby generating the appropriate SQL.
465     *
466     * @param EmptyCollectionComparisonExpression
467     * @return string The SQL.
468     */
469    public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
470    {
471        foreach ($this->_walkers as $walker) {
472            $walker->walkEmptyCollectionComparisonExpression($emptyCollCompExpr);
473        }
474    }
475
476    /**
477     * Walks down a NullComparisonExpression AST node, thereby generating the appropriate SQL.
478     *
479     * @param NullComparisonExpression
480     * @return string The SQL.
481     */
482    public function walkNullComparisonExpression($nullCompExpr)
483    {
484        foreach ($this->_walkers as $walker) {
485            $walker->walkNullComparisonExpression($nullCompExpr);
486        }
487    }
488
489    /**
490     * Walks down an InExpression AST node, thereby generating the appropriate SQL.
491     *
492     * @param InExpression
493     * @return string The SQL.
494     */
495    public function walkInExpression($inExpr)
496    {
497        foreach ($this->_walkers as $walker) {
498            $walker->walkInExpression($inExpr);
499        }
500    }
501
502    /**
503     * Walks down an InstanceOfExpression AST node, thereby generating the appropriate SQL.
504     *
505     * @param InstanceOfExpression
506     * @return string The SQL.
507     */
508    function walkInstanceOfExpression($instanceOfExpr)
509    {
510        foreach ($this->_walkers as $walker) {
511            $walker->walkInstanceOfExpression($instanceOfExpr);
512        }
513    }
514
515    /**
516     * Walks down a literal that represents an AST node, thereby generating the appropriate SQL.
517     *
518     * @param mixed
519     * @return string The SQL.
520     */
521    public function walkLiteral($literal)
522    {
523        foreach ($this->_walkers as $walker) {
524            $walker->walkLiteral($literal);
525        }
526    }
527
528    /**
529     * Walks down a BetweenExpression AST node, thereby generating the appropriate SQL.
530     *
531     * @param BetweenExpression
532     * @return string The SQL.
533     */
534    public function walkBetweenExpression($betweenExpr)
535    {
536        foreach ($this->_walkers as $walker) {
537            $walker->walkBetweenExpression($betweenExpr);
538        }
539    }
540
541    /**
542     * Walks down a LikeExpression AST node, thereby generating the appropriate SQL.
543     *
544     * @param LikeExpression
545     * @return string The SQL.
546     */
547    public function walkLikeExpression($likeExpr)
548    {
549        foreach ($this->_walkers as $walker) {
550            $walker->walkLikeExpression($likeExpr);
551        }
552    }
553
554    /**
555     * Walks down a StateFieldPathExpression AST node, thereby generating the appropriate SQL.
556     *
557     * @param StateFieldPathExpression
558     * @return string The SQL.
559     */
560    public function walkStateFieldPathExpression($stateFieldPathExpression)
561    {
562        foreach ($this->_walkers as $walker) {
563            $walker->walkStateFieldPathExpression($stateFieldPathExpression);
564        }
565    }
566
567    /**
568     * Walks down a ComparisonExpression AST node, thereby generating the appropriate SQL.
569     *
570     * @param ComparisonExpression
571     * @return string The SQL.
572     */
573    public function walkComparisonExpression($compExpr)
574    {
575        foreach ($this->_walkers as $walker) {
576            $walker->walkComparisonExpression($compExpr);
577        }
578    }
579
580    /**
581     * Walks down an InputParameter AST node, thereby generating the appropriate SQL.
582     *
583     * @param InputParameter
584     * @return string The SQL.
585     */
586    public function walkInputParameter($inputParam)
587    {
588        foreach ($this->_walkers as $walker) {
589            $walker->walkInputParameter($inputParam);
590        }
591    }
592
593    /**
594     * Walks down an ArithmeticExpression AST node, thereby generating the appropriate SQL.
595     *
596     * @param ArithmeticExpression
597     * @return string The SQL.
598     */
599    public function walkArithmeticExpression($arithmeticExpr)
600    {
601        foreach ($this->_walkers as $walker) {
602            $walker->walkArithmeticExpression($arithmeticExpr);
603        }
604    }
605
606    /**
607     * Walks down an ArithmeticTerm AST node, thereby generating the appropriate SQL.
608     *
609     * @param mixed
610     * @return string The SQL.
611     */
612    public function walkArithmeticTerm($term)
613    {
614        foreach ($this->_walkers as $walker) {
615            $walker->walkArithmeticTerm($term);
616        }
617    }
618
619    /**
620     * Walks down a StringPrimary that represents an AST node, thereby generating the appropriate SQL.
621     *
622     * @param mixed
623     * @return string The SQL.
624     */
625    public function walkStringPrimary($stringPrimary)
626    {
627        foreach ($this->_walkers as $walker) {
628            $walker->walkStringPrimary($stringPrimary);
629        }
630    }
631
632    /**
633     * Walks down an ArithmeticFactor that represents an AST node, thereby generating the appropriate SQL.
634     *
635     * @param mixed
636     * @return string The SQL.
637     */
638    public function walkArithmeticFactor($factor)
639    {
640        foreach ($this->_walkers as $walker) {
641            $walker->walkArithmeticFactor($factor);
642        }
643    }
644
645    /**
646     * Walks down an SimpleArithmeticExpression AST node, thereby generating the appropriate SQL.
647     *
648     * @param SimpleArithmeticExpression
649     * @return string The SQL.
650     */
651    public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
652    {
653        foreach ($this->_walkers as $walker) {
654            $walker->walkSimpleArithmeticExpression($simpleArithmeticExpr);
655        }
656    }
657
658    /**
659     * Walks down an PathExpression AST node, thereby generating the appropriate SQL.
660     *
661     * @param mixed
662     * @return string The SQL.
663     */
664    public function walkPathExpression($pathExpr)
665    {
666        foreach ($this->_walkers as $walker) {
667            $walker->walkPathExpression($pathExpr);
668        }
669    }
670
671    /**
672     * Walks down an ResultVariable AST node, thereby generating the appropriate SQL.
673     *
674     * @param string $resultVariable
675     * @return string The SQL.
676     */
677    public function walkResultVariable($resultVariable)
678    {
679        foreach ($this->_walkers as $walker) {
680            $walker->walkResultVariable($resultVariable);
681        }
682    }
683
684    /**
685     * Gets an executor that can be used to execute the result of this walker.
686     *
687     * @return AbstractExecutor
688     */
689    public function getExecutor($AST)
690    {}
691}