forked from beberlei/DoctrineExtensions
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from ToshY/feature/35-mysql-bin-to-uuid
Add MySQL BinToUuid and UuidToBin
- Loading branch information
Showing
6 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Query\Mysql; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\Lexer; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
|
||
class BinToUuid extends FunctionNode | ||
{ | ||
public $binaryUuid = null; | ||
|
||
public $swapFlag = null; | ||
|
||
public function parse(Parser $parser): void | ||
{ | ||
$parser->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; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Query\Mysql; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\Lexer; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
|
||
class UuidToBin extends FunctionNode | ||
{ | ||
public $stringUuid = null; | ||
|
||
public $swapFlag = null; | ||
|
||
public function parse(Parser $parser): void | ||
{ | ||
$parser->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; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Tests\Query\Mysql; | ||
|
||
use DoctrineExtensions\Tests\Query\MysqlTestCase; | ||
|
||
final class BinToUuidTest extends MysqlTestCase | ||
{ | ||
public function testBinToUuid(): void | ||
{ | ||
$this->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_' | ||
); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Tests\Query\Mysql; | ||
|
||
use DoctrineExtensions\Tests\Query\MysqlTestCase; | ||
|
||
final class UuidToBinTest extends MysqlTestCase | ||
{ | ||
public function testUuidToBin(): void | ||
{ | ||
$this->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_' | ||
); | ||
} | ||
} |