diff --git a/CHANGELOG.md b/CHANGELOG.md index e413b33..e057607 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,9 @@ - Enh #277: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) - New #276, #288: Implement `ColumnFactory` class (@Tigrov) - Enh #279: Separate column type constants (@Tigrov) -- New #280: Realize `ColumnBuilder` class (@Tigrov) +- New #280, #291: Realize `ColumnBuilder` class (@Tigrov) - Enh #281: Update according changes in `ColumnSchemaInterface` (@Tigrov) -- New #282: Add `ColumnDefinitionBuilder` class (@Tigrov) +- New #282, #291: Add `ColumnDefinitionBuilder` class (@Tigrov) - Bug #285: Fix `DMLQueryBuilder::insertBatch()` method (@Tigrov) - Enh #283: Refactor `Dsn` class (@Tigrov) - Enh #286: Use constructor to create columns and initialize properties (@Tigrov) diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php index 41e9457..0e5f26b 100644 --- a/src/Column/ColumnBuilder.php +++ b/src/Column/ColumnBuilder.php @@ -9,26 +9,6 @@ final class ColumnBuilder extends \Yiisoft\Db\Schema\Column\ColumnBuilder { - public static function tinyint(int|null $size = 3): ColumnSchemaInterface - { - return parent::tinyint($size); - } - - public static function smallint(int|null $size = 5): ColumnSchemaInterface - { - return parent::smallint($size); - } - - public static function integer(int|null $size = 10): ColumnSchemaInterface - { - return parent::integer($size); - } - - public static function bigint(int|null $size = 20): ColumnSchemaInterface - { - return parent::bigint($size); - } - public static function binary(int|null $size = null): ColumnSchemaInterface { return new BinaryColumnSchema(ColumnType::BINARY, size: $size); diff --git a/src/Column/ColumnDefinitionBuilder.php b/src/Column/ColumnDefinitionBuilder.php index baad503..10c5eb1 100644 --- a/src/Column/ColumnDefinitionBuilder.php +++ b/src/Column/ColumnDefinitionBuilder.php @@ -19,6 +19,10 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder protected const GENERATE_UUID_EXPRESSION = 'sys_guid()'; protected const TYPES_WITH_SIZE = [ + 'char', + 'nchar', + 'character', + 'varchar', 'varchar2', 'nvarchar2', 'number', @@ -27,8 +31,6 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder 'interval day(0) to second', 'raw', 'urowid', - 'char', - 'nchar', ]; protected const TYPES_WITH_SCALE = [ @@ -67,7 +69,7 @@ protected function getDbType(ColumnSchemaInterface $column): string $size = $column->getSize(); /** @psalm-suppress DocblockTypeContradiction */ - return match ($column->getType()) { + return $column->getDbType() ?? match ($column->getType()) { ColumnType::BOOLEAN => 'number(1)', ColumnType::BIT => match (true) { $size === null => 'number(38)', @@ -83,7 +85,7 @@ protected function getDbType(ColumnSchemaInterface $column): string ColumnType::DECIMAL => 'number(' . ($size ?? 10) . ',' . ($column->getScale() ?? 0) . ')', ColumnType::MONEY => 'number(' . ($size ?? 19) . ',' . ($column->getScale() ?? 4) . ')', ColumnType::CHAR => 'char', - ColumnType::STRING => 'varchar2', + ColumnType::STRING => 'varchar2(' . ($size ?? 255) . ')', ColumnType::TEXT => 'clob', ColumnType::BINARY => 'blob', ColumnType::UUID => 'raw(16)', @@ -91,9 +93,9 @@ protected function getDbType(ColumnSchemaInterface $column): string ColumnType::TIMESTAMP => 'timestamp', ColumnType::DATE => 'date', ColumnType::TIME => 'interval day(0) to second', - ColumnType::ARRAY => 'json', - ColumnType::STRUCTURED => 'json', - ColumnType::JSON => 'json', + ColumnType::ARRAY => 'clob', + ColumnType::STRUCTURED => 'clob', + ColumnType::JSON => 'clob', default => 'varchar2', }; } diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index c3d9c2b..1411493 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -25,6 +25,8 @@ final class ColumnFactory extends AbstractColumnFactory protected const TYPE_MAP = [ 'char' => ColumnType::CHAR, 'nchar' => ColumnType::CHAR, + 'character' => ColumnType::CHAR, + 'varchar' => ColumnType::STRING, 'varchar2' => ColumnType::STRING, 'nvarchar2' => ColumnType::STRING, 'clob' => ColumnType::TEXT, diff --git a/tests/Provider/ColumnBuilderProvider.php b/tests/Provider/ColumnBuilderProvider.php index 41bbb5e..a846cde 100644 --- a/tests/Provider/ColumnBuilderProvider.php +++ b/tests/Provider/ColumnBuilderProvider.php @@ -12,16 +12,6 @@ public static function buildingMethods(): array { $values = parent::buildingMethods(); - $values['primaryKey()'][4]['getSize'] = 10; - $values['primaryKey(false)'][4]['getSize'] = 10; - $values['smallPrimaryKey()'][4]['getSize'] = 5; - $values['smallPrimaryKey(false)'][4]['getSize'] = 5; - $values['bigPrimaryKey()'][4]['getSize'] = 20; - $values['bigPrimaryKey(false)'][4]['getSize'] = 20; - $values['tinyint()'][4]['getSize'] = 3; - $values['smallint()'][4]['getSize'] = 5; - $values['integer()'][4]['getSize'] = 10; - $values['bigint()'][4]['getSize'] = 20; $values['binary()'][2] = BinaryColumnSchema::class; $values['binary(8)'][2] = BinaryColumnSchema::class; diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 5f8b8ec..95da6dc 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -248,7 +248,7 @@ public static function buildColumnDefinition(): array $values[PseudoType::UBIGPK][0] = 'number(20) GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY'; $values[PseudoType::UUID_PK][0] = 'raw(16) DEFAULT sys_guid() PRIMARY KEY'; $values[PseudoType::UUID_PK_SEQ][0] = 'raw(16) DEFAULT sys_guid() PRIMARY KEY'; - $values['STRING'][0] = 'varchar2'; + $values['STRING'][0] = 'varchar2(255)'; $values['STRING(100)'][0] = 'varchar2(100)'; $values['primaryKey()'][0] = 'number(10) GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY'; $values['primaryKey(false)'][0] = 'number(10) PRIMARY KEY'; @@ -263,7 +263,7 @@ public static function buildColumnDefinition(): array $values['bit()'][0] = 'number(38)'; $values['bit(1)'][0] = 'number(1)'; $values['bit(8)'][0] = 'number(3)'; - $values['bit(1000)'][0] = 'raw(125)'; + $values['bit(64)'][0] = 'number(20)'; $values['tinyint()'][0] = 'number(3)'; $values['tinyint(2)'][0] = 'number(2)'; $values['smallint()'][0] = 'number(5)'; @@ -288,7 +288,7 @@ public static function buildColumnDefinition(): array $values['money(null)'][0] = 'number(19,4)'; $values['string()'][0] = 'varchar2(255)'; $values['string(100)'][0] = 'varchar2(100)'; - $values['string(null)'][0] = 'varchar2'; + $values['string(null)'][0] = 'varchar2(255)'; $values['text()'][0] = 'clob'; $values['text(1000)'][0] = 'clob'; $values['binary()'][0] = 'blob'; @@ -300,18 +300,23 @@ public static function buildColumnDefinition(): array $values['time()'][0] = 'interval day(0) to second(0)'; $values['time(6)'][0] = 'interval day(0) to second(6)'; $values['time(null)'][0] = 'interval day(0) to second'; + $values['array()'][0] = 'clob'; + $values['structured()'][0] = 'clob'; + $values["structured('json')"] = ['blob', ColumnBuilder::structured('blob')]; + $values['json()'][0] = 'clob'; + $values['json(100)'][0] = 'clob'; + $values["extra('NOT NULL')"][0] = 'varchar2(255) NOT NULL'; + $values["extra('')"][0] = 'varchar2(255)'; + $values["check('value > 5')"][0] = 'number(10) CHECK ("col_59" > 5)'; + $values["check('')"][0] = 'number(10)'; + $values['check(null)'][0] = 'number(10)'; $values["comment('comment')"][0] = 'varchar2(255)'; $values["comment('')"][0] = 'varchar2(255)'; $values['comment(null)'][0] = 'varchar2(255)'; - $values["extra('bar')"][0] = 'varchar2(255) bar'; - $values["extra('')"][0] = 'varchar2(255)'; - $values["check('value > 5')"][0] = 'varchar2(255) CHECK (value > 5)'; - $values["check('')"][0] = 'varchar2(255)'; - $values['check(null)'][0] = 'varchar2(255)'; $values["defaultValue('value')"][0] = "varchar2(255) DEFAULT 'value'"; $values["defaultValue('')"][0] = "varchar2(255) DEFAULT ''"; $values['defaultValue(null)'][0] = 'varchar2(255) DEFAULT NULL'; - $values['defaultValue($expression)'][0] = 'varchar2(255) DEFAULT expression'; + $values['defaultValue($expression)'][0] = 'number(10) DEFAULT (1 + 2)'; $values['notNull()->defaultValue(null)'][0] = 'varchar2(255) NOT NULL'; $values['notNull()'][0] = 'varchar2(255) NOT NULL'; $values['null()'][0] = 'varchar2(255) NULL';