Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ColumnFactory #325

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading