Skip to content

Commit

Permalink
Refactor ColumnDefinitionBuilder (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Nov 17, 2024
1 parent 9110d15 commit 66bcb27
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 48 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 0 additions & 20 deletions src/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 9 additions & 7 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -27,8 +31,6 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
'interval day(0) to second',
'raw',
'urowid',
'char',
'nchar',
];

protected const TYPES_WITH_SCALE = [
Expand Down Expand Up @@ -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)',
Expand All @@ -83,17 +85,17 @@ 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)',
ColumnType::DATETIME => 'timestamp',
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',
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 0 additions & 10 deletions tests/Provider/ColumnBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
23 changes: 14 additions & 9 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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)';
Expand All @@ -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';
Expand All @@ -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';
Expand Down

0 comments on commit 66bcb27

Please sign in to comment.