CustomTreeWalkersTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Tests\ORM\Functional;
  20. use Doctrine\ORM\Query;
  21. require_once __DIR__ . '/../../TestInit.php';
  22. /**
  23. * Test case for custom AST walking and modification.
  24. *
  25. * @author Roman Borschel <roman@code-factory.org>
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link http://www.doctrine-project.org
  28. * @since 2.0
  29. */
  30. class CustomTreeWalkersTest extends \Doctrine\Tests\OrmTestCase
  31. {
  32. private $_em;
  33. protected function setUp()
  34. {
  35. $this->_em = $this->_getTestEntityManager();
  36. }
  37. public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
  38. {
  39. try {
  40. $query = $this->_em->createQuery($dqlToBeTested);
  41. $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\Tests\ORM\Functional\CustomTreeWalker'))
  42. ->useQueryCache(false);
  43. $this->assertEquals($sqlToBeConfirmed, $query->getSql());
  44. $query->free();
  45. } catch (\Exception $e) {
  46. $this->fail($e->getMessage() . ' at "' . $e->getFile() . '" on line ' . $e->getLine());
  47. }
  48. }
  49. public function testSupportsQueriesWithoutWhere()
  50. {
  51. $this->assertSqlGeneration(
  52. 'select u from Doctrine\Tests\Models\CMS\CmsUser u',
  53. "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ WHERE c0_.id = 1"
  54. );
  55. }
  56. public function testSupportsQueriesWithMultipleConditionalExpressions()
  57. {
  58. $this->assertSqlGeneration(
  59. 'select u from Doctrine\Tests\Models\CMS\CmsUser u where u.name = :name or u.name = :otherName',
  60. "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ WHERE (c0_.name = ? OR c0_.name = ?) AND c0_.id = 1"
  61. );
  62. }
  63. public function testSupportsQueriesWithSimpleConditionalExpression()
  64. {
  65. $this->assertSqlGeneration(
  66. 'select u from Doctrine\Tests\Models\CMS\CmsUser u where u.name = :name',
  67. "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ WHERE c0_.name = ? AND c0_.id = 1"
  68. );
  69. }
  70. }
  71. class CustomTreeWalker extends Query\TreeWalkerAdapter
  72. {
  73. public function walkSelectStatement(Query\AST\SelectStatement $selectStatement)
  74. {
  75. // Get the DQL aliases of all the classes we want to modify
  76. $dqlAliases = array();
  77. foreach ($this->_getQueryComponents() as $dqlAlias => $comp) {
  78. // Hard-coded check just for demonstration: We want to modify the query if
  79. // it involves the CmsUser class.
  80. if ($comp['metadata']->name == 'Doctrine\Tests\Models\CMS\CmsUser') {
  81. $dqlAliases[] = $dqlAlias;
  82. }
  83. }
  84. // Create our conditions for all involved classes
  85. $factors = array();
  86. foreach ($dqlAliases as $alias) {
  87. $pathExpr = new Query\AST\PathExpression(Query\AST\PathExpression::TYPE_STATE_FIELD, $alias, 'id');
  88. $pathExpr->type = Query\AST\PathExpression::TYPE_STATE_FIELD;
  89. $comparisonExpr = new Query\AST\ComparisonExpression($pathExpr, '=', 1);
  90. $condPrimary = new Query\AST\ConditionalPrimary;
  91. $condPrimary->simpleConditionalExpression = $comparisonExpr;
  92. $factor = new Query\AST\ConditionalFactor($condPrimary);
  93. $factors[] = $factor;
  94. }
  95. if (($whereClause = $selectStatement->whereClause) !== null) {
  96. // There is already a WHERE clause, so append the conditions
  97. $condExpr = $whereClause->conditionalExpression;
  98. // Since Phase 1 AST optimizations were included, we need to re-add the ConditionalExpression
  99. if ( ! ($condExpr instanceof Query\AST\ConditionalExpression)) {
  100. $condExpr = new Query\AST\ConditionalExpression(array($condExpr));
  101. $whereClause->conditionalExpression = $condExpr;
  102. }
  103. $existingTerms = $whereClause->conditionalExpression->conditionalTerms;
  104. if (count($existingTerms) > 1) {
  105. // More than one term, so we need to wrap all these terms in a single root term
  106. // i.e: "WHERE u.name = :foo or u.other = :bar" => "WHERE (u.name = :foo or u.other = :bar) AND <our condition>"
  107. $primary = new Query\AST\ConditionalPrimary;
  108. $primary->conditionalExpression = new Query\AST\ConditionalExpression($existingTerms);
  109. $existingFactor = new Query\AST\ConditionalFactor($primary);
  110. $term = new Query\AST\ConditionalTerm(array_merge(array($existingFactor), $factors));
  111. $selectStatement->whereClause->conditionalExpression->conditionalTerms = array($term);
  112. } else {
  113. // Just one term so we can simply append our factors to that term
  114. $singleTerm = $selectStatement->whereClause->conditionalExpression->conditionalTerms[0];
  115. // Since Phase 1 AST optimizations were included, we need to re-add the ConditionalExpression
  116. if ( ! ($singleTerm instanceof Query\AST\ConditionalTerm)) {
  117. $singleTerm = new Query\AST\ConditionalTerm(array($singleTerm));
  118. $selectStatement->whereClause->conditionalExpression->conditionalTerms[0] = $singleTerm;
  119. }
  120. $singleTerm->conditionalFactors = array_merge($singleTerm->conditionalFactors, $factors);
  121. $selectStatement->whereClause->conditionalExpression->conditionalTerms = array($singleTerm);
  122. }
  123. } else {
  124. // Create a new WHERE clause with our factors
  125. $term = new Query\AST\ConditionalTerm($factors);
  126. $condExpr = new Query\AST\ConditionalExpression(array($term));
  127. $whereClause = new Query\AST\WhereClause($condExpr);
  128. $selectStatement->whereClause = $whereClause;
  129. }
  130. }
  131. }