Created
November 24, 2016 08:30
-
-
Save edwinyoyada/03fadcc77121e304cc586e9b00af63fd to your computer and use it in GitHub Desktop.
Doctrine SQL If Function
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
<?php | |
namespace Your\PathToBundle\DQL; | |
use Doctrine\ORM\Query\Lexer; | |
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | |
use Doctrine\ORM\Query\Parser; | |
use Doctrine\ORM\Query\SqlWalker; | |
/** | |
* Created by PhpStorm. | |
* User: Edwin Yoyada Pratama | |
* Date: 11/24/2016 | |
* Time: 12:19 PM | |
*/ | |
class IfFunction extends FunctionNode { | |
public $firstStringExpression = null; | |
public $secondStringExpression = null; | |
public $isTrue = null; | |
public $isFalse = null; | |
public function parse(Parser $parser) | |
{ | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->firstStringExpression = $parser->SimpleEntityExpression(); | |
$parser->match(Lexer::T_EQUALS); | |
$this->secondStringExpression = $parser->StringPrimary(); | |
$parser->match(Lexer::T_COMMA); | |
$this->isTrue = $parser->SimpleArithmeticExpression(); | |
$parser->match(Lexer::T_COMMA); | |
$this->isFalse = $parser->SimpleArithmeticExpression(); | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
public function getSql(SqlWalker $sqlWalker) | |
{ | |
return "IF(" . $this->firstStringExpression->dispatch($sqlWalker) . " = " . $this->secondStringExpression->dispatch($sqlWalker) . ", " . $this->isTrue->dispatch($sqlWalker) . ", " . $this->isFalse->dispatch($sqlWalker) . ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice