Skip to content

Commit

Permalink
Driver::getColumns() added 'length' & 'scale' fields, replaces 'size'…
Browse files Browse the repository at this point in the history
… (BC break)
  • Loading branch information
dg committed Aug 18, 2024
1 parent 3dc5e58 commit a5e0f99
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 46 deletions.
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, length: ?int, precision: ?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
4 changes: 3 additions & 1 deletion 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 @@ -122,7 +123,8 @@ public function getColumns(string $table): array
'name' => $row['COLUMN_NAME'],
'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'] ?? null,
'length' => $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
3 changes: 2 additions & 1 deletion src/Database/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ public function getColumns(string $table): array
'name' => $row['field'],
'table' => $table,
'nativeType' => strtoupper($typeInfo['type']),
'size' => $typeInfo['length'],
'length' => $typeInfo['length'],
'scale' => $typeInfo['scale'],
'nullable' => $row['null'] === 'YES',
'default' => $row['default'],
'autoIncrement' => $row['extra'] === 'auto_increment',
Expand Down
10 changes: 9 additions & 1 deletion src/Database/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ public function getColumns(string $table): array
a.attname::varchar AS name,
c.relname::varchar AS table,
upper(t.typname) AS "nativeType",
CASE WHEN a.atttypmod = -1 THEN NULL ELSE a.atttypmod -4 END AS size,
CASE
WHEN a.atttypmod > 0 THEN a.atttypmod - 4
WHEN t.typlen > 0 THEN t.typlen
ELSE NULL
END AS length,
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
3 changes: 2 additions & 1 deletion src/Database/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ public function getColumns(string $table): array
'name' => $column,
'table' => $table,
'nativeType' => strtoupper($typeInfo['type']),
'size' => $typeInfo['length'],
'length' => $typeInfo['length'],
'scale' => $typeInfo['scale'],
'nullable' => $row['notnull'] == 0,
'default' => $row['dflt_value'],
'autoIncrement' => $createSql && preg_match($pattern, $createSql['sql']),
Expand Down
5 changes: 4 additions & 1 deletion src/Database/Drivers/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public function getColumns(string $table): array
WHEN c.precision <> 0 THEN c.precision
WHEN c.max_length <> -1 THEN c.max_length
ELSE NULL
END AS size,
END AS length,
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,8 @@ public function getColumns(string $table): array
while ($row = $rows->fetch()) {
$row = (array) $row;
$row['vendor'] = $row;
$row['length'] = $row['length'] ? (int) $row['length'] : null;
$row['scale'] = $row['scale'] ? (int) $row['scale'] : null;
$row['nullable'] = (bool) $row['nullable'];
$row['autoIncrement'] = (bool) $row['autoIncrement'];
$row['primary'] = (bool) $row['primary'];
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/Reflection.columns.mysql.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'DECIMAL',
'length' => 10,
'scale' => null,
'scale' => 0,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -124,7 +124,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'DECIMAL',
'length' => 10,
'scale' => null,
'scale' => 2,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand Down
38 changes: 19 additions & 19 deletions tests/Database/Reflection.columns.postgre.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $expectedColumns = [
'name' => 'smallint',
'table' => 'types',
'nativeType' => 'INT2',
'length' => null,
'length' => 2,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -34,7 +34,7 @@ $expectedColumns = [
'name' => 'integer',
'table' => 'types',
'nativeType' => 'INT4',
'length' => null,
'length' => 4,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -45,7 +45,7 @@ $expectedColumns = [
'name' => 'bigint',
'table' => 'types',
'nativeType' => 'INT8',
'length' => null,
'length' => 8,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -57,7 +57,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'NUMERIC',
'length' => null,
'scale' => null,
'scale' => 65531,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -67,7 +67,7 @@ $expectedColumns = [
'name' => 'real',
'table' => 'types',
'nativeType' => 'FLOAT4',
'length' => null,
'length' => 4,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -78,7 +78,7 @@ $expectedColumns = [
'name' => 'double',
'table' => 'types',
'nativeType' => 'FLOAT8',
'length' => null,
'length' => 8,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -89,7 +89,7 @@ $expectedColumns = [
'name' => 'money',
'table' => 'types',
'nativeType' => 'MONEY',
'length' => null,
'length' => 8,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -100,7 +100,7 @@ $expectedColumns = [
'name' => 'bool',
'table' => 'types',
'nativeType' => 'BOOL',
'length' => null,
'length' => 1,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -111,7 +111,7 @@ $expectedColumns = [
'name' => 'date',
'table' => 'types',
'nativeType' => 'DATE',
'length' => null,
'length' => 4,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -122,7 +122,7 @@ $expectedColumns = [
'name' => 'time',
'table' => 'types',
'nativeType' => 'TIME',
'length' => null,
'length' => 8,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -133,7 +133,7 @@ $expectedColumns = [
'name' => 'timestamp',
'table' => 'types',
'nativeType' => 'TIMESTAMP',
'length' => null,
'length' => 8,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -144,7 +144,7 @@ $expectedColumns = [
'name' => 'timestampZone',
'table' => 'types',
'nativeType' => 'TIMESTAMPTZ',
'length' => null,
'length' => 8,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -155,7 +155,7 @@ $expectedColumns = [
'name' => 'interval',
'table' => 'types',
'nativeType' => 'INTERVAL',
'length' => null,
'length' => 16,
'scale' => null,
'nullable' => true,
'default' => null,
Expand Down Expand Up @@ -221,7 +221,7 @@ $expectedColumns = [
'name' => 'uuid',
'table' => 'types',
'nativeType' => 'UUID',
'length' => null,
'length' => 16,
'scale' => null,
'nullable' => true,
'default' => null,
Expand Down Expand Up @@ -265,7 +265,7 @@ $expectedColumns = [
'name' => 'macaddr',
'table' => 'types',
'nativeType' => 'MACADDR',
'length' => null,
'length' => 6,
'scale' => null,
'nullable' => true,
'default' => null,
Expand Down Expand Up @@ -309,7 +309,7 @@ $expectedColumns = [
'name' => 'box',
'table' => 'types',
'nativeType' => 'BOX',
'length' => null,
'length' => 32,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -320,7 +320,7 @@ $expectedColumns = [
'name' => 'circle',
'table' => 'types',
'nativeType' => 'CIRCLE',
'length' => null,
'length' => 24,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -331,7 +331,7 @@ $expectedColumns = [
'name' => 'lseg',
'table' => 'types',
'nativeType' => 'LSEG',
'length' => null,
'length' => 32,
'scale' => null,
'nullable' => true,
'default' => null,
Expand All @@ -353,7 +353,7 @@ $expectedColumns = [
'name' => 'point',
'table' => 'types',
'nativeType' => 'POINT',
'length' => null,
'length' => 16,
'scale' => null,
'nullable' => true,
'default' => null,
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/Reflection.columns.sqlite.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'DECIMAL',
'length' => 10,
'scale' => null,
'scale' => 5,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand Down
12 changes: 6 additions & 6 deletions tests/Database/Reflection.columns.sqlsrv.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'DATETIME',
'length' => 23,
'scale' => null,
'scale' => 3,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -90,7 +90,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'DATETIME2',
'length' => 27,
'scale' => null,
'scale' => 7,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand Down Expand Up @@ -167,7 +167,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'MONEY',
'length' => 19,
'scale' => null,
'scale' => 4,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand Down Expand Up @@ -211,7 +211,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'NUMERIC',
'length' => 10,
'scale' => null,
'scale' => 2,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand Down Expand Up @@ -266,7 +266,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'SMALLMONEY',
'length' => 10,
'scale' => null,
'scale' => 4,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand All @@ -288,7 +288,7 @@ $expectedColumns = [
'table' => 'types',
'nativeType' => 'TIME',
'length' => 16,
'scale' => null,
'scale' => 7,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
Expand Down
Loading

0 comments on commit a5e0f99

Please sign in to comment.