Skip to content

Commit 032e1b3

Browse files
authored
Typecast refactoring (#260)
* Typecast refactoring * Fix test issues * Fix test issues * Fix test issues * Fix test issues * Fix test issues * Fix test issues * Rename `$columnSchema` to `$column` * Update * Update * Add test `DEFAULT CURRENT_TIMESTAMP` for `text` field * Add test `DEFAULT CURRENT_TIMESTAMP` for `text` field * Fix tests issue * Add line to changelog
1 parent 4d46cd8 commit 032e1b3

6 files changed

Lines changed: 78 additions & 14 deletions

File tree

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ composer.phar
3131

3232
# phpunit itself is not needed
3333
phpunit.phar
34-
phpunit.result.cache
3534

36-
# local phpunit config
35+
# local phpunit config and cache
3736
/phpunit.xml
37+
/.phpunit.result.cache
3838

3939
# NPM packages
4040
/node_modules

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## 1.0.1 under development
44

5-
- no changes in this release.
5+
- Enh #260: Typecast refactoring (@Tigrov)
66

77
## 1.0.0 April 12, 2023
88

9-
- Initial release.
9+
- Initial release.

src/Schema.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -493,19 +493,32 @@ protected function loadColumnSchema(array $info): ColumnSchemaInterface
493493
}
494494

495495
$column->phpType($this->getColumnPhpType($column));
496+
$column->defaultValue($this->normalizeDefaultValue($info['dflt_value'], $column));
496497

497-
if (!$column->isPrimaryKey()) {
498-
if ($info['dflt_value'] === 'null' || $info['dflt_value'] === '' || $info['dflt_value'] === null) {
499-
$column->defaultValue(null);
500-
} elseif ($info['dflt_value'] === 'CURRENT_TIMESTAMP' && $column->getType() === 'timestamp') {
501-
$column->defaultValue(new Expression('CURRENT_TIMESTAMP'));
502-
} else {
503-
$value = trim($info['dflt_value'], "'\"");
504-
$column->defaultValue($column->phpTypecast($value));
505-
}
498+
return $column;
499+
}
500+
501+
/**
502+
* Converts column's default value according to {@see ColumnSchema::phpType} after retrieval from the database.
503+
*
504+
* @param string|null $defaultValue The default value retrieved from the database.
505+
* @param ColumnSchemaInterface $column The column schema object.
506+
*
507+
* @return mixed The normalized default value.
508+
*
509+
* @psalm-suppress PossiblyNullArgument
510+
*/
511+
private function normalizeDefaultValue(?string $defaultValue, ColumnSchemaInterface $column): mixed
512+
{
513+
if ($column->isPrimaryKey()) {
514+
return null;
506515
}
507516

508-
return $column;
517+
return match ($defaultValue) {
518+
null, 'null', '' => null,
519+
'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME' => new Expression($defaultValue),
520+
default => $column->phpTypecast(trim($defaultValue, "'\"")),
521+
};
509522
}
510523

511524
/**

tests/Provider/SchemaProvider.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,50 @@ public static function columns(): array
243243
],
244244
'animal',
245245
],
246+
[
247+
[
248+
'id' => [
249+
'type' => 'integer',
250+
'dbType' => 'integer',
251+
'phpType' => 'integer',
252+
'primaryKey' => true,
253+
'allowNull' => true,
254+
'autoIncrement' => true,
255+
'enumValues' => null,
256+
'size' => null,
257+
'precision' => null,
258+
'scale' => null,
259+
'defaultValue' => null,
260+
],
261+
'text_col' => [
262+
'type' => 'text',
263+
'dbType' => 'text',
264+
'phpType' => 'string',
265+
'primaryKey' => false,
266+
'allowNull' => false,
267+
'autoIncrement' => false,
268+
'enumValues' => null,
269+
'size' => null,
270+
'precision' => null,
271+
'scale' => null,
272+
'defaultValue' => 'CURRENT_TIMESTAMP',
273+
],
274+
'timestamp_text' => [
275+
'type' => 'text',
276+
'dbType' => 'text',
277+
'phpType' => 'string',
278+
'primaryKey' => false,
279+
'allowNull' => false,
280+
'autoIncrement' => false,
281+
'enumValues' => null,
282+
'size' => null,
283+
'precision' => null,
284+
'scale' => null,
285+
'defaultValue' => new Expression('CURRENT_TIMESTAMP'),
286+
],
287+
],
288+
'timestamp_default',
289+
],
246290
];
247291
}
248292

tests/Support/Fixture/sqlite.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ CREATE TABLE "notauto_pk" (
162162
PRIMARY KEY (id_1, id_2)
163163
);
164164

165+
CREATE TABLE "timestamp_default" (
166+
id INTEGER PRIMARY KEY,
167+
text_col TEXT NOT NULL DEFAULT 'CURRENT_TIMESTAMP',
168+
timestamp_text TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
169+
); -- STRICT
170+
165171
CREATE VIEW "animal_view" AS SELECT * FROM "animal";
166172

167173
INSERT INTO "animal" ("type") VALUES ('yiiunit\data\ar\Cat');

tests/Support/Runtime/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/yiitest.sq3

0 commit comments

Comments
 (0)