Unverified Commit acb9d148 authored by Sergei Morozov's avatar Sergei Morozov

Merge pull request #3511 from morozov/column-type-hints

Added type hints to the Column class
parents 8d359773 eba7b78c
This diff is collapsed.
......@@ -47,12 +47,9 @@ class DB2SchemaManager extends AbstractSchemaManager
{
$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'];
......@@ -68,7 +65,6 @@ class DB2SchemaManager extends AbstractSchemaManager
switch (strtolower($tableColumn['typename'])) {
case 'varchar':
$length = $tableColumn['length'];
$fixed = false;
break;
case 'character':
$length = $tableColumn['length'];
......@@ -88,12 +84,10 @@ class DB2SchemaManager extends AbstractSchemaManager
$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,
......
......@@ -118,13 +118,13 @@ class MySqlSchemaManager extends AbstractSchemaManager
$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 @@ class MySqlSchemaManager extends AbstractSchemaManager
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 @@ class MySqlSchemaManager extends AbstractSchemaManager
$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'])) {
......
......@@ -133,7 +133,9 @@ class OracleSchemaManager extends AbstractSchemaManager
}
}
$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 @@ class OracleSchemaManager extends AbstractSchemaManager
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,
......
......@@ -20,7 +20,6 @@ use function explode;
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 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
{
$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 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
}
}
$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 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
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 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
$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;
......
......@@ -70,16 +70,27 @@ class SQLServerSchemaManager extends AbstractSchemaManager
$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 @@ class SQLServerSchemaManager extends AbstractSchemaManager
$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,
];
......
......@@ -12,10 +12,9 @@ use Doctrine\DBAL\Types\TextType;
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;
......@@ -300,23 +299,26 @@ class SqliteSchemaManager extends AbstractSchemaManager
*/
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') {
......@@ -336,31 +338,13 @@ class SqliteSchemaManager extends AbstractSchemaManager
$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,
......
......@@ -164,10 +164,7 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
$table = new Table('sqlsrv_column_comment');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('comment_null', 'integer', ['comment' => null]);
$table->addColumn('comment_false', 'integer', ['comment' => false]);
$table->addColumn('comment_empty_string', 'integer', ['comment' => '']);
$table->addColumn('comment_integer_0', 'integer', ['comment' => 0]);
$table->addColumn('comment_float_0', 'integer', ['comment' => 0.0]);
$table->addColumn('comment_string_0', 'integer', ['comment' => '0']);
$table->addColumn('comment', 'integer', ['comment' => 'Doctrine 0wnz you!']);
$table->addColumn('`comment_quoted`', 'integer', ['comment' => 'Doctrine 0wnz comments for explicitly quoted columns!']);
......@@ -179,13 +176,10 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->schemaManager->createTable($table);
$columns = $this->schemaManager->listTableColumns('sqlsrv_column_comment');
self::assertCount(12, $columns);
self::assertCount(9, $columns);
self::assertNull($columns['id']->getComment());
self::assertNull($columns['comment_null']->getComment());
self::assertNull($columns['comment_false']->getComment());
self::assertNull($columns['comment_empty_string']->getComment());
self::assertEquals('0', $columns['comment_integer_0']->getComment());
self::assertEquals('0', $columns['comment_float_0']->getComment());
self::assertEquals('0', $columns['comment_string_0']->getComment());
self::assertEquals('Doctrine 0wnz you!', $columns['comment']->getComment());
self::assertEquals('Doctrine 0wnz comments for explicitly quoted columns!', $columns['comment_quoted']->getComment());
......@@ -197,10 +191,7 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
$tableDiff->fromTable = $table;
$tableDiff->addedColumns['added_comment_none'] = new Column('added_comment_none', Type::getType('integer'));
$tableDiff->addedColumns['added_comment_null'] = new Column('added_comment_null', Type::getType('integer'), ['comment' => null]);
$tableDiff->addedColumns['added_comment_false'] = new Column('added_comment_false', Type::getType('integer'), ['comment' => false]);
$tableDiff->addedColumns['added_comment_empty_string'] = new Column('added_comment_empty_string', Type::getType('integer'), ['comment' => '']);
$tableDiff->addedColumns['added_comment_integer_0'] = new Column('added_comment_integer_0', Type::getType('integer'), ['comment' => 0]);
$tableDiff->addedColumns['added_comment_float_0'] = new Column('added_comment_float_0', Type::getType('integer'), ['comment' => 0.0]);
$tableDiff->addedColumns['added_comment_string_0'] = new Column('added_comment_string_0', Type::getType('integer'), ['comment' => '0']);
$tableDiff->addedColumns['added_comment'] = new Column('added_comment', Type::getType('integer'), ['comment' => 'Doctrine']);
$tableDiff->addedColumns['`added_comment_quoted`'] = new Column('`added_comment_quoted`', Type::getType('integer'), ['comment' => 'rulez']);
......@@ -208,8 +199,6 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
$tableDiff->addedColumns['added_commented_type'] = new Column('added_commented_type', Type::getType('object'));
$tableDiff->addedColumns['added_commented_type_with_comment'] = new Column('added_commented_type_with_comment', Type::getType('array'), ['comment' => '666']);
$tableDiff->renamedColumns['comment_float_0'] = new Column('comment_double_0', Type::getType('decimal'), ['comment' => 'Double for real!']);
// Add comment to non-commented column.
$tableDiff->changedColumns['id'] = new ColumnDiff(
'id',
......@@ -226,14 +215,6 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
new Column('comment_null', Type::getType('integer'), ['comment' => null])
);
// Add comment to false-commented column.
$tableDiff->changedColumns['comment_false'] = new ColumnDiff(
'comment_false',
new Column('comment_false', Type::getType('integer'), ['comment' => 'false']),
['comment'],
new Column('comment_false', Type::getType('integer'), ['comment' => false])
);
// Change type to custom type from empty string commented column.
$tableDiff->changedColumns['comment_empty_string'] = new ColumnDiff(
'comment_empty_string',
......@@ -242,10 +223,10 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
new Column('comment_empty_string', Type::getType('integer'), ['comment' => ''])
);
// Change comment to false-comment from zero-string commented column.
// Change comment to empty comment from zero-string commented column.
$tableDiff->changedColumns['comment_string_0'] = new ColumnDiff(
'comment_string_0',
new Column('comment_string_0', Type::getType('integer'), ['comment' => false]),
new Column('comment_string_0', Type::getType('integer'), ['comment' => '']),
['comment'],
new Column('comment_string_0', Type::getType('integer'), ['comment' => '0'])
);
......@@ -290,17 +271,13 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
new Column('commented_type_with_comment', Type::getType('array'), ['comment' => 'Doctrine array type.'])
);
$tableDiff->removedColumns['comment_integer_0'] = new Column('comment_integer_0', Type::getType('integer'), ['comment' => 0]);
$this->schemaManager->alterTable($tableDiff);
$columns = $this->schemaManager->listTableColumns('sqlsrv_column_comment');
self::assertCount(23, $columns);
self::assertCount(18, $columns);
self::assertEquals('primary', $columns['id']->getComment());
self::assertNull($columns['comment_null']->getComment());
self::assertEquals('false', $columns['comment_false']->getComment());
self::assertNull($columns['comment_empty_string']->getComment());
self::assertEquals('0', $columns['comment_double_0']->getComment());
self::assertNull($columns['comment_string_0']->getComment());
self::assertNull($columns['comment']->getComment());
self::assertEquals('Doctrine array.', $columns['comment_quoted']->getComment());
......@@ -309,10 +286,7 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
self::assertNull($columns['commented_type_with_comment']->getComment());
self::assertNull($columns['added_comment_none']->getComment());
self::assertNull($columns['added_comment_null']->getComment());
self::assertNull($columns['added_comment_false']->getComment());
self::assertNull($columns['added_comment_empty_string']->getComment());
self::assertEquals('0', $columns['added_comment_integer_0']->getComment());
self::assertEquals('0', $columns['added_comment_float_0']->getComment());
self::assertEquals('0', $columns['added_comment_string_0']->getComment());
self::assertEquals('Doctrine', $columns['added_comment']->getComment());
self::assertEquals('rulez', $columns['added_comment_quoted']->getComment());
......
......@@ -1224,7 +1224,7 @@ class ComparatorTest extends TestCase
$column2 = new Column(
'foo',
Type::getType('guid'),
['notnull' => false, 'length' => '36', 'fixed' => true, 'default' => 'NEWID()', 'comment' => 'GUID 2.']
['notnull' => false, 'length' => 36, 'fixed' => true, 'default' => 'NEWID()', 'comment' => 'GUID 2.']
);
self::assertEquals(['notnull', 'default', 'comment'], $comparator->diffColumn($column1, $column2));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment