[PATCH] UTF-8 bug in quote()

Dominic Mitchell <[email protected]>
Newsgroups gmane.comp.db.mysql.perl
Organization Semantico
Message-ID <[email protected]>
It appears that quote() doesn't handle UTF-8 data correctly.  This patch 
adds support for doing that, and a test to check that it works ok.

-Dom


-- 
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe:    http://lists.mysql.com/[email protected]
dbd-mysql-utf8-quote.patch (text/plain, 3.3 KB)
diff -burN -x mysql.mtest DBD-mysql-3.0002_5/MANIFEST DBD-mysql-3.0002_5.dom/MANIFEST
--- DBD-mysql-3.0002_5/MANIFEST	2005-09-27 00:15:01.000000000 +0100
+++ DBD-mysql-3.0002_5.dom/MANIFEST	2006-02-26 16:16:34.000000000 +0000
@@ -21,6 +21,7 @@
 t/mysql.dbtest
 t/mysql.t
 t/mysql2.t
+t/utf8.t
 lib/DBD/mysql/GetInfo.pm
 lib/DBD/mysql/INSTALL.pod
 lib/DBD/mysql.pm
diff -burN -x mysql.mtest DBD-mysql-3.0002_5/dbdimp.c DBD-mysql-3.0002_5.dom/dbdimp.c
--- DBD-mysql-3.0002_5/dbdimp.c	2006-02-01 22:49:02.000000000 +0000
+++ DBD-mysql-3.0002_5.dom/dbdimp.c	2006-02-26 16:17:58.369396128 +0000
@@ -4256,6 +4256,7 @@
 
     ptr= SvPV(str, len);
     result= newSV(len*2+3);
+    if (SvUTF8(str)) SvUTF8_on(result);
     sptr= SvPVX(result);
 
     *sptr++ = '\'';
diff -burN -x mysql.mtest DBD-mysql-3.0002_5/t/utf8.t DBD-mysql-3.0002_5.dom/t/utf8.t
--- DBD-mysql-3.0002_5/t/utf8.t	1970-01-01 01:00:00.000000000 +0100
+++ DBD-mysql-3.0002_5.dom/t/utf8.t	2006-02-26 16:15:46.000000000 +0000
@@ -0,0 +1,99 @@
+#!/usr/local/bin/perl
+#
+#   $Id$
+#
+#   This checks for UTF-8 support.
+#
+
+
+#
+#   Make -w happy
+#
+use vars qw($test_dsn $test_user $test_password $mdriver $verbose $state
+	    $dbdriver);
+use vars qw($COL_NULLABLE $COL_KEY);
+$test_dsn = '';
+$test_user = '';
+$test_password = '';
+
+
+#
+#   Include lib.pl
+#
+use DBI;
+use strict;
+$mdriver = "";
+{
+    my $file;
+    foreach $file ("lib.pl", "t/lib.pl") {
+	do $file; if ($@) { print STDERR "Error while executing lib.pl: $@\n";
+			    exit 10;
+			}
+	if ($mdriver ne '') {
+	    last;
+	}
+    }
+}
+
+sub ServerError() {
+    print STDERR ("Cannot connect: ", $DBI::errstr, "\n",
+	"\tEither your server is not up and running or you have no\n",
+	"\tpermissions for acessing the DSN $test_dsn.\n",
+	"\tThis test requires a running server and write permissions.\n",
+	"\tPlease make sure your server is running and you have\n",
+	"\tpermissions, then retry.\n");
+    exit 10;
+}
+
+#
+#   Main loop; leave this untouched, put tests after creating
+#   the new table.
+#
+while (Testing()) {
+    my ($dbh, $sth, $query);
+
+    #
+    #   Connect to the database
+    Test($state or ($dbh = DBI->connect($test_dsn, $test_user,
+					$test_password)))
+	   or ServerError();
+
+    #
+    #   Find a possible new table name
+    #
+    my $table = '';
+    Test($state or $table = FindNewTable($dbh))
+	   or ErrMsgF("Cannot determine a legal table name: Error %s.\n",
+		      $dbh->errstr);
+
+    #
+    #   Create a new table; EDIT THIS!
+    #
+    Test($state or ($query = TableDefinition($table,
+				      ["id",   "INTEGER",  4, $COL_NULLABLE],
+				      ["name", "CHAR",    64, $COL_NULLABLE]),
+		    $dbh->do($query)))
+	or ErrMsgF("Cannot create table: Error %s.\n",
+		      $dbh->errstr);
+
+
+    #
+    #   and here's the right place for inserting new tests:
+    #
+
+    my $utf8_str        = "\x{0100}dam";     # "Adam" with a macron.
+    my $quoted_utf8_str = "'\x{0100}dam'";
+    Test( $state or ( $dbh->quote( $utf8_str ) eq $quoted_utf8_str ) )
+      or ErrMsg( "Failed to retain UTF-8 flag when quoteing.\n" );
+
+    #
+    #   Finally drop the test table.
+    #
+    Test($state or $dbh->do("DROP TABLE $table"))
+	   or ErrMsgF("Cannot DROP test table $table: %s.\n",
+		      $dbh->errstr);
+
+    #   ... and disconnect
+    Test($state or $dbh->disconnect)
+	or ErrMsgF("Cannot disconnect: %s.\n", $dbh->errmsg);
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.