TreeWalkerChain.php 18KB

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