Skip to content

Commit

Permalink
Merge pull request #3511 from morozov/column-type-hints
Browse files Browse the repository at this point in the history
Added type hints to the Column class
  • Loading branch information
morozov committed Jun 27, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 27864a5 + 4008365 commit f737105
Showing 10 changed files with 127 additions and 322 deletions.
220 changes: 52 additions & 168 deletions lib/Doctrine/DBAL/Schema/Column.php

Large diffs are not rendered by default.

16 changes: 5 additions & 11 deletions lib/Doctrine/DBAL/Schema/DB2SchemaManager.php
Original file line number Diff line number Diff line change
@@ -46,12 +46,9 @@ protected function _getPortableTableColumnDefinition($tableColumn)
{
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER);

$length = null;
$fixed = null;
$scale = false;
$precision = false;

$default = null;
$length = $precision = $default = null;
$scale = 0;
$fixed = false;

if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
$default = $tableColumn['default'];
@@ -67,7 +64,6 @@ protected function _getPortableTableColumnDefinition($tableColumn)
switch (strtolower($tableColumn['typename'])) {
case 'varchar':
$length = $tableColumn['length'];
$fixed = false;
break;
case 'character':
$length = $tableColumn['length'];
@@ -87,12 +83,10 @@ protected function _getPortableTableColumnDefinition($tableColumn)
$options = [
'length' => $length,
'unsigned' => false,
'fixed' => (bool) $fixed,
'fixed' => $fixed,
'default' => $default,
'autoincrement' => (bool) $tableColumn['autoincrement'],
'notnull' => (bool) ($tableColumn['nulls'] === 'N'),
'scale' => null,
'precision' => null,
'notnull' => $tableColumn['nulls'] === 'N',
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
? $tableColumn['comment']
: null,
19 changes: 7 additions & 12 deletions lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php
Original file line number Diff line number Diff line change
@@ -118,13 +118,13 @@ protected function _getPortableTableColumnDefinition($tableColumn)

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

$fixed = null;
$fixed = false;

if (! isset($tableColumn['name'])) {
$tableColumn['name'] = '';
}

$scale = null;
$scale = 0;
$precision = null;

$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'])
@@ -141,8 +141,8 @@ protected function _getPortableTableColumnDefinition($tableColumn)
case 'numeric':
case 'decimal':
if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
$precision = $match[1];
$scale = $match[2];
$precision = (int) $match[1];
$scale = (int) $match[2];
$length = null;
}
break;
@@ -184,22 +184,17 @@ protected function _getPortableTableColumnDefinition($tableColumn)
$options = [
'length' => $length !== null ? (int) $length : null,
'unsigned' => strpos($tableColumn['type'], 'unsigned') !== false,
'fixed' => (bool) $fixed,
'fixed' => $fixed,
'default' => $columnDefault,
'notnull' => $tableColumn['null'] !== 'YES',
'scale' => null,
'precision' => null,
'scale' => $scale,
'precision' => $precision,
'autoincrement' => strpos($tableColumn['extra'], 'auto_increment') !== false,
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
? $tableColumn['comment']
: null,
];

if ($scale !== null && $precision !== null) {
$options['scale'] = (int) $scale;
$options['precision'] = (int) $precision;
}

$column = new Column($tableColumn['field'], Type::getType($type), $options);

if (isset($tableColumn['characterset'])) {
14 changes: 7 additions & 7 deletions lib/Doctrine/DBAL/Schema/OracleSchemaManager.php
Original file line number Diff line number Diff line change
@@ -133,7 +133,9 @@ protected function _getPortableTableColumnDefinition($tableColumn)
}
}

$unsigned = $fixed = $precision = $scale = $length = null;
$length = $precision = null;
$scale = 0;
$fixed = false;

if (! isset($tableColumn['column_name'])) {
$tableColumn['column_name'] = '';
@@ -182,20 +184,18 @@ protected function _getPortableTableColumnDefinition($tableColumn)
case 'varchar':
case 'varchar2':
case 'nvarchar2':
$length = $tableColumn['char_length'];
$fixed = false;
$length = (int) $tableColumn['char_length'];
break;
case 'char':
case 'nchar':
$length = $tableColumn['char_length'];
$length = (int) $tableColumn['char_length'];
$fixed = true;
break;
}

$options = [
'notnull' => (bool) ($tableColumn['nullable'] === 'N'),
'fixed' => (bool) $fixed,
'unsigned' => (bool) $unsigned,
'notnull' => $tableColumn['nullable'] === 'N',
'fixed' => $fixed,
'default' => $tableColumn['data_default'],
'length' => $length,
'precision' => $precision,
27 changes: 12 additions & 15 deletions lib/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@
use function implode;
use function in_array;
use function preg_match;
use function preg_replace;
use function sprintf;
use function str_replace;
use function strlen;
@@ -316,10 +315,11 @@ protected function _getPortableTableColumnDefinition($tableColumn)
{
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER);

if (strtolower($tableColumn['type']) === 'varchar' || strtolower($tableColumn['type']) === 'bpchar') {
// get length from varchar definition
$length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $tableColumn['complete_type']);
$tableColumn['length'] = $length;
$length = null;

if (in_array(strtolower($tableColumn['type']), ['varchar', 'bpchar'], true)
&& preg_match('/\((\d*)\)/', $tableColumn['complete_type'], $matches)) {
$length = (int) $matches[1];
}

$matches = [];
@@ -339,21 +339,22 @@ protected function _getPortableTableColumnDefinition($tableColumn)
}
}

