Skip to content

Commit 4ed75b4

Browse files
authored
Merge pull request #3511 from morozov/column-type-hints
Added type hints to the Column class
2 parents 90495ab + 57fe3c6 commit 4ed75b4

File tree

10 files changed

+127
-326
lines changed

10 files changed

+127
-326
lines changed

lib/Doctrine/DBAL/Schema/Column.php

Lines changed: 52 additions & 168 deletions
Large diffs are not rendered by default.

lib/Doctrine/DBAL/Schema/DB2SchemaManager.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ protected function _getPortableTableColumnDefinition($tableColumn)
4545
{
4646
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
4747

48-
$length = null;
49-
$fixed = null;
50-
$scale = false;
51-
$precision = false;
52-
53-
$default = null;
48+
$length = $precision = $default = null;
49+
$scale = 0;
50+
$fixed = false;
5451

5552
if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
5653
$default = trim($tableColumn['default'], "'");
@@ -62,7 +59,6 @@ protected function _getPortableTableColumnDefinition($tableColumn)
6259
switch (strtolower($tableColumn['typename'])) {
6360
case 'varchar':
6461
$length = $tableColumn['length'];
65-
$fixed = false;
6662
break;
6763
case 'character':
6864
$length = $tableColumn['length'];
@@ -82,12 +78,10 @@ protected function _getPortableTableColumnDefinition($tableColumn)
8278
$options = [
8379
'length' => $length,
8480
'unsigned' => false,
85-
'fixed' => (bool) $fixed,
81+
'fixed' => $fixed,
8682
'default' => $default,
8783
'autoincrement' => (bool) $tableColumn['autoincrement'],
88-
'notnull' => (bool) ($tableColumn['nulls'] === 'N'),
89-
'scale' => null,
90-
'precision' => null,
84+
'notnull' => $tableColumn['nulls'] === 'N',
9185
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
9286
? $tableColumn['comment']
9387
: null,

lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ protected function _getPortableTableColumnDefinition($tableColumn)
9999

100100
$length = $tableColumn['length'] ?? strtok('(), ');
101101

102-
$fixed = null;
102+
$fixed = false;
103103

104104
if (! isset($tableColumn['name'])) {
105105
$tableColumn['name'] = '';
106106
}
107107

108-
$scale = null;
108+
$scale = 0;
109109
$precision = null;
110110

111111
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'])
@@ -122,8 +122,8 @@ protected function _getPortableTableColumnDefinition($tableColumn)
122122
case 'numeric':
123123
case 'decimal':
124124
if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
125-
$precision = $match[1];
126-
$scale = $match[2];
125+
$precision = (int) $match[1];
126+
$scale = (int) $match[2];
127127
$length = null;
128128
}
129129
break;
@@ -165,22 +165,17 @@ protected function _getPortableTableColumnDefinition($tableColumn)
165165
$options = [
166166
'length' => $length !== null ? (int) $length : null,
167167
'unsigned' => strpos($tableColumn['type'], 'unsigned') !== false,
168-
'fixed' => (bool) $fixed,
168+
'fixed' => $fixed,
169169
'default' => $columnDefault,
170170
'notnull' => $tableColumn['null'] !== 'YES',
171-
'scale' => null,
172-
'precision' => null,
171+
'scale' => $scale,
172+
'precision' => $precision,
173173
'autoincrement' => strpos($tableColumn['extra'], 'auto_increment') !== false,
174174
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
175175
? $tableColumn['comment']
176176
: null,
177177
];
178178

179-
if ($scale !== null && $precision !== null) {
180-
$options['scale'] = (int) $scale;
181-
$options['precision'] = (int) $precision;
182-
}
183-
184179
$column = new Column($tableColumn['field'], Type::getType($type), $options);
185180

186181
if (isset($tableColumn['characterset'])) {

lib/Doctrine/DBAL/Schema/OracleSchemaManager.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ protected function _getPortableTableColumnDefinition($tableColumn)
132132
}
133133
}
134134

135-
$unsigned = $fixed = $precision = $scale = $length = null;
135+
$length = $precision = null;
136+
$scale = 0;
137+
$fixed = false;
136138

137139
if (! isset($tableColumn['column_name'])) {
138140
$tableColumn['column_name'] = '';
@@ -179,20 +181,18 @@ protected function _getPortableTableColumnDefinition($tableColumn)
179181
case 'varchar':
180182
case 'varchar2':
181183
case 'nvarchar2':
182-
$length = $tableColumn['char_length'];
183-
$fixed = false;
184+
$length = (int) $tableColumn['char_length'];
184185
break;
185186
case 'char':
186187
case 'nchar':
187-
$length = $tableColumn['char_length'];
188+
$length = (int) $tableColumn['char_length'];
188189
$fixed = true;
189190
break;
190191
}
191192

192193
$options = [
193-
'notnull' => (bool) ($tableColumn['nullable'] === 'N'),
194-
'fixed' => (bool) $fixed,
195-
'unsigned' => (bool) $unsigned,
194+
'notnull' => $tableColumn['nullable'] === 'N',
195+
'fixed' => $fixed,
196196
'default' => $tableColumn['data_default'],
197197
'length' => $length,
198198
'precision' => $precision,

lib/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use function implode;
2121
use function in_array;
2222
use function preg_match;
23-
use function preg_replace;
2423
use function sprintf;
2524
use function str_replace;
2625
use function stripos;
@@ -317,10 +316,11 @@ protected function _getPortableTableColumnDefinition($tableColumn)
317316
{
318317
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
319318

320-
if (strtolower($tableColumn['type']) === 'varchar' || strtolower($tableColumn['type']) === 'bpchar') {
321-
// get length from varchar definition
322-
$length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $tableColumn['complete_type']);
323-
$tableColumn['length'] = $length;
319+
$length = null;
320+
321+
if (in_array(strtolower($tableColumn['type']), ['varchar', 'bpchar'], true)
322+
&& preg_match('/\((\d*)\)/', $tableColumn['complete_type'], $matches)) {
323+
$length = (int) $matches[1];
324324
}
325325

326326
$matches = [];
@@ -340,21 +340,22 @@ protected function _getPortableTableColumnDefinition($tableColumn)
340340
$tableColumn['default'] = null;
341341
}
342342

343-
$length = $tableColumn['length'] ?? null;
344-
if ($length === '-1' && isset($tableColumn['atttypmod'])) {
343+
if ($length === -1 && isset($tableColumn['atttypmod'])) {
345344
$length = $tableColumn['atttypmod'] - 4;
346345
}
346+
347347
if ((int) $length <= 0) {
348348
$length = null;
349349
}
350-
$fixed = null;
350+
351+
$fixed = false;
351352

352353
if (! isset($tableColumn['name'])) {
353354
$tableColumn['name'] = '';
354355
}
355356

356357
$precision = null;
357-
$scale = null;
358+
$scale = 0;
358359
$jsonb = null;
359360

360361
$dbType = strtolower($tableColumn['type']);
@@ -398,14 +399,6 @@ protected function _getPortableTableColumnDefinition($tableColumn)
398399

399400
$length = null;
400401
break;
401-
case 'text':
402-
$fixed = false;
403-
break;
404-
case 'varchar':
405-
case 'interval':
406-
case '_varchar':
407-
$fixed = false;
408-
break;
409402
case 'char':
410403
case 'bpchar':
411404
$fixed = true;
@@ -422,8 +415,8 @@ protected function _getPortableTableColumnDefinition($tableColumn)
422415
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
423416

424417
if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['complete_type'], $match)) {
425-
$precision = $match[1];
426-
$scale = $match[2];
418+
$precision = (int) $match[1];
419+
$scale = (int) $match[2];
427420
$length = null;
428421
}
429422
break;

lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,27 @@ protected function _getPortableTableColumnDefinition($tableColumn)
7070
$dbType = strtok($tableColumn['type'], '(), ');
7171
assert(is_string($dbType));
7272

73-
$fixed = null;
74-
$length = (int) $tableColumn['length'];
75-
$default = $tableColumn['default'];
73+
$length = (int) $tableColumn['length'];
74+
75+
$precision = $default = null;
76+
77+
$scale = 0;
78+
$fixed = false;
7679

7780
if (! isset($tableColumn['name'])) {
7881
$tableColumn['name'] = '';
7982
}
8083

81-
if ($default !== null) {
82-
$default = $this->parseDefaultExpression($default);
84+
if ($tableColumn['scale'] !== null) {
85+
$scale = (int) $tableColumn['scale'];
86+
}
87+
88+
if ($tableColumn['precision'] !== null) {
89+
$precision = (int) $tableColumn['precision'];
90+
}
91+
92+
if ($tableColumn['default'] !== null) {
93+
$default = $this->parseDefaultExpression($tableColumn['default']);
8394
}
8495

8596
switch ($dbType) {
@@ -106,12 +117,11 @@ protected function _getPortableTableColumnDefinition($tableColumn)
106117

107118
$options = [
108119
'length' => $length === 0 || ! in_array($type, ['text', 'string']) ? null : $length,
109-
'unsigned' => false,
110-
'fixed' => (bool) $fixed,
120+
'fixed' => $fixed,
111121
'default' => $default !== 'NULL' ? $default : null,
112122
'notnull' => (bool) $tableColumn['notnull'],
113-
'scale' => $tableColumn['scale'],
114-
'precision' => $tableColumn['precision'],
123+
'scale' => $scale,
124+
'precision' => $precision,
115125
'autoincrement' => (bool) $tableColumn['autoincrement'],
116126
'comment' => $tableColumn['comment'] !== '' ? $tableColumn['comment'] : null,
117127
];

lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
use Doctrine\DBAL\Types\Type;
1313
use const CASE_LOWER;
1414
use function array_change_key_case;
15-
use function array_map;
1615
use function array_reverse;
1716
use function array_values;
18-
use function explode;
17+
use function count;
1918
use function file_exists;
2019
use function preg_match;
2120
use function preg_match_all;
@@ -298,23 +297,26 @@ protected function _getPortableTableColumnList($table, $database, $tableColumns)
298297
*/
299298
protected function _getPortableTableColumnDefinition($tableColumn)
300299
{
301-
$parts = explode('(', $tableColumn['type']);
302-
$tableColumn['type'] = trim($parts[0]);
303-
if (isset($parts[1])) {
304-
$length = trim($parts[1], ')');
305-
$tableColumn['length'] = $length;
306-
}
300+
preg_match('/^([^()]*)\\s*(\\(((\\d+)(,\\s*(\\d+))?)\\))?/', $tableColumn['type'], $matches);
301+
302+
$dbType = trim(strtolower($matches[1]));
307303

308-
$dbType = strtolower($tableColumn['type']);
309-
$length = $tableColumn['length'] ?? null;
310-
$unsigned = false;
304+
$length = $precision = $unsigned = null;
305+
$fixed = $unsigned = false;
306+
$scale = 0;
307+
308+
if (count($matches) >= 6) {
309+
$precision = (int) $matches[4];
310+
$scale = (int) $matches[6];
311+
} elseif (count($matches) >= 4) {
312+
$length = (int) $matches[4];
313+
}
311314

312315
if (strpos($dbType, ' unsigned') !== false) {
313316
$dbType = str_replace(' unsigned', '', $dbType);
314317
$unsigned = true;
315318
}
316319

317-
$fixed = false;
318320
$type = $this->_platform->getDoctrineTypeMapping($dbType);
319321
$default = $tableColumn['dflt_value'];
320322
if ($default === 'NULL') {
@@ -330,31 +332,13 @@ protected function _getPortableTableColumnDefinition($tableColumn)
330332
$tableColumn['name'] = '';
331333
}
332334

333-
$precision = null;
334-
$scale = null;
335-
336-
switch ($dbType) {
337-
case 'char':
338-
$fixed = true;
339-
break;
340-
case 'float':
341-
case 'double':
342-
case 'real':
343-
case 'decimal':
344-
case 'numeric':
345-
if (isset($tableColumn['length'])) {
346-
if (strpos($tableColumn['length'], ',') === false) {
347-
$tableColumn['length'] .= ',0';
348-
}
349-
[$precision, $scale] = array_map('trim', explode(',', $tableColumn['length']));
350-
}
351-
$length = null;
352-
break;
335+
if ($dbType === 'char') {
336+
$fixed = true;
353337
}
354338

355339
$options = [
356340
'length' => $length,
357-
'unsigned' => (bool) $unsigned,
341+
'unsigned' => $unsigned,
358342
'fixed' => $fixed,
359343
'notnull' => $notnull,
360344
'default' => $default,

0 commit comments

Comments
 (0)