Skip to content

Instantly share code, notes, and snippets.

@olimsaidov
Created September 8, 2016 05:24
Show Gist options
  • Save olimsaidov/4bbd530b1b645ce75e1bbb781b5dd91f to your computer and use it in GitHub Desktop.
Save olimsaidov/4bbd530b1b645ce75e1bbb781b5dd91f to your computer and use it in GitHub Desktop.
Doctrine Greatest Function
<?php
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
class Greatest extends FunctionNode
{
public $exprs;
public function parse(Parser $parser)
{
$this->exprs = [];
$lexer = $parser->getLexer();
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->exprs[] = $parser->ArithmeticPrimary();
while (Lexer::T_COMMA === $lexer->lookahead['type']) {
$parser->match(Lexer::T_COMMA);
$this->exprs[] = $parser->ArithmeticPrimary();
}
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker)
{
return 'GREATEST(' . join(',', array_map(function(Node $expr) use ($sqlWalker) {
return $expr->dispatch($sqlWalker);
}, $this->exprs)) . ')';
}
}
@zhil
Copy link

zhil commented Mar 25, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment