It looks like $sth->bind_param should've supported passing named parameters.
...Unfortunately, it was broken from pre-historic times (before 1.14).
Patch with fix and regression test update attached.
>From afdb04d09c895d4a41897c63dd60d85525ea6db1 Mon Sep 17 00:00:00 2001
From: "Yuriy M. Kaminskiy" <yum...@gmail.com>
Date: Mon, 17 Oct 2011 07:33:01 +0400
Subject: [PATCH] Fix binding named parameters (:name, @name, $name)

---
 dbdimp.c            |    2 +-
 t/41_placeholders.t |   24 +++++++++++++++++++++++-
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/dbdimp.c b/dbdimp.c
index bd3f415..1da8034 100644
--- a/dbdimp.c
+++ b/dbdimp.c
@@ -1219,8 +1219,8 @@ sqlite_bind_ph(SV *sth, imp_sth_t *imp_sth,
             sqlite_error(sth, -2, "InOut bind params not implemented");
             return FALSE; /* -> &sv_no in SQLite.xsi */
         }
+        pos = 2 * (SvIV(param) - 1);
     }
-    pos = 2 * (SvIV(param) - 1);
     sqlite_trace(sth, imp_sth, 3, form("bind into 0x%p: %"IVdf" => %s (%"IVdf") pos %d", imp_sth->params, SvIV(param), SvPV_nolen_undef_ok(value), sql_type, pos));
     av_store(imp_sth->params, pos, SvREFCNT_inc(value));
     if (sql_type)
diff --git a/t/41_placeholders.t b/t/41_placeholders.t
index 0b6371d..eaedb00 100644
--- a/t/41_placeholders.t
+++ b/t/41_placeholders.t
@@ -10,7 +10,7 @@ use t::lib::Test qw/connect_ok/;
 use Test::More;
 use Test::NoWarnings;
 
-plan tests => 9;
+plan tests => 13;
 
 my $dbh = connect_ok( RaiseError => 1 );
 ok $dbh->do('create table foo (id integer, value integer)');
@@ -19,6 +19,28 @@ ok $dbh->do('insert into foo values(?, ?)',   undef, 1, 2);
 ok $dbh->do('insert into foo values(?1, ?2)', undef, 2, 3);
 ok $dbh->do('insert into foo values(:1, :2)', undef, 3, 4);
 ok $dbh->do('insert into foo values(@1, @2)', undef, 4, 4);
+my $sth = $dbh->prepare('insert into foo values(:foo, :bar)');
+ok $sth, "prepared sth with named parameters";
+$sth->bind_param(':foo', 5);
+$sth->bind_param(':bar', 6);
+my $warn;
+eval {
+    local $SIG{__WARN__} = sub { $warn = shift; };
+    $sth->bind_param(':baz', "AAAAAAA");
+};
+ok $@, "binding unexisting named parameters returns error";
+print "# expected bind error: $@";
+ok $warn, "... and warning";
+print "# expected bind warning: $@";
+$sth->execute;
+{
+    my ($count) = $dbh->selectrow_array(
+    	'select count(id) from foo where id = ? and value = ?',
+    	undef, 5, 6
+    );
+
+    ok $count == 1, "successfully inserted row with named placeholders";
+}
 
 SKIP: {
 	skip "this placeholder requires SQLite 3.6.19 and newer", 2 
-- 
1.7.6.3

_______________________________________________
DBD-SQLite mailing list
DBD-SQLite@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbd-sqlite

Reply via email to