From 7107a85041a38f2be8afce4e372a67e586315f98 Mon Sep 17 00:00:00 2001 From: shustrik Date: Thu, 6 Feb 2014 01:27:17 +0300 Subject: [PATCH 1/6] faild test with multiple HINT_CUSTOM_TREE_WALKERS failed test when we have more than 1 walker HINT_CUSTOM_TREE_WALKERS , because walkSelectStatement in TreeWalkerChain saves the last hint _queryComponents result. --- .../Tests/ORM/Query/CustomTreeWalkersTest.php | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersTest.php b/tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersTest.php index b00d43223..bd6bf43f3 100644 --- a/tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersTest.php +++ b/tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersTest.php @@ -42,7 +42,7 @@ class CustomTreeWalkersTest extends \Doctrine\Tests\OrmTestCase { $query = $this->_em->createQuery($dqlToBeTested); $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $treeWalkers) - ->useQueryCache(false); + ->useQueryCache(false); if ($outputWalker) { $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, $outputWalker); @@ -96,6 +96,15 @@ class CustomTreeWalkersTest extends \Doctrine\Tests\OrmTestCase __NAMESPACE__ . '\\AddUnknownQueryComponentWalker' ); } + + public function testSupportsSeveralHintsQueriesWithoutWhere() + { + $this->assertSqlGeneration( + 'select u from Doctrine\Tests\Models\CMS\CmsUser u', + "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c1_.id AS id4, c1_.country AS country5, c1_.zip AS zip6, c1_.city AS city7, c0_.email_id AS email_id8, c1_.user_id AS user_id9 FROM cms_users c0_ LEFT JOIN cms_addresses c1_ ON c0_.id = c1_.user_id WHERE c0_.id = 1", + array('Doctrine\Tests\ORM\Functional\CustomTreeWalkerJoin', 'Doctrine\Tests\ORM\Functional\CustomTreeWalker') + ); + } } class AddUnknownQueryComponentWalker extends Query\SqlWalker @@ -183,3 +192,43 @@ class CustomTreeWalker extends Query\TreeWalkerAdapter } } } + +class CustomTreeWalkerJoin extends Query\TreeWalkerAdapter +{ + public function walkSelectStatement(Query\AST\SelectStatement $selectStatement) + { + foreach ($selectStatement->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) { + if ($identificationVariableDeclaration->rangeVariableDeclaration->abstractSchemaName == 'Doctrine\Tests\Models\CMS\CmsUser') { + $identificationVariableDeclaration->joins[] = new Query\AST\Join( + Query\AST\Join::JOIN_TYPE_LEFT, + new Query\AST\JoinAssociationDeclaration( + new Query\AST\JoinAssociationPathExpression( + $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable, + 'address' + ), + $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable . 'a', + null + ) + ); + $selectStatement->selectClause->selectExpressions[] = + new Query\AST\SelectExpression( + $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable . 'a', + null, + false + ); + $meta1 = $this->_getQuery()->getEntityManager()->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + $meta = $this->_getQuery()->getEntityManager()->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); + $this->setQueryComponent($identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable . 'a', + array( + 'metadata' => $meta, + 'parent' => $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable, + 'relation' => $meta1->getAssociationMapping('address'), + 'map' => null, + 'nestingLevel' => 0, + 'token' => null + ) + ); + } + } + } +} From 543ab500da014fa8afef0eb2b4d540908e9e65a1 Mon Sep 17 00:00:00 2001 From: shustrik Date: Thu, 6 Feb 2014 10:53:34 +0300 Subject: [PATCH 2/6] rename test --- tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersTest.php b/tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersTest.php index bd6bf43f3..c603db6fa 100644 --- a/tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersTest.php +++ b/tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersTest.php @@ -97,7 +97,7 @@ class CustomTreeWalkersTest extends \Doctrine\Tests\OrmTestCase ); } - public function testSupportsSeveralHintsQueriesWithoutWhere() + public function testSupportsSeveralHintsQueries() { $this->assertSqlGeneration( 'select u from Doctrine\Tests\Models\CMS\CmsUser u', From a6b43b93ace3a04af9ca41d3afc9670df155e266 Mon Sep 17 00:00:00 2001 From: shustrik Date: Thu, 6 Feb 2014 16:06:00 +0300 Subject: [PATCH 3/6] fix TreeWalkerChain. Create walker object in foreach. --- lib/Doctrine/ORM/Query/TreeWalkerChain.php | 166 +++++++++++++++------ 1 file changed, 117 insertions(+), 49 deletions(-) diff --git a/lib/Doctrine/ORM/Query/TreeWalkerChain.php b/lib/Doctrine/ORM/Query/TreeWalkerChain.php index 4ce356d84..72027ce14 100644 --- a/lib/Doctrine/ORM/Query/TreeWalkerChain.php +++ b/lib/Doctrine/ORM/Query/TreeWalkerChain.php @@ -25,10 +25,17 @@ namespace Doctrine\ORM\Query; * the AST to influence the final output produced by the last walker. * * @author Roman Borschel - * @since 2.0 + * @since 2.0 */ class TreeWalkerChain implements TreeWalker { + /** + * The tree walkers. + * + * @var string[] + */ + private $_walkersClasses = array(); + /** * The tree walkers. * @@ -100,7 +107,7 @@ class TreeWalkerChain implements TreeWalker */ public function addTreeWalker($walkerClass) { - $this->_walkers[] = new $walkerClass($this->_query, $this->_parserResult, $this->_queryComponents); + $this->_walkersClasses[] = $walkerClass; } /** @@ -108,7 +115,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkSelectStatement(AST\SelectStatement $AST) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkSelectStatement($AST); $this->_queryComponents = $walker->getQueryComponents(); @@ -120,7 +128,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkSelectClause($selectClause) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkSelectClause($selectClause); } } @@ -130,7 +139,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkFromClause($fromClause) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkFromClause($fromClause); } } @@ -140,7 +150,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkFunction($function) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkFunction($function); } } @@ -150,7 +161,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkOrderByClause($orderByClause) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkOrderByClause($orderByClause); } } @@ -160,7 +172,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkOrderByItem($orderByItem) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkOrderByItem($orderByItem); } } @@ -170,7 +183,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkHavingClause($havingClause) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkHavingClause($havingClause); } } @@ -180,7 +194,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkJoin($join) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkJoin($join); } } @@ -190,7 +205,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkSelectExpression($selectExpression) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkSelectExpression($selectExpression); } } @@ -200,7 +216,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkQuantifiedExpression($qExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkQuantifiedExpression($qExpr); } } @@ -210,7 +227,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkSubselect($subselect) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkSubselect($subselect); } } @@ -220,7 +238,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkSubselectFromClause($subselectFromClause) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkSubselectFromClause($subselectFromClause); } } @@ -230,7 +249,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkSimpleSelectClause($simpleSelectClause) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkSimpleSelectClause($simpleSelectClause); } } @@ -240,7 +260,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkSimpleSelectExpression($simpleSelectExpression) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkSimpleSelectExpression($simpleSelectExpression); } } @@ -250,7 +271,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkAggregateExpression($aggExpression) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkAggregateExpression($aggExpression); } } @@ -260,7 +282,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkGroupByClause($groupByClause) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkGroupByClause($groupByClause); } } @@ -270,7 +293,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkGroupByItem($groupByItem) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkGroupByItem($groupByItem); } } @@ -280,7 +304,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkUpdateStatement(AST\UpdateStatement $AST) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkUpdateStatement($AST); } } @@ -290,7 +315,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkDeleteStatement(AST\DeleteStatement $AST) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkDeleteStatement($AST); } } @@ -300,7 +326,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkDeleteClause(AST\DeleteClause $deleteClause) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkDeleteClause($deleteClause); } } @@ -310,7 +337,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkUpdateClause($updateClause) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkUpdateClause($updateClause); } } @@ -320,7 +348,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkUpdateItem($updateItem) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkUpdateItem($updateItem); } } @@ -330,7 +359,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkWhereClause($whereClause) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkWhereClause($whereClause); } } @@ -340,7 +370,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkConditionalExpression($condExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkConditionalExpression($condExpr); } } @@ -350,7 +381,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkConditionalTerm($condTerm) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkConditionalTerm($condTerm); } } @@ -360,7 +392,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkConditionalFactor($factor) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkConditionalFactor($factor); } } @@ -370,7 +403,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkConditionalPrimary($condPrimary) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkConditionalPrimary($condPrimary); } } @@ -380,7 +414,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkExistsExpression($existsExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkExistsExpression($existsExpr); } } @@ -390,7 +425,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkCollectionMemberExpression($collMemberExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkCollectionMemberExpression($collMemberExpr); } } @@ -400,7 +436,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkEmptyCollectionComparisonExpression($emptyCollCompExpr); } } @@ -410,7 +447,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkNullComparisonExpression($nullCompExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkNullComparisonExpression($nullCompExpr); } } @@ -420,7 +458,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkInExpression($inExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkInExpression($inExpr); } } @@ -430,7 +469,8 @@ class TreeWalkerChain implements TreeWalker */ function walkInstanceOfExpression($instanceOfExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkInstanceOfExpression($instanceOfExpr); } } @@ -440,7 +480,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkLiteral($literal) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkLiteral($literal); } } @@ -450,7 +491,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkBetweenExpression($betweenExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkBetweenExpression($betweenExpr); } } @@ -460,7 +502,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkLikeExpression($likeExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkLikeExpression($likeExpr); } } @@ -470,7 +513,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkStateFieldPathExpression($stateFieldPathExpression) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkStateFieldPathExpression($stateFieldPathExpression); } } @@ -480,7 +524,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkComparisonExpression($compExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkComparisonExpression($compExpr); } } @@ -490,7 +535,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkInputParameter($inputParam) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkInputParameter($inputParam); } } @@ -500,7 +546,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkArithmeticExpression($arithmeticExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkArithmeticExpression($arithmeticExpr); } } @@ -510,8 +557,9 @@ class TreeWalkerChain implements TreeWalker */ public function walkArithmeticTerm($term) { - foreach ($this->_walkers as $walker) { - $walker->walkArithmeticTerm($term); + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); + $walkerClass->walkArithmeticTerm($term); } } @@ -520,7 +568,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkStringPrimary($stringPrimary) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkStringPrimary($stringPrimary); } } @@ -530,7 +579,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkArithmeticFactor($factor) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkArithmeticFactor($factor); } } @@ -540,7 +590,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkSimpleArithmeticExpression($simpleArithmeticExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkSimpleArithmeticExpression($simpleArithmeticExpr); } } @@ -550,7 +601,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkPathExpression($pathExpr) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkPathExpression($pathExpr); } } @@ -560,7 +612,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkResultVariable($resultVariable) { - foreach ($this->_walkers as $walker) { + foreach ($this->_walkersClasses as $walkerClass) { + $walker = $this->getWalker($walkerClass); $walker->walkResultVariable($resultVariable); } } @@ -571,4 +624,19 @@ class TreeWalkerChain implements TreeWalker public function getExecutor($AST) { } + + /** + * @param $walkerClass + * + * @return TreeWalker + */ + private function getWalker($walkerClass) + { + $key = array_search($walkerClass, $this->_walkersClasses); + if (isset($this->_walkers[$key])) { + return $this->_walkers[$key]; + } + + return new $walkerClass($this->_query, $this->_parserResult, $this->_queryComponents); + } } From 6fe40b055fe70db755f91316299002000aad3756 Mon Sep 17 00:00:00 2001 From: shustrik Date: Thu, 6 Feb 2014 16:18:35 +0300 Subject: [PATCH 4/6] fix return walker --- lib/Doctrine/ORM/Query/TreeWalkerChain.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Query/TreeWalkerChain.php b/lib/Doctrine/ORM/Query/TreeWalkerChain.php index 72027ce14..feb1f2556 100644 --- a/lib/Doctrine/ORM/Query/TreeWalkerChain.php +++ b/lib/Doctrine/ORM/Query/TreeWalkerChain.php @@ -633,10 +633,10 @@ class TreeWalkerChain implements TreeWalker private function getWalker($walkerClass) { $key = array_search($walkerClass, $this->_walkersClasses); - if (isset($this->_walkers[$key])) { - return $this->_walkers[$key]; + if (!isset($this->_walkers[$key])) { + $this->_walkers[$key] = new $walkerClass($this->_query, $this->_parserResult, $this->_queryComponents); } - return new $walkerClass($this->_query, $this->_parserResult, $this->_queryComponents); + return $this->_walkers[$key]; } } From 12985b7811bd63039c0505d22b4cb9f70aedb9b2 Mon Sep 17 00:00:00 2001 From: shustrik Date: Thu, 6 Feb 2014 19:39:40 +0300 Subject: [PATCH 5/6] add iterator to tree walker chain --- lib/Doctrine/ORM/Query/TreeWalkerChain.php | 167 ++++++------------ .../ORM/Query/TreeWalkerChainIterator.php | 126 +++++++++++++ 2 files changed, 176 insertions(+), 117 deletions(-) create mode 100644 lib/Doctrine/ORM/Query/TreeWalkerChainIterator.php diff --git a/lib/Doctrine/ORM/Query/TreeWalkerChain.php b/lib/Doctrine/ORM/Query/TreeWalkerChain.php index feb1f2556..19597ed1a 100644 --- a/lib/Doctrine/ORM/Query/TreeWalkerChain.php +++ b/lib/Doctrine/ORM/Query/TreeWalkerChain.php @@ -29,19 +29,12 @@ namespace Doctrine\ORM\Query; */ class TreeWalkerChain implements TreeWalker { - /** - * The tree walkers. - * - * @var string[] - */ - private $_walkersClasses = array(); - /** * The tree walkers. * * @var TreeWalker[] */ - private $_walkers = array(); + private $_walkers; /** * The original Query. @@ -96,6 +89,7 @@ class TreeWalkerChain implements TreeWalker $this->_query = $query; $this->_parserResult = $parserResult; $this->_queryComponents = $queryComponents; + $this->_walkers = new TreeWalkerChainIterator($this, $query, $parserResult); } /** @@ -107,7 +101,7 @@ class TreeWalkerChain implements TreeWalker */ public function addTreeWalker($walkerClass) { - $this->_walkersClasses[] = $walkerClass; + $this->_walkers[] = $walkerClass; } /** @@ -115,8 +109,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkSelectStatement(AST\SelectStatement $AST) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkSelectStatement($AST); $this->_queryComponents = $walker->getQueryComponents(); @@ -128,8 +121,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkSelectClause($selectClause) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkSelectClause($selectClause); } } @@ -139,8 +131,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkFromClause($fromClause) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkFromClause($fromClause); } } @@ -150,8 +141,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkFunction($function) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkFunction($function); } } @@ -161,8 +151,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkOrderByClause($orderByClause) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkOrderByClause($orderByClause); } } @@ -172,8 +161,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkOrderByItem($orderByItem) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkOrderByItem($orderByItem); } } @@ -183,8 +171,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkHavingClause($havingClause) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkHavingClause($havingClause); } } @@ -194,8 +181,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkJoin($join) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkJoin($join); } } @@ -205,8 +191,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkSelectExpression($selectExpression) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkSelectExpression($selectExpression); } } @@ -216,8 +201,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkQuantifiedExpression($qExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkQuantifiedExpression($qExpr); } } @@ -227,8 +211,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkSubselect($subselect) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkSubselect($subselect); } } @@ -238,8 +221,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkSubselectFromClause($subselectFromClause) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkSubselectFromClause($subselectFromClause); } } @@ -249,8 +231,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkSimpleSelectClause($simpleSelectClause) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkSimpleSelectClause($simpleSelectClause); } } @@ -260,8 +241,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkSimpleSelectExpression($simpleSelectExpression) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkSimpleSelectExpression($simpleSelectExpression); } } @@ -271,8 +251,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkAggregateExpression($aggExpression) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkAggregateExpression($aggExpression); } } @@ -282,8 +261,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkGroupByClause($groupByClause) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkGroupByClause($groupByClause); } } @@ -293,8 +271,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkGroupByItem($groupByItem) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkGroupByItem($groupByItem); } } @@ -304,8 +281,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkUpdateStatement(AST\UpdateStatement $AST) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkUpdateStatement($AST); } } @@ -315,8 +291,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkDeleteStatement(AST\DeleteStatement $AST) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkDeleteStatement($AST); } } @@ -326,8 +301,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkDeleteClause(AST\DeleteClause $deleteClause) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkDeleteClause($deleteClause); } } @@ -337,8 +311,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkUpdateClause($updateClause) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkUpdateClause($updateClause); } } @@ -348,8 +321,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkUpdateItem($updateItem) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkUpdateItem($updateItem); } } @@ -359,8 +331,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkWhereClause($whereClause) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkWhereClause($whereClause); } } @@ -370,8 +341,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkConditionalExpression($condExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkConditionalExpression($condExpr); } } @@ -381,8 +351,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkConditionalTerm($condTerm) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkConditionalTerm($condTerm); } } @@ -392,8 +361,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkConditionalFactor($factor) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkConditionalFactor($factor); } } @@ -403,8 +371,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkConditionalPrimary($condPrimary) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkConditionalPrimary($condPrimary); } } @@ -414,8 +381,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkExistsExpression($existsExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkExistsExpression($existsExpr); } } @@ -425,8 +391,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkCollectionMemberExpression($collMemberExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkCollectionMemberExpression($collMemberExpr); } } @@ -436,8 +401,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkEmptyCollectionComparisonExpression($emptyCollCompExpr); } } @@ -447,8 +411,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkNullComparisonExpression($nullCompExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkNullComparisonExpression($nullCompExpr); } } @@ -458,8 +421,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkInExpression($inExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkInExpression($inExpr); } } @@ -469,8 +431,7 @@ class TreeWalkerChain implements TreeWalker */ function walkInstanceOfExpression($instanceOfExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkInstanceOfExpression($instanceOfExpr); } } @@ -480,8 +441,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkLiteral($literal) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkLiteral($literal); } } @@ -491,8 +451,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkBetweenExpression($betweenExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkBetweenExpression($betweenExpr); } } @@ -502,8 +461,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkLikeExpression($likeExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkLikeExpression($likeExpr); } } @@ -513,8 +471,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkStateFieldPathExpression($stateFieldPathExpression) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkStateFieldPathExpression($stateFieldPathExpression); } } @@ -524,8 +481,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkComparisonExpression($compExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkComparisonExpression($compExpr); } } @@ -535,8 +491,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkInputParameter($inputParam) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkInputParameter($inputParam); } } @@ -546,8 +501,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkArithmeticExpression($arithmeticExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkArithmeticExpression($arithmeticExpr); } } @@ -557,9 +511,8 @@ class TreeWalkerChain implements TreeWalker */ public function walkArithmeticTerm($term) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); - $walkerClass->walkArithmeticTerm($term); + foreach ($this->_walkers as $walker) { + $walker->walkArithmeticTerm($term); } } @@ -568,8 +521,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkStringPrimary($stringPrimary) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkStringPrimary($stringPrimary); } } @@ -579,8 +531,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkArithmeticFactor($factor) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkArithmeticFactor($factor); } } @@ -590,8 +541,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkSimpleArithmeticExpression($simpleArithmeticExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkSimpleArithmeticExpression($simpleArithmeticExpr); } } @@ -601,8 +551,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkPathExpression($pathExpr) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkPathExpression($pathExpr); } } @@ -612,8 +561,7 @@ class TreeWalkerChain implements TreeWalker */ public function walkResultVariable($resultVariable) { - foreach ($this->_walkersClasses as $walkerClass) { - $walker = $this->getWalker($walkerClass); + foreach ($this->_walkers as $walker) { $walker->walkResultVariable($resultVariable); } } @@ -624,19 +572,4 @@ class TreeWalkerChain implements TreeWalker public function getExecutor($AST) { } - - /** - * @param $walkerClass - * - * @return TreeWalker - */ - private function getWalker($walkerClass) - { - $key = array_search($walkerClass, $this->_walkersClasses); - if (!isset($this->_walkers[$key])) { - $this->_walkers[$key] = new $walkerClass($this->_query, $this->_parserResult, $this->_queryComponents); - } - - return $this->_walkers[$key]; - } } diff --git a/lib/Doctrine/ORM/Query/TreeWalkerChainIterator.php b/lib/Doctrine/ORM/Query/TreeWalkerChainIterator.php new file mode 100644 index 000000000..d693b422a --- /dev/null +++ b/lib/Doctrine/ORM/Query/TreeWalkerChainIterator.php @@ -0,0 +1,126 @@ +treeWalkerChain = $treeWalkerChain; + $this->query = $query; + $this->parserResult = $parserResult; + } + + /** + * {@inheritdoc} + */ + function rewind() + { + return reset($this->walkers); + } + + /** + * {@inheritdoc} + */ + function current() + { + return $this->offsetGet(key($this->walkers)); + } + + /** + * {@inheritdoc} + */ + function key() + { + return key($this->walkers); + } + + /** + * {@inheritdoc} + */ + function next() + { + next($this->walkers); + + return $this->offsetGet(key($this->walkers)); + } + + /** + * {@inheritdoc} + */ + function valid() + { + return key($this->walkers) !== null; + } + + + /** + * {@inheritdoc} + */ + public function offsetExists($offset) + { + return isset($this->walkers[$offset]); + } + + /** + * {@inheritdoc} + */ + public function offsetGet($offset) + { + if ($this->offsetExists($offset)) { + return new $this->walkers[$offset]( + $this->query, + $this->parserResult, + $this->treeWalkerChain->getQueryComponents() + ); + } + + return null; + } + + /** + * {@inheritdoc} + */ + public function offsetSet($offset, $value) + { + if (is_null($offset)) { + $this->walkers[] = $value; + } else { + $this->walkers[$offset] = $value; + } + } + + /** + * {@inheritdoc} + */ + public function offsetUnset($offset) + { + if ($this->offsetExists($offset)) { + unset($this->walkers[$offset]); + } + } +} \ No newline at end of file From b167a64544f01f6097fa1bf33d32d0a1b5d6c6b9 Mon Sep 17 00:00:00 2001 From: shustrik Date: Thu, 6 Feb 2014 20:08:07 +0300 Subject: [PATCH 6/6] remove doc --- .../ORM/Query/TreeWalkerChainIterator.php | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ORM/Query/TreeWalkerChainIterator.php b/lib/Doctrine/ORM/Query/TreeWalkerChainIterator.php index d693b422a..1b0fb8a4f 100644 --- a/lib/Doctrine/ORM/Query/TreeWalkerChainIterator.php +++ b/lib/Doctrine/ORM/Query/TreeWalkerChainIterator.php @@ -1,14 +1,27 @@ . */ namespace Doctrine\ORM\Query; - +/** + * + */ class TreeWalkerChainIterator implements \Iterator, \ArrayAccess { /**