Skip to content

Commit

Permalink
Refactor ColumnFactory (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Nov 8, 2024
1 parent 953a1c5 commit edbecbc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 47 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- New #310: Add JSON overlaps condition builder (@Tigrov)
- Enh #312: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov)
- Enh #315: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
- New #314: Implement `ColumnFactory` class (@Tigrov)
- New #314, #325: Implement `ColumnFactory` class (@Tigrov)
- Enh #317: Separate column type constants (@Tigrov)
- New #318: Realize `ColumnBuilder` class (@Tigrov)
- Enh #320: Update according changes in `ColumnSchemaInterface` (@Tigrov)
Expand Down
27 changes: 8 additions & 19 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ final class ColumnFactory extends AbstractColumnFactory
* Mapping from physical column types (keys) to abstract column types (values).
*
* @var string[]
*
* @psalm-suppress MissingClassConstType
* @psalm-var array<string, ColumnType::*>
*/
private const TYPE_MAP = [
protected const TYPE_MAP = [
'bool' => ColumnType::BOOLEAN,
'boolean' => ColumnType::BOOLEAN,
'bit' => ColumnType::BIT,
Expand Down Expand Up @@ -49,21 +48,11 @@ final class ColumnFactory extends AbstractColumnFactory

protected function getType(string $dbType, array $info = []): string
{
$type = self::TYPE_MAP[$dbType] ?? ColumnType::STRING;

if (
($type === ColumnType::BIT || $type === ColumnType::TINYINT)
&& isset($info['size'])
&& $info['size'] === 1
) {
return ColumnType::BOOLEAN;
}

return $type;
}

protected function isDbType(string $dbType): bool
{
return isset(self::TYPE_MAP[$dbType]);
return match ($dbType) {
'bit', 'tinyint' => isset($info['size']) && $info['size'] === 1
? ColumnType::BOOLEAN
: parent::getType($dbType, $info),
default => parent::getType($dbType, $info),
};
}
}
25 changes: 14 additions & 11 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use function preg_replace;
use function serialize;
use function strncasecmp;
use function strtolower;

/**
* Implements the SQLite Server specific schema, supporting SQLite 3.3.0 or higher.
Expand Down Expand Up @@ -72,6 +71,8 @@
* pk:string,
* size?: int,
* scale?: int,
* schema: string|null,
* table: string
* }
*/
final class Schema extends AbstractPdoSchema
Expand Down Expand Up @@ -342,6 +343,9 @@ protected function findColumns(TableSchemaInterface $table): bool
$info['type'] = ColumnType::JSON;
}

$info['schema'] = $table->getSchemaName();
$info['table'] = $table->getName();

$column = $this->loadColumnSchema($info);
$table->column($info['name'], $column);

Expand Down Expand Up @@ -446,16 +450,15 @@ public function getSchemaDefaultValues(string $schema = '', bool $refresh = fals
*/
private function loadColumnSchema(array $info): ColumnSchemaInterface
{
$columnFactory = $this->getColumnFactory();

$dbType = strtolower($info['type']);
$column = $columnFactory->fromDefinition($dbType, ['name' => $info['name']]);
$column->dbType($dbType);
$column->notNull((bool) $info['notnull']);
$column->primaryKey((bool) $info['pk']);
$column->defaultValue($this->normalizeDefaultValue($info['dflt_value'], $column));

return $column;
$column = $this->getColumnFactory()->fromDefinition($info['type'], [
'name' => $info['name'],
'notNull' => (bool) $info['notnull'],
'primaryKey' => (bool) $info['pk'],
'schema' => $info['schema'],
'table' => $info['table'],
]);

return $column->defaultValue($this->normalizeDefaultValue($info['dflt_value'], $column));
}

/**
Expand Down
32 changes: 16 additions & 16 deletions tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function columns(): array
],
'tinyint_col' => [
'type' => 'tinyint',
'dbType' => 'tinyint(3)',
'dbType' => 'tinyint',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -52,7 +52,7 @@ public static function columns(): array
],
'smallint_col' => [
'type' => 'smallint',
'dbType' => 'smallint(1)',
'dbType' => 'smallint',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -64,7 +64,7 @@ public static function columns(): array
],
'char_col' => [
'type' => 'char',
'dbType' => 'char(100)',
'dbType' => 'char',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => true,
Expand All @@ -76,7 +76,7 @@ public static function columns(): array
],
'char_col2' => [
'type' => 'string',
'dbType' => 'varchar(100)',
'dbType' => 'varchar',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -100,7 +100,7 @@ public static function columns(): array
],
'float_col' => [
'type' => 'double',
'dbType' => 'double(4,3)',
'dbType' => 'double',
'phpType' => 'float',
'primaryKey' => false,
'notNull' => true,
Expand Down Expand Up @@ -136,7 +136,7 @@ public static function columns(): array
],
'numeric_col' => [
'type' => 'decimal',
'dbType' => 'decimal(5,2)',
'dbType' => 'decimal',
'phpType' => 'float',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -160,7 +160,7 @@ public static function columns(): array
],
'bool_col' => [
'type' => 'boolean',
'dbType' => 'tinyint(1)',
'dbType' => 'tinyint',
'phpType' => 'bool',
'primaryKey' => false,
'notNull' => true,
Expand All @@ -172,7 +172,7 @@ public static function columns(): array
],
'bool_col2' => [
'type' => 'boolean',
'dbType' => 'tinyint(1)',
'dbType' => 'tinyint',
'phpType' => 'bool',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -196,7 +196,7 @@ public static function columns(): array
],
'bit_col' => [
'type' => 'bit',
'dbType' => 'bit(8)',
'dbType' => 'bit',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => true,
Expand Down Expand Up @@ -249,7 +249,7 @@ public static function columns(): array
],
'type' => [
'type' => 'string',
'dbType' => 'varchar(255)',
'dbType' => 'varchar',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => true,
Expand Down Expand Up @@ -313,7 +313,7 @@ public static function columnsTypeBit(): array
[
'bit_col_1' => [
'type' => 'boolean',
'dbType' => 'bit(1)',
'dbType' => 'bit',
'phpType' => 'bool',
'primaryKey' => false,
'notNull' => true,
Expand All @@ -325,7 +325,7 @@ public static function columnsTypeBit(): array
],
'bit_col_2' => [
'type' => 'boolean',
'dbType' => 'bit(1)',
'dbType' => 'bit',
'phpType' => 'bool',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -337,7 +337,7 @@ public static function columnsTypeBit(): array
],
'bit_col_3' => [
'type' => 'bit',
'dbType' => 'bit(32)',
'dbType' => 'bit',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => true,
Expand All @@ -349,7 +349,7 @@ public static function columnsTypeBit(): array
],
'bit_col_4' => [
'type' => 'bit',
'dbType' => 'bit(32)',
'dbType' => 'bit',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -361,7 +361,7 @@ public static function columnsTypeBit(): array
],
'bit_col_5' => [
'type' => 'bit',
'dbType' => 'bit(64)',
'dbType' => 'bit',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => true,
Expand All @@ -373,7 +373,7 @@ public static function columnsTypeBit(): array
],
'bit_col_6' => [
'type' => 'bit',
'dbType' => 'bit(64)',
'dbType' => 'bit',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => false,
Expand Down

0 comments on commit edbecbc

Please sign in to comment.