From: Dagfinn Ilmari Mannsåker Date: Fri, 23 Sep 2016 14:04:41 +0000 (+0100) Subject: Add support for parsing PostgreSQL dollar-quoted strings X-Git-Tag: v1.59_01~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1f742579d2d66e83f3f36c22e60acb5f4930e025;p=dbsrgits%2FSQL-Translator.git Add support for parsing PostgreSQL dollar-quoted strings --- diff --git a/Changes b/Changes index 0c7c0e2..7ccb06f 100644 --- a/Changes +++ b/Changes @@ -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 diff --git a/lib/SQL/Translator/Parser/PostgreSQL.pm b/lib/SQL/Translator/Parser/PostgreSQL.pm index ad4ec64..d77a31f 100644 --- a/lib/SQL/Translator/Parser/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/PostgreSQL.pm @@ -1012,8 +1012,12 @@ DQSTRING : '"' /((?:[^"]|"")+)/ '"' SQSTRING : "'" /((?:[^']|'')*)/ "'" { ($return = $item[3]) =~ s/''/'/g } +DOLLARSTRING : /\$[^\$]*\$/ /.*?(?=\Q$item[1]\E)/s "$item[1]" + { $return = $item[3]; } + VALUE : /[-+]?\d*\.?\d+(?:[eE]\d+)?/ | SQSTRING + | DOLLARSTRING | /null/i { 'NULL' } diff --git a/t/14postgres-parser.t b/t/14postgres-parser.t index c11d616..3c9dcd4 100644 --- a/t/14postgres-parser.t +++ b/t/14postgres-parser.t @@ -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, @@ -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 ( @@ -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"' ); @@ -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; @@ -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' );