forked from beberlei/DoctrineExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request beberlei#234 from bubnovKelnik/master
Add Query/Sqlite/IfElse with tests
- Loading branch information
Showing
4 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Query\Sqlite; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\Lexer; | ||
|
||
/** | ||
* @author Mikhail Bubnov <[email protected]> | ||
*/ | ||
class IfElse extends FunctionNode | ||
{ | ||
private $expr = array(); | ||
|
||
public function parse(\Doctrine\ORM\Query\Parser $parser) | ||
{ | ||
$parser->match(Lexer::T_IDENTIFIER); | ||
$parser->match(Lexer::T_OPEN_PARENTHESIS); | ||
$this->expr[] = $parser->ConditionalExpression(); | ||
|
||
for ($i = 0; $i < 2; $i++) | ||
{ | ||
$parser->match(Lexer::T_COMMA); | ||
$this->expr[] = $parser->ArithmeticExpression(); | ||
} | ||
|
||
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) | ||
{ | ||
return sprintf('CASE WHEN %s THEN %s ELSE %s END', | ||
$sqlWalker->walkConditionalExpression($this->expr[0]), | ||
$sqlWalker->walkArithmeticPrimary($this->expr[1]), | ||
$sqlWalker->walkArithmeticPrimary($this->expr[2])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Tests\Query\Sqlite; | ||
|
||
use DoctrineExtensions\Tests\Query\SqliteTestCase; | ||
|
||
class IfElse extends SqliteTestCase | ||
{ | ||
public function testIfElse() | ||
{ | ||
$dql = "SELECT ifelse(1 > 0, 1, 0) FROM DoctrineExtensions\\Tests\\Entities\\BlogPost b"; | ||
|
||
$this->assertEquals( | ||
"SELECT CASE WHEN 1 > 0 THEN 1 ELSE 0 END AS {$this->columnAlias} FROM BlogPost b0_", | ||
$this->entityManager->createQuery($dql)->getSql() | ||
); | ||
} | ||
} |