diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dd6d7eb..44e11823 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Enh #353: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov) - Enh #354: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov) - Enh #356: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) +- Enh #355: Implement `ColumnFactory` class (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/Column/ArrayColumnSchema.php b/src/Column/ArrayColumnSchema.php index 68911280..4925b991 100644 --- a/src/Column/ArrayColumnSchema.php +++ b/src/Column/ArrayColumnSchema.php @@ -12,9 +12,6 @@ use Yiisoft\Db\Pgsql\Schema; use Yiisoft\Db\Schema\Column\AbstractColumnSchema; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; -use Yiisoft\Db\Schema\Column\DoubleColumnSchema; -use Yiisoft\Db\Schema\Column\JsonColumnSchema; -use Yiisoft\Db\Schema\Column\StringColumnSchema; use Yiisoft\Db\Schema\SchemaInterface; use function array_map; @@ -57,26 +54,7 @@ public function column(ColumnSchemaInterface|null $column): static public function getColumn(): ColumnSchemaInterface { if ($this->column === null) { - $type = $this->getType(); - - $this->column = match ($type) { - SchemaInterface::TYPE_BOOLEAN => new BooleanColumnSchema($type), - SchemaInterface::TYPE_BIT => new BitColumnSchema($type), - SchemaInterface::TYPE_TINYINT => new IntegerColumnSchema($type), - SchemaInterface::TYPE_SMALLINT => new IntegerColumnSchema($type), - SchemaInterface::TYPE_INTEGER => new IntegerColumnSchema($type), - SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE !== 8 - ? new BigIntColumnSchema($type) - : new IntegerColumnSchema($type), - SchemaInterface::TYPE_DECIMAL => new DoubleColumnSchema($type), - SchemaInterface::TYPE_FLOAT => new DoubleColumnSchema($type), - SchemaInterface::TYPE_DOUBLE => new DoubleColumnSchema($type), - SchemaInterface::TYPE_BINARY => new BinaryColumnSchema($type), - SchemaInterface::TYPE_JSON => new JsonColumnSchema($type), - Schema::TYPE_STRUCTURED => new StructuredColumnSchema($type), - default => new StringColumnSchema($type), - }; - + $this->column = (new ColumnFactory())->fromType($this->getType()); $this->column->dbType($this->getDbType()); $this->column->enumValues($this->getEnumValues()); $this->column->precision($this->getPrecision()); diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php new file mode 100644 index 00000000..629eeb7a --- /dev/null +++ b/src/Column/ColumnFactory.php @@ -0,0 +1,150 @@ +, + * comment?: string|null, + * computed?: bool|string, + * db_type?: string|null, + * default_value?: mixed, + * dimension?: int|string, + * enum_values?: array|null, + * extra?: string|null, + * primary_key?: bool|string, + * name?: string, + * precision?: int|string|null, + * sequence_name?: string|null, + * scale?: int|string|null, + * schema?: string|null, + * size?: int|string|null, + * table?: string|null, + * type?: string, + * } + */ +final class ColumnFactory extends AbstractColumnFactory +{ + /** + * The mapping from physical column types (keys) to abstract column types (values). + * + * @link https://www.postgresql.org/docs/current/datatype.html#DATATYPE-TABLE + * + * @var string[] + * + * @psalm-suppress MissingClassConstType + */ + private const TYPE_MAP = [ + 'bool' => SchemaInterface::TYPE_BOOLEAN, + 'boolean' => SchemaInterface::TYPE_BOOLEAN, + 'bit' => SchemaInterface::TYPE_BIT, + 'bit varying' => SchemaInterface::TYPE_BIT, + 'varbit' => SchemaInterface::TYPE_BIT, + 'smallint' => SchemaInterface::TYPE_SMALLINT, + 'int2' => SchemaInterface::TYPE_SMALLINT, + 'smallserial' => SchemaInterface::TYPE_SMALLINT, + 'serial2' => SchemaInterface::TYPE_SMALLINT, + 'int4' => SchemaInterface::TYPE_INTEGER, + 'int' => SchemaInterface::TYPE_INTEGER, + 'integer' => SchemaInterface::TYPE_INTEGER, + 'serial' => SchemaInterface::TYPE_INTEGER, + 'serial4' => SchemaInterface::TYPE_INTEGER, + 'bigint' => SchemaInterface::TYPE_BIGINT, + 'int8' => SchemaInterface::TYPE_BIGINT, + 'bigserial' => SchemaInterface::TYPE_BIGINT, + 'serial8' => SchemaInterface::TYPE_BIGINT, + 'oid' => SchemaInterface::TYPE_BIGINT, // shouldn't be used. it's pg internal! + 'pg_lsn' => SchemaInterface::TYPE_BIGINT, + 'real' => SchemaInterface::TYPE_FLOAT, + 'float4' => SchemaInterface::TYPE_FLOAT, + 'float8' => SchemaInterface::TYPE_DOUBLE, + 'double precision' => SchemaInterface::TYPE_DOUBLE, + 'decimal' => SchemaInterface::TYPE_DECIMAL, + 'numeric' => SchemaInterface::TYPE_DECIMAL, + 'money' => SchemaInterface::TYPE_MONEY, + 'char' => SchemaInterface::TYPE_CHAR, + 'character' => SchemaInterface::TYPE_CHAR, + 'bpchar' => SchemaInterface::TYPE_CHAR, + 'character varying' => SchemaInterface::TYPE_STRING, + 'varchar' => SchemaInterface::TYPE_STRING, + 'text' => SchemaInterface::TYPE_TEXT, + 'bytea' => SchemaInterface::TYPE_BINARY, + 'date' => SchemaInterface::TYPE_DATE, + 'time' => SchemaInterface::TYPE_TIME, + 'time without time zone' => SchemaInterface::TYPE_TIME, + 'time with time zone' => SchemaInterface::TYPE_TIME, + 'timetz' => SchemaInterface::TYPE_TIME, + 'timestamp' => SchemaInterface::TYPE_TIMESTAMP, + 'timestamp without time zone' => SchemaInterface::TYPE_TIMESTAMP, + 'timestamp with time zone' => SchemaInterface::TYPE_TIMESTAMP, + 'timestamptz' => SchemaInterface::TYPE_TIMESTAMP, + 'abstime' => SchemaInterface::TYPE_TIMESTAMP, + 'interval' => SchemaInterface::TYPE_STRING, + 'box' => SchemaInterface::TYPE_STRING, + 'circle' => SchemaInterface::TYPE_STRING, + 'point' => SchemaInterface::TYPE_STRING, + 'line' => SchemaInterface::TYPE_STRING, + 'lseg' => SchemaInterface::TYPE_STRING, + 'polygon' => SchemaInterface::TYPE_STRING, + 'path' => SchemaInterface::TYPE_STRING, + 'cidr' => SchemaInterface::TYPE_STRING, + 'inet' => SchemaInterface::TYPE_STRING, + 'macaddr' => SchemaInterface::TYPE_STRING, + 'tsquery' => SchemaInterface::TYPE_STRING, + 'tsvector' => SchemaInterface::TYPE_STRING, + 'txid_snapshot' => SchemaInterface::TYPE_STRING, + 'unknown' => SchemaInterface::TYPE_STRING, + 'uuid' => SchemaInterface::TYPE_STRING, + 'xml' => SchemaInterface::TYPE_STRING, + 'json' => SchemaInterface::TYPE_JSON, + 'jsonb' => SchemaInterface::TYPE_JSON, + ]; + + /** + * @psalm-param ColumnInfo $info + * @psalm-suppress MoreSpecificImplementedParamType + */ + public function fromType(string $type, array $info = []): ColumnSchemaInterface + { + $dimension = (int)($info['dimension'] ?? 0); + + if ($dimension > 0) { + unset($info['dimension']); + $column = (new ArrayColumnSchema()) + ->dimension($dimension) + ->column($this->fromType($type, $info)); + } else { + $column = match ($type) { + SchemaInterface::TYPE_BOOLEAN => new BooleanColumnSchema($type), + SchemaInterface::TYPE_BIT => new BitColumnSchema($type), + SchemaInterface::TYPE_TINYINT => new IntegerColumnSchema($type), + SchemaInterface::TYPE_SMALLINT => new IntegerColumnSchema($type), + SchemaInterface::TYPE_INTEGER => new IntegerColumnSchema($type), + SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE !== 8 + ? new BigIntColumnSchema($type) + : new IntegerColumnSchema($type), + SchemaInterface::TYPE_BINARY => new BinaryColumnSchema($type), + Schema::TYPE_STRUCTURED => (new StructuredColumnSchema($type))->columns($info['columns'] ?? []), + default => parent::fromType($type, $info), + }; + } + + return $column; + } + + protected function getType(string $dbType, array $info = []): string + { + return self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING; + } +} diff --git a/src/Schema.php b/src/Schema.php index 3f3b53d9..48b141e6 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -18,17 +18,12 @@ use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Helper\DbArrayHelper; use Yiisoft\Db\Pgsql\Column\ArrayColumnSchema; -use Yiisoft\Db\Pgsql\Column\BigIntColumnSchema; -use Yiisoft\Db\Pgsql\Column\BinaryColumnSchema; -use Yiisoft\Db\Pgsql\Column\BitColumnSchema; -use Yiisoft\Db\Pgsql\Column\BooleanColumnSchema; -use Yiisoft\Db\Pgsql\Column\IntegerColumnSchema; +use Yiisoft\Db\Pgsql\Column\ColumnFactory; use Yiisoft\Db\Pgsql\Column\SequenceColumnSchemaInterface; -use Yiisoft\Db\Pgsql\Column\StructuredColumnSchema; use Yiisoft\Db\Pgsql\Column\StructuredColumnSchemaInterface; use Yiisoft\Db\Schema\Builder\ColumnInterface; +use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Schema\TableSchemaInterface; use function array_change_key_case; @@ -92,6 +87,8 @@ * dimension?: int|string, * columns?: array * } + * + * @psalm-suppress MissingClassConstType */ final class Schema extends AbstractPdoSchema { @@ -104,79 +101,6 @@ final class Schema extends AbstractPdoSchema */ public const TYPE_STRUCTURED = 'structured'; - /** - * The mapping from physical column types (keys) to abstract column types (values). - * - * @link https://www.postgresql.org/docs/current/datatype.html#DATATYPE-TABLE - * - * @var string[] - */ - private const TYPE_MAP = [ - 'bit' => self::TYPE_BIT, - 'bit varying' => self::TYPE_BIT, - 'varbit' => self::TYPE_BIT, - 'bool' => self::TYPE_BOOLEAN, - 'boolean' => self::TYPE_BOOLEAN, - 'box' => self::TYPE_STRING, - 'circle' => self::TYPE_STRING, - 'point' => self::TYPE_STRING, - 'line' => self::TYPE_STRING, - 'lseg' => self::TYPE_STRING, - 'polygon' => self::TYPE_STRING, - 'path' => self::TYPE_STRING, - 'character' => self::TYPE_CHAR, - 'char' => self::TYPE_CHAR, - 'bpchar' => self::TYPE_CHAR, - 'character varying' => self::TYPE_STRING, - 'varchar' => self::TYPE_STRING, - 'text' => self::TYPE_TEXT, - 'bytea' => self::TYPE_BINARY, - 'cidr' => self::TYPE_STRING, - 'inet' => self::TYPE_STRING, - 'macaddr' => self::TYPE_STRING, - 'real' => self::TYPE_FLOAT, - 'float4' => self::TYPE_FLOAT, - 'double precision' => self::TYPE_DOUBLE, - 'float8' => self::TYPE_DOUBLE, - 'decimal' => self::TYPE_DECIMAL, - 'numeric' => self::TYPE_DECIMAL, - 'money' => self::TYPE_MONEY, - 'smallint' => self::TYPE_SMALLINT, - 'int2' => self::TYPE_SMALLINT, - 'int4' => self::TYPE_INTEGER, - 'int' => self::TYPE_INTEGER, - 'integer' => self::TYPE_INTEGER, - 'bigint' => self::TYPE_BIGINT, - 'int8' => self::TYPE_BIGINT, - 'oid' => self::TYPE_BIGINT, // shouldn't be used. it's pg internal! - 'smallserial' => self::TYPE_SMALLINT, - 'serial2' => self::TYPE_SMALLINT, - 'serial4' => self::TYPE_INTEGER, - 'serial' => self::TYPE_INTEGER, - 'bigserial' => self::TYPE_BIGINT, - 'serial8' => self::TYPE_BIGINT, - 'pg_lsn' => self::TYPE_BIGINT, - 'date' => self::TYPE_DATE, - 'interval' => self::TYPE_STRING, - 'time without time zone' => self::TYPE_TIME, - 'time' => self::TYPE_TIME, - 'time with time zone' => self::TYPE_TIME, - 'timetz' => self::TYPE_TIME, - 'timestamp without time zone' => self::TYPE_TIMESTAMP, - 'timestamp' => self::TYPE_TIMESTAMP, - 'timestamp with time zone' => self::TYPE_TIMESTAMP, - 'timestamptz' => self::TYPE_TIMESTAMP, - 'abstime' => self::TYPE_TIMESTAMP, - 'tsquery' => self::TYPE_STRING, - 'tsvector' => self::TYPE_STRING, - 'txid_snapshot' => self::TYPE_STRING, - 'unknown' => self::TYPE_STRING, - 'uuid' => self::TYPE_STRING, - 'json' => self::TYPE_JSON, - 'jsonb' => self::TYPE_JSON, - 'xml' => self::TYPE_STRING, - ]; - /** * @var string|null The default schema used for the current session. */ @@ -194,6 +118,11 @@ public function createColumn(string $type, array|int|string $length = null): Col return new Column($type, $length); } + public function getColumnFactory(): ColumnFactoryInterface + { + return new ColumnFactory(); + } + /** * Resolves the table name and schema name (if any). * @@ -809,17 +738,19 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface $columns = []; if ($info['type_type'] === 'c') { - $type = self::TYPE_STRUCTURED; $structured = $this->resolveTableName($dbType); if ($this->findColumns($structured)) { $columns = $structured->getColumns(); } + + $column = $this->getColumnFactory() + ->fromType(self::TYPE_STRUCTURED, ['dimension' => $info['dimension'], 'columns' => $columns]); } else { - $type = self::TYPE_MAP[$dbType] ?? self::TYPE_STRING; + $column = $this->getColumnFactory() + ->fromDbType($dbType, ['dimension' => $info['dimension']]); } - $column = $this->createColumnSchema($type, dimension: $info['dimension'], columns: $columns); $column->name($info['column_name']); $column->dbType($dbType); $column->allowNull($info['is_nullable']); @@ -874,23 +805,6 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface return $column; } - protected function createColumnSchemaFromType(string $type, bool $isUnsigned = false): ColumnSchemaInterface - { - return match ($type) { - SchemaInterface::TYPE_BOOLEAN => new BooleanColumnSchema($type), - SchemaInterface::TYPE_BIT => new BitColumnSchema($type), - SchemaInterface::TYPE_TINYINT => new IntegerColumnSchema($type), - SchemaInterface::TYPE_SMALLINT => new IntegerColumnSchema($type), - SchemaInterface::TYPE_INTEGER => new IntegerColumnSchema($type), - SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE !== 8 - ? new BigIntColumnSchema($type) - : new IntegerColumnSchema($type), - SchemaInterface::TYPE_BINARY => new BinaryColumnSchema($type), - self::TYPE_STRUCTURED => new StructuredColumnSchema($type), - default => parent::createColumnSchemaFromType($type), - }; - } - /** * Converts column's default value according to {@see ColumnSchema::phpType} after retrieval from the database. * @@ -1065,35 +979,6 @@ private function loadTableConstraints(string $tableName, string $returnType): ar return $result[$returnType]; } - /** - * @psalm-param CreateInfo $info - * @psalm-suppress ImplementedParamTypeMismatch - */ - protected function createColumnSchema(string $type, mixed ...$info): ColumnSchemaInterface - { - /** @var CreateInfo $info */ - $dimension = isset($info['dimension']) ? (int) $info['dimension'] : 0; - - if ($dimension > 0) { - $column = new ArrayColumnSchema(); - $column->dimension($dimension); - - unset($info['dimension']); - - $column->column($this->createColumnSchema($type, ...$info)); - - return $column; - } - - $column = $this->createColumnSchemaFromType($type); - - if ($column instanceof StructuredColumnSchemaInterface) { - $column->columns($info['columns'] ?? []); - } - - return $column; - } - /** * Returns the cache key for the specified table name. * diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php new file mode 100644 index 00000000..e48306c1 --- /dev/null +++ b/tests/ColumnFactoryTest.php @@ -0,0 +1,34 @@ +assertFalse($tableIndexes[4]->isPrimary()); $this->assertFalse($tableIndexes[4]->isUnique()); } + + public function testGetColumnFactory(): void + { + $db = $this->getConnection(); + $factory = $db->getSchema()->getColumnFactory(); + + $this->assertInstanceOf(ColumnFactory::class, $factory); + } }