Skip to content

Commit edbecbc

Browse files
authored
Refactor ColumnFactory (#325)
1 parent 953a1c5 commit edbecbc

File tree

4 files changed

+39
-47
lines changed

4 files changed

+39
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- New #310: Add JSON overlaps condition builder (@Tigrov)
1010
- Enh #312: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov)
1111
- Enh #315: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
12-
- New #314: Implement `ColumnFactory` class (@Tigrov)
12+
- New #314, #325: Implement `ColumnFactory` class (@Tigrov)
1313
- Enh #317: Separate column type constants (@Tigrov)
1414
- New #318: Realize `ColumnBuilder` class (@Tigrov)
1515
- Enh #320: Update according changes in `ColumnSchemaInterface` (@Tigrov)

src/Column/ColumnFactory.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ final class ColumnFactory extends AbstractColumnFactory
1313
* Mapping from physical column types (keys) to abstract column types (values).
1414
*
1515
* @var string[]
16-
*
17-
* @psalm-suppress MissingClassConstType
16+
* @psalm-var array<string, ColumnType::*>
1817
*/
19-
private const TYPE_MAP = [
18+
protected const TYPE_MAP = [
2019
'bool' => ColumnType::BOOLEAN,
2120
'boolean' => ColumnType::BOOLEAN,
2221
'bit' => ColumnType::BIT,
@@ -49,21 +48,11 @@ final class ColumnFactory extends AbstractColumnFactory
4948

5049
protected function getType(string $dbType, array $info = []): string
5150
{
52-
$type = self::TYPE_MAP[$dbType] ?? ColumnType::STRING;
53-
54-
if (
55-
($type === ColumnType::BIT || $type === ColumnType::TINYINT)
56-
&& isset($info['size'])
57-
&& $info['size'] === 1
58-
) {
59-
return ColumnType::BOOLEAN;
60-
}
61-
62-
return $type;
63-
}
64-
65-
protected function isDbType(string $dbType): bool
66-
{
67-
return isset(self::TYPE_MAP[$dbType]);
51+
return match ($dbType) {
52+
'bit', 'tinyint' => isset($info['size']) && $info['size'] === 1
53+
? ColumnType::BOOLEAN
54+
: parent::getType($dbType, $info),
55+
default => parent::getType($dbType, $info),
56+
};
6857
}
6958
}

src/Schema.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use function preg_replace;
3333
use function serialize;
3434
use function strncasecmp;
35-
use function strtolower;
3635

3736
/**
3837
* Implements the SQLite Server specific schema, supporting SQLite 3.3.0 or higher.
@@ -72,6 +71,8 @@
7271
* pk:string,
7372
* size?: int,
7473
* scale?: int,
74+
* schema: string|null,
75+
* table: string
7576
* }
7677
*/
7778
final class Schema extends AbstractPdoSchema
@@ -342,6 +343,9 @@ protected function findColumns(TableSchemaInterface $table): bool
342343
$info['type'] = ColumnType::JSON;
343344
}
344345

346+
$info['schema'] = $table->getSchemaName();
347+
$info['table'] = $table->getName();
348+
345349
$column = $this->loadColumnSchema($info);
346350
$table->column($info['name'], $column);
347351

@@ -446,16 +450,15 @@ public function getSchemaDefaultValues(string $schema = '', bool $refresh = fals
446450
*/
447451
private function loadColumnSchema(array $info): ColumnSchemaInterface
448452
{
449-
$columnFactory = $this->getColumnFactory();
450-
451-
$dbType = strtolower($info['type']);
452-
$column = $columnFactory->fromDefinition($dbType, ['name' => $info['name']]);
453-
$column->dbType($dbType);
454-
$column->notNull((bool) $info['notnull']);
455-
$column->primaryKey((bool) $info['pk']);
456-
$column->defaultValue($this->normalizeDefaultValue($info['dflt_value'], $column));
457-
458-
return $column;
453+
$column = $this->getColumnFactory()->fromDefinition($info['type'], [
454+
'name' => $info['name'],
455+
'notNull' => (bool) $info['notnull'],
456+
'primaryKey' => (bool) $info['pk'],
457+
'schema' => $info['schema'],
458+
'table' => $info['table'],
459+
]);
460+
461+
return $column->defaultValue($this->normalizeDefaultValue($info['dflt_value'], $column));
459462
}
460463

461464
/**

tests/Provider/SchemaProvider.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function columns(): array
4040
],
4141
'tinyint_col' => [
4242
'type' => 'tinyint',
43-
'dbType' => 'tinyint(3)',
43+
'dbType' => 'tinyint',
4444
'phpType' => 'int',
4545
'primaryKey' => false,
4646
'notNull' => false,
@@ -52,7 +52,7 @@ public static function columns(): array
5252
],
5353
'smallint_col' => [
5454
'type' => 'smallint',
55-
'dbType' => 'smallint(1)',
55+
'dbType' => 'smallint',
5656
'phpType' => 'int',
5757
'primaryKey' => false,
5858
'notNull' => false,
@@ -64,7 +64,7 @@ public static function columns(): array
6464
],
6565
'char_col' => [
6666
'type' => 'char',
67-
'dbType' => 'char(100)',
67+
'dbType' => 'char',
6868
'phpType' => 'string',
6969
'primaryKey' => false,
7070
'notNull' => true,
@@ -76,7 +76,7 @@ public static function columns(): array
7676
],
7777
'char_col2' => [
7878
'type' => 'string',
79-
'dbType' => 'varchar(100)',
79+
'dbType' => 'varchar',
8080
'phpType' => 'string',
8181
'primaryKey' => false,
8282
'notNull' => false,
@@ -100,7 +100,7 @@ public static function columns(): array
100100
],
101101
'float_col' => [
102102
'type' => 'double',
103-
'dbType' => 'double(4,3)',
103+
'dbType' => 'double',
104104
'phpType' => 'float',
105105
'primaryKey' => false,
106106
'notNull' => true,
@@ -136,7 +136,7 @@ public static function columns(): array
136136
],
137137
'numeric_col' => [
138138
'type' => 'decimal',
139-
'dbType' => 'decimal(5,2)',
139+
'dbType' => 'decimal',
140140
'phpType' => 'float',
141141
'primaryKey' => false,
142142
'notNull' => false,
@@ -160,7 +160,7 @@ public static function columns(): array
160160
],
161161
'bool_col' => [
162162
'type' => 'boolean',
163-
'dbType' => 'tinyint(1)',
163+
'dbType' => 'tinyint',
164164
'phpType' => 'bool',
165165
'primaryKey' => false,
166166
'notNull' => true,
@@ -172,7 +172,7 @@ public static function columns(): array
172172
],
173173
'bool_col2' => [
174174
'type' => 'boolean',
175-
'dbType' => 'tinyint(1)',
175+
'dbType' => 'tinyint',
176176
'phpType' => 'bool',
177177
'primaryKey' => false,
178178
'notNull' => false,
@@ -196,7 +196,7 @@ public static function columns(): array
196196
],
197197
'bit_col' => [
198198
'type' => 'bit',
199-
'dbType' => 'bit(8)',
199+
'dbType' => 'bit',
200200
'phpType' => 'int',
201201
'primaryKey' => false,
202202
'notNull' => true,
@@ -249,7 +249,7 @@ public static function columns(): array
249249
],
250250
'type' => [
251251
'type' => 'string',
252-
'dbType' => 'varchar(255)',
252+
'dbType' => 'varchar',
253253
'phpType' => 'string',
254254
'primaryKey' => false,
255255
'notNull' => true,
@@ -313,7 +313,7 @@ public static function columnsTypeBit(): array
313313
[
314314
'bit_col_1' => [
315315
'type' => 'boolean',
316-
'dbType' => 'bit(1)',
316+
'dbType' => 'bit',
317317
'phpType' => 'bool',
318318
'primaryKey' => false,
319319
'notNull' => true,
@@ -325,7 +325,7 @@ public static function columnsTypeBit(): array
325325
],
326326
'bit_col_2' => [
327327
'type' => 'boolean',
328-
'dbType' => 'bit(1)',
328+
'dbType' => 'bit',
329329
'phpType' => 'bool',
330330
'primaryKey' => false,
331331
'notNull' => false,
@@ -337,7 +337,7 @@ public static function columnsTypeBit(): array
337337
],
338338
'bit_col_3' => [
339339
'type' => 'bit',
340-
'dbType' => 'bit(32)',
340+
'dbType' => 'bit',
341341
'phpType' => 'int',
342342
'primaryKey' => false,
343343
'notNull' => true,
@@ -349,7 +349,7 @@ public static function columnsTypeBit(): array
349349
],
350350
'bit_col_4' => [
351351
'type' => 'bit',
352-
'dbType' => 'bit(32)',
352+
'dbType' => 'bit',
353353
'phpType' => 'int',
354354
'primaryKey' => false,
355355
'notNull' => false,
@@ -361,7 +361,7 @@ public static function columnsTypeBit(): array
361361
],
362362
'bit_col_5' => [
363363
'type' => 'bit',
364-
'dbType' => 'bit(64)',
364+
'dbType' => 'bit',
365365
'phpType' => 'int',
366366
'primaryKey' => false,
367367
'notNull' => true,
@@ -373,7 +373,7 @@ public static function columnsTypeBit(): array
373373
],
374374
'bit_col_6' => [
375375
'type' => 'bit',
376-
'dbType' => 'bit(64)',
376+
'dbType' => 'bit',
377377
'phpType' => 'int',
378378
'primaryKey' => false,
379379
'notNull' => false,

0 commit comments

Comments
 (0)