Skip to content

Commit b8d12ed

Browse files
committed
Driver::getColumns() added 'length' & 'scale' fields, replaces 'size' (BC break)
1 parent 3dc5e58 commit b8d12ed

13 files changed

+70
-48
lines changed

src/Database/Driver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function applyLimit(string &$sql, ?int $limit, ?int $offset): void;
6363
/** @return list<array{name: string, fullName: string, view: bool}> */
6464
function getTables(): array;
6565

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

6969
/** @return list<array{name: string, columns: list<string>, unique: bool, primary: bool}> */

src/Database/Drivers/MsSqlDriver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function getColumns(string $table): array
107107
DATA_TYPE,
108108
CHARACTER_MAXIMUM_LENGTH,
109109
NUMERIC_PRECISION,
110+
NUMERIC_SCALE,
110111
IS_NULLABLE,
111112
COLUMN_DEFAULT,
112113
DOMAIN_NAME
@@ -122,7 +123,8 @@ public function getColumns(string $table): array
122123
'name' => $row['COLUMN_NAME'],
123124
'table' => $table,
124125
'nativeType' => strtoupper($row['DATA_TYPE']),
125-
'size' => $row['CHARACTER_MAXIMUM_LENGTH'] ?? $row['NUMERIC_PRECISION'] ?? null,
126+
'length' => $row['CHARACTER_MAXIMUM_LENGTH'] ?? $row['NUMERIC_PRECISION'],
127+
'scale' => $row['NUMERIC_SCALE'],
126128
'unsigned' => false,
127129
'nullable' => $row['IS_NULLABLE'] === 'YES',
128130
'default' => $row['COLUMN_DEFAULT'],

src/Database/Drivers/MySqlDriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ public function getColumns(string $table): array
137137
'name' => $row['field'],
138138
'table' => $table,
139139
'nativeType' => strtoupper($typeInfo['type']),
140-
'size' => $typeInfo['length'],
140+
'length' => $typeInfo['length'],
141+
'scale' => $typeInfo['scale'],
141142
'nullable' => $row['null'] === 'YES',
142143
'default' => $row['default'],
143144
'autoIncrement' => $row['extra'] === 'auto_increment',

src/Database/Drivers/PgSqlDriver.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,16 @@ public function getColumns(string $table): array
134134
a.attname::varchar AS name,
135135
c.relname::varchar AS table,
136136
upper(t.typname) AS "nativeType",
137-
CASE WHEN a.atttypmod = -1 THEN NULL ELSE a.atttypmod -4 END AS size,
137+
CASE
138+
WHEN a.atttypid IN (1700, 1231) THEN ((a.atttypmod - 4) >> 16) & 65535 -- precision for numeric/decimal
139+
WHEN a.atttypmod > 0 THEN a.atttypmod - 4 -- length for varchar etc.
140+
WHEN t.typlen > 0 THEN t.typlen -- length for fixed-length types
141+
ELSE NULL
142+
END AS length,
143+
CASE
144+
WHEN a.atttypid IN (1700, 1231) THEN (a.atttypmod - 4) & 65535
145+
ELSE null
146+
END AS scale,
138147
NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS nullable,
139148
pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass)::varchar AS default,
140149
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",

src/Database/Drivers/SqliteDriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ public function getColumns(string $table): array
150150
'name' => $column,
151151
'table' => $table,
152152
'nativeType' => strtoupper($typeInfo['type']),
153-
'size' => $typeInfo['length'],
153+
'length' => $typeInfo['length'],
154+
'scale' => $typeInfo['scale'],
154155
'nullable' => $row['notnull'] == 0,
155156
'default' => $row['dflt_value'],
156157
'autoIncrement' => $createSql && preg_match($pattern, $createSql['sql']),

