Skip to content

Instantly share code, notes, and snippets.

@ToshY
Last active April 16, 2022 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToshY/181ac4db0457dafcb7364f4b3cddd096 to your computer and use it in GitHub Desktop.
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
<?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) . ')';
}
}
doctrine:
orm:
dql:
string_functions:
bin_to_uuid: App\Service\Application\Doctrine\DqlFunctions\BinToUuid
uuid_to_bin: App\Service\Application\Doctrine\DqlFunctions\UuidToBin
<?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