Skip to content

Commit

Permalink
Fix #295: Fix multiline and single quote in default string value, add…
Browse files Browse the repository at this point in the history
… support for PostgreSQL 9.4 parentheses around negative numeric default values
  • Loading branch information
Tigrov committed Jul 24, 2023
1 parent 0dd905b commit 2e00673
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
- Chg #288: Typecast refactoring (@Tigrov)
- Chg #291: Update phpTypecast for bool type (@Tigrov)
- Enh #294: Refactoring of `Schema::normalizeDefaultValue()` method (@Tigrov)
- Bug #295: Fix multiline and single quote in default string value, add support for PostgreSQL 9.4 parentheses around negative numeric default values (@Tigrov)
- Bug #296: Prevent posible issues with array default values `('{one,two}'::text[])::varchar[]`, remove `ArrayParser::parseString()` (@Tigrov)

## 1.0.0 April 12, 2023
Expand Down
3 changes: 2 additions & 1 deletion src/Schema.php
Expand Up @@ -863,7 +863,8 @@ private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaIn
return new Expression($defaultValue);
}

$value = preg_replace("/^B?['(](.*?)[)']::[^:]+$/", '$1', $defaultValue);
$value = preg_replace("/^B?['(](.*?)[)'](?:::[^:]+)?$/s", '$1', $defaultValue);
$value = str_replace("''", "'", $value);

if ($column->getType() === self::TYPE_BINARY && str_starts_with($value, '\\x')) {
return hex2bin(substr($value, 2));
Expand Down
15 changes: 14 additions & 1 deletion tests/Provider/SchemaProvider.php
Expand Up @@ -89,7 +89,7 @@ public static function columns(): array
'size' => 100,
'precision' => null,
'scale' => null,
'defaultValue' => 'something',
'defaultValue' => 'some\'thing',
],
'char_col3' => [
'type' => 'text',
Expand All @@ -104,6 +104,19 @@ public static function columns(): array
'scale' => null,
'defaultValue' => null,
],
'char_col4' => [
'type' => 'string',
'dbType' => 'varchar',
'phpType' => 'string',
'primaryKey' => false,
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => "first line\nsecond line",
],
'float_col' => [
'type' => 'double',
'dbType' => 'float8',
Expand Down
3 changes: 2 additions & 1 deletion tests/Support/Fixture/pgsql.sql
Expand Up @@ -142,8 +142,9 @@ CREATE TABLE "type" (
tinyint_col smallint DEFAULT '1',
smallint_col smallint DEFAULT '1',
char_col char(100) NOT NULL,
char_col2 varchar(100) DEFAULT 'something',
char_col2 varchar(100) DEFAULT 'some''thing',
char_col3 text,
char_col4 character varying DEFAULT E'first line\nsecond line',
float_col double precision NOT NULL,
float_col2 double precision DEFAULT '1.23',
blob_col bytea DEFAULT 'a binary value',
Expand Down

0 comments on commit 2e00673

Please sign in to comment.