Skip to content

Commit

Permalink
Properly fixed DDC-1858. Added support for ResultVariable in NullComp…
Browse files Browse the repository at this point in the history
…arisons while using HavingClause.
  • Loading branch information
Guilherme Blanco committed Aug 3, 2013
1 parent a19106b commit d9c1782
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
26 changes: 24 additions & 2 deletions lib/Doctrine/ORM/Query/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3108,7 +3108,7 @@ public function LikeExpression()
}

/**
* NullComparisonExpression ::= (InputParameter | NullIfExpression | CoalesceExpression | SingleValuedPathExpression) "IS" ["NOT"] "NULL"
* NullComparisonExpression ::= (InputParameter | NullIfExpression | CoalesceExpression | SingleValuedPathExpression | ResultVariable) "IS" ["NOT"] "NULL"
*
* @return \Doctrine\ORM\Query\AST\NullComparisonExpression
*/
Expand All @@ -3134,7 +3134,29 @@ public function NullComparisonExpression()
break;

default:
$expr = $this->SingleValuedPathExpression();
// We need to check if we are in a IdentificationVariable or SingleValuedPathExpression
$glimpse = $this->lexer->glimpse();

if ($glimpse['type'] === Lexer::T_DOT) {
$expr = $this->SingleValuedPathExpression();

// Leave switch statement
break;
}

$lookaheadValue = $this->lexer->lookahead['value'];

// Validate existing component
if ( ! isset($this->queryComponents[$lookaheadValue])) {
$this->semanticalError('Cannot add having condition on undefined result variable.');
}

// Validating ResultVariable
if ( ! isset($this->queryComponents[$lookaheadValue]['resultVariable'])) {
$this->semanticalError('Cannot add having condition on a non result variable.');
}

$expr = $this->ResultVariable();
break;
}

Expand Down
10 changes: 8 additions & 2 deletions lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -1921,9 +1921,15 @@ public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
*/
public function walkNullComparisonExpression($nullCompExpr)
{
$expression = $nullCompExpr->expression;
$comparison = ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL';
$expression = $nullCompExpr->expression;
$comparison = ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL';

// Handle ResultVariable
if (is_string($expression) && isset($this->queryComponents[$expression]['resultVariable'])) {
return $this->walkResultVariable($expression) . $comparison;
}

// Handle InputParameter mapping inclusion to ParserResult
if ($expression instanceof AST\InputParameter) {
$this->parserResult->addParameterMapping($expression->name, $this->sqlParamIndex++);

Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,17 @@ public function testHavingSupportResultVariableInExpression()
'SELECT c0_.name AS name0 FROM cms_users c0_ HAVING name0 IN (?)'
);
}

/**
* @group DDC-1858
*/
public function testHavingSupportResultVariableInAggregateFunction()
{
$this->assertSqlGeneration(
'SELECT COUNT(u.name) AS countName FROM Doctrine\Tests\Models\CMS\CmsUser u HAVING countName IS NULL',
'SELECT COUNT(c0_.name) AS sclr0 FROM cms_users c0_ HAVING sclr0 IS NULL'
);
}
}

class MyAbsFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode
Expand Down

0 comments on commit d9c1782

Please sign in to comment.