src/Database/Drivers/SqlsrvDriver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public function getColumns(string $table): array
118118
WHEN c.precision <> 0 THEN c.precision
119119
WHEN c.max_length <> -1 THEN c.max_length
120120
ELSE NULL
121-
END AS size,
121+
END AS length,
122+
c.scale AS scale,
122123
c.is_nullable AS nullable,
123124
OBJECT_DEFINITION(c.default_object_id) AS [default],
124125
c.is_identity AS autoIncrement,
@@ -140,6 +141,8 @@ public function getColumns(string $table): array
140141
while ($row = $rows->fetch()) {
141142
$row = (array) $row;
142143
$row['vendor'] = $row;
144+
$row['length'] = $row['length'] ? (int) $row['length'] : null;
145+
$row['scale'] = $row['scale'] ? (int) $row['scale'] : null;
143146
$row['nullable'] = (bool) $row['nullable'];
144147
$row['autoIncrement'] = (bool) $row['autoIncrement'];
145148
$row['primary'] = (bool) $row['primary'];

tests/Database/Reflection.columns.mysql.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ $expectedColumns = [
113113
'table' => 'types',
114114
'nativeType' => 'DECIMAL',
115115
'length' => 10,
116-
'scale' => null,
116+
'scale' => 0,
117117
'nullable' => true,
118118
'default' => null,
119119
'autoIncrement' => false,
@@ -124,7 +124,7 @@ $expectedColumns = [
124124
'table' => 'types',
125125
'nativeType' => 'DECIMAL',
126126
'length' => 10,
127-
'scale' => null,
127+
'scale' => 2,
128128
'nullable' => true,
129129
'default' => null,
130130
'autoIncrement' => false,

tests/Database/Reflection.columns.postgre.phpt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $expectedColumns = [
2323
'name' => 'smallint',
2424
'table' => 'types',
2525
'nativeType' => 'INT2',
26-
'length' => null,
26+
'length' => 2,
2727
'scale' => null,
2828
'nullable' => true,
2929
'default' => null,
@@ -34,7 +34,7 @@ $expectedColumns = [
3434
'name' => 'integer',
3535
'table' => 'types',
3636
'nativeType' => 'INT4',
37-
'length' => null,
37+
'length' => 4,
3838
'scale' => null,
3939
'nullable' => true,
4040
'default' => null,
@@ -45,7 +45,7 @@ $expectedColumns = [
4545
'name' => 'bigint',
4646
'table' => 'types',
4747
'nativeType' => 'INT8',
48-
'length' => null,
48+
'length' => 8,
4949
'scale' => null,
5050
'nullable' => true,
5151
'default' => null,
@@ -56,8 +56,8 @@ $expectedColumns = [
5656
'name' => 'numeric',
5757
'table' => 'types',
5858
'nativeType' => 'NUMERIC',
59-
'length' => null,
60-
'scale' => null,
59+
'length' => 3,
60+
'scale' => 2,
6161
'nullable' => true,
6262
'default' => null,
6363
'autoIncrement' => false,
@@ -67,7 +67,7 @@ $expectedColumns = [
6767
'name' => 'real',
6868
'table' => 'types',
6969
'nativeType' => 'FLOAT4',
70-
'length' => null,
70+
'length' => 4,
7171
'scale' => null,
7272
'nullable' => true,
7373
'default' => null,
@@ -78,7 +78,7 @@ $expectedColumns = [
7878
'name' => 'double',
7979
'table' => 'types',
8080
'nativeType' => 'FLOAT8',
81-
'length' => null,
81+
'length' => 8,
8282
'scale' => null,
8383
'nullable' => true,
8484
'default' => null,
@@ -89,7 +89,7 @@ $expectedColumns = [
8989
'name' => 'money',
9090
'table' => 'types',
9191
'nativeType' => 'MONEY',
92-
'length' => null,
92+
'length' => 8,
9393
'scale' => null,
9494
'nullable' => true,
9595
'default' => null,
@@ -100,7 +100,7 @@ $expectedColumns = [
100100
'name' => 'bool',
101101
'table' => 'types',
102102
'nativeType' => 'BOOL',
103-
'length' => null,
103+
'length' => 1,
104104
'scale' => null,
105105
'nullable' => true,
106106
'default' => null,
@@ -111,7 +111,7 @@ $expectedColumns = [
111111
'name' => 'date',
112112
'table' => 'types',
113113
'nativeType' => 'DATE',
114-
'length' => null,
114+
'length' => 4,
115115
'scale' => null,
116116
'nullable' => true,
117117
'default' => null,
@@ -122,7 +122,7 @@ $expectedColumns = [
122122
'name' => 'time',
123123
'table' => 'types',
124124
'nativeType' => 'TIME',
125-
'length' => null,
125+
'length' => 8,
126126
'scale' => null,
127127
'nullable' => true,
128128
'default' => null,
@@ -133,7 +133,7 @@ $expectedColumns = [
133133
'name' => 'timestamp',
134134
'table' => 'types',
135135
'nativeType' => 'TIMESTAMP',
136-
'length' => null,
136+
'length' => 8,
137137
'scale' => null,
138138
'nullable' => true,
139139
'default' => null,
@@ -144,7 +144,7 @@ $expectedColumns = [
144144
'name' => 'timestampZone',
145145
'table' => 'types',
146146
'nativeType' => 'TIMESTAMPTZ',
147-
'length' => null,
147+
'length' => 8,
148148
'scale' => null,
149149
'nullable' => true,
150150
'default' => null,
@@ -155,7 +155,7 @@ $expectedColumns = [
155155
'name' => 'interval',
156156
'table' => 'types',
157157
'nativeType' => 'INTERVAL',
158-
'length' => null,
158+
'length' => 16,
159159
'scale' => null,
160160
'nullable' => true,
161161
'default' => null,
@@ -221,7 +221,7 @@ $expectedColumns = [
221221
'name' => 'uuid',
222222
'table' => 'types',
223223
'nativeType' => 'UUID',
224-
'length' => null,
224+
'length' => 16,
225225
'scale' => null,
226226
'nullable' => true,
227227
'default' => null,
@@ -265,7 +265,7 @@ $expectedColumns = [
265265
'name' => 'macaddr',
266266
'table' => 'types',
267267
'nativeType' => 'MACADDR',
268-
'length' => null,
268+
'length' => 6,
269269
'scale' => null,
270270
'nullable' => true,
271271
'default' => null,
@@ -309,7 +309,7 @@ $expectedColumns = [
309309
'name' => 'box',
310310
'table' => 'types',
311311
'nativeType' => 'BOX',
312-
'length' => null,
312+
'length' => 32,
313313
'scale' => null,
314314
'nullable' => true,
315315
'default' => null,
@@ -320,7 +320,7 @@ $expectedColumns = [
320320
'name' => 'circle',
321321
'table' => 'types',
322322
'nativeType' => 'CIRCLE',
323-
'length' => null,
323+
'length' => 24,
324324
'scale' => null,
325325
'nullable' => true,
326326
'default' => null,
@@ -331,7 +331,7 @@ $expectedColumns = [
331331
'name' => 'lseg',
332332
'table' => 'types',
333333
'nativeType' => 'LSEG',
334-
'length' => null,
334+
'length' => 32,
335335
'scale' => null,
336336
'nullable' => true,
337337
'default' => null,
@@ -353,7 +353,7 @@ $expectedColumns = [
353353
'name' => 'point',
354354
'table' => 'types',
355355
'nativeType' => 'POINT',
356-
'length' => null,
356+
'length' => 16,
357357
'scale' => null,
358358
'nullable' => true,
359359
'default' => null,

tests/Database/Reflection.columns.sqlite.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ $expectedColumns = [
277277
'table' => 'types',
278278
'nativeType' => 'DECIMAL',
279279
'length' => 10,
280-
'scale' => null,
280+
'scale' => 5,
281281
'nullable' => true,
282282
'default' => null,
283283
'autoIncrement' => false,

tests/Database/Reflection.columns.sqlsrv.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ $expectedColumns = [
7979
'table' => 'types',
8080
'nativeType' => 'DATETIME',
8181
'length' => 23,
82-
'scale' => null,
82+
'scale' => 3,
8383
'nullable' => true,
8484
'default' => null,
8585
'autoIncrement' => false,
@@ -90,7 +90,7 @@ $expectedColumns = [
9090
'table' => 'types',
9191
'nativeType' => 'DATETIME2',
9292
'length' => 27,
93-
'scale' => null,
93+
'scale' => 7,
9494
'nullable' => true,
9595
'default' => null,
9696
'autoIncrement' => false,
@@ -167,7 +167,7 @@ $expectedColumns = [
167167
'table' => 'types',
168168
'nativeType' => 'MONEY',
169169
'length' => 19,
170-
'scale' => null,
170+
'scale' => 4,
171171
'nullable' => true,
172172
'default' => null,
173173
'autoIncrement' => false,
@@ -211,7 +211,7 @@ $expectedColumns = [
211211
'table' => 'types',
212212
'nativeType' => 'NUMERIC',
213213
'length' => 10,
214-
'scale' => null,
214+
'scale' => 2,
215215
'nullable' => true,
216216
'default' => null,
217217
'autoIncrement' => false,
@@ -266,7 +266,7 @@ $expectedColumns = [
266266
'table' => 'types',
267267
'nativeType' => 'SMALLMONEY',
268268
'length' => 10,
269-
'scale' => null,
269+
'scale' => 4,
270270
'nullable' => true,
271271
'default' => null,
272272
'autoIncrement' => false,
@@ -288,7 +288,7 @@ $expectedColumns = [
288288
'table' => 'types',
289289
'nativeType' => 'TIME',
290290
'length' => 16,
291-
'scale' => null,
291+
'scale' => 7,
292292
'nullable' => true,
293293
'default' => null,
294294
'autoIncrement' => false,

0 commit comments

Comments
 (0)