Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MySQL function BIN_TO_UUID #397

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

misaert
Copy link

@misaert misaert commented Aug 30, 2022

This adds support for the BIN_TO_UUID function for MySQL.

https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_bin-to-uuid

@ToshY
Copy link

ToshY commented Nov 1, 2022

This does not seem to take into consideration that BIN_TO_UUID can have 2 arguments. If you're using something like ramsey/uuid-doctrine with InnoDB-optimised binary UUIDs, this would fail as it requires the swap-flag set to 1:
BIN_TO_UUID(...,1).

I've derived the following implementation (PHP 8.x) myself some time ago and still works fine.

<?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) . ')';
    }
}

If BIN_TO_UUID is going to get implemented, I would advise to add UUID_TO_BIN in this PR as well, as this is exactly the same implementation but just BIN_TO_UUID replaced with UUID_TO_BIN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants