Skip to content

Commit

Permalink
Reflection: added 'scale' field
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 18, 2024
1 parent e5f9c9f commit ea66c5c
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Database/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function applyLimit(string &$sql, ?int $limit, ?int $offset): void;
/** @return list<array{name: string, fullName: string, view: bool}> */
function getTables(): array;

/** @return list<array{name: string, table: string, nativeType: string, size: int|null, nullable: bool, default: mixed, autoIncrement: bool, primary: bool, vendor: array}> */
/** @return list<array{name: string, table: string, nativeType: string, size: ?int, scale: ?int, nullable: bool, default: mixed, autoIncrement: bool, primary: bool, vendor: array}> */
function getColumns(string $table): array;

/** @return list<array{name: string, columns: list<string>, unique: bool, primary: bool}> */
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Drivers/MsSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function getColumns(string $table): array
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION,
NUMERIC_SCALE,
IS_NULLABLE,
COLUMN_DEFAULT,
DOMAIN_NAME
Expand All @@ -123,6 +124,7 @@ public function getColumns(string $table): array
'table' => $table,
'nativeType' => strtoupper($row['DATA_TYPE']),

Check failure on line 125 in src/Database/Drivers/MsSqlDriver.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $string of function strtoupper expects string, Nette\Utils\TValue|null given.
'size' => $row['CHARACTER_MAXIMUM_LENGTH'] ?? $row['NUMERIC_PRECISION'],
'scale' => $row['NUMERIC_SCALE'],
'unsigned' => false,
'nullable' => $row['IS_NULLABLE'] === 'YES',

Check failure on line 129 in src/Database/Drivers/MsSqlDriver.php

View workflow job for this annotation

GitHub Actions / PHPStan

Strict comparison using === between Nette\Utils\TValue|null and 'YES' will always evaluate to false.
'default' => $row['COLUMN_DEFAULT'],
Expand Down
1 change: 1 addition & 0 deletions src/Database/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function getColumns(string $table): array
'table' => $table,
'nativeType' => strtoupper($typeInfo['type']),
'size' => $typeInfo['length'],
'scale' => $typeInfo['scale'],
'nullable' => $row['null'] === 'YES',
'default' => $row['default'],
'autoIncrement' => $row['extra'] === 'auto_increment',
Expand Down
4 changes: 4 additions & 0 deletions src/Database/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public function getColumns(string $table): array
WHEN t.typlen > 0 THEN t.typlen -- length for fixed-length types
ELSE NULL
END AS size,
CASE
WHEN a.atttypid IN (1700, 1231) THEN (a.atttypmod - 4) & 65535
ELSE null
END AS scale,
NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS nullable,
pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass)::varchar AS default,
coalesce(co.contype = 'p' AND (seq.relname IS NOT NULL OR strpos(pg_catalog.pg_get_expr(ad.adbin, ad.adrelid), 'nextval') = 1), FALSE) AS "autoIncrement",
Expand Down
1 change: 1 addition & 0 deletions src/Database/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public function getColumns(string $table): array
'table' => $table,
'nativeType' => strtoupper($typeInfo['type']),
'size' => $typeInfo['length'],
'scale' => $typeInfo['scale'],
'nullable' => $row['notnull'] == 0,
'default' => $row['dflt_value'],
'autoIncrement' => $createSql && preg_match($pattern, $createSql['sql']),
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Drivers/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public function getColumns(string $table): array
WHEN c.max_length <> -1 THEN c.max_length
ELSE NULL
END AS size,
c.scale AS scale,
c.is_nullable AS nullable,
OBJECT_DEFINITION(c.default_object_id) AS [default],
c.is_identity AS autoIncrement,
Expand All @@ -140,6 +141,7 @@ public function getColumns(string $table): array
while ($row = $rows->fetch()) {
$row = (array) $row;
$row['vendor'] = $row;
$row['scale'] = $row['scale'] ?: null;
$row['nullable'] = (bool) $row['nullable'];
$row['autoIncrement'] = (bool) $row['autoIncrement'];
$row['primary'] = (bool) $row['primary'];
Expand Down
1 change: 1 addition & 0 deletions src/Database/Reflection/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
public readonly ?Table $table = null,
public readonly string $nativeType = '',
public readonly ?int $size = null,
public readonly ?int $scale = null,
public readonly bool $nullable = false,
public readonly mixed $default = null,
public readonly bool $autoIncrement = false,
Expand Down
32 changes: 32 additions & 0 deletions tests/Database/Reflection.columns.mysql.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => $version80 ? 'INT UNSIGNED' : 'INT',
'size' => $version80 ? null : 11,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -35,6 +36,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'INT',
'size' => $version80 ? null : 11,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -45,6 +47,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'SMALLINT',
'size' => $version80 ? null : 6,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -55,6 +58,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'TINYINT',
'size' => $version80 ? null : 4,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -65,6 +69,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'MEDIUMINT',
'size' => $version80 ? null : 9,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -75,6 +80,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'BIGINT',
'size' => $version80 ? null : 20,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -85,6 +91,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'TINYINT',
'size' => 1,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -95,6 +102,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'BIT',
'size' => 1,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -105,6 +113,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'DECIMAL',
'size' => 10,
'scale' => 0,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -115,6 +124,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'DECIMAL',
'size' => 10,
'scale' => 2,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -125,6 +135,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'FLOAT',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -135,6 +146,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'DOUBLE',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -145,6 +157,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'DATE',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -155,6 +168,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'TIME',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -165,6 +179,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'DATETIME',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -175,6 +190,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'TIMESTAMP',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -185,6 +201,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'YEAR',
'size' => $version80 ? null : 4,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -195,6 +212,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'CHAR',
'size' => 1,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -205,6 +223,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'VARCHAR',
'size' => 30,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -215,6 +234,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'BINARY',
'size' => 1,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -225,6 +245,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'VARBINARY',
'size' => 30,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -235,6 +256,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'BLOB',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -245,6 +267,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'TINYBLOB',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -255,6 +278,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'MEDIUMBLOB',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -265,6 +289,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'LONGBLOB',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -275,6 +300,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'TEXT',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -285,6 +311,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'TINYTEXT',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -295,6 +322,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'MEDIUMTEXT',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -305,6 +333,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'LONGTEXT',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -315,6 +344,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'ENUM',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -325,6 +355,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'SET',
'size' => null,
'scale' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -339,6 +370,7 @@ Assert::same(
'table' => $c->table->name,
'nativeType' => $c->nativeType,
'size' => $c->size,
'scale' => $c->scale,
'nullable' => $c->nullable,
'default' => $c->default,
'autoIncrement' => $c->autoIncrement,
Expand Down
Loading

0 comments on commit ea66c5c

Please sign in to comment.