Author: turnstep
Date: Mon Jan 21 20:48:48 2008
New Revision: 10631

Modified:
   DBD-Pg/trunk/Changes
   DBD-Pg/trunk/dbdimp.c
   DBD-Pg/trunk/t/09arrays.t

Log:
Fix for bug 32479: utf-8 inside of arrays.


Modified: DBD-Pg/trunk/Changes
==============================================================================
--- DBD-Pg/trunk/Changes        (original)
+++ DBD-Pg/trunk/Changes        Mon Jan 21 20:48:48 2008
@@ -1,6 +1,7 @@
 ('GSM' is Greg Sabino Mullane, [EMAIL PROTECTED])
 
 2.0.0
+       - Make sure arrays handle UTF-8 smoothly (CPAN bug #32479) [GSM]
        - Check transaction status after each command, to allow 
                things such as 'PREPARE TRANSACTION' to work properly.
                (CPAN bug #32423) [GSM]

Modified: DBD-Pg/trunk/dbdimp.c
==============================================================================
--- DBD-Pg/trunk/dbdimp.c       (original)
+++ DBD-Pg/trunk/dbdimp.c       Mon Jan 21 20:48:48 2008
@@ -2251,12 +2251,16 @@
                        }
                        else {
                                sv_catpv(value, "\"");
+                               if (SvUTF8(svitem))
+                                       SvUTF8_on(value);
                                string = SvPV(svitem, svlen);
                                while (svlen--) {
-                                       sv_catpvf(value, "%s%c", /* upgrades to 
utf8 for us */
-                                                         '\"'==*string ? "\\" 
: 
-                                                         '\\'==*string ? 
"\\\\" :
-                                                         "", *string);
+                                       if ('\"' == *string)
+                                               sv_catpvn(value, "\\", 1);
+                                       if ('\\' == *string) {
+                                               sv_catpvn(value, "\\\\", 2);
+                                       }
+                                       sv_catpvn(value, string, 1);
                                        string++;
                                }
                                sv_catpv(value, "\"");

Modified: DBD-Pg/trunk/t/09arrays.t
==============================================================================
--- DBD-Pg/trunk/t/09arrays.t   (original)
+++ DBD-Pg/trunk/t/09arrays.t   Mon Jan 21 20:48:48 2008
@@ -15,7 +15,7 @@
 my $dbh = connect_database();
 
 if (defined $dbh) {
-       plan tests => 213;
+       plan tests => 220;
 }
 else {
        plan skip_all => 'Connection to database failed, cannot continue 
testing';
@@ -447,5 +447,61 @@
 
 }
 
+## Check utf-8 in and out of the database
+
+SKIP: {
+       eval { require Encode; };
+       skip 'Encode module is needed for unicode tests', 5 if $@;
+
+       local $dbh->{pg_enable_utf8} = 1;
+       my $utf8_str = chr(0x100).'dam'; # LATIN CAPITAL LETTER A WITH MACRON
+
+       my $quoted = $dbh->quote($utf8_str);
+       is( $quoted, qq{'$utf8_str'}, 'quote() handles utf8' );
+       my $quoted = $dbh->quote([$utf8_str, $utf8_str]);
+       is( $quoted, qq!{"$utf8_str","$utf8_str"}!, 'quote() handles utf8 
inside array' );
+
+       $dbh->do("DELETE FROM dbd_pg_test");
+       $SQL = "INSERT INTO dbd_pg_test (id, testarray, val) VALUES (1, 
'$quoted', 'one')";
+       eval {
+               $dbh->do($SQL);
+       };
+       is($@, q{}, 'Inserting utf-8 into an array via quoted do() works');
+
+       $SQL = "SELECT id, testarray, val FROM dbd_pg_test WHERE id = 1";
+       $sth = $dbh->prepare($SQL);
+       $sth->execute();
+       $result = $sth->fetchall_arrayref()->[0];
+       my $t = q{Retreiving an array containing utf-8 works};
+       my $expected = [1,[$utf8_str,$utf8_str],'one'];
+       is_deeply($result, $expected, $t);
+
+       $dbh->do("DELETE FROM dbd_pg_test");
+       $SQL = "INSERT INTO dbd_pg_test (id, testarray, val) VALUES (?, ?, 
'one')";
+       $sth = $dbh->prepare($SQL);
+       eval {
+               $sth->execute(1,['Bob',$utf8_str]);
+       };
+       is($@, q{}, 'Inserting utf-8 into an array via prepare and arrayref 
works');
+
+       $SQL = "SELECT id, testarray, val FROM dbd_pg_test WHERE id = 1";
+       $sth = $dbh->prepare($SQL);
+       $sth->execute();
+       $result = $sth->fetchall_arrayref()->[0];
+       $t = q{Retreiving an array containing utf-8 works};
+       $expected = [1,['Bob',$utf8_str],'one'];
+       is_deeply($result, $expected, $t);
+
+       $dbh->do("DELETE FROM dbd_pg_test");
+       $SQL = qq{INSERT INTO dbd_pg_test (id, testarray, val) VALUES (1, 
'{"noutfhere"}', 'one')};
+       $dbh->do($SQL);
+       $SQL = "SELECT testarray FROM dbd_pg_test WHERE id = 1";
+       $sth = $dbh->prepare($SQL);
+       $sth->execute();
+       $result = $sth->fetchall_arrayref()->[0][0];
+       ok( !Encode::is_utf8($result), 'Non utf-8 inside an array is not return 
as utf-8');
+       $sth->finish();
+}
+
 cleanup_database($dbh,'test');
 $dbh->disconnect;

Reply via email to