diff --git a/CHANGELOG.md b/CHANGELOG.md index d521dcb..c1281fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Enh #275: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov) - Enh #277: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) - Enh #276: Implement `ColumnFactory` class (@Tigrov) +- Enh #279: Separate column type constants (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/Column.php b/src/Column.php index 62ec349..51efdc1 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 3adc1b2..bfdea88 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -4,9 +4,9 @@ namespace Yiisoft\Db\Oracle\Column; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\AbstractColumnFactory; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; -use Yiisoft\Db\Schema\SchemaInterface; use function preg_replace; use function strtolower; @@ -23,28 +23,28 @@ final class ColumnFactory extends AbstractColumnFactory * @psalm-suppress MissingClassConstType */ private const TYPE_MAP = [ - 'char' => SchemaInterface::TYPE_CHAR, - 'nchar' => SchemaInterface::TYPE_CHAR, - 'varchar2' => SchemaInterface::TYPE_STRING, - 'nvarchar2' => SchemaInterface::TYPE_STRING, - 'clob' => SchemaInterface::TYPE_TEXT, - 'nclob' => SchemaInterface::TYPE_TEXT, - 'blob' => SchemaInterface::TYPE_BINARY, - 'bfile' => SchemaInterface::TYPE_BINARY, - 'long raw' => SchemaInterface::TYPE_BINARY, - 'raw' => SchemaInterface::TYPE_BINARY, - 'number' => SchemaInterface::TYPE_DECIMAL, - 'binary_float' => SchemaInterface::TYPE_FLOAT, // 32 bit - 'binary_double' => SchemaInterface::TYPE_DOUBLE, // 64 bit - 'float' => SchemaInterface::TYPE_DOUBLE, // 126 bit - 'date' => SchemaInterface::TYPE_DATE, - 'interval day to second' => SchemaInterface::TYPE_TIME, - 'timestamp' => SchemaInterface::TYPE_TIMESTAMP, - 'timestamp with time zone' => SchemaInterface::TYPE_TIMESTAMP, - 'timestamp with local time zone' => SchemaInterface::TYPE_TIMESTAMP, + 'char' => ColumnType::CHAR, + 'nchar' => ColumnType::CHAR, + 'varchar2' => ColumnType::STRING, + 'nvarchar2' => ColumnType::STRING, + 'clob' => ColumnType::TEXT, + 'nclob' => ColumnType::TEXT, + 'blob' => ColumnType::BINARY, + 'bfile' => ColumnType::BINARY, + 'long raw' => ColumnType::BINARY, + 'raw' => ColumnType::BINARY, + 'number' => ColumnType::DECIMAL, + 'binary_float' => ColumnType::FLOAT, // 32 bit + 'binary_double' => ColumnType::DOUBLE, // 64 bit + 'float' => ColumnType::DOUBLE, // 126 bit + 'date' => ColumnType::DATE, + 'interval day to second' => ColumnType::TIME, + 'timestamp' => ColumnType::TIMESTAMP, + 'timestamp with time zone' => ColumnType::TIMESTAMP, + 'timestamp with local time zone' => ColumnType::TIMESTAMP, /** Deprecated */ - 'long' => SchemaInterface::TYPE_TEXT, + 'long' => ColumnType::TEXT, ]; protected function getType(string $dbType, array $info = []): string @@ -55,24 +55,24 @@ protected function getType(string $dbType, array $info = []): string $scale = isset($info['scale']) ? (int) $info['scale'] : null; return match ($scale) { - null => SchemaInterface::TYPE_DOUBLE, - 0 => SchemaInterface::TYPE_INTEGER, - default => SchemaInterface::TYPE_DECIMAL, + null => ColumnType::DOUBLE, + 0 => ColumnType::INTEGER, + default => ColumnType::DECIMAL, }; } $dbType = preg_replace('/\([^)]+\)/', '', $dbType); if ($dbType === 'interval day to second' && isset($info['precision']) && $info['precision'] > 0) { - return SchemaInterface::TYPE_STRING; + return ColumnType::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 d396a16..f82d8ea 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Oracle; +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,29 +19,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 => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY', - SchemaInterface::TYPE_UPK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY', - SchemaInterface::TYPE_BIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY', - SchemaInterface::TYPE_UBIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY', - SchemaInterface::TYPE_CHAR => 'CHAR(1)', - SchemaInterface::TYPE_STRING => 'VARCHAR2(255)', - SchemaInterface::TYPE_TEXT => 'CLOB', - SchemaInterface::TYPE_TINYINT => 'NUMBER(3)', - SchemaInterface::TYPE_SMALLINT => 'NUMBER(5)', - SchemaInterface::TYPE_INTEGER => 'NUMBER(10)', - SchemaInterface::TYPE_BIGINT => 'NUMBER(20)', - SchemaInterface::TYPE_FLOAT => 'BINARY_FLOAT', - SchemaInterface::TYPE_DOUBLE => 'BINARY_DOUBLE', - SchemaInterface::TYPE_DECIMAL => 'NUMBER(10,0)', - SchemaInterface::TYPE_DATETIME => 'TIMESTAMP(0)', - SchemaInterface::TYPE_TIMESTAMP => 'TIMESTAMP(0)', - SchemaInterface::TYPE_TIME => 'INTERVAL DAY(0) TO SECOND(0)', - SchemaInterface::TYPE_DATE => 'DATE', - SchemaInterface::TYPE_BINARY => 'BLOB', - SchemaInterface::TYPE_BOOLEAN => 'NUMBER(1)', - SchemaInterface::TYPE_MONEY => 'NUMBER(19,4)', - SchemaInterface::TYPE_UUID => 'RAW(16)', - SchemaInterface::TYPE_UUID_PK => 'RAW(16) DEFAULT SYS_GUID() PRIMARY KEY', + PseudoType::PK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY', + PseudoType::UPK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY', + PseudoType::BIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY', + PseudoType::UBIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY', + ColumnType::CHAR => 'CHAR(1)', + ColumnType::STRING => 'VARCHAR2(255)', + ColumnType::TEXT => 'CLOB', + ColumnType::TINYINT => 'NUMBER(3)', + ColumnType::SMALLINT => 'NUMBER(5)', + ColumnType::INTEGER => 'NUMBER(10)', + ColumnType::BIGINT => 'NUMBER(20)', + ColumnType::FLOAT => 'BINARY_FLOAT', + ColumnType::DOUBLE => 'BINARY_DOUBLE', + ColumnType::DECIMAL => 'NUMBER(10,0)', + ColumnType::DATETIME => 'TIMESTAMP(0)', + ColumnType::TIMESTAMP => 'TIMESTAMP(0)', + ColumnType::TIME => 'INTERVAL DAY(0) TO SECOND(0)', + ColumnType::DATE => 'DATE', + ColumnType::BINARY => 'BLOB', + ColumnType::BOOLEAN => 'NUMBER(1)', + ColumnType::MONEY => 'NUMBER(19,4)', + ColumnType::UUID => 'RAW(16)', + PseudoType::UUID_PK => 'RAW(16) DEFAULT SYS_GUID() PRIMARY KEY', ]; public function __construct(QuoterInterface $quoter, SchemaInterface $schema) diff --git a/src/Schema.php b/src/Schema.php index a5d76d0..d8b712f 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -7,6 +7,7 @@ use Throwable; use Yiisoft\Db\Cache\SchemaCache; use Yiisoft\Db\Connection\ConnectionInterface; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constraint\CheckConstraint; use Yiisoft\Db\Constraint\Constraint; use Yiisoft\Db\Constraint\ForeignKeyConstraint; @@ -467,7 +468,7 @@ private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaIn return null; } - if ($column->getType() === self::TYPE_TIMESTAMP && $defaultValue === 'CURRENT_TIMESTAMP') { + if ($column->getType() === ColumnType::TIMESTAMP && $defaultValue === 'CURRENT_TIMESTAMP') { return new Expression($defaultValue); } diff --git a/tests/CommandTest.php b/tests/CommandTest.php index e48793a..8bdc7b1 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -6,6 +6,8 @@ use ReflectionException; use Throwable; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; @@ -16,7 +18,6 @@ use Yiisoft\Db\Oracle\Driver; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Tests\Common\CommonCommandTest; use Yiisoft\Db\Tests\Support\Assert; use Yiisoft\Db\Tests\Support\DbHelper; @@ -89,7 +90,7 @@ public function testCLOBStringInsertion(): void $command->dropTable('longstring')->execute(); } - $command->createTable('longstring', ['message' => SchemaInterface::TYPE_TEXT])->execute(); + $command->createTable('longstring', ['message' => ColumnType::TEXT])->execute(); $longData = str_pad('-', 4001, '-=', STR_PAD_LEFT); $command->insert('longstring', ['message' => $longData])->execute(); @@ -130,7 +131,7 @@ public function testCreateTable(): void $command->createTable( '{{testCreateTable}}', - ['id' => SchemaInterface::TYPE_PK, 'bar' => SchemaInterface::TYPE_INTEGER] + ['id' => PseudoType::PK, 'bar' => ColumnType::INTEGER] )->execute(); $command->setSql( <<createTable( '{{testCreateViewTable}}', [ - '[[id]]' => SchemaInterface::TYPE_PK, - '[[bar]]' => SchemaInterface::TYPE_INTEGER, + '[[id]]' => PseudoType::PK, + '[[bar]]' => ColumnType::INTEGER, ], )->execute(); $command->setSql( diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index 8c99a37..f371332 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Oracle\Tests\Provider; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Oracle\Column\BinaryColumnSchema; use Yiisoft\Db\Schema\Column\DoubleColumnSchema; use Yiisoft\Db\Schema\Column\StringColumnSchema; @@ -14,26 +15,26 @@ public static function dbTypes(): array { return [ // db type, expected abstract type, expected instance of - ['char', 'char', StringColumnSchema::class], - ['nchar', 'char', StringColumnSchema::class], - ['varchar2', 'string', StringColumnSchema::class], - ['nvarchar2', 'string', StringColumnSchema::class], - ['clob', 'text', StringColumnSchema::class], - ['nclob', 'text', StringColumnSchema::class], - ['long', 'text', StringColumnSchema::class], - ['blob', 'binary', BinaryColumnSchema::class], - ['bfile', 'binary', BinaryColumnSchema::class], - ['long raw', 'binary', BinaryColumnSchema::class], - ['raw', 'binary', BinaryColumnSchema::class], - ['number', 'double', DoubleColumnSchema::class], - ['binary_float', 'float', DoubleColumnSchema::class], - ['binary_double', 'double', DoubleColumnSchema::class], - ['float', 'double', DoubleColumnSchema::class], - ['date', 'date', StringColumnSchema::class], - ['interval day(0) to second', 'time', StringColumnSchema::class], - ['timestamp', 'timestamp', StringColumnSchema::class], - ['timestamp with time zone', 'timestamp', StringColumnSchema::class], - ['timestamp with local time zone', 'timestamp', StringColumnSchema::class], + ['char', ColumnType::CHAR, StringColumnSchema::class], + ['nchar', ColumnType::CHAR, StringColumnSchema::class], + ['varchar2', ColumnType::STRING, StringColumnSchema::class], + ['nvarchar2', ColumnType::STRING, StringColumnSchema::class], + ['clob', ColumnType::TEXT, StringColumnSchema::class], + ['nclob', ColumnType::TEXT, StringColumnSchema::class], + ['long', ColumnType::TEXT, StringColumnSchema::class], + ['blob', ColumnType::BINARY, BinaryColumnSchema::class], + ['bfile', ColumnType::BINARY, BinaryColumnSchema::class], + ['long raw', ColumnType::BINARY, BinaryColumnSchema::class], + ['raw', ColumnType::BINARY, BinaryColumnSchema::class], + ['number', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['binary_float', ColumnType::FLOAT, DoubleColumnSchema::class], + ['binary_double', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['float', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['date', ColumnType::DATE, StringColumnSchema::class], + ['interval day(0) to second', ColumnType::TIME, StringColumnSchema::class], + ['timestamp', ColumnType::TIMESTAMP, StringColumnSchema::class], + ['timestamp with time zone', ColumnType::TIMESTAMP, StringColumnSchema::class], + ['timestamp with local time zone', ColumnType::TIMESTAMP, StringColumnSchema::class], ]; } diff --git a/tests/Provider/ColumnSchemaBuilderProvider.php b/tests/Provider/ColumnSchemaBuilderProvider.php index 5badb0e..9c065cf 100644 --- a/tests/Provider/ColumnSchemaBuilderProvider.php +++ b/tests/Provider/ColumnSchemaBuilderProvider.php @@ -4,7 +4,7 @@ namespace Yiisoft\Db\Oracle\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 e973c64..bec761e 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Oracle\Tests; use Throwable; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; @@ -13,7 +14,6 @@ use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest; /** @@ -110,7 +110,7 @@ public function testAlterColumn(): void <<alterColumn('customer', 'email', SchemaInterface::TYPE_STRING), + $qb->alterColumn('customer', 'email', ColumnType::STRING), ); $db->close();