Skip to content

Commit 6b29c6d

Browse files
committed
Convert UUID_BINARY in PK during update
1 parent bf3ebb5 commit 6b29c6d

File tree

5 files changed

+55
-31
lines changed

5 files changed

+55
-31
lines changed

src/Propel/Generator/Builder/Om/ObjectBuilder.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ protected function addLazyLoaderBody(string &$script, Column $column): void
17231723
if (is_resource(\$firstColumn)) {
17241724
\$firstColumn = stream_get_contents(\$firstColumn);
17251725
}
1726-
\$this->$clo = (\$firstColumn) ? UuidConverter::binToUuid(\$firstColumn, $uuidSwapFlag) : null;";
1726+
\$this->$clo = UuidConverter::binToUuid(\$firstColumn, $uuidSwapFlag);";
17271727
} else {
17281728
$script .= "
17291729
\$this->$clo = \$firstColumn;";
@@ -2737,7 +2737,7 @@ protected function addHydrateBody(string &$script): void
27372737
if (is_resource(\$col)) {
27382738
\$col = stream_get_contents(\$col);
27392739
}
2740-
\$this->$clo = (\$col) ? UuidConverter::binToUuid(\$col, $uuidSwapFlag) : null;";
2740+
\$this->$clo = UuidConverter::binToUuid(\$col, $uuidSwapFlag);";
27412741
} elseif ($col->isPhpPrimitiveType()) {
27422742
$script .= "
27432743
\$this->$clo = (null !== \$col) ? (" . $col->getPhpType() . ') $col : null;';
@@ -2882,10 +2882,15 @@ protected function addBuildPkeyCriteriaBody(string &$script): void
28822882

28832883
$script .= "
28842884
\$criteria = " . $this->getQueryClassName() . '::create();';
2885-
foreach ($this->getTable()->getPrimaryKey() as $col) {
2886-
$clo = $col->getLowercasedName();
2885+
foreach ($this->getTable()->getPrimaryKey() as $column) {
2886+
$dataAccessExpression = '$this->' . $column->getLowercasedName();
2887+
if ($column->getType() === PropelTypes::UUID_BINARY) {
2888+
$uuidSwapFlag = $this->getUuidSwapFlagLiteral();
2889+
$dataAccessExpression = "UuidConverter::uuidToBin($dataAccessExpression, $uuidSwapFlag)";
2890+
}
2891+
$columnConstant = $this->getColumnConstant($column);
28872892
$script .= "
2888-
\$criteria->add(" . $this->getColumnConstant($col) . ", \$this->$clo);";
2893+
\$criteria->add($columnConstant, $dataAccessExpression);";
28892894
}
28902895
}
28912896

@@ -6689,7 +6694,7 @@ protected function getAccessValueStatement(Column $column): string
66896694
if ($column->isUuidBinaryType()) {
66906695
$uuidSwapFlag = $this->getUuidSwapFlagLiteral();
66916696

6692-
return "(\$this->$columnName) ? UuidConverter::uuidToBin(\$this->$columnName, $uuidSwapFlag) : null";
6697+
return "UuidConverter::uuidToBin(\$this->$columnName, $uuidSwapFlag)";
66936698
}
66946699

66956700
return "\$this->$columnName";

src/Propel/Runtime/Util/UuidConverter.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ class UuidConverter
1616
/**
1717
* Transforms a UUID string to a binary string.
1818
*
19-
* @param string $uuid
19+
* @param string|null $uuid
2020
* @param bool $swapFlag Swap first four bytes for better indexing of version-1 UUIDs (@link https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_uuid-to-bin)
2121
*
22-
* @return string
22+
* @return string|null
2323
*/
24-
public static function uuidToBin(string $uuid, bool $swapFlag = true): string
24+
public static function uuidToBin(?string $uuid, bool $swapFlag = true): ?string
2525
{
26+
if (!$uuid) {
27+
return null;
28+
}
2629
$rawHex = (!$swapFlag)
2730
? str_replace('-', '', $uuid)
2831
: preg_replace(
@@ -37,13 +40,16 @@ public static function uuidToBin(string $uuid, bool $swapFlag = true): string
3740
/**
3841
* Transforms a binary string to a UUID string.
3942
*
40-
* @param string $bin
43+
* @param string|null $bin
4144
* @param bool $swapFlag Assume bytes were swapped (@link https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_bin-to-uuid)
4245
*
43-
* @return string
46+
* @return string|null
4447
*/
45-
public static function binToUuid(string $bin, bool $swapFlag = true): string
48+
public static function binToUuid(?string $bin, bool $swapFlag = true): ?string
4649
{
50+
if (!$bin) {
51+
return null;
52+
}
4753
$rawHex = bin2hex($bin);
4854
$recombineFormat = $swapFlag ? '$3$4-$2-$1-$5-$6' : '$1$2-$3-$4-$5-$6';
4955

tests/Fixtures/bookstore/schema.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,15 @@
354354
</index>
355355
</table>
356356

357+
358+
<table name="book_uuid_binary" phpName="BookUuidBinary">
359+
<column name="id" phpName="Id" type="UUID_BINARY" primaryKey="true" required="true"/>
360+
<column name="title" type="VARCHAR"/>
361+
<vendor type="mysql">
362+
<parameter name="Engine" value="InnoDB"/>
363+
</vendor>
364+
</table>
365+
357366
<!-- Test single table inheritance with Abstract true -->
358367
<table name="distribution" abstract="true">
359368
<column name="id" type="INTEGER" primaryKey="true" autoIncrement="true"/>

tests/Propel/Tests/Runtime/TypeTests/UuidBinaryTypeTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
namespace Propel\Tests\Runtime\TypeTest;
1010

1111
use Propel\Runtime\Util\UuidConverter;
12-
use Propel\Tests\Bookstore\Base\Book2Query;
1312
use Propel\Tests\Bookstore\Book2;
13+
use Propel\Tests\Bookstore\Book2Query;
14+
use Propel\Tests\Bookstore\BookUuidBinary;
15+
use Propel\Tests\Bookstore\BookUuidBinaryQuery;
1416
use Propel\Tests\Bookstore\Map\Book2TableMap;
1517
use Propel\Tests\Helpers\Bookstore\BookstoreTestBase;
1618

@@ -123,4 +125,21 @@ public function testModelCanUpdateUuid()
123125

124126
$this->assertSame($updateUuid, $book->getUuidBin());
125127
}
128+
129+
/**
130+
* @return void
131+
*/
132+
public function testModelCanUpdateUuidBinaryPk()
133+
{
134+
BookUuidBinaryQuery::create()->deleteAll();
135+
$uuid = 'b41a29db-cf78-4d43-83a9-4cd3e1e1b41a';
136+
$book = new BookUuidBinary();
137+
$book->setId($uuid)->setTitle('First Title')->save();
138+
139+
$updatedTitle = 'Second Title';
140+
$book->setTitle($updatedTitle)->save();
141+
$book->reload();
142+
143+
$this->assertSame($updatedTitle, $book->getTitle());
144+
}
126145
}

tests/Propel/Tests/Runtime/Util/UuidConverterTest.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function uuidDataProvider(): array
1919
// uuid, hex, hexWithSwap
2020
['11112222-3333-4444-5555-666677778888', '11112222333344445555666677778888', '44443333111122225555666677778888'],
2121
['aab5d5fd-70c1-11e5-a4fb-b026b977eb28', 'aab5d5fd70c111e5a4fbb026b977eb28', '11e570c1aab5d5fda4fbb026b977eb28'],
22+
[null, null, null],
2223
];
2324
}
2425

@@ -44,9 +45,9 @@ public function testUuidToBinWithoutSwap($uuid, $hex, $hexWithSwap)
4445

4546
/**
4647
*/
47-
public function assertBinaryEquals(string $expected, $result)
48+
public function assertBinaryEquals(?string $expected, ?string $result)
4849
{
49-
$expected = hex2bin($expected);
50+
$expected = $expected ? hex2bin($expected) : $expected;
5051
$this->assertEquals($expected, $result);
5152
}
5253

@@ -71,20 +72,4 @@ public function testBinToUuidWithoutSwap($uuid, $hex, $hexWithSwap)
7172
$result = UuidConverter::binToUuid($bin, false);
7273
$this->assertEquals($uuid, $result);
7374
}
74-
75-
public function testFasterUuidToBin(){
76-
$this->markTestSkipped();
77-
$uuid = [];
78-
79-
for($i = 0; $i < 100000; $i++){
80-
$uuids[] = $this->guidv4();
81-
}
82-
$swapFlag = true;
83-
$regexDuration = $this->measure([UuidConverter::class, 'uuidToBinRegex'], $uuids, $swapFlag);
84-
$regularDuration = $this->measure([UuidConverter::class, 'uuidToBin'], $uuids, $swapFlag);
85-
86-
echo "regular took $regularDuration, regex took $regexDuration";
87-
$this->assertLessThanOrEqual($regexDuration, $regularDuration, "regular took $regularDuration, regex took $regexDuration");
88-
}
89-
9075
}

0 commit comments

Comments
 (0)