Commit 56a3ae1e authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Doctrine ORM : support de la fonction coalesce(), y compris dans un order by.

parent 59fa208a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ use UnicaenApp\Message\View\Helper\MessageHelperFactory;
use UnicaenApp\Mvc\RedirectResponse;
use UnicaenApp\Mvc\RedirectResponseFactory;
use UnicaenApp\ORM\Query\Functions\Chr;
use UnicaenApp\ORM\Query\Functions\Coalesce;
use UnicaenApp\ORM\Query\Functions\CompriseEntre;
use UnicaenApp\ORM\Query\Functions\PasHistorise;
use UnicaenApp\ORM\Query\Functions\Replace;
@@ -467,6 +468,7 @@ return [
                    'pasHistorise' => PasHistorise::class,
                    'replace' => Replace::class,
                    'chr' => Chr::class,
                    'coalesce' => Coalesce::class,
                ],
            ],
        ],
+38 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenApp\ORM\Query\Functions;

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 Coalesce extends FunctionNode
{
    public Node $inputString;
    public Node $replacementString;

    /**
     * @throws \Doctrine\ORM\Query\QueryException
     */
    public function parse(Parser $parser): void
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->inputString = $parser->StringPrimary();
        $parser->match(Lexer::T_COMMA);
        $this->replacementString = $parser->StringPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    /**
     * @throws \Doctrine\ORM\Query\AST\ASTException
     */
    public function getSql(SqlWalker $sqlWalker): string
    {
        return sprintf('coalesce(%s, %s)',
                $this->inputString->dispatch($sqlWalker), 
                $this->replacementString->dispatch($sqlWalker));
    }
}
 No newline at end of file