1
0
mirror of synced 2025-02-20 22:23:14 +03:00

Order by clause support case expressions

This commit is contained in:
Fabio B. Silva 2012-02-15 19:24:06 -02:00
parent ce9643bce1
commit df0632258a
3 changed files with 20 additions and 3 deletions

View File

@ -1347,6 +1347,14 @@ class Parser
$expr = $this->SingleValuedPathExpression();
break;
case ($this->_lexer->lookahead['type'] === Lexer::T_CASE):
case ($this->_lexer->lookahead['type'] === Lexer::T_COALESCE):
case ($this->_lexer->lookahead['type'] === Lexer::T_NULLIF):
// Since NULLIF and COALESCE can be identified as a function,
// we need to check if before check for FunctionDeclaration
$expr = $this->CaseExpression();
break;
case ($this->_isFunction()):
$this->_lexer->peek(); // "("

View File

@ -786,6 +786,10 @@ class SqlWalker implements TreeWalker
case ($expr instanceof AST\AggregateExpression):
case ($expr instanceof AST\Functions\FunctionNode):
case ($expr instanceof AST\SimpleArithmeticExpression):
case ($expr instanceof AST\NullIfExpression):
case ($expr instanceof AST\CoalesceExpression):
case ($expr instanceof AST\SimpleCaseExpression):
case ($expr instanceof AST\GeneralCaseExpression):
$sql = $expr->dispatch($this);
break;
default:

View File

@ -1608,15 +1608,20 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
*/
public function testOrderByClauseSupportNullIfAndCoalesce()
{
$this->markTestIncomplete();
$this->assertSqlGeneration(
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY NULLIF(u.name, u.username)',
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY NULLIF(c0_.name, c0_.username) ASC'
);
$this->assertSqlGeneration(
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY NULLIF(u.name, u.username)',
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY COALESCE(NULLIF(u.name, \'\'), u.username) ASC'
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY COALESCE(NULLIF(u.name, u.username), u.id)',
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY COALESCE(NULLIF(c0_.name, c0_.username), c0_.id) ASC'
);
$this->assertSqlGeneration(
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY CASE u.id WHEN 1 THEN 1 ELSE 0 END',
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY CASE c0_.id WHEN 1 THEN 1 ELSE 0 END ASC'
);
}
}