-
-
Notifications
You must be signed in to change notification settings - Fork 13
New: Add SQLite implementation of UuidValue expression
#432
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
3
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.
+108
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Sqlite\Builder; | ||
|
|
||
| use Yiisoft\Db\Constant\DataType; | ||
| use Yiisoft\Db\Expression\Value\Builder\UuidValueBuilder as BaseUuidValueBuilder; | ||
| use Yiisoft\Db\Expression\Value\Param; | ||
| use Yiisoft\Db\Expression\Value\UuidValue; | ||
| use Yiisoft\Db\Helper\DbUuidHelper; | ||
|
|
||
| /** | ||
| * Builds a {@see UuidValue} expression for SQLite. | ||
| * | ||
| * SQLite stores a UUID as 16 raw bytes in a `blob(16)` column, so the canonical string form is converted to bytes and | ||
| * bound as {@see DataType::LOB}. | ||
| */ | ||
| final class UuidValueBuilder extends BaseUuidValueBuilder | ||
| { | ||
| protected function prepareValue(UuidValue $expression): Param | ||
| { | ||
| return new Param(DbUuidHelper::uuidToBlob($expression->value), DataType::LOB); | ||
| } | ||
| } | ||
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,79 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Sqlite\Tests\Builder; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Yiisoft\Db\Constant\DataType; | ||
| use Yiisoft\Db\Expression\Value\Param; | ||
| use Yiisoft\Db\Expression\Value\UuidValue; | ||
| use Yiisoft\Db\Helper\DbUuidHelper; | ||
| use Yiisoft\Db\Sqlite\Builder\UuidValueBuilder; | ||
| use Yiisoft\Db\Sqlite\Tests\Support\IntegrationTestTrait; | ||
| use Yiisoft\Db\Tests\Support\IntegrationTestCase; | ||
|
|
||
| use function hex2bin; | ||
| use function strlen; | ||
|
|
||
| /** | ||
| * @group sqlite | ||
| */ | ||
| final class UuidValueBuilderTest extends IntegrationTestCase | ||
| { | ||
| use IntegrationTestTrait; | ||
|
|
||
| private const UUID = '738146be-87b1-49f2-9913-36142fb6fcbe'; | ||
| private const HEX = '738146be87b149f2991336142fb6fcbe'; | ||
|
|
||
| /** | ||
| * Every form {@see UuidValue} accepts, all denoting the same UUID. | ||
| */ | ||
| public static function values(): iterable | ||
| { | ||
| yield 'canonical' => [self::UUID]; | ||
| yield 'canonical in upper case' => ['738146BE-87B1-49F2-9913-36142FB6FCBE']; | ||
| yield 'hexadecimal' => [self::HEX]; | ||
| yield 'hexadecimal in upper case' => ['738146BE87B149F2991336142FB6FCBE']; | ||
| yield 'bytes' => [hex2bin(self::HEX)]; | ||
| } | ||
|
|
||
| /** | ||
| * `UuidValue` normalizes the value to the canonical form on construction, so the builder binds the same 16 bytes | ||
| * whichever form it was created from. | ||
| */ | ||
| #[DataProvider('values')] | ||
| public function testBuildBindsRawBytesAsLob(string $value): void | ||
| { | ||
| $db = $this->getSharedConnection(); | ||
| $builder = new UuidValueBuilder($db->getQueryBuilder()); | ||
|
|
||
| $params = []; | ||
| $result = $builder->build(new UuidValue($value), $params); | ||
|
|
||
| $this->assertSame(':qp0', $result); | ||
| $this->assertEquals( | ||
| [':qp0' => new Param(DbUuidHelper::uuidToBlob(self::UUID), DataType::LOB)], | ||
| $params, | ||
| ); | ||
| } | ||
|
|
||
| #[DataProvider('values')] | ||
| public function testInsertAndSelectUuid(string $value): void | ||
| { | ||
| $db = $this->getSharedConnection(); | ||
|
|
||
| $this->dropTable('uuid_value'); | ||
| $this->executeStatements('CREATE TABLE [[uuid_value]] ([[id]] blob(16) NOT NULL)'); | ||
|
|
||
| $db->createCommand()->insert('uuid_value', ['id' => new UuidValue($value)])->execute(); | ||
|
|
||
| $bytes = $db->createCommand('SELECT [[id]] FROM [[uuid_value]]')->queryScalar(); | ||
|
|
||
| $this->assertIsString($bytes); | ||
| $this->assertSame(16, strlen($bytes)); | ||
| $this->assertSame(self::UUID, DbUuidHelper::toUuid($bytes)); | ||
|
|
||
| $this->dropTable('uuid_value'); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.