diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e5666c2..69152211 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - 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) - Enh #314: Implement `ColumnFactory` class (@Tigrov) +- Enh #317: Separate column type constants (@Tigrov) ## 1.2.0 March 21, 2024 diff --git a/src/Column.php b/src/Column.php index 7253eebb..d6869619 100644 --- a/src/Column.php +++ b/src/Column.php @@ -15,7 +15,7 @@ * For example, the following code creates a column schema for an integer column: * * ```php - * $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0); + * $column = (new Column(ColumnType::INTEGER))->notNull()->defaultValue(0); * ``` * * Provides a fluent interface, which means that the methods can be chained together to create a column schema with diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 7b759d7b..96ab9d18 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -4,8 +4,8 @@ namespace Yiisoft\Db\Sqlite\Column; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\AbstractColumnFactory; -use Yiisoft\Db\Schema\SchemaInterface; final class ColumnFactory extends AbstractColumnFactory { @@ -17,47 +17,47 @@ final class ColumnFactory extends AbstractColumnFactory * @psalm-suppress MissingClassConstType */ private const TYPE_MAP = [ - 'bool' => SchemaInterface::TYPE_BOOLEAN, - 'boolean' => SchemaInterface::TYPE_BOOLEAN, - 'bit' => SchemaInterface::TYPE_BIT, - 'tinyint' => SchemaInterface::TYPE_TINYINT, - 'smallint' => SchemaInterface::TYPE_SMALLINT, - 'mediumint' => SchemaInterface::TYPE_INTEGER, - 'int' => SchemaInterface::TYPE_INTEGER, - 'integer' => SchemaInterface::TYPE_INTEGER, - 'bigint' => SchemaInterface::TYPE_BIGINT, - 'float' => SchemaInterface::TYPE_FLOAT, - 'real' => SchemaInterface::TYPE_FLOAT, - 'double' => SchemaInterface::TYPE_DOUBLE, - 'decimal' => SchemaInterface::TYPE_DECIMAL, - 'numeric' => SchemaInterface::TYPE_DECIMAL, - 'char' => SchemaInterface::TYPE_CHAR, - 'varchar' => SchemaInterface::TYPE_STRING, - 'string' => SchemaInterface::TYPE_STRING, - 'enum' => SchemaInterface::TYPE_STRING, - 'tinytext' => SchemaInterface::TYPE_TEXT, - 'mediumtext' => SchemaInterface::TYPE_TEXT, - 'longtext' => SchemaInterface::TYPE_TEXT, - 'text' => SchemaInterface::TYPE_TEXT, - 'blob' => SchemaInterface::TYPE_BINARY, - 'year' => SchemaInterface::TYPE_DATE, - 'date' => SchemaInterface::TYPE_DATE, - 'time' => SchemaInterface::TYPE_TIME, - 'datetime' => SchemaInterface::TYPE_DATETIME, - 'timestamp' => SchemaInterface::TYPE_TIMESTAMP, - 'json' => SchemaInterface::TYPE_JSON, + 'bool' => ColumnType::BOOLEAN, + 'boolean' => ColumnType::BOOLEAN, + 'bit' => ColumnType::BIT, + 'tinyint' => ColumnType::TINYINT, + 'smallint' => ColumnType::SMALLINT, + 'mediumint' => ColumnType::INTEGER, + 'int' => ColumnType::INTEGER, + 'integer' => ColumnType::INTEGER, + 'bigint' => ColumnType::BIGINT, + 'float' => ColumnType::FLOAT, + 'real' => ColumnType::FLOAT, + 'double' => ColumnType::DOUBLE, + 'decimal' => ColumnType::DECIMAL, + 'numeric' => ColumnType::DECIMAL, + 'char' => ColumnType::CHAR, + 'varchar' => ColumnType::STRING, + 'string' => ColumnType::STRING, + 'enum' => ColumnType::STRING, + 'tinytext' => ColumnType::TEXT, + 'mediumtext' => ColumnType::TEXT, + 'longtext' => ColumnType::TEXT, + 'text' => ColumnType::TEXT, + 'blob' => ColumnType::BINARY, + 'year' => ColumnType::DATE, + 'date' => ColumnType::DATE, + 'time' => ColumnType::TIME, + 'datetime' => ColumnType::DATETIME, + 'timestamp' => ColumnType::TIMESTAMP, + 'json' => ColumnType::JSON, ]; protected function getType(string $dbType, array $info = []): string { - $type = self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING; + $type = self::TYPE_MAP[$dbType] ?? ColumnType::STRING; if ( - ($type === SchemaInterface::TYPE_BIT || $type === SchemaInterface::TYPE_TINYINT) + ($type === ColumnType::BIT || $type === ColumnType::TINYINT) && isset($info['size']) && $info['size'] === 1 ) { - return SchemaInterface::TYPE_BOOLEAN; + return ColumnType::BOOLEAN; } return $type; diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 4f867bd2..1b255808 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Sqlite; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder; use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; @@ -17,30 +19,30 @@ final class QueryBuilder extends AbstractQueryBuilder * @var string[] Mapping from abstract column types (keys) to physical column types (values). */ protected array $typeMap = [ - SchemaInterface::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - SchemaInterface::TYPE_UPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - SchemaInterface::TYPE_BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - SchemaInterface::TYPE_UBIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - SchemaInterface::TYPE_CHAR => 'char(1)', - SchemaInterface::TYPE_STRING => 'varchar(255)', - SchemaInterface::TYPE_TEXT => 'text', - SchemaInterface::TYPE_TINYINT => 'tinyint', - SchemaInterface::TYPE_SMALLINT => 'smallint', - SchemaInterface::TYPE_INTEGER => 'integer', - SchemaInterface::TYPE_BIGINT => 'bigint', - SchemaInterface::TYPE_FLOAT => 'float', - SchemaInterface::TYPE_DOUBLE => 'double', - SchemaInterface::TYPE_DECIMAL => 'decimal(10,0)', - SchemaInterface::TYPE_DATETIME => 'datetime', - SchemaInterface::TYPE_TIMESTAMP => 'timestamp', - SchemaInterface::TYPE_TIME => 'time', - SchemaInterface::TYPE_DATE => 'date', - SchemaInterface::TYPE_BINARY => 'blob', - SchemaInterface::TYPE_BOOLEAN => 'boolean', - SchemaInterface::TYPE_MONEY => 'decimal(19,4)', - SchemaInterface::TYPE_UUID => 'blob(16)', - SchemaInterface::TYPE_UUID_PK => 'blob(16) PRIMARY KEY', - SchemaInterface::TYPE_JSON => 'json', + PseudoType::PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + PseudoType::UPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + PseudoType::BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + PseudoType::UBIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + ColumnType::CHAR => 'char(1)', + ColumnType::STRING => 'varchar(255)', + ColumnType::TEXT => 'text', + ColumnType::TINYINT => 'tinyint', + ColumnType::SMALLINT => 'smallint', + ColumnType::INTEGER => 'integer', + ColumnType::BIGINT => 'bigint', + ColumnType::FLOAT => 'float', + ColumnType::DOUBLE => 'double', + ColumnType::DECIMAL => 'decimal(10,0)', + ColumnType::DATETIME => 'datetime', + ColumnType::TIMESTAMP => 'timestamp', + ColumnType::TIME => 'time', + ColumnType::DATE => 'date', + ColumnType::BINARY => 'blob', + ColumnType::BOOLEAN => 'boolean', + ColumnType::MONEY => 'decimal(19,4)', + ColumnType::UUID => 'blob(16)', + PseudoType::UUID_PK => 'blob(16) PRIMARY KEY', + ColumnType::JSON => 'json', ]; public function __construct(QuoterInterface $quoter, SchemaInterface $schema) diff --git a/src/Schema.php b/src/Schema.php index 0252be1c..29a321c1 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Sqlite; use Throwable; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constraint\CheckConstraint; use Yiisoft\Db\Constraint\Constraint; use Yiisoft\Db\Constraint\ForeignKeyConstraint; @@ -337,7 +338,7 @@ protected function findColumns(TableSchemaInterface $table): bool foreach ($columns as $info) { if (in_array($info['name'], $jsonColumns, true)) { - $info['type'] = self::TYPE_JSON; + $info['type'] = ColumnType::JSON; } $column = $this->loadColumnSchema($info); diff --git a/tests/CommandTest.php b/tests/CommandTest.php index aaa7c38a..c68b5ffa 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -5,11 +5,12 @@ namespace Yiisoft\Db\Sqlite\Tests; use Throwable; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\JsonExpression; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Sqlite\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonCommandTest; @@ -391,7 +392,7 @@ public function testResetSequence(): void $command->createTable( '{{testCreateTable}}', - ['id' => SchemaInterface::TYPE_PK, 'bar' => SchemaInterface::TYPE_INTEGER], + ['id' => PseudoType::PK, 'bar' => ColumnType::INTEGER], )->execute(); $command->insert('{{testCreateTable}}', ['bar' => 1])->execute(); @@ -508,8 +509,8 @@ public function testJsonTable(): void } $command->createTable('json_table', [ - 'id' => SchemaInterface::TYPE_PK, - 'json_col' => SchemaInterface::TYPE_JSON, + 'id' => PseudoType::PK, + 'json_col' => ColumnType::JSON, ])->execute(); $command->insert('json_table', ['id' => 1, 'json_col' => ['a' => 1, 'b' => 2]])->execute(); diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index c095a7e1..9904e2d1 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Sqlite\Tests\Provider; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\BinaryColumnSchema; use Yiisoft\Db\Schema\Column\BitColumnSchema; use Yiisoft\Db\Schema\Column\BooleanColumnSchema; @@ -18,35 +19,35 @@ public static function dbTypes(): array { return [ // db type, expected abstract type, expected instance of - ['bool', 'boolean', BooleanColumnSchema::class], - ['boolean', 'boolean', BooleanColumnSchema::class], - ['bit', 'bit', BitColumnSchema::class], - ['tinyint', 'tinyint', IntegerColumnSchema::class], - ['smallint', 'smallint', IntegerColumnSchema::class], - ['mediumint', 'integer', IntegerColumnSchema::class], - ['int', 'integer', IntegerColumnSchema::class], - ['integer', 'integer', IntegerColumnSchema::class], - ['bigint', 'bigint', IntegerColumnSchema::class], - ['float', 'float', DoubleColumnSchema::class], - ['real', 'float', DoubleColumnSchema::class], - ['double', 'double', DoubleColumnSchema::class], - ['decimal', 'decimal', DoubleColumnSchema::class], - ['numeric', 'decimal', DoubleColumnSchema::class], - ['char', 'char', StringColumnSchema::class], - ['varchar', 'string', StringColumnSchema::class], - ['string', 'string', StringColumnSchema::class], - ['enum', 'string', StringColumnSchema::class], - ['tinytext', 'text', StringColumnSchema::class], - ['mediumtext', 'text', StringColumnSchema::class], - ['longtext', 'text', StringColumnSchema::class], - ['text', 'text', StringColumnSchema::class], - ['blob', 'binary', BinaryColumnSchema::class], - ['year', 'date', StringColumnSchema::class], - ['date', 'date', StringColumnSchema::class], - ['time', 'time', StringColumnSchema::class], - ['datetime', 'datetime', StringColumnSchema::class], - ['timestamp', 'timestamp', StringColumnSchema::class], - ['json', 'json', JsonColumnSchema::class], + ['bool', ColumnType::BOOLEAN, BooleanColumnSchema::class], + ['boolean', ColumnType::BOOLEAN, BooleanColumnSchema::class], + ['bit', ColumnType::BIT, BitColumnSchema::class], + ['tinyint', ColumnType::TINYINT, IntegerColumnSchema::class], + ['smallint', ColumnType::SMALLINT, IntegerColumnSchema::class], + ['mediumint', ColumnType::INTEGER, IntegerColumnSchema::class], + ['int', ColumnType::INTEGER, IntegerColumnSchema::class], + ['integer', ColumnType::INTEGER, IntegerColumnSchema::class], + ['bigint', ColumnType::BIGINT, IntegerColumnSchema::class], + ['float', ColumnType::FLOAT, DoubleColumnSchema::class], + ['real', ColumnType::FLOAT, DoubleColumnSchema::class], + ['double', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['decimal', ColumnType::DECIMAL, DoubleColumnSchema::class], + ['numeric', ColumnType::DECIMAL, DoubleColumnSchema::class], + ['char', ColumnType::CHAR, StringColumnSchema::class], + ['varchar', ColumnType::STRING, StringColumnSchema::class], + ['string', ColumnType::STRING, StringColumnSchema::class], + ['enum', ColumnType::STRING, StringColumnSchema::class], + ['tinytext', ColumnType::TEXT, StringColumnSchema::class], + ['mediumtext', ColumnType::TEXT, StringColumnSchema::class], + ['longtext', ColumnType::TEXT, StringColumnSchema::class], + ['text', ColumnType::TEXT, StringColumnSchema::class], + ['blob', ColumnType::BINARY, BinaryColumnSchema::class], + ['year', ColumnType::DATE, StringColumnSchema::class], + ['date', ColumnType::DATE, StringColumnSchema::class], + ['time', ColumnType::TIME, StringColumnSchema::class], + ['datetime', ColumnType::DATETIME, StringColumnSchema::class], + ['timestamp', ColumnType::TIMESTAMP, StringColumnSchema::class], + ['json', ColumnType::JSON, JsonColumnSchema::class], ]; } @@ -54,8 +55,8 @@ public static function definitions(): array { $definitions = parent::definitions(); - $definitions[] = ['bit(1)', 'boolean', BooleanColumnSchema::class, ['getSize' => 1]]; - $definitions[] = ['tinyint(1)', 'boolean', BooleanColumnSchema::class, ['getSize' => 1]]; + $definitions[] = ['bit(1)', ColumnType::BOOLEAN, BooleanColumnSchema::class, ['getDbType' => 'bit', 'getSize' => 1]]; + $definitions[] = ['tinyint(1)', ColumnType::BOOLEAN, BooleanColumnSchema::class, ['getDbType' => 'tinyint', 'getSize' => 1]]; return $definitions; } diff --git a/tests/Provider/ColumnSchemaBuilderProvider.php b/tests/Provider/ColumnSchemaBuilderProvider.php index cd05c57b..d92bf44c 100644 --- a/tests/Provider/ColumnSchemaBuilderProvider.php +++ b/tests/Provider/ColumnSchemaBuilderProvider.php @@ -4,7 +4,7 @@ namespace Yiisoft\Db\Sqlite\Tests\Provider; -use Yiisoft\Db\Schema\SchemaInterface; +use Yiisoft\Db\Constant\ColumnType; final class ColumnSchemaBuilderProvider extends \Yiisoft\Db\Tests\Provider\ColumnSchemaBuilderProvider { @@ -19,7 +19,7 @@ public static function types(): array return [ ...$types, - ['integer UNSIGNED', SchemaInterface::TYPE_INTEGER, null, [['unsigned']]], + ['integer UNSIGNED', ColumnType::INTEGER, null, [['unsigned']]], ]; } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 44145585..3f65ed1d 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Sqlite\Tests; use JsonException; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; @@ -15,7 +16,6 @@ use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Sqlite\Column; use Yiisoft\Db\Sqlite\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest; @@ -172,7 +172,7 @@ public function testAlterColumn(): void $this->expectException(NotSupportedException::class); $this->expectExceptionMessage('Yiisoft\Db\Sqlite\DDLQueryBuilder::alterColumn is not supported by SQLite.'); - $qb->alterColumn('customer', 'email', SchemaInterface::TYPE_STRING); + $qb->alterColumn('customer', 'email', ColumnType::STRING); } /** @@ -750,7 +750,7 @@ public function testUpsertExecute( public function testJsonColumn() { $qb = $this->getConnection()->getQueryBuilder(); - $columnSchemaBuilder = new Column(SchemaInterface::TYPE_JSON); + $columnSchemaBuilder = new Column(ColumnType::JSON); $this->assertSame( 'ALTER TABLE `json_table` ADD `json_col` json',