1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

DDC-1858 Added missing support to ResultVariable in LikeExpression.

This commit is contained in:
Guilherme Blanco 2013-08-18 23:52:32 -04:00
parent 605c32dbb3
commit 43fc8bafa7
5 changed files with 47 additions and 18 deletions

View file

@ -1583,7 +1583,7 @@ Scalar and Type Expressions
.. code-block:: php .. code-block:: php
ScalarExpression ::= SimpleArithmeticExpression | StringPrimary | DateTimePrimary | StateFieldPathExpression | BooleanPrimary | CaseExpression | InstanceOfExpression ScalarExpression ::= SimpleArithmeticExpression | StringPrimary | DateTimePrimary | StateFieldPathExpression | BooleanPrimary | CaseExpression | InstanceOfExpression
StringExpression ::= StringPrimary | "(" Subselect ")" StringExpression ::= StringPrimary | ResultVariable | "(" Subselect ")"
StringPrimary ::= StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression | CaseExpression StringPrimary ::= StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression | CaseExpression
BooleanExpression ::= BooleanPrimary | "(" Subselect ")" BooleanExpression ::= BooleanPrimary | "(" Subselect ")"
BooleanPrimary ::= StateFieldPathExpression | boolean | InputParameter BooleanPrimary ::= StateFieldPathExpression | boolean | InputParameter

View file

@ -2776,23 +2776,29 @@ class Parser
} }
/** /**
* StringExpression ::= StringPrimary | "(" Subselect ")" * StringExpression ::= StringPrimary | ResultVariable | "(" Subselect ")"
* *
* @return \Doctrine\ORM\Query\AST\StringPrimary | * @return \Doctrine\ORM\Query\AST\StringPrimary |
* \Doctrine\ORM\Query\AST\Subselect * \Doctrine\ORM\Query\AST\Subselect |
* string
*/ */
public function StringExpression() public function StringExpression()
{ {
if ($this->lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS)) { $peek = $this->lexer->glimpse();
$peek = $this->lexer->glimpse();
if ($peek['type'] === Lexer::T_SELECT) { // Subselect
$this->match(Lexer::T_OPEN_PARENTHESIS); if ($this->lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS) && $peek['type'] === Lexer::T_SELECT) {
$expr = $this->Subselect(); $this->match(Lexer::T_OPEN_PARENTHESIS);
$this->match(Lexer::T_CLOSE_PARENTHESIS); $expr = $this->Subselect();
$this->match(Lexer::T_CLOSE_PARENTHESIS);
return $expr; return $expr;
} }
// ResultVariable (string)
if ($this->lexer->isNextToken(Lexer::T_IDENTIFIER) &&
isset($this->queryComponents[$this->lexer->lookahead['value']]['resultVariable'])) {
return $this->ResultVariable();
} }
return $this->StringPrimary(); return $this->StringPrimary();

View file

@ -1944,8 +1944,8 @@ class SqlWalker implements TreeWalker
*/ */
public function walkNullComparisonExpression($nullCompExpr) public function walkNullComparisonExpression($nullCompExpr)
{ {
$expression = $nullCompExpr->expression; $expression = $nullCompExpr->expression;
$comparison = ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL'; $comparison = ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL';
// Handle ResultVariable // Handle ResultVariable
if (is_string($expression) && isset($this->queryComponents[$expression]['resultVariable'])) { if (is_string($expression) && isset($this->queryComponents[$expression]['resultVariable'])) {
@ -2001,9 +2001,7 @@ class SqlWalker implements TreeWalker
$sqlParameterList = array(); $sqlParameterList = array();
foreach ($instanceOfExpr->value as $parameter) { foreach ($instanceOfExpr->value as $parameter) {
if ($parameter instanceof AST\InputParameter) { if ($parameter instanceof AST\InputParameter) {
$this->rsm->addMetadataParameterMapping($parameter->name, 'discriminatorValue'); $this->rsm->addMetadataParameterMapping($parameter->name, 'discriminatorValue');
$sqlParameterList[] = $this->walkInputParameter($parameter); $sqlParameterList[] = $this->walkInputParameter($parameter);
@ -2073,7 +2071,9 @@ class SqlWalker implements TreeWalker
{ {
$sql = $this->walkArithmeticExpression($betweenExpr->expression); $sql = $this->walkArithmeticExpression($betweenExpr->expression);
if ($betweenExpr->not) $sql .= ' NOT'; if ($betweenExpr->not) {
$sql .= ' NOT';
}
$sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->leftBetweenExpression) $sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->leftBetweenExpression)
. ' AND ' . $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression); . ' AND ' . $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression);
@ -2087,14 +2087,18 @@ class SqlWalker implements TreeWalker
public function walkLikeExpression($likeExpr) public function walkLikeExpression($likeExpr)
{ {
$stringExpr = $likeExpr->stringExpression; $stringExpr = $likeExpr->stringExpression;
$sql = $stringExpr->dispatch($this) . ($likeExpr->not ? ' NOT' : '') . ' LIKE '; $leftExpr = (is_string($stringExpr) && isset($this->queryComponents[$stringExpr]['resultVariable']))
? $this->walkResultVariable($stringExpr)
: $stringExpr->dispatch($this);
$sql = $leftExpr . ($likeExpr->not ? ' NOT' : '') . ' LIKE ';
if ($likeExpr->stringPattern instanceof AST\InputParameter) { if ($likeExpr->stringPattern instanceof AST\InputParameter) {
$inputParam = $likeExpr->stringPattern; $inputParam = $likeExpr->stringPattern;
$dqlParamKey = $inputParam->name; $dqlParamKey = $inputParam->name;
$this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++); $this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++);
$sql .= '?'; $sql .= '?';
} elseif ($likeExpr->stringPattern instanceof AST\Functions\FunctionNode ) { } elseif ($likeExpr->stringPattern instanceof AST\Functions\FunctionNode) {
$sql .= $this->walkFunction($likeExpr->stringPattern); $sql .= $this->walkFunction($likeExpr->stringPattern);
} elseif ($likeExpr->stringPattern instanceof AST\PathExpression) { } elseif ($likeExpr->stringPattern instanceof AST\PathExpression) {
$sql .= $this->walkPathExpression($likeExpr->stringPattern); $sql .= $this->walkPathExpression($likeExpr->stringPattern);

View file

@ -598,6 +598,14 @@ class LanguageRecognitionTest extends \Doctrine\Tests\OrmTestCase
{ {
$this->assertValidDQL("SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u HAVING u.username IS NULL"); $this->assertValidDQL("SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u HAVING u.username IS NULL");
} }
/**
* @gorup DDC-1858
*/
public function testHavingSupportLikeExpression()
{
$this->assertValidDQL("SELECT _u.id, count(_articles) as uuuu FROM Doctrine\Tests\Models\CMS\CmsUser _u LEFT JOIN _u.articles _articles GROUP BY _u HAVING uuuu LIKE '3'");
}
} }
/** @Entity */ /** @Entity */

View file

@ -2142,6 +2142,17 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
); );
} }
/**
* @group DDC-1858
*/
public function testHavingSupportResultVariableLikeExpression()
{
$this->assertSqlGeneration(
"SELECT u.name AS foo FROM Doctrine\Tests\Models\CMS\CmsUser u HAVING foo LIKE '3'",
"SELECT c0_.name AS name0 FROM cms_users c0_ HAVING name0 LIKE '3'"
);
}
/** /**
* @group DDC-1858 * @group DDC-1858
*/ */