1
0
mirror of synced 2024-12-14 15:16:04 +03:00

Merge pull request #288 from FabioBatSilva/DDC-775

DDC 775
This commit is contained in:
Benjamin Eberlei 2012-03-22 14:00:48 -07:00
commit 7d7edbbd4a
3 changed files with 51 additions and 6 deletions

View File

@ -1333,18 +1333,43 @@ class Parser
} }
/** /**
* OrderByItem ::= (ResultVariable | SingleValuedPathExpression) ["ASC" | "DESC"] * OrderByItem ::= (
* SimpleArithmeticExpression | SingleValuedPathExpression |
* ScalarExpression | ResultVariable
* ) ["ASC" | "DESC"]
* *
* @return \Doctrine\ORM\Query\AST\OrderByItem * @return \Doctrine\ORM\Query\AST\OrderByItem
*/ */
public function OrderByItem() public function OrderByItem()
{ {
$type = 'ASC';
// We need to check if we are in a ResultVariable or StateFieldPathExpression $this->_lexer->peek(); // lookahead => '.'
$this->_lexer->peek(); // lookahead => token after '.'
$peek = $this->_lexer->peek(); // lookahead => token after the token after the '.'
$this->_lexer->resetPeek();
$glimpse = $this->_lexer->glimpse(); $glimpse = $this->_lexer->glimpse();
$expr = ($glimpse['type'] != Lexer::T_DOT) ? $this->ResultVariable() : $this->SingleValuedPathExpression();
switch (true) {
case ($this->_isMathOperator($peek)):
$expr = $this->SimpleArithmeticExpression();
break;
case ($glimpse['type'] === Lexer::T_DOT):
$expr = $this->SingleValuedPathExpression();
break;
case ($this->_lexer->peek() && $this->_isMathOperator($this->_peekBeyondClosingParenthesis())):
$expr = $this->ScalarExpression();
break;
default:
$expr = $this->ResultVariable();
break;
}
$type = 'ASC';
$item = new AST\OrderByItem($expr); $item = new AST\OrderByItem($expr);
switch (true) { switch (true) {

View File

@ -780,8 +780,8 @@ class SqlWalker implements TreeWalker
public function walkOrderByItem($orderByItem) public function walkOrderByItem($orderByItem)
{ {
$expr = $orderByItem->expression; $expr = $orderByItem->expression;
$sql = ($expr instanceof AST\PathExpression) $sql = ($expr instanceof AST\Node)
? $this->walkPathExpression($expr) ? $expr->dispatch($this)
: $this->walkResultVariable($this->_queryComponents[$expr]['token']['value']); : $this->walkResultVariable($this->_queryComponents[$expr]['token']['value']);
return $sql . ' ' . strtoupper($orderByItem->type); return $sql . ' ' . strtoupper($orderByItem->type);

View File

@ -1571,6 +1571,26 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
"SELECT c0_.id AS id0, c0_.completed AS completed1, c0_.fixPrice AS fixPrice2, c1_.id AS id3, c1_.completed AS completed4, c1_.hoursWorked AS hoursWorked5, c1_.pricePerHour AS pricePerHour6, c1_.maxPrice AS maxPrice7, c0_.discr AS discr8, c1_.discr AS discr9 FROM company_contracts c0_, company_contracts c1_ WHERE (c0_.discr IN ('fix') AND c1_.discr IN ('flexible', 'flexultra'))" "SELECT c0_.id AS id0, c0_.completed AS completed1, c0_.fixPrice AS fixPrice2, c1_.id AS id3, c1_.completed AS completed4, c1_.hoursWorked AS hoursWorked5, c1_.pricePerHour AS pricePerHour6, c1_.maxPrice AS maxPrice7, c0_.discr AS discr8, c1_.discr AS discr9 FROM company_contracts c0_, company_contracts c1_ WHERE (c0_.discr IN ('fix') AND c1_.discr IN ('flexible', 'flexultra'))"
); );
} }
/**
* @group DDC-775
*/
public function testOrderByClauseSupportsSimpleArithmeticExpression()
{
$this->assertSqlGeneration(
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.id + 1 ',
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY c0_.id + 1 ASC'
);
$this->assertSqlGeneration(
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY ( ( (u.id + 1) * (u.id - 1) ) / 2)',
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY (c0_.id + 1) * (c0_.id - 1) / 2 ASC'
);
$this->assertSqlGeneration(
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY ((u.id + 5000) * u.id + 3) ',
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY (c0_.id + 5000) * c0_.id + 3 ASC'
);
}
} }