|
1 <?php |
|
2 /* |
|
3 * $Id$ |
|
4 * |
|
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
16 * |
|
17 * This software consists of voluntary contributions made by many individuals |
|
18 * and is licensed under the LGPL. For more information, see |
|
19 * <http://www.doctrine-project.org>. |
|
20 */ |
|
21 |
|
22 namespace Doctrine\ORM\Query; |
|
23 |
|
24 /** |
|
25 * Represents a chain of tree walkers that modify an AST and finally emit output. |
|
26 * Only the last walker in the chain can emit output. Any previous walkers can modify |
|
27 * the AST to influence the final output produced by the last walker. |
|
28 * |
|
29 * @author Roman Borschel <roman@code-factory.org> |
|
30 * @since 2.0 |
|
31 */ |
|
32 class TreeWalkerChain implements TreeWalker |
|
33 { |
|
34 /** The tree walkers. */ |
|
35 private $_walkers = array(); |
|
36 /** The original Query. */ |
|
37 private $_query; |
|
38 /** The ParserResult of the original query that was produced by the Parser. */ |
|
39 private $_parserResult; |
|
40 /** The query components of the original query (the "symbol table") that was produced by the Parser. */ |
|
41 private $_queryComponents; |
|
42 |
|
43 /** |
|
44 * @inheritdoc |
|
45 */ |
|
46 public function __construct($query, $parserResult, array $queryComponents) |
|
47 { |
|
48 $this->_query = $query; |
|
49 $this->_parserResult = $parserResult; |
|
50 $this->_queryComponents = $queryComponents; |
|
51 } |
|
52 |
|
53 /** |
|
54 * Adds a tree walker to the chain. |
|
55 * |
|
56 * @param string $walkerClass The class of the walker to instantiate. |
|
57 */ |
|
58 public function addTreeWalker($walkerClass) |
|
59 { |
|
60 $this->_walkers[] = new $walkerClass($this->_query, $this->_parserResult, $this->_queryComponents); |
|
61 } |
|
62 |
|
63 /** |
|
64 * Walks down a SelectStatement AST node, thereby generating the appropriate SQL. |
|
65 * |
|
66 * @return string The SQL. |
|
67 */ |
|
68 public function walkSelectStatement(AST\SelectStatement $AST) |
|
69 { |
|
70 foreach ($this->_walkers as $walker) { |
|
71 $walker->walkSelectStatement($AST); |
|
72 } |
|
73 } |
|
74 |
|
75 /** |
|
76 * Walks down a SelectClause AST node, thereby generating the appropriate SQL. |
|
77 * |
|
78 * @return string The SQL. |
|
79 */ |
|
80 public function walkSelectClause($selectClause) |
|
81 { |
|
82 foreach ($this->_walkers as $walker) { |
|
83 $walker->walkSelectClause($selectClause); |
|
84 } |
|
85 } |
|
86 |
|
87 /** |
|
88 * Walks down a FromClause AST node, thereby generating the appropriate SQL. |
|
89 * |
|
90 * @return string The SQL. |
|
91 */ |
|
92 public function walkFromClause($fromClause) |
|
93 { |
|
94 foreach ($this->_walkers as $walker) { |
|
95 $walker->walkFromClause($fromClause); |
|
96 } |
|
97 } |
|
98 |
|
99 /** |
|
100 * Walks down a FunctionNode AST node, thereby generating the appropriate SQL. |
|
101 * |
|
102 * @return string The SQL. |
|
103 */ |
|
104 public function walkFunction($function) |
|
105 { |
|
106 foreach ($this->_walkers as $walker) { |
|
107 $walker->walkFunction($function); |
|
108 } |
|
109 } |
|
110 |
|
111 /** |
|
112 * Walks down an OrderByClause AST node, thereby generating the appropriate SQL. |
|
113 * |
|
114 * @param OrderByClause |
|
115 * @return string The SQL. |
|
116 */ |
|
117 public function walkOrderByClause($orderByClause) |
|
118 { |
|
119 foreach ($this->_walkers as $walker) { |
|
120 $walker->walkOrderByClause($orderByClause); |
|
121 } |
|
122 } |
|
123 |
|
124 /** |
|
125 * Walks down an OrderByItem AST node, thereby generating the appropriate SQL. |
|
126 * |
|
127 * @param OrderByItem |
|
128 * @return string The SQL. |
|
129 */ |
|
130 public function walkOrderByItem($orderByItem) |
|
131 { |
|
132 foreach ($this->_walkers as $walker) { |
|
133 $walker->walkOrderByItem($orderByItem); |
|
134 } |
|
135 } |
|
136 |
|
137 /** |
|
138 * Walks down a HavingClause AST node, thereby generating the appropriate SQL. |
|
139 * |
|
140 * @param HavingClause |
|
141 * @return string The SQL. |
|
142 */ |
|
143 public function walkHavingClause($havingClause) |
|
144 { |
|
145 foreach ($this->_walkers as $walker) { |
|
146 $walker->walkHavingClause($havingClause); |
|
147 } |
|
148 } |
|
149 |
|
150 /** |
|
151 * Walks down a JoinVariableDeclaration AST node and creates the corresponding SQL. |
|
152 * |
|
153 * @param JoinVariableDeclaration $joinVarDecl |
|
154 * @return string The SQL. |
|
155 */ |
|
156 public function walkJoinVariableDeclaration($joinVarDecl) |
|
157 { |
|
158 foreach ($this->_walkers as $walker) { |
|
159 $walker->walkJoinVariableDeclaration($joinVarDecl); |
|
160 } |
|
161 } |
|
162 |
|
163 /** |
|
164 * Walks down a SelectExpression AST node and generates the corresponding SQL. |
|
165 * |
|
166 * @param SelectExpression $selectExpression |
|
167 * @return string The SQL. |
|
168 */ |
|
169 public function walkSelectExpression($selectExpression) |
|
170 { |
|
171 foreach ($this->_walkers as $walker) { |
|
172 $walker->walkSelectExpression($selectExpression); |
|
173 } |
|
174 } |
|
175 |
|
176 /** |
|
177 * Walks down a QuantifiedExpression AST node, thereby generating the appropriate SQL. |
|
178 * |
|
179 * @param QuantifiedExpression |
|
180 * @return string The SQL. |
|
181 */ |
|
182 public function walkQuantifiedExpression($qExpr) |
|
183 { |
|
184 foreach ($this->_walkers as $walker) { |
|
185 $walker->walkQuantifiedExpression($qExpr); |
|
186 } |
|
187 } |
|
188 |
|
189 /** |
|
190 * Walks down a Subselect AST node, thereby generating the appropriate SQL. |
|
191 * |
|
192 * @param Subselect |
|
193 * @return string The SQL. |
|
194 */ |
|
195 public function walkSubselect($subselect) |
|
196 { |
|
197 foreach ($this->_walkers as $walker) { |
|
198 $walker->walkSubselect($subselect); |
|
199 } |
|
200 } |
|
201 |
|
202 /** |
|
203 * Walks down a SubselectFromClause AST node, thereby generating the appropriate SQL. |
|
204 * |
|
205 * @param SubselectFromClause |
|
206 * @return string The SQL. |
|
207 */ |
|
208 public function walkSubselectFromClause($subselectFromClause) |
|
209 { |
|
210 foreach ($this->_walkers as $walker) { |
|
211 $walker->walkSubselectFromClause($subselectFromClause); |
|
212 } |
|
213 } |
|
214 |
|
215 /** |
|
216 * Walks down a SimpleSelectClause AST node, thereby generating the appropriate SQL. |
|
217 * |
|
218 * @param SimpleSelectClause |
|
219 * @return string The SQL. |
|
220 */ |
|
221 public function walkSimpleSelectClause($simpleSelectClause) |
|
222 { |
|
223 foreach ($this->_walkers as $walker) { |
|
224 $walker->walkSimpleSelectClause($simpleSelectClause); |
|
225 } |
|
226 } |
|
227 |
|
228 /** |
|
229 * Walks down a SimpleSelectExpression AST node, thereby generating the appropriate SQL. |
|
230 * |
|
231 * @param SimpleSelectExpression |
|
232 * @return string The SQL. |
|
233 */ |
|
234 public function walkSimpleSelectExpression($simpleSelectExpression) |
|
235 { |
|
236 foreach ($this->_walkers as $walker) { |
|
237 $walker->walkSimpleSelectExpression($simpleSelectExpression); |
|
238 } |
|
239 } |
|
240 |
|
241 /** |
|
242 * Walks down an AggregateExpression AST node, thereby generating the appropriate SQL. |
|
243 * |
|
244 * @param AggregateExpression |
|
245 * @return string The SQL. |
|
246 */ |
|
247 public function walkAggregateExpression($aggExpression) |
|
248 { |
|
249 foreach ($this->_walkers as $walker) { |
|
250 $walker->walkAggregateExpression($aggExpression); |
|
251 } |
|
252 } |
|
253 |
|
254 /** |
|
255 * Walks down a GroupByClause AST node, thereby generating the appropriate SQL. |
|
256 * |
|
257 * @param GroupByClause |
|
258 * @return string The SQL. |
|
259 */ |
|
260 public function walkGroupByClause($groupByClause) |
|
261 { |
|
262 foreach ($this->_walkers as $walker) { |
|
263 $walker->walkGroupByClause($groupByClause); |
|
264 } |
|
265 } |
|
266 |
|
267 /** |
|
268 * Walks down a GroupByItem AST node, thereby generating the appropriate SQL. |
|
269 * |
|
270 * @param GroupByItem |
|
271 * @return string The SQL. |
|
272 */ |
|
273 public function walkGroupByItem(AST\PathExpression $pathExpr) |
|
274 { |
|
275 foreach ($this->_walkers as $walker) { |
|
276 $walker->walkGroupByItem($pathExpr); |
|
277 } |
|
278 } |
|
279 |
|
280 /** |
|
281 * Walks down an UpdateStatement AST node, thereby generating the appropriate SQL. |
|
282 * |
|
283 * @param UpdateStatement |
|
284 * @return string The SQL. |
|
285 */ |
|
286 public function walkUpdateStatement(AST\UpdateStatement $AST) |
|
287 { |
|
288 foreach ($this->_walkers as $walker) { |
|
289 $walker->walkUpdateStatement($AST); |
|
290 } |
|
291 } |
|
292 |
|
293 /** |
|
294 * Walks down a DeleteStatement AST node, thereby generating the appropriate SQL. |
|
295 * |
|
296 * @param DeleteStatement |
|
297 * @return string The SQL. |
|
298 */ |
|
299 public function walkDeleteStatement(AST\DeleteStatement $AST) |
|
300 { |
|
301 foreach ($this->_walkers as $walker) { |
|
302 $walker->walkDeleteStatement($AST); |
|
303 } |
|
304 } |
|
305 |
|
306 /** |
|
307 * Walks down a DeleteClause AST node, thereby generating the appropriate SQL. |
|
308 * |
|
309 * @param DeleteClause |
|
310 * @return string The SQL. |
|
311 */ |
|
312 public function walkDeleteClause(AST\DeleteClause $deleteClause) |
|
313 { |
|
314 foreach ($this->_walkers as $walker) { |
|
315 $walker->walkDeleteClause($deleteClause); |
|
316 } |
|
317 } |
|
318 |
|
319 /** |
|
320 * Walks down an UpdateClause AST node, thereby generating the appropriate SQL. |
|
321 * |
|
322 * @param UpdateClause |
|
323 * @return string The SQL. |
|
324 */ |
|
325 public function walkUpdateClause($updateClause) |
|
326 { |
|
327 foreach ($this->_walkers as $walker) { |
|
328 $walker->walkUpdateClause($updateClause); |
|
329 } |
|
330 } |
|
331 |
|
332 /** |
|
333 * Walks down an UpdateItem AST node, thereby generating the appropriate SQL. |
|
334 * |
|
335 * @param UpdateItem |
|
336 * @return string The SQL. |
|
337 */ |
|
338 public function walkUpdateItem($updateItem) |
|
339 { |
|
340 foreach ($this->_walkers as $walker) { |
|
341 $walker->walkUpdateItem($updateItem); |
|
342 } |
|
343 } |
|
344 |
|
345 /** |
|
346 * Walks down a WhereClause AST node, thereby generating the appropriate SQL. |
|
347 * |
|
348 * @param WhereClause |
|
349 * @return string The SQL. |
|
350 */ |
|
351 public function walkWhereClause($whereClause) |
|
352 { |
|
353 foreach ($this->_walkers as $walker) { |
|
354 $walker->walkWhereClause($whereClause); |
|
355 } |
|
356 } |
|
357 |
|
358 /** |
|
359 * Walks down a ConditionalExpression AST node, thereby generating the appropriate SQL. |
|
360 * |
|
361 * @param ConditionalExpression |
|
362 * @return string The SQL. |
|
363 */ |
|
364 public function walkConditionalExpression($condExpr) |
|
365 { |
|
366 foreach ($this->_walkers as $walker) { |
|
367 $walker->walkConditionalExpression($condExpr); |
|
368 } |
|
369 } |
|
370 |
|
371 /** |
|
372 * Walks down a ConditionalTerm AST node, thereby generating the appropriate SQL. |
|
373 * |
|
374 * @param ConditionalTerm |
|
375 * @return string The SQL. |
|
376 */ |
|
377 public function walkConditionalTerm($condTerm) |
|
378 { |
|
379 foreach ($this->_walkers as $walker) { |
|
380 $walker->walkConditionalTerm($condTerm); |
|
381 } |
|
382 } |
|
383 |
|
384 /** |
|
385 * Walks down a ConditionalFactor AST node, thereby generating the appropriate SQL. |
|
386 * |
|
387 * @param ConditionalFactor |
|
388 * @return string The SQL. |
|
389 */ |
|
390 public function walkConditionalFactor($factor) |
|
391 { |
|
392 foreach ($this->_walkers as $walker) { |
|
393 $walker->walkConditionalFactor($factor); |
|
394 } |
|
395 } |
|
396 |
|
397 /** |
|
398 * Walks down a ConditionalPrimary AST node, thereby generating the appropriate SQL. |
|
399 * |
|
400 * @param ConditionalPrimary |
|
401 * @return string The SQL. |
|
402 */ |
|
403 public function walkConditionalPrimary($condPrimary) |
|
404 { |
|
405 foreach ($this->_walkers as $walker) { |
|
406 $walker->walkConditionalPrimary($condPrimary); |
|
407 } |
|
408 } |
|
409 |
|
410 /** |
|
411 * Walks down an ExistsExpression AST node, thereby generating the appropriate SQL. |
|
412 * |
|
413 * @param ExistsExpression |
|
414 * @return string The SQL. |
|
415 */ |
|
416 public function walkExistsExpression($existsExpr) |
|
417 { |
|
418 foreach ($this->_walkers as $walker) { |
|
419 $walker->walkExistsExpression($existsExpr); |
|
420 } |
|
421 } |
|
422 |
|
423 /** |
|
424 * Walks down a CollectionMemberExpression AST node, thereby generating the appropriate SQL. |
|
425 * |
|
426 * @param CollectionMemberExpression |
|
427 * @return string The SQL. |
|
428 */ |
|
429 public function walkCollectionMemberExpression($collMemberExpr) |
|
430 { |
|
431 foreach ($this->_walkers as $walker) { |
|
432 $walker->walkCollectionMemberExpression($collMemberExpr); |
|
433 } |
|
434 } |
|
435 |
|
436 /** |
|
437 * Walks down an EmptyCollectionComparisonExpression AST node, thereby generating the appropriate SQL. |
|
438 * |
|
439 * @param EmptyCollectionComparisonExpression |
|
440 * @return string The SQL. |
|
441 */ |
|
442 public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr) |
|
443 { |
|
444 foreach ($this->_walkers as $walker) { |
|
445 $walker->walkEmptyCollectionComparisonExpression($emptyCollCompExpr); |
|
446 } |
|
447 } |
|
448 |
|
449 /** |
|
450 * Walks down a NullComparisonExpression AST node, thereby generating the appropriate SQL. |
|
451 * |
|
452 * @param NullComparisonExpression |
|
453 * @return string The SQL. |
|
454 */ |
|
455 public function walkNullComparisonExpression($nullCompExpr) |
|
456 { |
|
457 foreach ($this->_walkers as $walker) { |
|
458 $walker->walkNullComparisonExpression($nullCompExpr); |
|
459 } |
|
460 } |
|
461 |
|
462 /** |
|
463 * Walks down an InExpression AST node, thereby generating the appropriate SQL. |
|
464 * |
|
465 * @param InExpression |
|
466 * @return string The SQL. |
|
467 */ |
|
468 public function walkInExpression($inExpr) |
|
469 { |
|
470 foreach ($this->_walkers as $walker) { |
|
471 $walker->walkInExpression($inExpr); |
|
472 } |
|
473 } |
|
474 |
|
475 /** |
|
476 * Walks down an InstanceOfExpression AST node, thereby generating the appropriate SQL. |
|
477 * |
|
478 * @param InstanceOfExpression |
|
479 * @return string The SQL. |
|
480 */ |
|
481 function walkInstanceOfExpression($instanceOfExpr) |
|
482 { |
|
483 foreach ($this->_walkers as $walker) { |
|
484 $walker->walkInstanceOfExpression($instanceOfExpr); |
|
485 } |
|
486 } |
|
487 |
|
488 /** |
|
489 * Walks down a literal that represents an AST node, thereby generating the appropriate SQL. |
|
490 * |
|
491 * @param mixed |
|
492 * @return string The SQL. |
|
493 */ |
|
494 public function walkLiteral($literal) |
|
495 { |
|
496 foreach ($this->_walkers as $walker) { |
|
497 $walker->walkLiteral($literal); |
|
498 } |
|
499 } |
|
500 |
|
501 /** |
|
502 * Walks down a BetweenExpression AST node, thereby generating the appropriate SQL. |
|
503 * |
|
504 * @param BetweenExpression |
|
505 * @return string The SQL. |
|
506 */ |
|
507 public function walkBetweenExpression($betweenExpr) |
|
508 { |
|
509 foreach ($this->_walkers as $walker) { |
|
510 $walker->walkBetweenExpression($betweenExpr); |
|
511 } |
|
512 } |
|
513 |
|
514 /** |
|
515 * Walks down a LikeExpression AST node, thereby generating the appropriate SQL. |
|
516 * |
|
517 * @param LikeExpression |
|
518 * @return string The SQL. |
|
519 */ |
|
520 public function walkLikeExpression($likeExpr) |
|
521 { |
|
522 foreach ($this->_walkers as $walker) { |
|
523 $walker->walkLikeExpression($likeExpr); |
|
524 } |
|
525 } |
|
526 |
|
527 /** |
|
528 * Walks down a StateFieldPathExpression AST node, thereby generating the appropriate SQL. |
|
529 * |
|
530 * @param StateFieldPathExpression |
|
531 * @return string The SQL. |
|
532 */ |
|
533 public function walkStateFieldPathExpression($stateFieldPathExpression) |
|
534 { |
|
535 foreach ($this->_walkers as $walker) { |
|
536 $walker->walkStateFieldPathExpression($stateFieldPathExpression); |
|
537 } |
|
538 } |
|
539 |
|
540 /** |
|
541 * Walks down a ComparisonExpression AST node, thereby generating the appropriate SQL. |
|
542 * |
|
543 * @param ComparisonExpression |
|
544 * @return string The SQL. |
|
545 */ |
|
546 public function walkComparisonExpression($compExpr) |
|
547 { |
|
548 foreach ($this->_walkers as $walker) { |
|
549 $walker->walkComparisonExpression($compExpr); |
|
550 } |
|
551 } |
|
552 |
|
553 /** |
|
554 * Walks down an InputParameter AST node, thereby generating the appropriate SQL. |
|
555 * |
|
556 * @param InputParameter |
|
557 * @return string The SQL. |
|
558 */ |
|
559 public function walkInputParameter($inputParam) |
|
560 { |
|
561 foreach ($this->_walkers as $walker) { |
|
562 $walker->walkInputParameter($inputParam); |
|
563 } |
|
564 } |
|
565 |
|
566 /** |
|
567 * Walks down an ArithmeticExpression AST node, thereby generating the appropriate SQL. |
|
568 * |
|
569 * @param ArithmeticExpression |
|
570 * @return string The SQL. |
|
571 */ |
|
572 public function walkArithmeticExpression($arithmeticExpr) |
|
573 { |
|
574 foreach ($this->_walkers as $walker) { |
|
575 $walker->walkArithmeticExpression($arithmeticExpr); |
|
576 } |
|
577 } |
|
578 |
|
579 /** |
|
580 * Walks down an ArithmeticTerm AST node, thereby generating the appropriate SQL. |
|
581 * |
|
582 * @param mixed |
|
583 * @return string The SQL. |
|
584 */ |
|
585 public function walkArithmeticTerm($term) |
|
586 { |
|
587 foreach ($this->_walkers as $walker) { |
|
588 $walker->walkArithmeticTerm($term); |
|
589 } |
|
590 } |
|
591 |
|
592 /** |
|
593 * Walks down a StringPrimary that represents an AST node, thereby generating the appropriate SQL. |
|
594 * |
|
595 * @param mixed |
|
596 * @return string The SQL. |
|
597 */ |
|
598 public function walkStringPrimary($stringPrimary) |
|
599 { |
|
600 foreach ($this->_walkers as $walker) { |
|
601 $walker->walkStringPrimary($stringPrimary); |
|
602 } |
|
603 } |
|
604 |
|
605 /** |
|
606 * Walks down an ArithmeticFactor that represents an AST node, thereby generating the appropriate SQL. |
|
607 * |
|
608 * @param mixed |
|
609 * @return string The SQL. |
|
610 */ |
|
611 public function walkArithmeticFactor($factor) |
|
612 { |
|
613 foreach ($this->_walkers as $walker) { |
|
614 $walker->walkArithmeticFactor($factor); |
|
615 } |
|
616 } |
|
617 |
|
618 /** |
|
619 * Walks down an SimpleArithmeticExpression AST node, thereby generating the appropriate SQL. |
|
620 * |
|
621 * @param SimpleArithmeticExpression |
|
622 * @return string The SQL. |
|
623 */ |
|
624 public function walkSimpleArithmeticExpression($simpleArithmeticExpr) |
|
625 { |
|
626 foreach ($this->_walkers as $walker) { |
|
627 $walker->walkSimpleArithmeticExpression($simpleArithmeticExpr); |
|
628 } |
|
629 } |
|
630 |
|
631 /** |
|
632 * Walks down an PathExpression AST node, thereby generating the appropriate SQL. |
|
633 * |
|
634 * @param mixed |
|
635 * @return string The SQL. |
|
636 */ |
|
637 public function walkPathExpression($pathExpr) |
|
638 { |
|
639 foreach ($this->_walkers as $walker) { |
|
640 $walker->walkPathExpression($pathExpr); |
|
641 } |
|
642 } |
|
643 |
|
644 /** |
|
645 * Gets an executor that can be used to execute the result of this walker. |
|
646 * |
|
647 * @return AbstractExecutor |
|
648 */ |
|
649 public function getExecutor($AST) |
|
650 {} |
|
651 } |