Last active
April 16, 2022 19:34
-
-
Save ToshY/181ac4db0457dafcb7364f4b3cddd096 to your computer and use it in GitHub Desktop.
Doctrine Extensions UUID_TO_BIN / BIN_TO_UUID with Symfony 5.x / PHP 8.x
This file contains hidden or 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 | |
declare(strict_types=1); | |
namespace App\Service\Application\Doctrine\DqlFunctions; | |
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 BinToUuid extends FunctionNode | |
{ | |
private Node|string|null $firstExpression = null; | |
private Node|string|null $secondExpression = null; | |
public function parse(Parser $parser): void | |
{ | |
$lexer = $parser->getLexer(); | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->firstExpression = $parser->ArithmeticPrimary(); | |
// parse second parameter if available | |
if (Lexer::T_COMMA === $lexer->lookahead['type']) { | |
$parser->match(Lexer::T_COMMA); | |
$this->secondExpression = $parser->ArithmeticPrimary(); | |
} | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
public function getSql(SqlWalker $sqlWalker): string | |
{ | |
if (null !== $this->secondExpression) { | |
return 'BIN_TO_UUID(' | |
. $this->firstExpression->dispatch($sqlWalker) | |
. ', ' | |
. $this->secondExpression->dispatch($sqlWalker) | |
. ')'; | |
} | |
return 'BIN_TO_UUID(' . $this->firstExpression->dispatch($sqlWalker) . ')'; | |
} | |
} |
This file contains hidden or 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
doctrine: | |
orm: | |
dql: | |
string_functions: | |
bin_to_uuid: App\Service\Application\Doctrine\DqlFunctions\BinToUuid | |
uuid_to_bin: App\Service\Application\Doctrine\DqlFunctions\UuidToBin |
This file contains hidden or 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 | |
declare(strict_types=1); | |
namespace App\Service\Application\Doctrine\DqlFunctions; | |
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 UuidToBin extends FunctionNode | |
{ | |
private Node|string|null $firstExpression = null; | |
private Node|string|null $secondExpression = null; | |
public function parse(Parser $parser): void | |
{ | |
$lexer = $parser->getLexer(); | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->firstExpression = $parser->ArithmeticPrimary(); | |
// parse second parameter if available | |
if (Lexer::T_COMMA === $lexer->lookahead['type']) { | |
$parser->match(Lexer::T_COMMA); | |
$this->secondExpression = $parser->ArithmeticPrimary(); | |
} | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
public function getSql(SqlWalker $sqlWalker): string | |
{ | |
if (null !== $this->secondExpression) { | |
return 'UUID_TO_BIN(' | |
. $this->firstExpression->dispatch($sqlWalker) | |
. ', ' | |
. $this->secondExpression->dispatch($sqlWalker) | |
. ')'; | |
} | |
return 'UUID_TO_BIN(' . $this->firstExpression->dispatch($sqlWalker) . ')'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment