Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added type hints to the Column class #3511

Merged
merged 1 commit into from
Apr 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added type hints to the Column class
morozov committed Apr 17, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 57fe3c6e07e2b3dda7a92344138607b9e74a81a9
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
@@ -45,12 +45,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 = trim($tableColumn['default'], "'");
@@ -62,7 +59,6 @@ protected function _getPortableTableColumnDefinition($tableColumn)
switch (strtolower($tableColumn['typename'])) {
case 'varchar':
$length = $tableColumn['length'];
$fixed = false;
break;
case 'character':
$length = $tableColumn['length'];
@@ -82,12 +78,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
@@ -99,13 +99,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'])
@@ -122,8 +122,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;
@@ -165,22 +165,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
@@ -132,7 +132,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'] = '';
@@ -179,20 +181,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,
31 changes: 12 additions & 19 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 stripos;
@@ -317,10 +316,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 = [];
@@ -340,21 +340,22 @@ protected function _getPortableTableColumnDefinition($tableColumn)
$tableColumn['default'] = null;
}

$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']);
@@ -398,14 +399,6 @@ protected function _getPortableTableColumnDefinition($tableColumn)

$length = null;
break;
case 'text':
$fixed = false;
break;
case 'varchar':
case 'interval':
case '_varchar':
$fixed = false;
break;
case 'char':
case 'bpchar':
$fixed = true;
@@ -422,8 +415,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
@@ -70,16 +70,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) {
@@ -106,12 +117,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 !== 'NULL' ? $default : null,
'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') {
@@ -330,31 +332,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