diff --git a/CHANGELOG.md b/CHANGELOG.md index 04b1c557..a851c58b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - New #310: Add JSON overlaps condition builder (@Tigrov) - Enh #312: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov) - Enh #315: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) -- New #314: Implement `ColumnFactory` class (@Tigrov) +- New #314, #325: Implement `ColumnFactory` class (@Tigrov) - Enh #317: Separate column type constants (@Tigrov) - New #318: Realize `ColumnBuilder` class (@Tigrov) - Enh #320: Update according changes in `ColumnSchemaInterface` (@Tigrov) diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 6c428d78..4e19ace6 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -13,10 +13,9 @@ final class ColumnFactory extends AbstractColumnFactory * Mapping from physical column types (keys) to abstract column types (values). * * @var string[] - * - * @psalm-suppress MissingClassConstType + * @psalm-var array */ - private const TYPE_MAP = [ + protected const TYPE_MAP = [ 'bool' => ColumnType::BOOLEAN, 'boolean' => ColumnType::BOOLEAN, 'bit' => ColumnType::BIT, @@ -49,21 +48,11 @@ final class ColumnFactory extends AbstractColumnFactory protected function getType(string $dbType, array $info = []): string { - $type = self::TYPE_MAP[$dbType] ?? ColumnType::STRING; - - if ( - ($type === ColumnType::BIT || $type === ColumnType::TINYINT) - && isset($info['size']) - && $info['size'] === 1 - ) { - return ColumnType::BOOLEAN; - } - - return $type; - } - - protected function isDbType(string $dbType): bool - { - return isset(self::TYPE_MAP[$dbType]); + return match ($dbType) { + 'bit', 'tinyint' => isset($info['size']) && $info['size'] === 1 + ? ColumnType::BOOLEAN + : parent::getType($dbType, $info), + default => parent::getType($dbType, $info), + }; } } diff --git a/src/Schema.php b/src/Schema.php index 6ed3be62..08ea64fe 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -32,7 +32,6 @@ use function preg_replace; use function serialize; use function strncasecmp; -use function strtolower; /** * Implements the SQLite Server specific schema, supporting SQLite 3.3.0 or higher. @@ -72,6 +71,8 @@ * pk:string, * size?: int, * scale?: int, + * schema: string|null, + * table: string * } */ final class Schema extends AbstractPdoSchema @@ -342,6 +343,9 @@ protected function findColumns(TableSchemaInterface $table): bool $info['type'] = ColumnType::JSON; } + $info['schema'] = $table->getSchemaName(); + $info['table'] = $table->getName(); + $column = $this->loadColumnSchema($info); $table->column($info['name'], $column); @@ -446,16 +450,15 @@ public function getSchemaDefaultValues(string $schema = '', bool $refresh = fals */ private function loadColumnSchema(array $info): ColumnSchemaInterface { - $columnFactory = $this->getColumnFactory(); - - $dbType = strtolower($info['type']); - $column = $columnFactory->fromDefinition($dbType, ['name' => $info['name']]); - $column->dbType($dbType); - $column->notNull((bool) $info['notnull']); - $column->primaryKey((bool) $info['pk']); - $column->defaultValue($this->normalizeDefaultValue($info['dflt_value'], $column)); - - return $column; + $column = $this->getColumnFactory()->fromDefinition($info['type'], [ + 'name' => $info['name'], + 'notNull' => (bool) $info['notnull'], + 'primaryKey' => (bool) $info['pk'], + 'schema' => $info['schema'], + 'table' => $info['table'], + ]); + + return $column->defaultValue($this->normalizeDefaultValue($info['dflt_value'], $column)); } /** diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index f04bc4c9..81f554c4 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -40,7 +40,7 @@ public static function columns(): array ], 'tinyint_col' => [ 'type' => 'tinyint', - 'dbType' => 'tinyint(3)', + 'dbType' => 'tinyint', 'phpType' => 'int', 'primaryKey' => false, 'notNull' => false, @@ -52,7 +52,7 @@ public static function columns(): array ], 'smallint_col' => [ 'type' => 'smallint', - 'dbType' => 'smallint(1)', + 'dbType' => 'smallint', 'phpType' => 'int', 'primaryKey' => false, 'notNull' => false, @@ -64,7 +64,7 @@ public static function columns(): array ], 'char_col' => [ 'type' => 'char', - 'dbType' => 'char(100)', + 'dbType' => 'char', 'phpType' => 'string', 'primaryKey' => false, 'notNull' => true, @@ -76,7 +76,7 @@ public static function columns(): array ], 'char_col2' => [ 'type' => 'string', - 'dbType' => 'varchar(100)', + 'dbType' => 'varchar', 'phpType' => 'string', 'primaryKey' => false, 'notNull' => false, @@ -100,7 +100,7 @@ public static function columns(): array ], 'float_col' => [ 'type' => 'double', - 'dbType' => 'double(4,3)', + 'dbType' => 'double', 'phpType' => 'float', 'primaryKey' => false, 'notNull' => true, @@ -136,7 +136,7 @@ public static function columns(): array ], 'numeric_col' => [ 'type' => 'decimal', - 'dbType' => 'decimal(5,2)', + 'dbType' => 'decimal', 'phpType' => 'float', 'primaryKey' => false, 'notNull' => false, @@ -160,7 +160,7 @@ public static function columns(): array ], 'bool_col' => [ 'type' => 'boolean', - 'dbType' => 'tinyint(1)', + 'dbType' => 'tinyint', 'phpType' => 'bool', 'primaryKey' => false, 'notNull' => true, @@ -172,7 +172,7 @@ public static function columns(): array ], 'bool_col2' => [ 'type' => 'boolean', - 'dbType' => 'tinyint(1)', + 'dbType' => 'tinyint', 'phpType' => 'bool', 'primaryKey' => false, 'notNull' => false, @@ -196,7 +196,7 @@ public static function columns(): array ], 'bit_col' => [ 'type' => 'bit', - 'dbType' => 'bit(8)', + 'dbType' => 'bit', 'phpType' => 'int', 'primaryKey' => false, 'notNull' => true, @@ -249,7 +249,7 @@ public static function columns(): array ], 'type' => [ 'type' => 'string', - 'dbType' => 'varchar(255)', + 'dbType' => 'varchar', 'phpType' => 'string', 'primaryKey' => false, 'notNull' => true, @@ -313,7 +313,7 @@ public static function columnsTypeBit(): array [ 'bit_col_1' => [ 'type' => 'boolean', - 'dbType' => 'bit(1)', + 'dbType' => 'bit', 'phpType' => 'bool', 'primaryKey' => false, 'notNull' => true, @@ -325,7 +325,7 @@ public static function columnsTypeBit(): array ], 'bit_col_2' => [ 'type' => 'boolean', - 'dbType' => 'bit(1)', + 'dbType' => 'bit', 'phpType' => 'bool', 'primaryKey' => false, 'notNull' => false, @@ -337,7 +337,7 @@ public static function columnsTypeBit(): array ], 'bit_col_3' => [ 'type' => 'bit', - 'dbType' => 'bit(32)', + 'dbType' => 'bit', 'phpType' => 'int', 'primaryKey' => false, 'notNull' => true, @@ -349,7 +349,7 @@ public static function columnsTypeBit(): array ], 'bit_col_4' => [ 'type' => 'bit', - 'dbType' => 'bit(32)', + 'dbType' => 'bit', 'phpType' => 'int', 'primaryKey' => false, 'notNull' => false, @@ -361,7 +361,7 @@ public static function columnsTypeBit(): array ], 'bit_col_5' => [ 'type' => 'bit', - 'dbType' => 'bit(64)', + 'dbType' => 'bit', 'phpType' => 'int', 'primaryKey' => false, 'notNull' => true, @@ -373,7 +373,7 @@ public static function columnsTypeBit(): array ], 'bit_col_6' => [ 'type' => 'bit', - 'dbType' => 'bit(64)', + 'dbType' => 'bit', 'phpType' => 'int', 'primaryKey' => false, 'notNull' => false,