diff --git a/dbdimp.c b/dbdimp.c index e95c117..fcc2161 100644 --- a/dbdimp.c +++ b/dbdimp.c @@ -1396,6 +1396,20 @@ sqlite_st_FETCH_attrib(SV *sth, imp_sth_t *imp_sth, SV *keysv) else if (strEQ(key, "NUM_OF_PARAMS")) { retsv = sv_2mortal(newSViv(sqlite3_bind_parameter_count(imp_sth->stmt))); } + else if (strEQ(key, "ParamValues")) { + HV *hv = newHV(); + int num_params = DBIc_NUM_PARAMS(imp_sth); + if (num_params) { + for (n = 0; n < num_params; n++) { + SV **pvalue = av_fetch(imp_sth->params, 2 * n, 0); + SV *value = pvalue ? *pvalue : &PL_sv_undef; + const char *pname = sqlite3_bind_parameter_name(imp_sth->stmt, n + 1); + SV *sv_name = pname ? newSVpv(pname, 0) : newSViv(n + 1); + hv_store_ent(hv, sv_name, newSVsv(value), 0); + } + } + retsv = sv_2mortal(newRV_noinc((SV*)hv)); + } return retsv; } diff --git a/t/63_param_values.t b/t/63_param_values.t new file mode 100644 index 0000000..0acb54c --- /dev/null +++ b/t/63_param_values.t @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +use strict; +BEGIN { + $| = 1; + $^W = 1; +} + +use lib "t/lib"; +use SQLiteTest qw/connect_ok/; +use Test::More; +use Test::NoWarnings; + +plan tests => 7; + +my $dbh = connect_ok( RaiseError => 1 ); +ok $dbh->do('create table foo (id integer, value integer)'); +{ + my $sth = $dbh->prepare('select * from foo where id = ?'); + $sth->execute(100); + is_deeply $sth->{ParamValues} => {1 => 100}, "ParamValues after execution"; +} +{ + my $sth = $dbh->prepare('select * from foo where id = :AAA'); + $sth->execute(100); + is_deeply $sth->{ParamValues} => {':AAA' => 100}, "ParamValues after execution (named parameter)"; +} +{ + my $sth = $dbh->prepare('select * from foo where id = ?'); + $sth->bind_param(1, 100); + is_deeply $sth->{ParamValues} => {1 => 100}, "ParamValues before execution"; +} +{ + my $sth = $dbh->prepare('select * from foo where id = ?'); + is_deeply $sth->{ParamValues} => {1 => undef}, "ParamValues without binding"; +}