$length = $tableColumn['length'] ?? null;
if ($length === '-1' && isset($tableColumn['atttypmod'])) {
if ($length === -1 && isset($tableColumn['atttypmod'])) {
$length = $tableColumn['atttypmod'] - 4;
}

if ((int) $length <= 0) {
$length = null;
}
$fixed = null;

$fixed = false;

if (! isset($tableColumn['name'])) {
$tableColumn['name'] = '';
}

$precision = null;
$scale = null;
$scale = 0;
$jsonb = null;

$dbType = strtolower($tableColumn['type']);
@@ -401,10 +402,6 @@ protected function _getPortableTableColumnDefinition($tableColumn)
case '_varchar':
case 'varchar':
$tableColumn['default'] = $this->parseDefaultExpression($tableColumn['default']);
$fixed = false;
break;
case 'interval':
$fixed = false;
break;
case 'char':
case 'bpchar':
@@ -422,8 +419,8 @@ protected function _getPortableTableColumnDefinition($tableColumn)
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);

if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['complete_type'], $match)) {
$precision = $match[1];
$scale = $match[2];
$precision = (int) $match[1];
$scale = (int) $match[2];
$length = null;
}
break;
28 changes: 19 additions & 9 deletions lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php
Original file line number Diff line number Diff line change
@@ -69,16 +69,27 @@ protected function _getPortableTableColumnDefinition($tableColumn)
$dbType = strtok($tableColumn['type'], '(), ');
assert(is_string($dbType));

$fixed = null;
$length = (int) $tableColumn['length'];
$default = $tableColumn['default'];
$length = (int) $tableColumn['length'];

$precision = $default = null;

$scale = 0;
$fixed = false;

if (! isset($tableColumn['name'])) {
$tableColumn['name'] = '';
}

if ($default !== null) {
$default = $this->parseDefaultExpression($default);
if ($tableColumn['scale'] !== null) {
$scale = (int) $tableColumn['scale'];
}

if ($tableColumn['precision'] !== null) {
$precision = (int) $tableColumn['precision'];
}

if ($tableColumn['default'] !== null) {
$default = $this->parseDefaultExpression($tableColumn['default']);
}

switch ($dbType) {
@@ -105,12 +116,11 @@ protected function _getPortableTableColumnDefinition($tableColumn)

$options = [
'length' => $length === 0 || ! in_array($type, ['text', 'string']) ? null : $length,
'unsigned' => false,
'fixed' => (bool) $fixed,
'fixed' => $fixed,
'default' => $default,
'notnull' => (bool) $tableColumn['notnull'],
'scale' => $tableColumn['scale'],
'precision' => $tableColumn['precision'],
'scale' => $scale,
'precision' => $precision,
'autoincrement' => (bool) $tableColumn['autoincrement'],
'comment' => $tableColumn['comment'] !== '' ? $tableColumn['comment'] : null,
];
50 changes: 17 additions & 33 deletions lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php
Original file line number Diff line number Diff line change
@@ -12,10 +12,9 @@
use Doctrine\DBAL\Types\Type;
use const CASE_LOWER;
use function array_change_key_case;
use function array_map;
use function array_reverse;
use function array_values;
use function explode;
use function count;
use function file_exists;
use function preg_match;
use function preg_match_all;
@@ -298,23 +297,26 @@ protected function _getPortableTableColumnList($table, $database, $tableColumns)
*/
protected function _getPortableTableColumnDefinition($tableColumn)
{
$parts = explode('(', $tableColumn['type']);
$tableColumn['type'] = trim($parts[0]);
if (isset($parts[1])) {
$length = trim($parts[1], ')');
$tableColumn['length'] = $length;
}
preg_match('/^([^()]*)\\s*(\\(((\\d+)(,\\s*(\\d+))?)\\))?/', $tableColumn['type'], $matches);

$dbType = trim(strtolower($matches[1]));

$dbType = strtolower($tableColumn['type']);
$length = $tableColumn['length'] ?? null;
$unsigned = false;
$length = $precision = $unsigned = null;
$fixed = $unsigned = false;
$scale = 0;

if (count($matches) >= 6) {
$precision = (int) $matches[4];
$scale = (int) $matches[6];
} elseif (count($matches) >= 4) {
$length = (int) $matches[4];
}

if (strpos($dbType, ' unsigned') !== false) {
$dbType = str_replace(' unsigned', '', $dbType);
$unsigned = true;
}

$fixed = false;
$type = $this->_platform->getDoctrineTypeMapping($dbType);
$default = $tableColumn['dflt_value'];
if ($default === 'NULL') {
@@ -334,31 +336,13 @@ protected function _getPortableTableColumnDefinition($tableColumn)
$tableColumn['name'] = '';
}

$precision = null;
$scale = null;

switch ($dbType) {
case 'char':
$fixed = true;
break;
case 'float':
case 'double':
case 'real':
case 'decimal':
case 'numeric':
if (isset($tableColumn['length'])) {
if (strpos($tableColumn['length'], ',') === false) {
$tableColumn['length'] .= ',0';
}
[$precision, $scale] = array_map('trim', explode(',', $tableColumn['length']));
}
$length = null;
break;
if ($dbType === 'char') {
$fixed = true;
}

$options = [
'length' => $length,
'unsigned' => (bool) $unsigned,
'unsigned' => $unsigned,
'fixed' => $fixed,
'notnull' => $notnull,
'default' => $default,
Loading

0 comments on commit f737105

Please sign in to comment.