From 6c57a3e2aa882bc53c16f6084c1a040b3e9f77e8 Mon Sep 17 00:00:00 2001 From: Laurent Dami Date: Fri, 4 Jul 2014 10:53:33 +0200 Subject: [PATCH 1/2] fix RT#96877 ... but this breakx some previous tests which made incorrect assumptions about utf8 in statements :-( --- lib/DBD/SQLite.pm | 3 +++ t/rt_96877_unicode_statements.t | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 t/rt_96877_unicode_statements.t diff --git a/lib/DBD/SQLite.pm b/lib/DBD/SQLite.pm index 759efdd..87ca909 100644 --- a/lib/DBD/SQLite.pm +++ b/lib/DBD/SQLite.pm @@ -196,6 +196,9 @@ sub prepare { my $sql = shift; $sql = '' unless defined $sql; + # sqlite3_prepare wants an utf8-encoded SQL statement + utf8::upgrade($sql); + my $sth = DBI::_new_sth( $dbh, { Statement => $sql, } ); diff --git a/t/rt_96877_unicode_statements.t b/t/rt_96877_unicode_statements.t new file mode 100644 index 0000000..d72ff55 --- /dev/null +++ b/t/rt_96877_unicode_statements.t @@ -0,0 +1,40 @@ +#!/usr/bin/perl +# According to the sqlite doc, the SQL argument to sqlite3_prepare_v2 +# should be in utf8, but DBD::SQLite does not ensure this (even with +# sqlite_unicode => 1). Only bind values are properly converted. + +use strict; +BEGIN { + $| = 1; + $^W = 1; +} + +use t::lib::Test; +use Test::More; +BEGIN { + if ( $] >= 5.008005 ) { + plan( tests => 16 ); + } else { + plan( skip_all => 'Unicode is not supported before 5.8.5' ); + } +} +use Test::NoWarnings; + +my $dbh = connect_ok( sqlite_unicode => 1 ); +is( $dbh->{sqlite_unicode}, 1, 'Unicode is on' ); + +ok( $dbh->do(<<'END_SQL'), 'CREATE TABLE' ); +CREATE TABLE foo ( + bar varchar(255) +) +END_SQL + +foreach ( "A", "\xe9", "\x{20ac}" ) { + diag sprintf "testing \\x{%x}", ord($_); + ok( $dbh->do("INSERT INTO foo VALUES ( ? )", {}, $_), 'INSERT with bind' ); + ok( $dbh->do("INSERT INTO foo VALUES ( '$_' )"), 'INSERT without bind' ); + my $vals = $dbh->selectcol_arrayref("SELECT bar FROM foo"); + is $vals->[0], $vals->[1], "both values are equal"; + + ok( $dbh->do("DELETE FROM foo"), 'DELETE ok' ); +} From 28ea4bfad784f9075baf9b05241cd2279ae63905 Mon Sep 17 00:00:00 2001 From: Kenichi Ishigaki Date: Mon, 7 Jul 2014 09:42:23 +0900 Subject: [PATCH 2/2] moved utf8::upgrade in dbdimp.c for performance - and uprade occurs only if imp_dbh->unicode is true so that we can minimize impact on existing applications --- dbdimp.c | 10 +++++++++- dbdimp.h | 2 +- lib/DBD/SQLite.pm | 3 --- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/dbdimp.c b/dbdimp.c index f47e618..5d161fa 100644 --- a/dbdimp.c +++ b/dbdimp.c @@ -586,11 +586,12 @@ sqlite_db_last_insert_id(SV *dbh, imp_dbh_t *imp_dbh, SV *catalog, SV *schema, S } int -sqlite_st_prepare(SV *sth, imp_sth_t *imp_sth, char *statement, SV *attribs) +sqlite_st_prepare_sv(SV *sth, imp_sth_t *imp_sth, SV *sv_statement, SV *attribs) { dTHX; int rc = 0; const char *extra; + char *statement; D_imp_dbh_from_sth; if (!DBIc_ACTIVE(imp_dbh)) { @@ -598,6 +599,13 @@ sqlite_st_prepare(SV *sth, imp_sth_t *imp_sth, char *statement, SV *attribs) return FALSE; /* -> undef in lib/DBD/SQLite.pm */ } + /* sqlite3_prepare wants an utf8-encoded SQL statement */ + if (imp_dbh->unicode) { + sv_utf8_upgrade(sv_statement); + } + + statement = SvPV_nolen(sv_statement); + #if 0 if (*statement == '\0') { sqlite_error(sth, -2, "attempt to prepare empty statement"); diff --git a/dbdimp.h b/dbdimp.h index 944d5d2..de2da1e 100644 --- a/dbdimp.h +++ b/dbdimp.h @@ -64,7 +64,7 @@ struct imp_sth_st { #define dbd_db_STORE_attrib sqlite_db_STORE_attrib #define dbd_db_FETCH_attrib sqlite_db_FETCH_attrib #define dbd_db_last_insert_id sqlite_db_last_insert_id -#define dbd_st_prepare sqlite_st_prepare +#define dbd_st_prepare_sv sqlite_st_prepare_sv #define dbd_st_rows sqlite_st_rows #define dbd_st_execute sqlite_st_execute #define dbd_st_fetch sqlite_st_fetch diff --git a/lib/DBD/SQLite.pm b/lib/DBD/SQLite.pm index 87ca909..759efdd 100644 --- a/lib/DBD/SQLite.pm +++ b/lib/DBD/SQLite.pm @@ -196,9 +196,6 @@ sub prepare { my $sql = shift; $sql = '' unless defined $sql; - # sqlite3_prepare wants an utf8-encoded SQL statement - utf8::upgrade($sql); - my $sth = DBI::_new_sth( $dbh, { Statement => $sql, } );