From 99cb4dd439b78751c0976b6eeed349809badeb3e Mon Sep 17 00:00:00 2001 From: Matthew Musgrove Date: Fri, 9 Dec 2016 16:53:07 -0600 Subject: [PATCH 1/4] Issues/83 - Add unit tests for an index on an expression with more than one argument --- t/47postgres-producer.t | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/t/47postgres-producer.t b/t/47postgres-producer.t index 9c50db736..c2db8444c 100644 --- a/t/47postgres-producer.t +++ b/t/47postgres-producer.t @@ -686,6 +686,14 @@ is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2'); ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote); is($def, 'CREATE INDEX "myindex" on "foobar" USING hash ("bar", lower(foo)) WHERE upper(foo) = \'bar\' AND bar = \'foo\'', 'index using & where created w/ quotes'); } + + { + my $index = $table->add_index(name => 'myindex', fields => ['coalesce(foo, 0)']); + my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index); + is($def, "CREATE INDEX myindex on foobar (coalesce(foo, 0))", 'index created'); + ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote); + is($def, 'CREATE INDEX "myindex" on "foobar" (coalesce(foo, 0))', 'index created w/ quotes'); + } } my $drop_view_opts1 = { add_drop_view => 1, no_comments => 1, postgres_version => 8.001 }; From b4840b2e00ee73ef9d4c6a8505e678a18a85ab9d Mon Sep 17 00:00:00 2001 From: Matthew Musgrove Date: Fri, 9 Dec 2016 16:55:28 -0600 Subject: [PATCH 2/4] Issues/83 - Update parse_list_arg to only do the comma split on portions of strings that do not contain balanced parentheses --- lib/SQL/Translator/Utils.pm | 59 +++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/lib/SQL/Translator/Utils.pm b/lib/SQL/Translator/Utils.pm index ccc7ad3a2..12b1b931c 100644 --- a/lib/SQL/Translator/Utils.pm +++ b/lib/SQL/Translator/Utils.pm @@ -7,6 +7,7 @@ use File::Spec; use Scalar::Util qw(blessed); use Try::Tiny; use Carp qw(carp croak); +use Regexp::Common qw/ balanced /; our $VERSION = '1.59'; our $DEFAULT_COMMENT = '-- '; @@ -124,24 +125,50 @@ HEADER_COMMENT return $header_comment; } -sub parse_list_arg { - my $list = UNIVERSAL::isa( $_[0], 'ARRAY' ) ? shift : [ @_ ]; +{ + # declare variabled shared in this scope + my @chars = ('a'..'z', 'A'..'Z', '_', '-', 0..9); + my %symbols; + + sub save_string { + my ($str) = @_; + my $sym; + # iterate on the off chance that our random string isn't unique + do { $sym = "STRING_" . join '', map { $chars[rand(@chars)] } 0..15; } + while exists $symbols{$sym}; + $symbols{$sym} = $str; + return $sym + } - # - # This protects stringification of references. - # - if ( @$list && ref $list->[0] ) { - return $list; + sub restore_strings { + for my $re (keys %symbols) { + $_[0] =~ s/($re)/$symbols{$1}/g; + } } - # - # This processes string-like arguments. - # - else { - return [ - map { s/^\s+|\s+$//g; $_ } - map { split /,/ } - grep { defined && length } @$list - ]; + + sub clear_saved_strings { + %symbols = (); + } ++ + sub parse_list_arg { + my $list = UNIVERSAL::isa( $_[0], 'ARRAY' ) ? shift : [ @_ ]; + # + # This protects stringification of references. + # + if ( @$list && ref $list->[0] ) { + return $list; + } + # + # This processes string-like arguments. + # + else { + clear_saved_strings(); # start fresh each time + return [ + map { restore_strings($_); s/^\s+|\s+$//g; $_ } + map { $_ =~ s/$RE{balanced}{-parens=>'()'}/save_string($1)/ge; split /,/ } + grep { defined && length } @$list + ]; + } } } From 25a8221818e9548588de119886219a435d145cd8 Mon Sep 17 00:00:00 2001 From: Matthew Musgrove Date: Fri, 9 Dec 2016 16:55:42 -0600 Subject: [PATCH 3/4] Issues/83 - Add Regexp::Common to Makefile.PL --- Makefile.PL | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile.PL b/Makefile.PL index 9b2418ac5..98b0aa7ab 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -19,6 +19,7 @@ my $deps = { 'Sub::Quote' => '0', 'Try::Tiny' => '0.04', 'Scalar::Util' => '0', + 'Regexp::Common' => '0', }, recommends => { 'Template' => '2.20', From e868d500d819be0a9a5fdf9c4490f7a43bbd7b40 Mon Sep 17 00:00:00 2001 From: Matthew Musgrove Date: Fri, 9 Dec 2016 17:02:23 -0600 Subject: [PATCH 4/4] Issues/83 - Remove randomly pasted character --- lib/SQL/Translator/Utils.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/SQL/Translator/Utils.pm b/lib/SQL/Translator/Utils.pm index 12b1b931c..729f85112 100644 --- a/lib/SQL/Translator/Utils.pm +++ b/lib/SQL/Translator/Utils.pm @@ -149,7 +149,7 @@ HEADER_COMMENT sub clear_saved_strings { %symbols = (); } -+ + sub parse_list_arg { my $list = UNIVERSAL::isa( $_[0], 'ARRAY' ) ? shift : [ @_ ]; #