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.
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>.
20 namespace Doctrine\Tests\ORM\Functional;
22 use Doctrine\ORM\Query;
24 require_once __DIR__ . '/../../TestInit.php';
27 * Test case for custom AST walking and modification.
29 * @author Roman Borschel <roman@code-factory.org>
30 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
31 * @link http://www.doctrine-project.org
34 class CustomTreeWalkersTest extends \Doctrine\Tests\OrmTestCase
38 protected function setUp()
40 $this->_em = $this->_getTestEntityManager();
43 public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
46 $query = $this->_em->createQuery($dqlToBeTested);
47 $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\Tests\ORM\Functional\CustomTreeWalker'))
48 ->useQueryCache(false);
50 $this->assertEquals($sqlToBeConfirmed, $query->getSql());
52 } catch (\Exception $e) {
53 $this->fail($e->getMessage() . ' at "' . $e->getFile() . '" on line ' . $e->getLine());
58 public function testSupportsQueriesWithoutWhere()
60 $this->assertSqlGeneration(
61 'select u from Doctrine\Tests\Models\CMS\CmsUser u',
62 "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"
66 public function testSupportsQueriesWithMultipleConditionalExpressions()
68 $this->assertSqlGeneration(
69 'select u from Doctrine\Tests\Models\CMS\CmsUser u where u.name = :name or u.name = :otherName',
70 "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"
74 public function testSupportsQueriesWithSimpleConditionalExpression()
76 $this->assertSqlGeneration(
77 'select u from Doctrine\Tests\Models\CMS\CmsUser u where u.name = :name',
78 "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"
83 class CustomTreeWalker extends Query\TreeWalkerAdapter
85 public function walkSelectStatement(Query\AST\SelectStatement $selectStatement)
87 // Get the DQL aliases of all the classes we want to modify
88 $dqlAliases = array();
90 foreach ($this->_getQueryComponents() as $dqlAlias => $comp) {
91 // Hard-coded check just for demonstration: We want to modify the query if
92 // it involves the CmsUser class.
93 if ($comp['metadata']->name == 'Doctrine\Tests\Models\CMS\CmsUser') {
94 $dqlAliases[] = $dqlAlias;
98 // Create our conditions for all involved classes
100 foreach ($dqlAliases as $alias) {
101 $pathExpr = new Query\AST\PathExpression(Query\AST\PathExpression::TYPE_STATE_FIELD, $alias, 'id');
102 $pathExpr->type = Query\AST\PathExpression::TYPE_STATE_FIELD;
103 $comparisonExpr = new Query\AST\ComparisonExpression($pathExpr, '=', 1);
105 $condPrimary = new Query\AST\ConditionalPrimary;
106 $condPrimary->simpleConditionalExpression = $comparisonExpr;
108 $factor = new Query\AST\ConditionalFactor($condPrimary);
109 $factors[] = $factor;
112 if (($whereClause = $selectStatement->whereClause) !== null) {
113 // There is already a WHERE clause, so append the conditions
114 $condExpr = $whereClause->conditionalExpression;
116 // Since Phase 1 AST optimizations were included, we need to re-add the ConditionalExpression
117 if ( ! ($condExpr instanceof Query\AST\ConditionalExpression)) {
118 $condExpr = new Query\AST\ConditionalExpression(array($condExpr));
120 $whereClause->conditionalExpression = $condExpr;
123 $existingTerms = $whereClause->conditionalExpression->conditionalTerms;
125 if (count($existingTerms) > 1) {
126 // More than one term, so we need to wrap all these terms in a single root term
127 // i.e: "WHERE u.name = :foo or u.other = :bar" => "WHERE (u.name = :foo or u.other = :bar) AND <our condition>"
129 $primary = new Query\AST\ConditionalPrimary;
130 $primary->conditionalExpression = new Query\AST\ConditionalExpression($existingTerms);
131 $existingFactor = new Query\AST\ConditionalFactor($primary);
132 $term = new Query\AST\ConditionalTerm(array_merge(array($existingFactor), $factors));
134 $selectStatement->whereClause->conditionalExpression->conditionalTerms = array($term);
136 // Just one term so we can simply append our factors to that term
137 $singleTerm = $selectStatement->whereClause->conditionalExpression->conditionalTerms[0];
139 // Since Phase 1 AST optimizations were included, we need to re-add the ConditionalExpression
140 if ( ! ($singleTerm instanceof Query\AST\ConditionalTerm)) {
141 $singleTerm = new Query\AST\ConditionalTerm(array($singleTerm));
143 $selectStatement->whereClause->conditionalExpression->conditionalTerms[0] = $singleTerm;
146 $singleTerm->conditionalFactors = array_merge($singleTerm->conditionalFactors, $factors);
147 $selectStatement->whereClause->conditionalExpression->conditionalTerms = array($singleTerm);
150 // Create a new WHERE clause with our factors
151 $term = new Query\AST\ConditionalTerm($factors);
152 $condExpr = new Query\AST\ConditionalExpression(array($term));
153 $whereClause = new Query\AST\WhereClause($condExpr);
154 $selectStatement->whereClause = $whereClause;