Created
April 27, 2011 14:57
-
-
Save iksela/944391 to your computer and use it in GitHub Desktop.
Custom DQL functions
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
$config = new Doctrine\ORM\Configuration(); | |
[...] | |
// add custom DQL extensions | |
$config->addCustomStringFunction('NVL', 'DqlNvl'); | |
$config->addCustomStringFunction('CONVERT', 'DqlConvert'); | |
$config->addCustomStringFunction('TO_NUMBER', 'DqlToNumber'); |
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 | |
use Doctrine\ORM\Query\Lexer; | |
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | |
class DqlConvert extends FunctionNode { | |
public $field; | |
public $convertMode; | |
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) { | |
return 'CONVERT(' . | |
$this->field->dispatch($sqlWalker) . ', ' . | |
$this->convertMode->dispatch($sqlWalker) . | |
')'; | |
} | |
public function parse(\Doctrine\ORM\Query\Parser $parser) { | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->field = $parser->StringPrimary(); | |
$parser->match(Lexer::T_COMMA); | |
$this->convertMode = $parser->ArithmeticPrimary(); | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
} |
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 | |
use Doctrine\ORM\Query\Lexer; | |
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | |
class DqlNvl extends FunctionNode { | |
public $field; | |
public $fallback; | |
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) { | |
return 'NVL(' . | |
$this->field->dispatch($sqlWalker) . ', ' . | |
$this->fallback->dispatch($sqlWalker) . | |
')'; | |
} | |
public function parse(\Doctrine\ORM\Query\Parser $parser) { | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->field = $parser->StringPrimary(); | |
$parser->match(Lexer::T_COMMA); | |
$this->fallback = $parser->StringPrimary(); | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
} |
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 | |
use Doctrine\ORM\Query\Lexer; | |
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | |
class DqlToNumber extends FunctionNode { | |
public $field; | |
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) { | |
return 'TO_NUMBER(' . | |
$this->field->dispatch($sqlWalker) . | |
')'; | |
} | |
public function parse(\Doctrine\ORM\Query\Parser $parser) { | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->field = $parser->StringPrimary(); | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment