Skip to content

Commit

Permalink
Add support for parsing PostgreSQL dollar-quoted strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmari committed Jan 27, 2018
1 parent 6e9cebc commit 1f74257
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Changes for SQL::Translator

* Add support for parsing PostgreSQL dollar-quoted strings

0.11024 2018-01-09

* Remove temporary diagnostics added in 0.11023_01
Expand Down
4 changes: 4 additions & 0 deletions lib/SQL/Translator/Parser/PostgreSQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,12 @@ DQSTRING : '"' <skip: ''> /((?:[^"]|"")+)/ '"'
SQSTRING : "'" <skip: ''> /((?:[^']|'')*)/ "'"
{ ($return = $item[3]) =~ s/''/'/g }
DOLLARSTRING : /\$[^\$]*\$/ <skip: ''> /.*?(?=\Q$item[1]\E)/s "$item[1]"
{ $return = $item[3]; }
VALUE : /[-+]?\d*\.?\d+(?:[eE]\d+)?/
| SQSTRING
| DOLLARSTRING
| /null/i
{ 'NULL' }
Expand Down
28 changes: 24 additions & 4 deletions t/14postgres-parser.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ my $sql = q{
f_bool boolean,
f_bin bytea,
f_tz timestamp default '1970-01-01 00:00:00'::TIMESTAMP,
f_text text,
f_text text default $foo$ foo
$bar$
baz $foo$,
f_fk1 integer not null references t_test2 (f_id),
f_dropped text,
f_timestamp timestamp(0) with time zone,
Expand All @@ -35,7 +37,9 @@ my $sql = q{
f_numarray numeric(7,2) [ ],
f_uuid uuid,
f_time time(0) with time zone,
f_time2 time without time zone
f_time2 time without time zone,
f_text2 text default $$$$,
f_text3 text default $$$ $$
);
create table t_test2 (
Expand Down Expand Up @@ -127,7 +131,7 @@ is( $t1->name, 't_test1', 'Table t_test1 exists' );
is( $t1->comments, 'comment on t_test1', 'Table comment exists' );

my @t1_fields = $t1->get_fields;
is( scalar @t1_fields, 19, '19 fields in t_test1' );
is( scalar @t1_fields, 21, '21 fields in t_test1' );

my $f1 = shift @t1_fields;
is( $f1->name, 'f_serial', 'First field is "f_serial"' );
Expand Down Expand Up @@ -201,7 +205,7 @@ is( $f9->name, 'f_text', 'Ninth field is "f_text"' );
is( $f9->data_type, 'text', 'Field is text' );
is( $f9->is_nullable, 1, 'Field can be null' );
is( $f9->size, 64000, 'Size is "64,000"' );
is( $f9->default_value, undef, 'Default value is undefined' );
is( $f9->default_value, " foo\n\$bar\$\nbaz ", 'Dollar-quoted default value is " foo\n$bar$\nbaz "' );
is( $f9->is_primary_key, 0, 'Field is not PK' );

my $f10 = shift @t1_fields;
Expand Down Expand Up @@ -288,6 +292,22 @@ is( $f18->default_value, undef, 'Default value is "undef"' );
is( $f18->is_primary_key, 0, 'Field is not PK' );
is( $f18->is_foreign_key, 0, 'Field is not FK' );

my $f19 = shift @t1_fields;
is( $f19->name, 'f_text2', '19th field is "f_text2"' );
is( $f19->data_type, 'text', 'Field is text' );
is( $f19->is_nullable, 1, 'Field can be null' );
is( $f19->size, 64000, 'Size is "64,000"' );
is( $f19->default_value, '', 'Dollar-quoted default value is empty' );
is( $f19->is_primary_key, 0, 'Field is not PK' );

my $f20 = shift @t1_fields;
is( $f20->name, 'f_text3', '20th field is "f_text3"' );
is( $f20->data_type, 'text', 'Field is text' );
is( $f20->is_nullable, 1, 'Field can be null' );
is( $f20->size, 64000, 'Size is "64,000"' );
is( $f20->default_value, '$ ', 'Dollar-quoted default value is "$ "' );
is( $f20->is_primary_key, 0, 'Field is not PK' );

# my $fk_ref2 = $f11->foreign_key_reference;
# isa_ok( $fk_ref2, 'SQL::Translator::Schema::Constraint', 'FK' );
# is( $fk_ref2->reference_table, 't_test2', 'FK is to "t_test2" table' );
Expand Down

0 comments on commit 1f74257

Please sign in to comment.