From 441e24a9405bae5d65206110a5f61dc7d4bc25ee Mon Sep 17 00:00:00 2001 From: ToshY <31921460+ToshY@users.noreply.github.com> Date: Sat, 4 Nov 2023 21:07:32 +0100 Subject: [PATCH] add mysql bintouuid and uuidtobin --- README.md | 2 +- config/mysql.yml | 2 ++ src/Query/Mysql/BinToUuid.php | 41 +++++++++++++++++++++++++++++ src/Query/Mysql/UuidToBin.php | 41 +++++++++++++++++++++++++++++ tests/Query/Mysql/BinToUuidTest.php | 24 +++++++++++++++++ tests/Query/Mysql/UuidToBinTest.php | 24 +++++++++++++++++ 6 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/Query/Mysql/BinToUuid.php create mode 100644 src/Query/Mysql/UuidToBin.php create mode 100644 tests/Query/Mysql/BinToUuidTest.php create mode 100644 tests/Query/Mysql/UuidToBinTest.php diff --git a/README.md b/README.md index fe2e9cb1..3ebe2a66 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ MySQL, Oracle, PostgreSQL and SQLite. | DB | Functions | |:--:|:---------:| -| MySQL | `ACOS, ADDTIME, AES_DECRYPT, AES_ENCRYPT, ANY_VALUE, ASCII, ASIN, ATAN, ATAN2, BINARY, BIT_COUNT, BIT_XOR, CAST, CEIL, CHAR_LENGTH, COLLATE, CONCAT_WS, CONV, CONVERT_TZ, COS, COT, COUNTIF, CRC32, DATE, DATE_FORMAT, DATEADD, DATEDIFF, DATESUB, DAY, DAYNAME, DAYOFWEEK, DAYOFYEAR, DEGREES, DIV, EXP, EXTRACT, FIELD, FIND_IN_SET, FLOOR, FORMAT, FROM_BASE64, FROM_UNIXTIME, GREATEST, GROUP_CONCAT, HEX, HOUR, IFELSE, IFNULL, INET_ATON, INET_NTOA, INET6_ATON, INET6_NTOA, INSTR, IS_IPV4, IS_IPV4_COMPAT, IS_IPV4_MAPPED, IS_IPV6, JSON_CONTAINS, JSON_DEPTH, JSON_EXTRACT, JSON_LENGTH, JSON_UNQUOTE, LAG, LAST_DAY, LAST_INSERT_ID, LEAD, LEAST, LOG, LOG10, LOG2, LPAD, MAKEDATE, MATCH, MD5, MINUTE, MOD, MONTH, MONTHNAME, NOW, NULLIF, OVER, PERIOD_DIFF, PI, POINT, POWER, QUARTER, RADIANS, RAND, REGEXP, REGEXP_REPLACE, REGEXP_SUBSTR, REPLACE, ROUND, RPAD, SECOND, SECTOTIME, SHA1, SHA2, SIN, SOUNDEX, STD, STDDEV, STRTODATE, ST_DISTANCE_SPHERE, STR_TO_DATE, SUBSTRING_INDEX, TAN, TIME, TIMEDIFF, TIMESTAMPADD, TIMESTAMPDIFF, TIMETOSEC, TRUNCATE, UNHEX, UNIX_TIMESTAMP, UTC_TIMESTAMP, UUID_SHORT, VARIANCE, WEEK, WEEKDAY, WEEKOFYEAR, YEAR, YEARMONTH, YEARWEEK` | +| MySQL | `ACOS, ADDTIME, AES_DECRYPT, AES_ENCRYPT, ANY_VALUE, ASCII, ASIN, ATAN, ATAN2, BIN_TO_UUID, BINARY, BIT_COUNT, BIT_XOR, CAST, CEIL, CHAR_LENGTH, COLLATE, CONCAT_WS, CONV, CONVERT_TZ, COS, COT, COUNTIF, CRC32, DATE, DATE_FORMAT, DATEADD, DATEDIFF, DATESUB, DAY, DAYNAME, DAYOFWEEK, DAYOFYEAR, DEGREES, DIV, EXP, EXTRACT, FIELD, FIND_IN_SET, FLOOR, FORMAT, FROM_BASE64, FROM_UNIXTIME, GREATEST, GROUP_CONCAT, HEX, HOUR, IFELSE, IFNULL, INET_ATON, INET_NTOA, INET6_ATON, INET6_NTOA, INSTR, IS_IPV4, IS_IPV4_COMPAT, IS_IPV4_MAPPED, IS_IPV6, JSON_CONTAINS, JSON_DEPTH, JSON_EXTRACT, JSON_LENGTH, JSON_UNQUOTE, LAG, LAST_DAY, LAST_INSERT_ID, LEAD, LEAST, LOG, LOG10, LOG2, LPAD, MAKEDATE, MATCH, MD5, MINUTE, MOD, MONTH, MONTHNAME, NOW, NULLIF, OVER, PERIOD_DIFF, PI, POINT, POWER, QUARTER, RADIANS, RAND, REGEXP, REGEXP_REPLACE, REGEXP_SUBSTR, REPLACE, ROUND, RPAD, SECOND, SECTOTIME, SHA1, SHA2, SIN, SOUNDEX, STD, STDDEV, STRTODATE, ST_DISTANCE_SPHERE, STR_TO_DATE, SUBSTRING_INDEX, TAN, TIME, TIMEDIFF, TIMESTAMPADD, TIMESTAMPDIFF, TIMETOSEC, TRUNCATE, UNHEX, UNIX_TIMESTAMP, UTC_TIMESTAMP, UUID_SHORT, UUID_TO_BIN, VARIANCE, WEEK, WEEKDAY, WEEKOFYEAR, YEAR, YEARMONTH, YEARWEEK` | | Oracle | `CEIL, DAY, FLOOR, HOUR, LISTAGG, MINUTE, MONTH, NVL, SECOND, TO_CHAR, TO_DATE, TRUNC, YEAR` | | SQLite | `CASE WHEN THEN ELSE END, DATE, DATE_FORMAT*, DAY, HOUR, IFNULL, JULIANDAY, MINUTE, MONTH, REPLACE, ROUND, SECOND, STRFTIME, WEEK, WEEKDAY, YEAR` | | PostgreSQL | `AGE, AT_TIME_ZONE, COUNT_FILTER, DATE, DATE_PART, DATE_TRUNC, DAY, EXTRACT, GREATEST, HOUR, ILIKE, LEAST, MINUTE, MONTH, QUARTER, REGEXP_REPLACE, SECOND, STRING_AGG, TO_CHAR, TO_DATE, YEAR` | diff --git a/config/mysql.yml b/config/mysql.yml index a63bc3da..487a08fc 100644 --- a/config/mysql.yml +++ b/config/mysql.yml @@ -85,6 +85,7 @@ doctrine: any_value: DoctrineExtensions\Query\Mysql\AnyValue ascii: DoctrineExtensions\Query\Mysql\Ascii binary: DoctrineExtensions\Query\Mysql\Binary + bin_to_uuid: DoctrineExtensions\Query\Mysql\BinToUuid cast: DoctrineExtensions\Query\Mysql\Cast char_length: DoctrineExtensions\Query\Mysql\CharLength collate: DoctrineExtensions\Query\Mysql\Collate @@ -132,3 +133,4 @@ doctrine: substring_index: DoctrineExtensions\Query\Mysql\SubstringIndex unhex: DoctrineExtensions\Query\Mysql\Unhex uuid_short: DoctrineExtensions\Query\Mysql\UuidShort + uuid_to_bin: DoctrineExtensions\Query\Mysql\UuidToBin diff --git a/src/Query/Mysql/BinToUuid.php b/src/Query/Mysql/BinToUuid.php new file mode 100644 index 00000000..8fa78683 --- /dev/null +++ b/src/Query/Mysql/BinToUuid.php @@ -0,0 +1,41 @@ +match(Lexer::T_IDENTIFIER); + $parser->match(Lexer::T_OPEN_PARENTHESIS); + + $this->binaryUuid = $parser->ArithmeticPrimary(); + + if (Lexer::T_COMMA === $parser->getLexer()->lookahead->type) { + $parser->match(Lexer::T_COMMA); + $this->swapFlag = $parser->Literal(); + } + + $parser->match(Lexer::T_CLOSE_PARENTHESIS); + } + + public function getSql(SqlWalker $sqlWalker): string + { + $sql = 'BIN_TO_UUID(' . $this->binaryUuid->dispatch($sqlWalker); + if ($this->swapFlag !== null) { + $sql .= ', ' . $this->swapFlag->dispatch($sqlWalker); + } + $sql .= ')'; + + return $sql; + } +} diff --git a/src/Query/Mysql/UuidToBin.php b/src/Query/Mysql/UuidToBin.php new file mode 100644 index 00000000..e1546b6a --- /dev/null +++ b/src/Query/Mysql/UuidToBin.php @@ -0,0 +1,41 @@ +match(Lexer::T_IDENTIFIER); + $parser->match(Lexer::T_OPEN_PARENTHESIS); + + $this->stringUuid = $parser->ArithmeticPrimary(); + + if (Lexer::T_COMMA === $parser->getLexer()->lookahead->type) { + $parser->match(Lexer::T_COMMA); + $this->swapFlag = $parser->Literal(); + } + + $parser->match(Lexer::T_CLOSE_PARENTHESIS); + } + + public function getSql(SqlWalker $sqlWalker): string + { + $sql = 'UUID_TO_BIN(' . $this->stringUuid->dispatch($sqlWalker); + if ($this->swapFlag !== null) { + $sql .= ', ' . $this->swapFlag->dispatch($sqlWalker); + } + $sql .= ')'; + + return $sql; + } +} diff --git a/tests/Query/Mysql/BinToUuidTest.php b/tests/Query/Mysql/BinToUuidTest.php new file mode 100644 index 00000000..e408fea8 --- /dev/null +++ b/tests/Query/Mysql/BinToUuidTest.php @@ -0,0 +1,24 @@ +assertDqlProducesSql( + "SELECT BIN_TO_UUID(b.id) from DoctrineExtensions\Tests\Entities\Blank b", + 'SELECT BIN_TO_UUID(b0_.id) AS sclr_0 FROM Blank b0_' + ); + } + + public function testBinToUuidSwapFlag(): void + { + $this->assertDqlProducesSql( + "SELECT BIN_TO_UUID(b.id, 1) from DoctrineExtensions\Tests\Entities\Blank b", + 'SELECT BIN_TO_UUID(b0_.id, 1) AS sclr_0 FROM Blank b0_' + ); + } +} diff --git a/tests/Query/Mysql/UuidToBinTest.php b/tests/Query/Mysql/UuidToBinTest.php new file mode 100644 index 00000000..dec4c4ec --- /dev/null +++ b/tests/Query/Mysql/UuidToBinTest.php @@ -0,0 +1,24 @@ +assertDqlProducesSql( + "SELECT UUID_TO_BIN(b.id) from DoctrineExtensions\Tests\Entities\Blank b", + 'SELECT UUID_TO_BIN(b0_.id) AS sclr_0 FROM Blank b0_' + ); + } + + public function testBinToUuidSwapFlag(): void + { + $this->assertDqlProducesSql( + "SELECT BIN_TO_UUID(b.id, 1) from DoctrineExtensions\Tests\Entities\Blank b", + 'SELECT BIN_TO_UUID(b0_.id, 1) AS sclr_0 FROM Blank b0_' + ); + } +}