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