Skip to content

Commit d23c5b4

Browse files
committed
Refactor, conversion to lower case db types
1 parent cf8bace commit d23c5b4

File tree

7 files changed

+61
-54
lines changed

7 files changed

+61
-54
lines changed

src/Column/BinaryColumn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class BinaryColumn extends BaseBinaryColumn
1414
{
1515
public function dbTypecast(mixed $value): mixed
1616
{
17-
if ($this->getDbType() === 'BLOB') {
17+
if ($this->getDbType() === 'blob') {
1818
if ($value instanceof ParamInterface && is_string($value->getValue())) {
1919
/** @var string */
2020
$value = $value->getValue();

src/Column/ColumnDefinitionBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
2626
'number',
2727
'float',
2828
'timestamp',
29-
'interval day to second',
29+
'interval day(0) to second',
3030
'raw',
3131
'urowid',
3232
];
@@ -94,7 +94,7 @@ protected function getDbType(ColumnInterface $column): string
9494
ColumnType::DATETIME => 'timestamp',
9595
ColumnType::TIMESTAMP => 'timestamp',
9696
ColumnType::DATE => 'date',
97-
ColumnType::TIME => 'interval day(0) to second' . ($size !== null ? "($size)" : ''),
97+
ColumnType::TIME => 'interval day(0) to second',
9898
ColumnType::ARRAY => 'clob',
9999
ColumnType::STRUCTURED => 'clob',
100100
ColumnType::JSON => 'clob',

src/Column/ColumnFactory.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
use Yiisoft\Db\Schema\Column\AbstractColumnFactory;
99
use Yiisoft\Db\Schema\Column\ColumnInterface;
1010

11-
use function in_array;
12-
use function preg_replace;
1311
use function rtrim;
14-
use function strtolower;
1512

1613
final class ColumnFactory extends AbstractColumnFactory
1714
{
@@ -41,10 +38,11 @@ final class ColumnFactory extends AbstractColumnFactory
4138
'binary_double' => ColumnType::DOUBLE, // 64 bit
4239
'float' => ColumnType::DOUBLE, // 126 bit
4340
'date' => ColumnType::DATE,
44-
'interval day to second' => ColumnType::TIME,
4541
'timestamp' => ColumnType::TIMESTAMP,
4642
'timestamp with time zone' => ColumnType::TIMESTAMP,
4743
'timestamp with local time zone' => ColumnType::TIMESTAMP,
44+
'interval day to second' => ColumnType::STRING,
45+
'interval year to month' => ColumnType::STRING,
4846

4947
/** Deprecated */
5048
'long' => ColumnType::TEXT,
@@ -57,8 +55,6 @@ protected function columnDefinitionParser(): ColumnDefinitionParser
5755

5856
protected function getType(string $dbType, array $info = []): string
5957
{
60-
$dbType = strtolower($dbType);
61-
6258
if ($dbType === 'number') {
6359
return match ($info['scale'] ?? null) {
6460
null => ColumnType::DOUBLE,
@@ -67,20 +63,8 @@ protected function getType(string $dbType, array $info = []): string
6763
};
6864
}
6965

70-
$dbType = preg_replace('/\([^)]+\)/', '', $dbType);
71-
72-
if (in_array($dbType, [
73-
'timestamp',
74-
'timestamp with time zone',
75-
'timestamp with local time zone',
76-
'interval day to second',
77-
'interval year to month',
78-
], true)) {
79-
[$info['size'], $info['scale']] = [$info['scale'] ?? null, $info['size'] ?? null];
80-
81-
if ($dbType === 'interval day to second' && $info['scale'] > 0) {
82-
return ColumnType::STRING;
83-
}
66+
if ($dbType === 'interval day to second' && isset($info['scale']) && $info['scale'] === 0) {
67+
return ColumnType::TIME;
8468
}
8569

8670
return parent::getType($dbType, $info);

src/Schema.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
use function implode;
2828
use function is_array;
2929
use function md5;
30+
use function preg_replace;
3031
use function serialize;
32+
use function strtolower;
3133

3234
/**
3335
* Implements the Oracle Server specific schema, supporting Oracle Server 11C and above.
@@ -437,7 +439,18 @@ protected function getTableSequenceName(string $tableName): string|null
437439
*/
438440
private function loadColumn(array $info): ColumnInterface
439441
{
440-
return $this->getColumnFactory()->fromDbType($info['data_type'], [
442+
$dbType = strtolower(preg_replace('/\([^)]+\)/', '', $info['data_type']));
443+
444+
match ($dbType) {
445+
'timestamp',
446+
'timestamp with time zone',
447+
'timestamp with local time zone',
448+
'interval day to second',
449+
'interval year to month' => [$info['size'], $info['data_scale']] = [$info['data_scale'], $info['size']],
450+
default => null,
451+
};
452+
453+
return $this->getColumnFactory()->fromDbType($dbType, [
441454
'autoIncrement' => $info['identity_column'] === 'YES',
442455
'comment' => $info['column_comment'],
443456
'defaultValueRaw' => $info['data_default'],

tests/ColumnTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testDbTypecastColumns(string $className, array $values): void
9696
public function testBinaryColumn(): void
9797
{
9898
$binaryCol = new BinaryColumn();
99-
$binaryCol->dbType('BLOB');
99+
$binaryCol->dbType('blob');
100100

101101
$this->assertInstanceOf(Expression::class, $binaryCol->dbTypecast("\x10\x11\x12"));
102102
$this->assertInstanceOf(

tests/Provider/ColumnFactoryProvider.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ public static function dbTypes(): array
3232
['binary_double', ColumnType::DOUBLE, DoubleColumn::class],
3333
['float', ColumnType::DOUBLE, DoubleColumn::class],
3434
['date', ColumnType::DATE, StringColumn::class],
35-
['interval day(0) to second', ColumnType::TIME, StringColumn::class],
3635
['timestamp', ColumnType::TIMESTAMP, StringColumn::class],
3736
['timestamp with time zone', ColumnType::TIMESTAMP, StringColumn::class],
3837
['timestamp with local time zone', ColumnType::TIMESTAMP, StringColumn::class],
38+
['timestamp with local time zone', ColumnType::TIMESTAMP, StringColumn::class],
39+
['interval day to second', ColumnType::STRING, StringColumn::class],
40+
['interval year to month', ColumnType::STRING, StringColumn::class],
3941
];
4042
}
4143

@@ -52,7 +54,15 @@ public static function definitions(): array
5254

5355
unset($definitions['bigint UNSIGNED']);
5456

55-
return $definitions;
57+
return [
58+
...$definitions,
59+
['interval day to second', ColumnType::STRING, StringColumn::class, ['getDbType' => 'interval day to second']],
60+
['interval day(0) to second', ColumnType::TIME, StringColumn::class, ['getDbType' => 'interval day to second', 'getScale' => 0]],
61+
['interval day (0) to second(6)', ColumnType::TIME, StringColumn::class, ['getDbType' => 'interval day to second', 'getScale' => 0, 'getSize' => 6]],
62+
['interval day to second (0)', ColumnType::STRING, StringColumn::class, ['getDbType' => 'interval day to second', 'getSize' => 0]],
63+
['interval year to month', ColumnType::STRING, StringColumn::class, ['getDbType' => 'interval year to month']],
64+
['interval year (2) to month', ColumnType::STRING, StringColumn::class, ['getDbType' => 'interval year to month', 'getScale' => 2]],
65+
];
5666
}
5767

5868
public static function defaultValueRaw(): array

tests/Provider/SchemaProvider.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static function columns(): array
1717
[
1818
'int_col' => [
1919
'type' => 'integer',
20-
'dbType' => 'NUMBER',
20+
'dbType' => 'number',
2121
'phpType' => 'int',
2222
'primaryKey' => false,
2323
'notNull' => true,
@@ -29,7 +29,7 @@ public static function columns(): array
2929
],
3030
'int_col2' => [
3131
'type' => 'integer',
32-
'dbType' => 'NUMBER',
32+
'dbType' => 'number',
3333
'phpType' => 'int',
3434
'primaryKey' => false,
3535
'notNull' => false,
@@ -41,7 +41,7 @@ public static function columns(): array
4141
],
4242
'tinyint_col' => [
4343
'type' => 'integer',
44-
'dbType' => 'NUMBER',
44+
'dbType' => 'number',
4545
'phpType' => 'int',
4646
'primaryKey' => false,
4747
'notNull' => false,
@@ -53,7 +53,7 @@ public static function columns(): array
5353
],
5454
'smallint_col' => [
5555
'type' => 'integer',
56-
'dbType' => 'NUMBER',
56+
'dbType' => 'number',
5757
'phpType' => 'int',
5858
'primaryKey' => false,
5959
'notNull' => false,
@@ -65,7 +65,7 @@ public static function columns(): array
6565
],
6666
'char_col' => [
6767
'type' => 'char',
68-
'dbType' => 'CHAR',
68+
'dbType' => 'char',
6969
'phpType' => 'string',
7070
'primaryKey' => false,
7171
'notNull' => true,
@@ -77,7 +77,7 @@ public static function columns(): array
7777
],
7878
'char_col2' => [
7979
'type' => 'string',
80-
'dbType' => 'VARCHAR2',
80+
'dbType' => 'varchar2',
8181
'phpType' => 'string',
8282
'primaryKey' => false,
8383
'notNull' => false,
@@ -89,7 +89,7 @@ public static function columns(): array
8989
],
9090
'char_col3' => [
9191
'type' => 'string',
92-
'dbType' => 'VARCHAR2',
92+
'dbType' => 'varchar2',
9393
'phpType' => 'string',
9494
'primaryKey' => false,
9595
'notNull' => false,
@@ -101,7 +101,7 @@ public static function columns(): array
101101
],
102102
'nvarchar_col' => [
103103
'type' => 'string',
104-
'dbType' => 'NVARCHAR2',
104+
'dbType' => 'nvarchar2',
105105
'phpType' => 'string',
106106
'primaryKey' => false,
107107
'notNull' => false,
@@ -113,7 +113,7 @@ public static function columns(): array
113113
],
114114
'float_col' => [
115115
'type' => 'double',
116-
'dbType' => 'FLOAT',
116+
'dbType' => 'float',
117117
'phpType' => 'float',
118118
'primaryKey' => false,
119119
'notNull' => true,
@@ -125,7 +125,7 @@ public static function columns(): array
125125
],
126126
'float_col2' => [
127127
'type' => 'double',
128-
'dbType' => 'FLOAT',
128+
'dbType' => 'float',
129129
'phpType' => 'float',
130130
'primaryKey' => false,
131131
'notNull' => false,
@@ -137,7 +137,7 @@ public static function columns(): array
137137
],
138138
'blob_col' => [
139139
'type' => 'binary',
140-
'dbType' => 'BLOB',
140+
'dbType' => 'blob',
141141
'phpType' => 'mixed',
142142
'primaryKey' => false,
143143
'notNull' => false,
@@ -149,7 +149,7 @@ public static function columns(): array
149149
],
150150
'numeric_col' => [
151151
'type' => 'decimal',
152-
'dbType' => 'NUMBER',
152+
'dbType' => 'number',
153153
'phpType' => 'float',
154154
'primaryKey' => false,
155155
'notNull' => false,
@@ -161,19 +161,19 @@ public static function columns(): array
161161
],
162162
'timestamp_col' => [
163163
'type' => 'timestamp',
164-
'dbType' => 'TIMESTAMP(6)',
164+
'dbType' => 'timestamp',
165165
'phpType' => 'string',
166166
'primaryKey' => false,
167167
'notNull' => true,
168168
'autoIncrement' => false,
169169
'enumValues' => null,
170-
'size' => null,
171-
'scale' => 6,
170+
'size' => 6,
171+
'scale' => null,
172172
'defaultValue' => new Expression("to_timestamp('2002-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')"),
173173
],
174174
'time_col' => [
175175
'type' => 'time',
176-
'dbType' => 'INTERVAL DAY(0) TO SECOND(0)',
176+
'dbType' => 'interval day to second',
177177
'phpType' => 'string',
178178
'primaryKey' => false,
179179
'notNull' => false,
@@ -185,19 +185,19 @@ public static function columns(): array
185185
],
186186
'interval_day_col' => [
187187
'type' => 'string',
188-
'dbType' => 'INTERVAL DAY(1) TO SECOND(0)',
188+
'dbType' => 'interval day to second',
189189
'phpType' => 'string',
190190
'primaryKey' => false,
191191
'notNull' => false,
192192
'autoIncrement' => false,
193193
'enumValues' => null,
194-
'size' => 1,
195-
'scale' => 0,
194+
'size' => 0,
195+
'scale' => 1,
196196
'defaultValue' => new Expression("INTERVAL '2 04:56:12' DAY(1) TO SECOND(0)"),
197197
],
198198
'bool_col' => [
199199
'type' => 'char',
200-
'dbType' => 'CHAR',
200+
'dbType' => 'char',
201201
'phpType' => 'string',
202202
'primaryKey' => false,
203203
'notNull' => true,
@@ -209,7 +209,7 @@ public static function columns(): array
209209
],
210210
'bool_col2' => [
211211
'type' => 'char',
212-
'dbType' => 'CHAR',
212+
'dbType' => 'char',
213213
'phpType' => 'string',
214214
'primaryKey' => false,
215215
'notNull' => false,
@@ -221,19 +221,19 @@ public static function columns(): array
221221
],
222222
'ts_default' => [
223223
'type' => 'timestamp',
224-
'dbType' => 'TIMESTAMP(6)',
224+
'dbType' => 'timestamp',
225225
'phpType' => 'string',
226226
'primaryKey' => false,
227227
'notNull' => true,
228228
'autoIncrement' => false,
229229
'enumValues' => null,
230-
'size' => null,
231-
'scale' => 6,
230+
'size' => 6,
231+
'scale' => null,
232232
'defaultValue' => new Expression('CURRENT_TIMESTAMP'),
233233
],
234234
'bit_col' => [
235235
'type' => 'integer',
236-
'dbType' => 'NUMBER',
236+
'dbType' => 'number',
237237
'phpType' => 'int',
238238
'primaryKey' => false,
239239
'notNull' => true,
@@ -250,7 +250,7 @@ public static function columns(): array
250250
[
251251
'id' => [
252252
'type' => 'integer',
253-
'dbType' => 'NUMBER',
253+
'dbType' => 'number',
254254
'phpType' => 'int',
255255
'primaryKey' => true,
256256
'notNull' => true,
@@ -262,7 +262,7 @@ public static function columns(): array
262262
],
263263
'type' => [
264264
'type' => 'string',
265-
'dbType' => 'VARCHAR2',
265+
'dbType' => 'varchar2',
266266
'phpType' => 'string',
267267
'primaryKey' => false,
268268
'notNull' => true,

0 commit comments

Comments
 (0)