From 67ad0ce1da7e640390174bbd790c7b849e9b566c Mon Sep 17 00:00:00 2001 From: Roy Storey Date: Wed, 19 Dec 2012 11:54:44 +1300 Subject: [PATCH 1/3] correct quoting of timestamp fields in output and keyword fields in select Signed-off-by: Roy Storey --- lib/SQL/Translator/Producer/Dumper.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/SQL/Translator/Producer/Dumper.pm b/lib/SQL/Translator/Producer/Dumper.pm index 0a24cd6ca..83f030b48 100644 --- a/lib/SQL/Translator/Producer/Dumper.pm +++ b/lib/SQL/Translator/Producer/Dumper.pm @@ -169,7 +169,7 @@ FOREACH table IN schema.get_tables; field_name = field.name; fname_len = field.name.length; max_field = fname_len > max_field ? fname_len : max_field; - types.$field_name = field.data_type.match( '(char|str|long|text|enum|date)' ) + types.$field_name = field.data_type.match( '(char|str|long|text|enum|date|timestamp)' ) ? 'string' : 'number'; field_names.push( field_name ); END; @@ -223,7 +223,7 @@ for my $table ( @tables ) { } my $sql = - 'select ' . join(', ', @{ $table->{'fields'} } ) . " from $table_name" + 'select ' . join(', ', map { qq["$_"] } @{ $table->{'fields'} } ) . " from $table_name" ; my $sth = $db->prepare( $sql ); $sth->execute; From 7c8074bb35cb1e7b85bd0320cb4deb28aa529f80 Mon Sep 17 00:00:00 2001 From: Roy Storey Date: Wed, 19 Dec 2012 12:52:57 +1300 Subject: [PATCH 2/3] add bytea to the datatypes that map to string and are hence quoted --- lib/SQL/Translator/Producer/Dumper.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/SQL/Translator/Producer/Dumper.pm b/lib/SQL/Translator/Producer/Dumper.pm index 83f030b48..33e587867 100644 --- a/lib/SQL/Translator/Producer/Dumper.pm +++ b/lib/SQL/Translator/Producer/Dumper.pm @@ -169,7 +169,7 @@ FOREACH table IN schema.get_tables; field_name = field.name; fname_len = field.name.length; max_field = fname_len > max_field ? fname_len : max_field; - types.$field_name = field.data_type.match( '(char|str|long|text|enum|date|timestamp)' ) + types.$field_name = field.data_type.match( '(char|str|long|text|enum|date|timestamp|bytea)' ) ? 'string' : 'number'; field_names.push( field_name ); END; From 321aab20addb6a7b8847fc89c492c73240c7d9c8 Mon Sep 17 00:00:00 2001 From: Roy Storey Date: Wed, 19 Dec 2012 14:36:44 +1300 Subject: [PATCH 3/3] use dbi methods where we can Signed-off-by: Roy Storey --- lib/SQL/Translator/Producer/Dumper.pm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/SQL/Translator/Producer/Dumper.pm b/lib/SQL/Translator/Producer/Dumper.pm index 33e587867..90e2ad50e 100644 --- a/lib/SQL/Translator/Producer/Dumper.pm +++ b/lib/SQL/Translator/Producer/Dumper.pm @@ -221,9 +221,9 @@ for my $table ( @tables ) { if ( $add_truncate ) { print "TRUNCATE TABLE $table_name;\n"; } - + ## use dbh->quote_identifier() as a more rigourous option. my $sql = - 'select ' . join(', ', map { qq["$_"] } @{ $table->{'fields'} } ) . " from $table_name" + 'select ' . join(', ', map { $db->quote_identifier($_) } @{ $table->{'fields'} } ) . " from $table_name" ; my $sth = $db->prepare( $sql ); $sth->execute; @@ -233,7 +233,11 @@ for my $table ( @tables ) { for my $fld ( @{ $table->{'fields'} } ) { my $val = $rec->{ $fld }; if ( $table->{'types'}{ $fld } eq 'string' ) { + ## would like to be able to use dbh->quote, but that quotes + ## according to the input database handle type. + ## $val = $db->quote($val) if ( defined $val ) { + $val =~ s/\\/\\\\/g; $val =~ s/'/\\'/g; $val = qq['$val'] }