TreeWalkerChain.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. /**
  31. * The tree walkers.
  32. *
  33. * @var TreeWalker[]
  34. */
  35. private $_walkers = array();
  36. /**
  37. * The original Query.
  38. *
  39. * @var \Doctrine\ORM\AbstractQuery
  40. */
  41. private $_query;
  42. /**
  43. * The ParserResult of the original query that was produced by the Parser.
  44. *
  45. * @var \Doctrine\ORM\Query\ParserResult
  46. */
  47. private $_parserResult;
  48. /**
  49. * The query components of the original query (the "symbol table") that was produced by the Parser.
  50. *
  51. * @var array
  52. */
  53. private $_queryComponents;
  54. /**
  55. * Returns the internal queryComponents array.
  56. *
  57. * @return array
  58. */
  59. public function getQueryComponents()
  60. {
  61. return $this->_queryComponents;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function setQueryComponent($dqlAlias, array $queryComponent)
  67. {
  68. $requiredKeys = array('metadata', 'parent', 'relation', 'map', 'nestingLevel', 'token');
  69. if (array_diff($requiredKeys, array_keys($queryComponent))) {
  70. throw QueryException::invalidQueryComponent($dqlAlias);
  71. }
  72. $this->_queryComponents[$dqlAlias] = $queryComponent;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function __construct($query, $parserResult, array $queryComponents)
  78. {
  79. $this->_query = $query;
  80. $this->_parserResult = $parserResult;
  81. $this->_queryComponents = $queryComponents;
  82. }
  83. /**
  84. * Adds a tree walker to the chain.
  85. *
  86. * @param string $walkerClass The class of the walker to instantiate.
  87. *
  88. * @return void
  89. */
  90. public function addTreeWalker($walkerClass)
  91. {
  92. $this->_walkers[] = new $walkerClass($this->_query, $this->_parserResult, $this->_queryComponents);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function walkSelectStatement(AST\SelectStatement $AST)
  98. {
  99. foreach ($this->_walkers as $walker) {
  100. $walker->walkSelectStatement($AST);
  101. $this->_queryComponents = $walker->getQueryComponents();
  102. }
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function walkSelectClause($selectClause)
  108. {
  109. foreach ($this->_walkers as $walker) {
  110. $walker->walkSelectClause($selectClause);
  111. }
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function walkFromClause($fromClause)
  117. {
  118. foreach ($this->_walkers as $walker) {
  119. $walker->walkFromClause($fromClause);
  120. }
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function walkFunction($function)
  126. {
  127. foreach ($this->_walkers as $walker) {
  128. $walker->walkFunction($function);
  129. }
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function walkOrderByClause($orderByClause)
  135. {
  136. foreach ($this->_walkers as $walker) {
  137. $walker->walkOrderByClause($orderByClause);
  138. }
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function walkOrderByItem($orderByItem)
  144. {
  145. foreach ($this->_walkers as $walker) {
  146. $walker->walkOrderByItem($orderByItem);
  147. }
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function walkHavingClause($havingClause)
  153. {
  154. foreach ($this->_walkers as $walker) {
  155. $walker->walkHavingClause($havingClause);
  156. }
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function walkJoin($join)
  162. {
  163. foreach ($this->_walkers as $walker) {
  164. $walker->walkJoin($join);
  165. }
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function walkSelectExpression($selectExpression)
  171. {
  172. foreach ($this->_walkers as $walker) {
  173. $walker->walkSelectExpression($selectExpression);
  174. }
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function walkQuantifiedExpression($qExpr)
  180. {
  181. foreach ($this->_walkers as $walker) {
  182. $walker->walkQuantifiedExpression($qExpr);
  183. }
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. public function walkSubselect($subselect)
  189. {
  190. foreach ($this->_walkers as $walker) {
  191. $walker->walkSubselect($subselect);
  192. }
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function walkSubselectFromClause($subselectFromClause)
  198. {
  199. foreach ($this->_walkers as $walker) {
  200. $walker->walkSubselectFromClause($subselectFromClause);
  201. }
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function walkSimpleSelectClause($simpleSelectClause)
  207. {
  208. foreach ($this->_walkers as $walker) {
  209. $walker->walkSimpleSelectClause($simpleSelectClause);
  210. }
  211. }
  212. /**
  213. * {@inheritdoc}
  214. */
  215. public function walkSimpleSelectExpression($simpleSelectExpression)
  216. {
  217. foreach ($this->_walkers as $walker) {
  218. $walker->walkSimpleSelectExpression($simpleSelectExpression);
  219. }
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. public function walkAggregateExpression($aggExpression)
  225. {
  226. foreach ($this->_walkers as $walker) {
  227. $walker->walkAggregateExpression($aggExpression);
  228. }
  229. }
  230. /**
  231. * {@inheritdoc}
  232. */
  233. public function walkGroupByClause($groupByClause)
  234. {
  235. foreach ($this->_walkers as $walker) {
  236. $walker->walkGroupByClause($groupByClause);
  237. }
  238. }
  239. /**
  240. * {@inheritdoc}
  241. */
  242. public function walkGroupByItem($groupByItem)
  243. {
  244. foreach ($this->_walkers as $walker) {
  245. $walker->walkGroupByItem($groupByItem);
  246. }
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. public function walkUpdateStatement(AST\UpdateStatement $AST)
  252. {
  253. foreach ($this->_walkers as $walker) {
  254. $walker->walkUpdateStatement($AST);
  255. }
  256. }
  257. /**
  258. * {@inheritdoc}
  259. */
  260. public function walkDeleteStatement(AST\DeleteStatement $AST)
  261. {
  262. foreach ($this->_walkers as $walker) {
  263. $walker->walkDeleteStatement($AST);
  264. }
  265. }
  266. /**
  267. * {@inheritdoc}
  268. */
  269. public function walkDeleteClause(AST\DeleteClause $deleteClause)
  270. {
  271. foreach ($this->_walkers as $walker) {
  272. $walker->walkDeleteClause($deleteClause);
  273. }
  274. }
  275. /**
  276. * {@inheritdoc}
  277. */
  278. public function walkUpdateClause($updateClause)
  279. {
  280. foreach ($this->_walkers as $walker) {
  281. $walker->walkUpdateClause($updateClause);
  282. }
  283. }
  284. /**
  285. * {@inheritdoc}
  286. */
  287. public function walkUpdateItem($updateItem)
  288. {
  289. foreach ($this->_walkers as $walker) {
  290. $walker->walkUpdateItem($updateItem);
  291. }
  292. }
  293. /**
  294. * {@inheritdoc}
  295. */
  296. public function walkWhereClause($whereClause)
  297. {
  298. foreach ($this->_walkers as $walker) {
  299. $walker->walkWhereClause($whereClause);
  300. }
  301. }
  302. /**
  303. * {@inheritdoc}
  304. */
  305. public function walkConditionalExpression($condExpr)
  306. {
  307. foreach ($this->_walkers as $walker) {
  308. $walker->walkConditionalExpression($condExpr);
  309. }
  310. }
  311. /**
  312. * {@inheritdoc}
  313. */
  314. public function walkConditionalTerm($condTerm)
  315. {
  316. foreach ($this->_walkers as $walker) {
  317. $walker->walkConditionalTerm($condTerm);
  318. }
  319. }
  320. /**
  321. * {@inheritdoc}
  322. */
  323. public function walkConditionalFactor($factor)
  324. {
  325. foreach ($this->_walkers as $walker) {
  326. $walker->walkConditionalFactor($factor);
  327. }
  328. }
  329. /**
  330. * {@inheritdoc}
  331. */
  332. public function walkConditionalPrimary($condPrimary)
  333. {
  334. foreach ($this->_walkers as $walker) {
  335. $walker->walkConditionalPrimary($condPrimary);
  336. }
  337. }
  338. /**
  339. * {@inheritdoc}
  340. */
  341. public function walkExistsExpression($existsExpr)
  342. {
  343. foreach ($this->_walkers as $walker) {
  344. $walker->walkExistsExpression($existsExpr);
  345. }
  346. }
  347. /**
  348. * {@inheritdoc}
  349. */
  350. public function walkCollectionMemberExpression($collMemberExpr)
  351. {
  352. foreach ($this->_walkers as $walker) {
  353. $walker->walkCollectionMemberExpression($collMemberExpr);
  354. }
  355. }
  356. /**
  357. * {@inheritdoc}
  358. */
  359. public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
  360. {
  361. foreach ($this->_walkers as $walker) {
  362. $walker->walkEmptyCollectionComparisonExpression($emptyCollCompExpr);
  363. }
  364. }
  365. /**
  366. * {@inheritdoc}
  367. */
  368. public function walkNullComparisonExpression($nullCompExpr)
  369. {
  370. foreach ($this->_walkers as $walker) {
  371. $walker->walkNullComparisonExpression($nullCompExpr);
  372. }
  373. }
  374. /**
  375. * {@inheritdoc}
  376. */
  377. public function walkInExpression($inExpr)
  378. {
  379. foreach ($this->_walkers as $walker) {
  380. $walker->walkInExpression($inExpr);
  381. }
  382. }
  383. /**
  384. * {@inheritdoc}
  385. */
  386. function walkInstanceOfExpression($instanceOfExpr)
  387. {
  388. foreach ($this->_walkers as $walker) {
  389. $walker->walkInstanceOfExpression($instanceOfExpr);
  390. }
  391. }
  392. /**
  393. * {@inheritdoc}
  394. */
  395. public function walkLiteral($literal)
  396. {
  397. foreach ($this->_walkers as $walker) {
  398. $walker->walkLiteral($literal);
  399. }
  400. }
  401. /**
  402. * {@inheritdoc}
  403. */
  404. public function walkBetweenExpression($betweenExpr)
  405. {
  406. foreach ($this->_walkers as $walker) {
  407. $walker->walkBetweenExpression($betweenExpr);
  408. }
  409. }
  410. /**
  411. * {@inheritdoc}
  412. */
  413. public function walkLikeExpression($likeExpr)
  414. {
  415. foreach ($this->_walkers as $walker) {
  416. $walker->walkLikeExpression($likeExpr);
  417. }
  418. }
  419. /**
  420. * {@inheritdoc}
  421. */
  422. public function walkStateFieldPathExpression($stateFieldPathExpression)
  423. {
  424. foreach ($this->_walkers as $walker) {
  425. $walker->walkStateFieldPathExpression($stateFieldPathExpression);
  426. }
  427. }
  428. /**
  429. * {@inheritdoc}
  430. */
  431. public function walkComparisonExpression($compExpr)
  432. {
  433. foreach ($this->_walkers as $walker) {
  434. $walker->walkComparisonExpression($compExpr);
  435. }
  436. }
  437. /**
  438. * {@inheritdoc}
  439. */
  440. public function walkInputParameter($inputParam)
  441. {
  442. foreach ($this->_walkers as $walker) {
  443. $walker->walkInputParameter($inputParam);
  444. }
  445. }
  446. /**
  447. * {@inheritdoc}
  448. */
  449. public function walkArithmeticExpression($arithmeticExpr)
  450. {
  451. foreach ($this->_walkers as $walker) {
  452. $walker->walkArithmeticExpression($arithmeticExpr);
  453. }
  454. }
  455. /**
  456. * {@inheritdoc}
  457. */
  458. public function walkArithmeticTerm($term)
  459. {
  460. foreach ($this->_walkers as $walker) {
  461. $walker->walkArithmeticTerm($term);
  462. }
  463. }
  464. /**
  465. * {@inheritdoc}
  466. */
  467. public function walkStringPrimary($stringPrimary)
  468. {
  469. foreach ($this->_walkers as $walker) {
  470. $walker->walkStringPrimary($stringPrimary);
  471. }
  472. }
  473. /**
  474. * {@inheritdoc}
  475. */
  476. public function walkArithmeticFactor($factor)
  477. {
  478. foreach ($this->_walkers as $walker) {
  479. $walker->walkArithmeticFactor($factor);
  480. }
  481. }
  482. /**
  483. * {@inheritdoc}
  484. */
  485. public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
  486. {
  487. foreach ($this->_walkers as $walker) {
  488. $walker->walkSimpleArithmeticExpression($simpleArithmeticExpr);
  489. }
  490. }
  491. /**
  492. * {@inheritdoc}
  493. */
  494. public function walkPathExpression($pathExpr)
  495. {
  496. foreach ($this->_walkers as $walker) {
  497. $walker->walkPathExpression($pathExpr);
  498. }
  499. }
  500. /**
  501. * {@inheritdoc}
  502. */
  503. public function walkResultVariable($resultVariable)
  504. {
  505. foreach ($this->_walkers as $walker) {
  506. $walker->walkResultVariable($resultVariable);
  507. }
  508. }
  509. /**
  510. * {@inheritdoc}
  511. */
  512. public function getExecutor($AST)
  513. {
  514. }
  515. }