diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c6e305e..985306c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Enh #315: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov) - Enh #317: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) - Enh #316: Implement `ColumnFactory` class (@Tigrov) +- Enh #319: Separate column type constants (@Tigrov) ## 1.2.0 March 21, 2024 diff --git a/src/Column.php b/src/Column.php index efbedea1..1ce3b73e 100644 --- a/src/Column.php +++ b/src/Column.php @@ -16,7 +16,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 dee9e8a5..52d22dab 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -4,9 +4,9 @@ namespace Yiisoft\Db\Mssql\Column; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\AbstractColumnFactory; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; -use Yiisoft\Db\Schema\SchemaInterface; final class ColumnFactory extends AbstractColumnFactory { @@ -17,63 +17,63 @@ final class ColumnFactory extends AbstractColumnFactory */ private const TYPE_MAP = [ /** Exact numbers */ - 'bit' => SchemaInterface::TYPE_BOOLEAN, - 'tinyint' => SchemaInterface::TYPE_TINYINT, - 'smallint' => SchemaInterface::TYPE_SMALLINT, - 'int' => SchemaInterface::TYPE_INTEGER, - 'bigint' => SchemaInterface::TYPE_BIGINT, - 'numeric' => SchemaInterface::TYPE_DECIMAL, - 'decimal' => SchemaInterface::TYPE_DECIMAL, - 'smallmoney' => SchemaInterface::TYPE_MONEY, - 'money' => SchemaInterface::TYPE_MONEY, + 'bit' => ColumnType::BOOLEAN, + 'tinyint' => ColumnType::TINYINT, + 'smallint' => ColumnType::SMALLINT, + 'int' => ColumnType::INTEGER, + 'bigint' => ColumnType::BIGINT, + 'numeric' => ColumnType::DECIMAL, + 'decimal' => ColumnType::DECIMAL, + 'smallmoney' => ColumnType::MONEY, + 'money' => ColumnType::MONEY, /** Approximate numbers */ - 'float' => SchemaInterface::TYPE_FLOAT, - 'real' => SchemaInterface::TYPE_FLOAT, - 'double' => SchemaInterface::TYPE_DOUBLE, + 'float' => ColumnType::FLOAT, + 'real' => ColumnType::FLOAT, + 'double' => ColumnType::DOUBLE, /** Date and time */ - 'date' => SchemaInterface::TYPE_DATE, - 'time' => SchemaInterface::TYPE_TIME, - 'smalldatetime' => SchemaInterface::TYPE_DATETIME, - 'datetime' => SchemaInterface::TYPE_DATETIME, - 'datetime2' => SchemaInterface::TYPE_DATETIME, - 'datetimeoffset' => SchemaInterface::TYPE_DATETIME, + 'date' => ColumnType::DATE, + 'time' => ColumnType::TIME, + 'smalldatetime' => ColumnType::DATETIME, + 'datetime' => ColumnType::DATETIME, + 'datetime2' => ColumnType::DATETIME, + 'datetimeoffset' => ColumnType::DATETIME, /** Character strings */ - 'char' => SchemaInterface::TYPE_CHAR, - 'varchar' => SchemaInterface::TYPE_STRING, - 'text' => SchemaInterface::TYPE_TEXT, + 'char' => ColumnType::CHAR, + 'varchar' => ColumnType::STRING, + 'text' => ColumnType::TEXT, /** Unicode character strings */ - 'nchar' => SchemaInterface::TYPE_CHAR, - 'nvarchar' => SchemaInterface::TYPE_STRING, - 'ntext' => SchemaInterface::TYPE_TEXT, + 'nchar' => ColumnType::CHAR, + 'nvarchar' => ColumnType::STRING, + 'ntext' => ColumnType::TEXT, /** Binary strings */ - 'binary' => SchemaInterface::TYPE_BINARY, - 'varbinary' => SchemaInterface::TYPE_BINARY, - 'image' => SchemaInterface::TYPE_BINARY, + 'binary' => ColumnType::BINARY, + 'varbinary' => ColumnType::BINARY, + 'image' => ColumnType::BINARY, /** * Other data types 'cursor' type can't be used with tables */ - 'timestamp' => SchemaInterface::TYPE_TIMESTAMP, - 'hierarchyid' => SchemaInterface::TYPE_STRING, - 'uniqueidentifier' => SchemaInterface::TYPE_STRING, - 'sql_variant' => SchemaInterface::TYPE_STRING, - 'xml' => SchemaInterface::TYPE_STRING, - 'table' => SchemaInterface::TYPE_STRING, + 'timestamp' => ColumnType::TIMESTAMP, + 'hierarchyid' => ColumnType::STRING, + 'uniqueidentifier' => ColumnType::STRING, + 'sql_variant' => ColumnType::STRING, + 'xml' => ColumnType::STRING, + 'table' => ColumnType::STRING, ]; protected function getType(string $dbType, array $info = []): string { - return self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING; + return self::TYPE_MAP[$dbType] ?? ColumnType::STRING; } public function fromType(string $type, array $info = []): ColumnSchemaInterface { - if ($type === SchemaInterface::TYPE_BINARY) { + if ($type === ColumnType::BINARY) { return (new BinaryColumnSchema($type))->load($info); } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index bdde10f6..89bb9cbe 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Mssql; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder; use Yiisoft\Db\Schema\Builder\ColumnInterface; use Yiisoft\Db\Schema\QuoterInterface; @@ -20,29 +22,29 @@ final class QueryBuilder extends AbstractQueryBuilder * @psalm-var string[] $typeMap Mapping from abstract column types (keys) to physical column types (values). */ protected array $typeMap = [ - SchemaInterface::TYPE_PK => 'int IDENTITY PRIMARY KEY', - SchemaInterface::TYPE_UPK => 'int IDENTITY PRIMARY KEY', - SchemaInterface::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY', - SchemaInterface::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY', - SchemaInterface::TYPE_CHAR => 'nchar(1)', - SchemaInterface::TYPE_STRING => 'nvarchar(255)', - SchemaInterface::TYPE_TEXT => 'nvarchar(max)', - SchemaInterface::TYPE_TINYINT => 'tinyint', - SchemaInterface::TYPE_SMALLINT => 'smallint', - SchemaInterface::TYPE_INTEGER => 'int', - SchemaInterface::TYPE_BIGINT => 'bigint', - SchemaInterface::TYPE_FLOAT => 'float', - SchemaInterface::TYPE_DOUBLE => 'float', - SchemaInterface::TYPE_DECIMAL => 'decimal(18,0)', - SchemaInterface::TYPE_DATETIME => 'datetime', - SchemaInterface::TYPE_TIMESTAMP => 'datetime', - SchemaInterface::TYPE_TIME => 'time', - SchemaInterface::TYPE_DATE => 'date', - SchemaInterface::TYPE_BINARY => 'varbinary(max)', - SchemaInterface::TYPE_BOOLEAN => 'bit', - SchemaInterface::TYPE_MONEY => 'decimal(19,4)', - SchemaInterface::TYPE_UUID => 'UNIQUEIDENTIFIER', - SchemaInterface::TYPE_UUID_PK => 'UNIQUEIDENTIFIER PRIMARY KEY', + PseudoType::PK => 'int IDENTITY PRIMARY KEY', + PseudoType::UPK => 'int IDENTITY PRIMARY KEY', + PseudoType::BIGPK => 'bigint IDENTITY PRIMARY KEY', + PseudoType::UBIGPK => 'bigint IDENTITY PRIMARY KEY', + ColumnType::CHAR => 'nchar(1)', + ColumnType::STRING => 'nvarchar(255)', + ColumnType::TEXT => 'nvarchar(max)', + ColumnType::TINYINT => 'tinyint', + ColumnType::SMALLINT => 'smallint', + ColumnType::INTEGER => 'int', + ColumnType::BIGINT => 'bigint', + ColumnType::FLOAT => 'float', + ColumnType::DOUBLE => 'float', + ColumnType::DECIMAL => 'decimal(18,0)', + ColumnType::DATETIME => 'datetime', + ColumnType::TIMESTAMP => 'datetime', + ColumnType::TIME => 'time', + ColumnType::DATE => 'date', + ColumnType::BINARY => 'varbinary(max)', + ColumnType::BOOLEAN => 'bit', + ColumnType::MONEY => 'decimal(19,4)', + ColumnType::UUID => 'UNIQUEIDENTIFIER', + PseudoType::UUID_PK => 'UNIQUEIDENTIFIER PRIMARY KEY', ]; public function __construct(QuoterInterface $quoter, SchemaInterface $schema) diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index 1b115a64..5f0d8350 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Mssql\Tests\Provider; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Mssql\Column\BinaryColumnSchema; use Yiisoft\Db\Schema\Column\BooleanColumnSchema; use Yiisoft\Db\Schema\Column\DoubleColumnSchema; @@ -16,39 +17,39 @@ public static function dbTypes(): array { return [ // db type, expected abstract type, expected instance of - ['bit', 'boolean', BooleanColumnSchema::class], - ['tinyint', 'tinyint', IntegerColumnSchema::class], - ['smallint', 'smallint', IntegerColumnSchema::class], - ['int', 'integer', IntegerColumnSchema::class], - ['bigint', 'bigint', IntegerColumnSchema::class], - ['numeric', 'decimal', DoubleColumnSchema::class], - ['decimal', 'decimal', DoubleColumnSchema::class], - ['float', 'float', DoubleColumnSchema::class], - ['real', 'float', DoubleColumnSchema::class], - ['double', 'double', DoubleColumnSchema::class], - ['smallmoney', 'money', StringColumnSchema::class], - ['money', 'money', StringColumnSchema::class], - ['date', 'date', StringColumnSchema::class], - ['time', 'time', StringColumnSchema::class], - ['smalldatetime', 'datetime', StringColumnSchema::class], - ['datetime', 'datetime', StringColumnSchema::class], - ['datetime2', 'datetime', StringColumnSchema::class], - ['datetimeoffset', 'datetime', StringColumnSchema::class], - ['char', 'char', StringColumnSchema::class], - ['varchar', 'string', StringColumnSchema::class], - ['text', 'text', StringColumnSchema::class], - ['nchar', 'char', StringColumnSchema::class], - ['nvarchar', 'string', StringColumnSchema::class], - ['ntext', 'text', StringColumnSchema::class], - ['binary', 'binary', BinaryColumnSchema::class], - ['varbinary', 'binary', BinaryColumnSchema::class], - ['image', 'binary', BinaryColumnSchema::class], - ['timestamp', 'timestamp', StringColumnSchema::class], - ['hierarchyid', 'string', StringColumnSchema::class], - ['uniqueidentifier', 'string', StringColumnSchema::class], - ['sql_variant', 'string', StringColumnSchema::class], - ['xml', 'string', StringColumnSchema::class], - ['table', 'string', StringColumnSchema::class], + ['bit', ColumnType::BOOLEAN, BooleanColumnSchema::class], + ['tinyint', ColumnType::TINYINT, IntegerColumnSchema::class], + ['smallint', ColumnType::SMALLINT, IntegerColumnSchema::class], + ['int', ColumnType::INTEGER, IntegerColumnSchema::class], + ['bigint', ColumnType::BIGINT, IntegerColumnSchema::class], + ['numeric', ColumnType::DECIMAL, DoubleColumnSchema::class], + ['decimal', ColumnType::DECIMAL, DoubleColumnSchema::class], + ['float', ColumnType::FLOAT, DoubleColumnSchema::class], + ['real', ColumnType::FLOAT, DoubleColumnSchema::class], + ['double', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['smallmoney', ColumnType::MONEY, StringColumnSchema::class], + ['money', ColumnType::MONEY, StringColumnSchema::class], + ['date', ColumnType::DATE, StringColumnSchema::class], + ['time', ColumnType::TIME, StringColumnSchema::class], + ['smalldatetime', ColumnType::DATETIME, StringColumnSchema::class], + ['datetime', ColumnType::DATETIME, StringColumnSchema::class], + ['datetime2', ColumnType::DATETIME, StringColumnSchema::class], + ['datetimeoffset', ColumnType::DATETIME, StringColumnSchema::class], + ['char', ColumnType::CHAR, StringColumnSchema::class], + ['varchar', ColumnType::STRING, StringColumnSchema::class], + ['text', ColumnType::TEXT, StringColumnSchema::class], + ['nchar', ColumnType::CHAR, StringColumnSchema::class], + ['nvarchar', ColumnType::STRING, StringColumnSchema::class], + ['ntext', ColumnType::TEXT, StringColumnSchema::class], + ['binary', ColumnType::BINARY, BinaryColumnSchema::class], + ['varbinary', ColumnType::BINARY, BinaryColumnSchema::class], + ['image', ColumnType::BINARY, BinaryColumnSchema::class], + ['timestamp', ColumnType::TIMESTAMP, StringColumnSchema::class], + ['hierarchyid', ColumnType::STRING, StringColumnSchema::class], + ['uniqueidentifier', ColumnType::STRING, StringColumnSchema::class], + ['sql_variant', ColumnType::STRING, StringColumnSchema::class], + ['xml', ColumnType::STRING, StringColumnSchema::class], + ['table', ColumnType::STRING, StringColumnSchema::class], ]; } } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 626f59b8..ea8e9f97 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -6,6 +6,7 @@ use JsonException; use Throwable; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\IntegrityException; use Yiisoft\Db\Exception\InvalidArgumentException; @@ -17,7 +18,6 @@ use Yiisoft\Db\Mssql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest; use function json_encode; @@ -668,7 +668,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 255))->notNull() + (new Column(ColumnType::STRING, 255))->notNull() ); $this->assertEquals($expected, $sql); @@ -700,7 +700,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 255))->check('LEN(bar) > 5') + (new Column(ColumnType::STRING, 255))->check('LEN(bar) > 5') ); $this->assertEquals($expected, $sql); @@ -732,7 +732,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 255))->defaultValue('') + (new Column(ColumnType::STRING, 255))->defaultValue('') ); $this->assertEquals($expected, $sql); @@ -764,7 +764,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 255))->defaultValue('AbCdE') + (new Column(ColumnType::STRING, 255))->defaultValue('AbCdE') ); $this->assertEquals($expected, $sql); @@ -796,7 +796,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_TIMESTAMP))->defaultExpression('CURRENT_TIMESTAMP') + (new Column(ColumnType::TIMESTAMP))->defaultExpression('CURRENT_TIMESTAMP') ); $this->assertEquals($expected, $sql); @@ -828,7 +828,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 30))->unique() + (new Column(ColumnType::STRING, 30))->unique() ); $this->assertEquals($expected, $sql); } @@ -852,7 +852,7 @@ public function testAlterColumnOnDb(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 128))->notNull() + (new Column(ColumnType::STRING, 128))->notNull() ); $db->createCommand($sql)->execute(); $schema = $db->getTableSchema('[foo1]', true); @@ -863,11 +863,11 @@ public function testAlterColumnOnDb(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_TIMESTAMP))->defaultExpression('CURRENT_TIMESTAMP') + (new Column(ColumnType::TIMESTAMP))->defaultExpression('CURRENT_TIMESTAMP') ); $db->createCommand($sql)->execute(); $schema = $db->getTableSchema('[foo1]', true); - $this->assertSame(SchemaInterface::TYPE_DATETIME, $schema?->getColumn('bar')->getDbType()); + $this->assertSame(ColumnType::DATETIME, $schema?->getColumn('bar')->getDbType()); $this->assertSame('getdate()', $schema?->getColumn('bar')->getDefaultValue()); } @@ -883,7 +883,7 @@ public function testAlterColumnWithCheckConstraintOnDb(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 128))->null()->check('LEN(bar) > 5') + (new Column(ColumnType::STRING, 128))->null()->check('LEN(bar) > 5') ); $db->createCommand($sql)->execute(); $schema = $db->getTableSchema('[foo1]', true); @@ -906,7 +906,7 @@ public function testAlterColumnWithCheckConstraintOnDbWithException(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 64))->check('LEN(bar) > 5') + (new Column(ColumnType::STRING, 64))->check('LEN(bar) > 5') ); $db->createCommand($sql)->execute(); @@ -927,7 +927,7 @@ public function testAlterColumnWithUniqueConstraintOnDbWithException(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 64))->unique() + (new Column(ColumnType::STRING, 64))->unique() ); $db->createCommand($sql)->execute(); @@ -1011,7 +1011,7 @@ public function testDropColumnOnDb(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 64)) + (new Column(ColumnType::STRING, 64)) ->defaultValue('') ->check('LEN(bar) < 5') ->unique() @@ -1061,7 +1061,7 @@ public function testAlterColumnWithNull(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_INTEGER))->null()->defaultValue(null) + (new Column(ColumnType::INTEGER))->null()->defaultValue(null) ); $this->assertEquals($expected, $sql); @@ -1093,7 +1093,7 @@ public function testAlterColumnWithNull(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_INTEGER))->defaultValue(null) + (new Column(ColumnType::INTEGER))->defaultValue(null) ); $this->assertEquals($expected, $sql); } @@ -1134,7 +1134,7 @@ public function testAlterColumnWithExpression(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_INTEGER)) + (new Column(ColumnType::INTEGER)) ->null() ->defaultValue(new Expression('CAST(GETDATE() AS INT)')) );