Skip to content

Commit d936981

Browse files
authored
Fix #229: Fix bugs related with default value
1 parent fdd6eb0 commit d936981

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Enh #226: Add support for auto increment in primary key column. (@terabytesoftw)
66
- Enh #225: Typecast refactoring (@Tigrov)
7+
- Bug #229: Fix bugs related with default value (@Tigrov)
78

89
## 1.0.0 April 12, 2023
910

src/Schema.php

+19-17
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
use function implode;
2727
use function is_array;
2828
use function md5;
29+
use function preg_match;
2930
use function serialize;
30-
use function strlen;
31-
use function substr;
31+
use function str_replace;
3232
use function trim;
3333

3434
/**
@@ -440,26 +440,28 @@ protected function createColumnSchema(array $info): ColumnSchemaInterface
440440
* @param ColumnSchemaInterface $column The column schema object.
441441
*
442442
* @return mixed The normalized default value.
443-
*
444-
* @psalm-suppress PossiblyNullArgument
445443
*/
446-
private function normalizeDefaultValue(?string $defaultValue, ColumnSchemaInterface $column): mixed
444+
private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaInterface $column): mixed
447445
{
448-
return match (true) {
449-
$defaultValue === null, $column->isPrimaryKey() => null,
446+
if ($defaultValue === null || $column->isPrimaryKey()) {
447+
return null;
448+
}
450449

451-
/** @var string $defaultValue */
452-
$defaultValue === 'CURRENT_TIMESTAMP'
453-
&& $column->getType() === self::TYPE_TIMESTAMP
454-
=> new Expression($defaultValue),
450+
$defaultValue = trim($defaultValue);
455451

456-
strlen($defaultValue) > 2
457-
&& str_starts_with($defaultValue, "'")
458-
&& str_ends_with($defaultValue, "'")
459-
=> $column->phpTypecast(substr($defaultValue, 1, -1)),
452+
if ($defaultValue === 'NULL') {
453+
return null;
454+
}
460455

461-
default => $column->phpTypecast(trim($defaultValue)),
462-
};
456+
if ($column->getType() === self::TYPE_TIMESTAMP && $defaultValue === 'CURRENT_TIMESTAMP') {
457+
return new Expression($defaultValue);
458+
}
459+
460+
if (preg_match("/^'(.*)'$/s", $defaultValue, $matches) === 1) {
461+
$defaultValue = str_replace("''", "'", $matches[1]);
462+
}
463+
464+
return $column->phpTypecast($defaultValue);
463465
}
464466

465467
/**

tests/Provider/SchemaProvider.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Db\Oracle\Tests\Provider;
66

77
use Yiisoft\Db\Constraint\CheckConstraint;
8+
use Yiisoft\Db\Expression\Expression;
89
use Yiisoft\Db\Tests\Support\AnyValue;
910

1011
final class SchemaProvider extends \Yiisoft\Db\Tests\Provider\SchemaProvider
@@ -90,7 +91,7 @@ public static function columns(): array
9091
'size' => 100,
9192
'precision' => null,
9293
'scale' => null,
93-
'defaultValue' => 'something',
94+
'defaultValue' => 'some\'thing',
9495
],
9596
'char_col3' => [
9697
'type' => 'string',
@@ -105,6 +106,19 @@ public static function columns(): array
105106
'scale' => null,
106107
'defaultValue' => null,
107108
],
109+
'nvarchar_col' => [
110+
'type' => 'string',
111+
'dbType' => 'NVARCHAR2',
112+
'phpType' => 'string',
113+
'primaryKey' => false,
114+
'allowNull' => true,
115+
'autoIncrement' => false,
116+
'enumValues' => null,
117+
'size' => 100,
118+
'precision' => null,
119+
'scale' => null,
120+
'defaultValue' => '',
121+
],
108122
'float_col' => [
109123
'type' => 'double',
110124
'dbType' => 'FLOAT',
@@ -207,7 +221,7 @@ public static function columns(): array
207221
'size' => 11,
208222
'precision' => null,
209223
'scale' => 6,
210-
'defaultValue' => 'CURRENT_TIMESTAMP',
224+
'defaultValue' => new Expression('CURRENT_TIMESTAMP'),
211225
],
212226
'bit_col' => [
213227
'type' => 'string',

tests/Support/Fixture/oci.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,12 @@ CREATE TABLE "type" (
167167
"tinyint_col" number(3) DEFAULT 1,
168168
"smallint_col" smallint DEFAULT 1,
169169
"char_col" char(100) NOT NULL,
170-
"char_col2" varchar2(100) DEFAULT 'something',
170+
"char_col2" varchar2(100) DEFAULT 'some''thing',
171171
"char_col3" varchar2(4000),
172+
"nvarchar_col" nvarchar2(100) DEFAULT '',
172173
"float_col" double precision NOT NULL,
173174
"float_col2" double precision DEFAULT 1.23,
174-
"blob_col" blob,
175+
"blob_col" blob DEFAULT NULL,
175176
"numeric_col" decimal(5,2) DEFAULT 33.22,
176177
"time" timestamp DEFAULT to_timestamp('2002-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') NOT NULL,
177178
"bool_col" char NOT NULL check ("bool_col" in (0,1)),

0 commit comments

Comments
 (0)