From 889094709e2df7d5a40dd09a7ef24211f97bb958 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Wed, 14 Apr 2010 00:04:44 -0300 Subject: [PATCH] [2.0] Added support to IdentificationVariable that was missing in ArithmeticPrimary (it was not correctly handling it). Uncommented a unit test that added coverage to it. --- .../Hydration/SingleScalarHydrator.php | 1 + lib/Doctrine/ORM/Query/AST/PathExpression.php | 1 + lib/Doctrine/ORM/Query/Parser.php | 32 +++++++++++-------- lib/Doctrine/ORM/Query/SqlWalker.php | 13 ++++++++ .../ORM/Query/SelectSqlGenerationTest.php | 5 +-- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/lib/Doctrine/ORM/Internal/Hydration/SingleScalarHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/SingleScalarHydrator.php index a095c2a7d..21846b6dc 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/SingleScalarHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/SingleScalarHydrator.php @@ -43,6 +43,7 @@ class SingleScalarHydrator extends AbstractHydrator } else if ($num > 1 || count($result[key($result)]) > 1) { throw new \Doctrine\ORM\NonUniqueResultException; } + $result = $this->_gatherScalarRowData($result[key($result)], $cache); return array_shift($result); diff --git a/lib/Doctrine/ORM/Query/AST/PathExpression.php b/lib/Doctrine/ORM/Query/AST/PathExpression.php index 4f8d0e277..996d35c27 100644 --- a/lib/Doctrine/ORM/Query/AST/PathExpression.php +++ b/lib/Doctrine/ORM/Query/AST/PathExpression.php @@ -41,6 +41,7 @@ namespace Doctrine\ORM\Query\AST; */ class PathExpression extends Node { + const TYPE_IDENTIFICATION_VARIABLE = 1; const TYPE_COLLECTION_VALUED_ASSOCIATION = 2; const TYPE_SINGLE_VALUED_ASSOCIATION = 4; const TYPE_STATE_FIELD = 8; diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index d3c7e3808..6f9e73c26 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -565,7 +565,8 @@ class Parser $aliasIdentificationVariable = $pathExpression->identificationVariable; $parentField = $pathExpression->identificationVariable; $class = $qComp['metadata']; - $fieldType = null; + $fieldType = ($pathExpression->expectedType == AST\PathExpression::TYPE_IDENTIFICATION_VARIABLE) + ? AST\PathExpression::TYPE_IDENTIFICATION_VARIABLE : null; $curIndex = 0; foreach ($parts as $field) { @@ -602,8 +603,8 @@ class Parser $class = $this->_em->getClassMetadata($assoc->targetEntityName); if ( - ($curIndex != $numParts - 1) && - ! isset($this->_queryComponents[$aliasIdentificationVariable . '.' . $field]) + ($curIndex != $numParts - 1) && + ! isset($this->_queryComponents[$aliasIdentificationVariable . '.' . $field]) ) { // Building queryComponent $joinQueryComponent = array( @@ -617,13 +618,13 @@ class Parser // Create AST node $joinVariableDeclaration = new AST\JoinVariableDeclaration( - new AST\Join( - AST\Join::JOIN_TYPE_INNER, - new AST\JoinAssociationPathExpression($aliasIdentificationVariable, $field), - $aliasIdentificationVariable . '.' . $field, - false - ), - null + new AST\Join( + AST\Join::JOIN_TYPE_INNER, + new AST\JoinAssociationPathExpression($aliasIdentificationVariable, $field), + $aliasIdentificationVariable . '.' . $field, + false + ), + null ); $AST->fromClause->identificationVariableDeclarations[0]->joinVariableDeclarations[] = $joinVariableDeclaration; @@ -649,6 +650,11 @@ class Parser // We need to recognize which was expected type(s) $expectedStringTypes = array(); + // Validate state field type + if ($expectedType & AST\PathExpression::TYPE_IDENTIFICATION_VARIABLE) { + $expectedStringTypes[] = 'IdentificationVariable'; + } + // Validate state field type if ($expectedType & AST\PathExpression::TYPE_STATE_FIELD) { $expectedStringTypes[] = 'StateFieldPathExpression'; @@ -915,12 +921,12 @@ class Parser $identVariable = $this->IdentificationVariable(); $parts = array(); - do { + while ($this->_lexer->isNextToken(Lexer::T_DOT)) { $this->match(Lexer::T_DOT); $this->match(Lexer::T_IDENTIFIER); $parts[] = $this->_lexer->token['value']; - } while ($this->_lexer->isNextToken(Lexer::T_DOT)); + } // Creating AST node $pathExpr = new AST\PathExpression($expectedTypes, $identVariable, $parts); @@ -2168,7 +2174,7 @@ class Parser return $this->SingleValuedPathExpression(); } - return $this->IdentificationVariable(); + return $this->PathExpression(AST\PathExpression::TYPE_IDENTIFICATION_VARIABLE); case Lexer::T_INPUT_PARAMETER: return $this->InputParameter(); diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index be583c625..7a0b362c1 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -446,6 +446,17 @@ class SqlWalker implements TreeWalker $sql = ''; switch ($pathExpr->type) { + case AST\PathExpression::TYPE_IDENTIFICATION_VARIABLE: + $dqlAlias = $pathExpr->identificationVariable; + $class = $this->_queryComponents[$dqlAlias]['metadata']; + + if ($this->_useSqlTableAliases) { + $sql .= $this->walkIdentificationVariable($dqlAlias) . '.'; + } + + $sql .= $class->getQuotedColumnName($class->identifier[0], $this->_platform); + break; + case AST\PathExpression::TYPE_STATE_FIELD: $parts = $pathExpr->parts; $fieldName = array_pop($parts); @@ -458,6 +469,7 @@ class SqlWalker implements TreeWalker $sql .= $class->getQuotedColumnName($fieldName, $this->_platform); break; + case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION: // 1- the owning side: // Just use the foreign key, i.e. u.group_id @@ -484,6 +496,7 @@ class SqlWalker implements TreeWalker throw QueryException::associationPathInverseSideNotSupported(); } break; + default: throw QueryException::invalidPathExpression($pathExpr); } diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 163bc594e..7ed9c6693 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -21,6 +21,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase $query = $this->_em->createQuery($dqlToBeTested); $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true) ->useQueryCache(false); + parent::assertEquals($sqlToBeConfirmed, $query->getSql()); $query->free(); } catch (\Exception $e) { @@ -385,7 +386,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.col_datetime > CURRENT_TIMESTAMP', $q->getSql()); } - /*public function testExistsExpressionInWhereCorrelatedSubqueryAssocCondition() + public function testExistsExpressionInWhereCorrelatedSubqueryAssocCondition() { $this->assertSqlGeneration( // DQL @@ -402,7 +403,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase . ')' ); - }*/ + } public function testLimitFromQueryClass() {