Subject: | Return new result set for repeated calls to $sth->execute() |
Hi,
this is in reference to my previous report on the mailing list http://groups.google.de/group/DBDMock/browse_thread/thread/5aa0916b7f19d28f/f2106c67ce8e83a5?lnk=st&q=dbdmock&rnum=1#f2106c67ce8e83a5
, against DBD::Mock-1.32.
After calling $sth->prepare(), repeated calls to $sth->execute() should return different result sets and handles as separate statements. This is the normal use case of
my $sth = $dbh->prepare("select * from foo where bar=?");
for my $i (1..3) {
$sth->execute($i);
my @results = $sth->fetchrow_array();
#...
}
Attached patch contains a test for this scenario and a fix (I still think my fix is butt-ugly but it works and passes all tests, so yay me ;-).
--- DBD-Mock-1.32/t/022_DBD_Mock_Session_bound_params.t 2005-12-14 16:34:03.000000000 +0100
+++ DBD-Mock-1.32-altered/t/022_DBD_Mock_Session_bound_params.t 2006-01-07 10:45:48.000000000 +0100
@@ -1,6 +1,6 @@
use strict;
-use Test::More tests => 24;
+use Test::More tests => 29;
BEGIN {
use_ok('DBD::Mock');
@@ -146,3 +146,35 @@
'... everything failed as planned');
}
+{
+ my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
+ isa_ok($dbh, 'DBI::db');
+
+ my $session = DBD::Mock::Session->new((
+ {
+ statement => 'SELECT foo FROM bar WHERE baz = ?',
+ bound_params => [ 100 ],
+ results => [[ 'foo' ], [ 10 ]]
+ },
+ {
+ statement => 'SELECT foo FROM bar WHERE baz = ?',
+ bound_params => [ 125 ],
+ results => [[ 'foo' ], [ 15 ]]
+ },
+ ));
+ isa_ok($session, 'DBD::Mock::Session');
+
+ $dbh->{mock_session} = $session;
+
+ eval {
+ my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
+ $sth->execute(100);
+ my ($result) = $sth->fetchrow_array();
+ cmp_ok($result, '==', 10, '... first execute got the right value');
+ $sth->execute(125);
+ my ($result) = $sth->fetchrow_array();
+ cmp_ok($result, '==', 15, '... second execute got the right value');
+ };
+ ok(!$@, '... everything worked as planned');
+
+ }
--- DBD-Mock-1.32/lib/DBD/Mock.pm 2005-12-14 16:34:03.000000000 +0100
+++ DBD-Mock-1.32-altered/lib/DBD/Mock.pm 2006-01-07 11:38:21.000000000 +0100
@@ -570,6 +570,10 @@
if (my $session = $dbh->{mock_session}) {
eval {
$session->verify_bound_params($dbh, $tracker->bound_params());
+ my $idx=$session->{state_index}-1;
+ my @results=@{$session->{states}[$idx]{results}};
+ shift @results;
+ $tracker->{return_data}=\@results;
};
if ($@) {
my $session_error = $@;