Skip to content

Commit

Permalink
add basic UUID type - handle it as varchar
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Aug 26, 2024
1 parent 5c2544d commit 2331590
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/DbReflection/InformationSchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ private function parseDbType(string $type): DbType
case 'mediumtext':
case 'longtext':
case 'char':
case 'uuid':
return new VarcharType();
case 'int':
case 'tinyint':
Expand Down
14 changes: 11 additions & 3 deletions tests/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,16 @@ private function provideValidDataTypeTestData(): iterable
col_float FLOAT NOT NULL,
col_double DOUBLE NOT NULL,
col_datetime DATETIME NOT NULL,
col_enum ENUM('a', 'b', 'c') NOT NULL
col_enum ENUM('a', 'b', 'c') NOT NULL,
col_uuid UUID NOT NULL
);
");
$db->query("
INSERT INTO {$dataTypesTable} (col_int, col_varchar_null, col_decimal, col_float, col_double, col_datetime)
VALUES (1, 'aa', 111.11, 11.11, 1.1, NOW()), (2, NULL, 222.22, 22.22, 2.2, NOW())
INSERT INTO {$dataTypesTable}
(col_int, col_varchar_null, col_decimal, col_float, col_double, col_datetime, col_uuid)
VALUES
(1, 'aa', 111.11, 11.11, 1.1, NOW(), UUID()),
(2, NULL, 222.22, 22.22, 2.2, NOW(), UUID())
");

yield 'column - int' => [
Expand Down Expand Up @@ -230,6 +234,10 @@ private function provideValidDataTypeTestData(): iterable
'query' => "SELECT col_enum FROM {$dataTypesTable}",
];

yield 'column - UUID' => [
'query' => "SELECT col_uuid FROM {$dataTypesTable}",
];

// TODO: fix missing types: ~ is unsigned 64b int, so it's too large for PHP.
// TODO: name of 2nd column contains comment: SELECT col_int, /*aaa*/ -col_int FROM mysqli_test_data_types
yield 'unary ops' => [
Expand Down
6 changes: 2 additions & 4 deletions tests/DbReflection/DbReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private static function initDb(): void
val_year YEAR NOT NULL,
val_enum ENUM('a', 'b', 'c') NOT NULL,
val_default INT NOT NULL DEFAULT (ABS(val_mediumint) + 5),
val_uuid UUID NOT NULL,
UNIQUE (id, name)
);
");
Expand Down Expand Up @@ -128,10 +129,6 @@ public static function provideDbReflections(): iterable

yield 'file - current' => [new MariaDbFileDbReflection($filename, $informationSchemaParser)];

yield 'file - v1' => [
new MariaDbFileDbReflection(__DIR__ . '/data/file-reflection.v1.bin', $informationSchemaParser),
];

yield 'file - v2' => [
new MariaDbFileDbReflection(__DIR__ . '/data/file-reflection.v2.bin', $informationSchemaParser),
];
Expand Down Expand Up @@ -169,6 +166,7 @@ public function test(DbReflection $reflection): void
'val_year' => new Column('val_year', new DateTimeType(), false),
'val_enum' => new Column('val_enum', new EnumType(['a', 'b', 'c']), false),
'val_default' => new Column('val_default', new IntType(), false, $valDefaultExpr),
'val_uuid' => new Column('val_uuid', new VarcharType(), false),
], $schema->columns);
}

Expand Down
1 change: 0 additions & 1 deletion tests/DbReflection/data/file-reflection.v1.bin

This file was deleted.

2 changes: 1 addition & 1 deletion tests/DbReflection/data/file-reflection.v2.bin

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions tests/PHPStan/Type/MySQLi/data/MySQLiTypeInferenceDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ public static function initData(mysqli $db): void
col_float FLOAT NOT NULL,
col_double DOUBLE NOT NULL,
col_datetime DATETIME NOT NULL,
col_enum ENUM('a', 'b', 'c') NOT NULL
col_enum ENUM('a', 'b', 'c') NOT NULL,
col_uuid UUID NOT NULL
);
");
$db->query("
INSERT INTO {$dataTypesTable}
(col_int, col_varchar_null, col_decimal, col_float, col_double, col_datetime, col_enum)
VALUES (1, 'aa', 111.11, 11.11, 1.1, NOW(), 'a'), (2, NULL, 222.22, 22.22, 2.2, NOW(), 'b')
(col_int, col_varchar_null, col_decimal, col_float, col_double, col_datetime, col_enum, col_uuid)
VALUES
(1, 'aa', 111.11, 11.11, 1.1, NOW(), 'a', UUID()),
(2, NULL, 222.22, 22.22, 2.2, NOW(), 'b', UUID())
");

$db->query('
Expand Down Expand Up @@ -673,6 +676,17 @@ public function testDataTypes(): void

$this->assertTrue(in_array($value, ['a', 'b', 'c'], true));
}

$rows = $db->query('SELECT col_uuid FROM mysqli_test_data_types')->fetch_all(MYSQLI_NUM);
$col = array_column($rows, 0);

foreach ($col as $value) {
if (function_exists('assertType')) {
assertType('string', $value);
}

$this->assertGettype(['string'], $value);
}
}

public function testFetchRow(): void
Expand Down

0 comments on commit 2331590

Please sign in to comment.