Author: turnstep
Date: Tue Aug  4 11:10:35 2009
New Revision: 13162

Modified:
   DBD-Pg/trunk/Pg.xs
   DBD-Pg/trunk/dbdimp.c
   DBD-Pg/trunk/dbdimp.h
   DBD-Pg/trunk/t/09arrays.t
   DBD-Pg/trunk/t/dbdpg_test_setup.pl

Log:
Fix and additinal tests for bug 48420: single quotes when quoting arrays.


Modified: DBD-Pg/trunk/Pg.xs
==============================================================================
--- DBD-Pg/trunk/Pg.xs  (original)
+++ DBD-Pg/trunk/Pg.xs  Tue Aug  4 11:10:35 2009
@@ -205,7 +205,7 @@
                else if (SvROK(to_quote_sv) && !SvAMAGIC(to_quote_sv)) {
                        if (SvTYPE(SvRV(to_quote_sv)) != SVt_PVAV)
                                croak("Cannot quote a reference");
-                       RETVAL = pg_stringify_array(to_quote_sv, ",", 
imp_dbh->pg_server_version);
+                       RETVAL = pg_stringify_array(to_quote_sv, ",", 
imp_dbh->pg_server_version, 1);
                }
                else {
                        sql_type_info_t *type_info;

Modified: DBD-Pg/trunk/dbdimp.c
==============================================================================
--- DBD-Pg/trunk/dbdimp.c       (original)
+++ DBD-Pg/trunk/dbdimp.c       Tue Aug  4 11:10:35 2009
@@ -2246,7 +2246,7 @@
                }
                else if (SvTYPE(SvRV(newvalue)) == SVt_PVAV) {
                        SV * quotedval;
-                       quotedval = 
pg_stringify_array(newvalue,",",imp_dbh->pg_server_version);
+                       quotedval = 
pg_stringify_array(newvalue,",",imp_dbh->pg_server_version, 0);
                        currph->valuelen = sv_len(quotedval);
                        Renew(currph->value, currph->valuelen+1, char); /* 
freed in dbd_st_destroy */
                        currph->value = SvUTF8(quotedval) ? 
SvPVutf8_nolen(quotedval) : SvPV_nolen(quotedval);
@@ -2380,7 +2380,7 @@
 
 
 /* ================================================================== */
-SV * pg_stringify_array(SV *input, const char * array_delim, int 
server_version) {
+SV * pg_stringify_array(SV *input, const char * array_delim, int 
server_version, int extraquotes) {
 
        dTHX;
        AV * toparr;
@@ -2399,12 +2399,14 @@
        if (TSTART) TRC(DBILOGFP, "%sBegin pg_stringify_array\n", THEADER);
 
        toparr = (AV *) SvRV(input);
-       value = newSVpv("{", 1);
+       value = extraquotes ? newSVpv("'{", 2) : newSVpv("{", 1);
 
        /* Empty arrays are easy */
        if (av_len(toparr) < 0) {
                av_clear(toparr);
                sv_catpv(value, "}");
+               if (extraquotes)
+                       sv_catpv(value, "'");
                if (TEND) TRC(DBILOGFP, "%sEnd pg_stringify_array (empty)\n", 
THEADER);
                return value;
        }
@@ -2506,6 +2508,8 @@
        for (xy=0; xy<array_depth; xy++) {
                sv_catpv(value, "}");
        }
+       if (extraquotes)
+               sv_catpv(value, "'");
 
        if (TEND) TRC(DBILOGFP, "%sEnd pg_stringify_array (string: %s)\n", 
THEADER, neatsvpv(value,0));
        return value;

Modified: DBD-Pg/trunk/dbdimp.h
==============================================================================
--- DBD-Pg/trunk/dbdimp.h       (original)
+++ DBD-Pg/trunk/dbdimp.h       Tue Aug  4 11:10:35 2009
@@ -187,7 +187,7 @@
 
 SV * pg_db_pg_notifies (SV *dbh, imp_dbh_t *imp_dbh);
 
-SV * pg_stringify_array(SV * input, const char * array_delim, int 
server_version);
+SV * pg_stringify_array(SV * input, const char * array_delim, int 
server_version, int extraquotes);
 
 int pg_quickexec (SV *dbh, const char *sql, const int asyncflag);
 

Modified: DBD-Pg/trunk/t/09arrays.t
==============================================================================
--- DBD-Pg/trunk/t/09arrays.t   (original)
+++ DBD-Pg/trunk/t/09arrays.t   Tue Aug  4 11:10:35 2009
@@ -18,7 +18,7 @@
 if (! defined $dbh) {
        plan skip_all => 'Connection to database failed, cannot continue 
testing';
 }
-plan tests => 256;
+plan tests => 257;
 
 isnt ($dbh, undef, 'Connect to database for array testing');
 
@@ -51,6 +51,17 @@
 $SQL = q{SELECT testarray3 FROM dbd_pg_test WHERE pname= 'Array Testing'};
 my $getarray_bool = $dbh->prepare($SQL);
 
+$t='Array quoting allows direct insertion into statements';
+$SQL = q{INSERT INTO dbd_pg_test (id,testarray2) VALUES };
+my $quoteid = $dbh->quote(123);
+my $quotearr = $dbh->quote([456]);
+$SQL .= qq{($quoteid, $quotearr)};
+eval {
+       $dbh->do($SQL);
+};
+is ($@, q{}, $t);
+$dbh->rollback();
+
 ## Input
 ## Expected
 ## Name of test
@@ -201,6 +212,9 @@
        if ($expected =~ s/\s*quote:\s*(.+)//) {
                $qexpected = $1;
        }
+       if ($qexpected !~ /^ERROR/) {
+               $qexpected = qq{'$qexpected'};
+       }
 
        if ($msg =~ s/NEED (\d+):\s*//) {
                my $ver = $1;
@@ -568,7 +582,7 @@
 
        $t='quote() handles utf8 inside array';
        $quoted = $dbh->quote([$utf8_str, $utf8_str]);
-       is ($quoted, qq!{"$utf8_str","$utf8_str"}!, $t);
+       is ($quoted, qq!'{"$utf8_str","$utf8_str"}'!, $t);
 
        $t='Quoted array of strings should be UTF-8';
     ok (Encode::is_utf8( $quoted ), $t);
@@ -581,7 +595,7 @@
 
        $t='Inserting utf-8 into an array via quoted do() works';
        $dbh->do('DELETE FROM dbd_pg_test');
-       $SQL = qq{INSERT INTO dbd_pg_test (id, testarray, val) VALUES (1, 
'$quoted', 'one')};
+       $SQL = qq{INSERT INTO dbd_pg_test (id, testarray, val) VALUES (1, 
$quoted, 'one')};
        eval {
                $dbh->do($SQL);
        };

Modified: DBD-Pg/trunk/t/dbdpg_test_setup.pl
==============================================================================
--- DBD-Pg/trunk/t/dbdpg_test_setup.pl  (original)
+++ DBD-Pg/trunk/t/dbdpg_test_setup.pl  Tue Aug  4 11:10:35 2009
@@ -398,7 +398,12 @@
                        $@ = qq{Could not open "$conf": $!};
                        last GETHANDLE; ## Fail - no conf file
                }
-               print $cfh "\n\n## DBD::Pg testing 
parameters\nport=$testport\nmax_connections=4\n";
+               print $cfh "\n\n## DBD::Pg testing parameters\n";
+               print $cfh "port=$testport\n";
+               print $cfh "max_connections=4\n";
+               print $cfh "log_statement = 'all'\n";
+               print $cfh "log_line_prefix = '%m [%p] '\n";
+               print $cfh "log_min_messages = 'DEBUG1'\n";
                print $cfh "listen_addresses='127.0.0.1'\n" if $^O =~ /Win32/;
                print $cfh "\n";
                close $cfh or die qq{Could not close "$conf": $!\n};

Reply via email to