-
-
Notifications
You must be signed in to change notification settings - Fork 51
New #1152: Add UuidValue expression for DBMS-independent UUID values
#1199
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
Open
KalimeroMK
wants to merge
2
commits into
yiisoft:master
Choose a base branch
from
KalimeroMK:add-1152-uuid-value
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Expression\Value\Builder; | ||
|
|
||
| use Yiisoft\Db\Constant\DataType; | ||
| use Yiisoft\Db\Expression\ExpressionBuilderInterface; | ||
| use Yiisoft\Db\Expression\ExpressionInterface; | ||
| use Yiisoft\Db\Expression\Value\Param; | ||
| use Yiisoft\Db\Expression\Value\UuidValue; | ||
| use Yiisoft\Db\Helper\DbUuidHelper; | ||
| use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; | ||
|
|
||
| /** | ||
| * Builder for {@see UuidValue} expressions. | ||
| * | ||
| * Binds the UUID as a string parameter in the canonical form, which is what PostgreSQL `uuid` and MSSQL | ||
| * `uniqueidentifier` columns expect. DBMS that store a UUID as raw bytes, such as MySQL, MariaDB, SQLite and Oracle, | ||
| * override {@see prepareValue()} to convert the value with {@see DbUuidHelper::uuidToBlob()} and bind it as | ||
| * {@see DataType::LOB}, so the driver sends it as binary rather than as a character string. | ||
| * | ||
| * @implements ExpressionBuilderInterface<UuidValue> | ||
| */ | ||
| class UuidValueBuilder implements ExpressionBuilderInterface | ||
| { | ||
| /** | ||
| * @param QueryBuilderInterface $queryBuilder The query builder instance. | ||
| */ | ||
| public function __construct( | ||
| protected readonly QueryBuilderInterface $queryBuilder, | ||
| ) {} | ||
|
|
||
| public function build(ExpressionInterface $expression, array &$params = []): string | ||
| { | ||
| return $this->queryBuilder->buildValue($this->prepareValue($expression), $params); | ||
| } | ||
|
|
||
| /** | ||
| * Converts the UUID to the parameter expected by the DBMS. | ||
| * | ||
| * @param UuidValue $expression The expression to convert. | ||
| * | ||
| * @return Param The parameter to bind, it's passed to {@see QueryBuilderInterface::buildValue()}. | ||
| */ | ||
| protected function prepareValue(UuidValue $expression): Param | ||
| { | ||
| return new Param($expression->value, DataType::STRING); | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Expression\Value; | ||
|
|
||
| use InvalidArgumentException; | ||
| use Stringable; | ||
| use Yiisoft\Db\Expression\ExpressionInterface; | ||
| use Yiisoft\Db\Helper\DbUuidHelper; | ||
|
|
||
| use function strtolower; | ||
|
|
||
| /** | ||
| * Represents a UUID value that should be stored in a DBMS-independent way. | ||
| * | ||
| * Different DBMS expect different representations of the same UUID: MySQL, MariaDB, SQLite and Oracle store it as 16 | ||
| * raw bytes, while PostgreSQL and MSSQL expect the canonical string form. Wrapping the value removes the need to know | ||
| * which one the current connection requires: | ||
| * | ||
| * ```php | ||
| * $db->createCommand()->insert('{{%page}}', [ | ||
| * 'id' => new UuidValue(Uuid::uuid7()), | ||
| * ])->execute(); | ||
| * ``` | ||
| * | ||
| * The value is normalized to the canonical lowercase form on construction, so all of the following are equivalent: | ||
| * | ||
| * ```php | ||
| * new UuidValue('738146be-87b1-49f2-9913-36142fb6fcbe'); | ||
| * new UuidValue('738146be87b149f2991336142fb6fcbe'); | ||
| * new UuidValue(hex2bin('738146be87b149f2991336142fb6fcbe')); | ||
| * ``` | ||
| */ | ||
| final class UuidValue implements ExpressionInterface | ||
| { | ||
| /** | ||
| * The UUID in the canonical lowercase form, for example `738146be-87b1-49f2-9913-36142fb6fcbe`. | ||
| */ | ||
| public readonly string $value; | ||
|
|
||
| /** | ||
| * @param string|Stringable $value The UUID to represent. It can be: | ||
| * - a UUID in the canonical form, for example `738146be-87b1-49f2-9913-36142fb6fcbe`; | ||
| * - a UUID as 32 hexadecimal characters without dashes, for example `738146be87b149f2991336142fb6fcbe`; | ||
| * - a UUID as 16 raw bytes, for example the result of `Ramsey\Uuid\UuidInterface::getBytes()`; | ||
| * - any {@see Stringable} instance that returns one of the above, for example `Ramsey\Uuid\UuidInterface` itself. | ||
| * | ||
| * @throws InvalidArgumentException If the value isn't a valid UUID. | ||
| */ | ||
| public function __construct(string|Stringable $value) | ||
| { | ||
| $this->value = strtolower(DbUuidHelper::toUuid((string) $value)); | ||
| } | ||
| } |
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
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
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
71 changes: 71 additions & 0 deletions
71
tests/Db/Expression/Value/Builder/UuidValueBuilderTest.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Tests\Db\Expression\Value\Builder; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Yiisoft\Db\Constant\DataType; | ||
| use Yiisoft\Db\Expression\Value\Builder\UuidValueBuilder; | ||
| use Yiisoft\Db\Expression\Value\Param; | ||
| use Yiisoft\Db\Expression\Value\UuidValue; | ||
| use Yiisoft\Db\Helper\DbUuidHelper; | ||
| use Yiisoft\Db\Tests\Support\TestHelper; | ||
|
|
||
| /** | ||
| * @group db | ||
| */ | ||
| final class UuidValueBuilderTest extends TestCase | ||
| { | ||
| private const UUID = '738146be-87b1-49f2-9913-36142fb6fcbe'; | ||
|
|
||
| public function testBuild(): void | ||
| { | ||
| $db = TestHelper::createSqliteMemoryConnection(); | ||
| $builder = new UuidValueBuilder($db->getQueryBuilder()); | ||
|
|
||
| $params = []; | ||
| $result = $builder->build(new UuidValue(self::UUID), $params); | ||
|
|
||
| $this->assertSame(':qp0', $result); | ||
| $this->assertEquals([':qp0' => new Param(self::UUID, DataType::STRING)], $params); | ||
| } | ||
|
|
||
| public function testBuildAppendsToExistingParams(): void | ||
| { | ||
| $db = TestHelper::createSqliteMemoryConnection(); | ||
| $builder = new UuidValueBuilder($db->getQueryBuilder()); | ||
|
|
||
| $params = [':qp0' => new Param('existing', DataType::STRING)]; | ||
| $result = $builder->build(new UuidValue(self::UUID), $params); | ||
|
|
||
| $this->assertSame(':qp1', $result); | ||
| $this->assertEquals( | ||
| [':qp0' => new Param('existing', DataType::STRING), ':qp1' => new Param(self::UUID, DataType::STRING)], | ||
| $params, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * DBMS that store a UUID as raw bytes override {@see UuidValueBuilder::prepareValue()}. | ||
| */ | ||
| public function testPrepareValueIsOverridable(): void | ||
| { | ||
| $db = TestHelper::createSqliteMemoryConnection(); | ||
| $builder = new class ($db->getQueryBuilder()) extends UuidValueBuilder { | ||
| protected function prepareValue(UuidValue $expression): Param | ||
| { | ||
| return new Param(DbUuidHelper::uuidToBlob($expression->value), DataType::LOB); | ||
| } | ||
| }; | ||
|
|
||
| $params = []; | ||
| $result = $builder->build(new UuidValue(self::UUID), $params); | ||
|
|
||
| $this->assertSame(':qp0', $result); | ||
| $this->assertEquals( | ||
| [':qp0' => new Param(DbUuidHelper::uuidToBlob(self::UUID), DataType::LOB)], | ||
| $params, | ||
| ); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Tests\Db\Expression\Value; | ||
|
|
||
| use InvalidArgumentException; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Stringable; | ||
| use Yiisoft\Db\Expression\Value\UuidValue; | ||
| use Yiisoft\Db\Tests\Support\Stringable as StringableObject; | ||
|
|
||
| use function hex2bin; | ||
|
|
||
| final class UuidValueTest extends TestCase | ||
| { | ||
| private const UUID = '738146be-87b1-49f2-9913-36142fb6fcbe'; | ||
|
|
||
| public static function values(): iterable | ||
| { | ||
| yield 'canonical' => [self::UUID]; | ||
| yield 'canonical in upper case' => ['738146BE-87B1-49F2-9913-36142FB6FCBE']; | ||
| yield 'hexadecimal' => ['738146be87b149f2991336142fb6fcbe']; | ||
| yield 'hexadecimal in upper case' => ['738146BE87B149F2991336142FB6FCBE']; | ||
| yield 'bytes' => [hex2bin('738146be87b149f2991336142fb6fcbe')]; | ||
| yield 'stringable' => [new StringableObject(self::UUID)]; | ||
| } | ||
|
|
||
| #[DataProvider('values')] | ||
| public function testValueIsNormalized(string|Stringable $value): void | ||
| { | ||
| $expression = new UuidValue($value); | ||
|
|
||
| $this->assertSame(self::UUID, $expression->value); | ||
| } | ||
|
|
||
| public static function invalidValues(): iterable | ||
| { | ||
| yield 'empty' => ['']; | ||
| yield 'misplaced dash' => ['738146be-87b149f2-9913-36142fb6fcbe']; | ||
| yield 'not hexadecimal' => ['738146be-87b1-K9f2-9913-36142fb6fcbe']; | ||
| yield 'wrong separator' => ['738146be+87b1-49f2-9913-36142fb6fcbe']; | ||
| yield 'too short' => ['738146be87b149f2991336142fb6fcb']; | ||
| yield '32 characters but not hexadecimal' => ['zzz146be87b149f2991336142fb6fcbe']; | ||
| } | ||
|
|
||
| #[DataProvider('invalidValues')] | ||
| public function testConstructWithInvalidValue(string $value): void | ||
| { | ||
| $this->expectException(InvalidArgumentException::class); | ||
| $this->expectExceptionMessage( | ||
| 'Value is not a valid UUID. Expected the canonical form, 32 hexadecimal characters or 16 raw bytes.', | ||
| ); | ||
|
|
||
| new UuidValue($value); | ||
| } | ||
| } |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If UUID is binary value, should it be built as binary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 6795ad2.
prepareValue()now returns aParam, so DBMS that store a UUID as raw bytes can bind it asDataType::LOBinstead of lettingbuildValue()inferDataType::STRING.