Skip to content

Commit

Permalink
Refactor ColumnDefinitionBuilder (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Nov 17, 2024
1 parent 154c05c commit 9e7ca32
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- Enh #319: Separate column type constants (@Tigrov)
- New #320: Realize `ColumnBuilder` class (@Tigrov)
- Enh #321: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #322: Add `ColumnDefinitionBuilder` class (@Tigrov)
- New #322, #330: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Enh #323: Refactor `Dsn` class (@Tigrov)
- Enh #324: Use constructor to create columns and initialize properties (@Tigrov)
- Enh #327: Refactor `Schema::findColumns()` method (@Tigrov)
Expand Down
24 changes: 17 additions & 7 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ public function build(ColumnSchemaInterface $column): string

protected function getDbType(ColumnSchemaInterface $column): string
{
$size = $column->getSize();

/** @psalm-suppress DocblockTypeContradiction */
return match ($column->getType()) {
$dbType = $column->getDbType() ?? match ($column->getType()) {
ColumnType::BOOLEAN => 'bit',
ColumnType::BIT => match (true) {
($size = $column->getSize()) === null => 'bigint',
$size === null => 'bigint',
$size === 1 => 'bit',
$size <= 8 => 'tinyint',
$size <= 16 => 'smallint',
Expand All @@ -72,18 +74,26 @@ protected function getDbType(ColumnSchemaInterface $column): string
ColumnType::DECIMAL => 'decimal',
ColumnType::MONEY => 'money',
ColumnType::CHAR => 'nchar',
ColumnType::STRING => 'nvarchar',
ColumnType::STRING => 'nvarchar(' . (($size ?? '255') ?: 'max') . ')',
ColumnType::TEXT => 'nvarchar(max)',
ColumnType::BINARY => 'varbinary(max)',
ColumnType::UUID => 'uniqueidentifier',
ColumnType::DATETIME => 'datetime2',
ColumnType::TIMESTAMP => 'datetime2',
ColumnType::DATE => 'date',
ColumnType::TIME => 'time',
ColumnType::ARRAY => 'json',
ColumnType::STRUCTURED => 'json',
ColumnType::JSON => 'json',
default => 'varchar',
ColumnType::ARRAY => 'nvarchar(max)',
ColumnType::STRUCTURED => 'nvarchar(max)',
ColumnType::JSON => 'nvarchar(max)',
default => 'nvarchar',
};

return match ($dbType) {
'timestamp' => 'datetime2',
'varchar' => 'varchar(' . ($size ?: 'max') . ')',
'nvarchar' => 'nvarchar(' . ($size ?: 'max') . ')',
'varbinary' => 'varbinary(' . ($size ?: 'max') . ')',
default => $dbType,
};
}
}
22 changes: 14 additions & 8 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Mssql\Column\ColumnBuilder;
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
Expand Down Expand Up @@ -349,7 +350,7 @@ public static function buildColumnDefinition(): array
$values[PseudoType::UBIGPK][0] = 'bigint IDENTITY PRIMARY KEY';
$values[PseudoType::UUID_PK][0] = 'uniqueidentifier PRIMARY KEY DEFAULT newid()';
$values[PseudoType::UUID_PK_SEQ][0] = 'uniqueidentifier PRIMARY KEY DEFAULT newsequentialid()';
$values['STRING'][0] = 'nvarchar';
$values['STRING'][0] = 'nvarchar(255)';
$values['STRING(100)'][0] = 'nvarchar(100)';
$values['primaryKey()'][0] = 'int IDENTITY PRIMARY KEY';
$values['primaryKey(false)'][0] = 'int PRIMARY KEY';
Expand All @@ -364,7 +365,7 @@ public static function buildColumnDefinition(): array
$values['bit()'][0] = 'bigint';
$values['bit(1)'][0] = 'bit';
$values['bit(8)'][0] = 'tinyint';
$values['bit(1000)'][0] = 'varbinary(125)';
$values['bit(64)'][0] = 'bigint';
$values['tinyint(2)'][0] = 'tinyint';
$values['smallint(4)'][0] = 'smallint';
$values['integer()'][0] = 'int';
Expand All @@ -381,7 +382,7 @@ public static function buildColumnDefinition(): array
$values['char(null)'][0] = 'nchar';
$values['string()'][0] = 'nvarchar(255)';
$values['string(100)'][0] = 'nvarchar(100)';
$values['string(null)'][0] = 'nvarchar';
$values['string(null)'][0] = 'nvarchar(255)';
$values['text()'][0] = 'nvarchar(max)';
$values['text(1000)'][0] = 'nvarchar(max)';
$values['binary()'][0] = 'varbinary(max)';
Expand All @@ -393,18 +394,23 @@ public static function buildColumnDefinition(): array
$values['timestamp(6)'][0] = 'datetime2(6)';
$values['timestamp(null)'][0] = 'datetime2';
$values['uuid()'][0] = 'uniqueidentifier';
$values["extra('bar')"][0] = 'nvarchar(255) bar';
$values['array()'][0] = 'nvarchar(max)';
$values['structured()'][0] = 'nvarchar(max)';
$values["structured('json')"] = ['varbinary(max)', ColumnBuilder::structured('varbinary(max)')];
$values['json()'][0] = 'nvarchar(max)';
$values['json(100)'][0] = 'nvarchar(max)';
$values["extra('NOT NULL')"][0] = 'nvarchar(255) NOT NULL';
$values["extra('')"][0] = 'nvarchar(255)';
$values["check('value > 5')"][0] = 'nvarchar(255) CHECK (value > 5)';
$values["check('')"][0] = 'nvarchar(255)';
$values['check(null)'][0] = 'nvarchar(255)';
$values["check('value > 5')"][0] = 'int CHECK ([col_59] > 5)';
$values["check('')"][0] = 'int';
$values['check(null)'][0] = 'int';
$values["comment('comment')"][0] = 'nvarchar(255)';
$values["comment('')"][0] = 'nvarchar(255)';
$values['comment(null)'][0] = 'nvarchar(255)';
$values["defaultValue('value')"][0] = "nvarchar(255) DEFAULT 'value'";
$values["defaultValue('')"][0] = "nvarchar(255) DEFAULT ''";
$values['defaultValue(null)'][0] = 'nvarchar(255) DEFAULT NULL';
$values['defaultValue($expression)'][0] = 'nvarchar(255) DEFAULT expression';
$values['defaultValue($expression)'][0] = 'int DEFAULT (1 + 2)';
$values['notNull()->defaultValue(null)'][0] = 'nvarchar(255) NOT NULL';
$values["integer()->defaultValue('')"][0] = 'int DEFAULT NULL';
$values['notNull()'][0] = 'nvarchar(255) NOT NULL';
Expand Down

0 comments on commit 9e7ca32

Please sign in to comment.