Skip Menu |

This queue is for tickets about the DBD-Mock CPAN distribution.

Report information
The Basics
Id: 71438
Status: resolved
Priority: 0/
Queue: DBD-Mock

People
Owner: Nobody in particular
Requestors: JIRA [...] cpan.org
Cc: dichoso [...] gmail.com
FBRIERE [...] cpan.org
AdminCc:

Bug Information
Severity: Important
Broken in: 1.43
Fixed in: 1.45



CC: dichoso [...] gmail.com
Subject: Problem with fetchrow_hashref
There is a problem with fetchrow_hashref. When used with session it always returns an empty hashref. my $query = 'SELECT foo, bar FROM baz WHERE id=?'; my $session = DBD::Mock::Session->new('test_session' => ( { statement => $query, results => [['foo', 'bar'], [1, 'test1'], [2, 'test2']], bound_params => [ 1 ], })); my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) || die "Cannot create handle: $DBI::errstr\n"; $dbh->{mock_session} = $session; my $sth = $dbh->prepare($query); $sth->execute(1); while (my $hr = $sth->fetchrow_hashref) { # Empty hash ref returned warn Dumper($hr); } # This works $dbh->{mock_session} = undef; $dbh->{mock_add_resultset} = [['foo', 'bar'], [1, 'test1'], [2, 'test2']], $sth = $dbh->prepare($query); $sth->execute(1); while (my $hr = $sth->fetchrow_hashref) { warn Dumper($hr); }
From: ejtrochim [...] alaska.edu
I ran into this problem myself and took a look at it. It seems that the field names were not being saved. Attached is a patch that seems to fix the issue. It properly runs your test program and passes all the automated tests.
Subject: save-field-names.patch
diff -Naur a/lib/DBD/Mock/st.pm b/lib/DBD/Mock/st.pm --- a/lib/DBD/Mock/st.pm 2011-08-28 19:12:03.000000000 -0800 +++ b/lib/DBD/Mock/st.pm 2011-11-12 10:22:52.000000000 -0900 @@ -71,8 +71,9 @@ # Load a copy of the results to return (minus the field # names) into the tracker my @results = @{ $state->{results} }; - shift @results; + my $fields = shift @results; $tracker->{return_data} = \@results; + $tracker->fields(@$fields) if ref $fields eq 'ARRAY'; }; if ($@) { my $session_error = $@;
From: Paul
On Sat Nov 12 14:32:04 2011, ejtrochim wrote: Show quoted text
> I ran into this problem myself and took a look at it. It seems that the > field names were not being saved. Attached is a patch that seems to fix > the issue. It properly runs your test program and passes all the > automated tests.
I had the same problem. The patch fixed it. Thanks!
RT-Send-CC: ejtrochim [...] alaska.edu
On Sat Nov 12 14:32:04 2011, ejtrochim wrote: Show quoted text
> Attached is a patch that seems to fix > the issue. It properly runs your test program and passes all the > automated tests.
Unfortunately, your patch only works correctly for the first session state. (Sorry!) Running the attached script against 1.41: $VAR1 = { 'answer' => 42 }; $VAR1 = { 'answer' => 42 }; $VAR1 = { 'answer' => 42 }; Against 1.43 unpatched: $VAR1 = {}; $VAR1 = { 'answer' => 42 }; $VAR1 = { 'answer' => 42 }; Against 1.43 patched: $VAR1 = { 'answer' => 42 }; $VAR1 = { 'answer' => undef }; $VAR1 = { 'answer' => undef };
Subject: bug_new.pl
use DBI; use Data::Dumper; use constant N => 3; my $dbh = DBI->connect('DBI:Mock:', '', '') or die "Cannot create handle: $DBI::errstr\n"; my $state = { statement => 'SELECT * FROM life', results => [ ['answer'], [42] ], }; $dbh->{mock_session} = new DBD::Mock::Session ('foo' => ($state) x N); for (1 .. N) { print Dumper $dbh->selectrow_hashref("SELECT * FROM life"); }
From: tnt [...] netsafe.cz
This bug has been introduced in 1.41 (in 1.39 it is working) - moving of calling ->verify_statement() broke this, because this method has side-effect - it set {mock_rs} attribute. If I revert this diff, fetchrow_hashref is working: diff -r DBD-Mock-1.39/lib/DBD/Mock.pm DBD-Mock-1.41/lib/DBD/Mock.pm 23c23 < our $VERSION = '1.39'; --- Show quoted text
> our $VERSION = '1.41';
248,259d247 < if (my $session = $dbh->FETCH('mock_session')) { < eval { < $session->verify_statement($dbh, $statement); < }; < if ($@) { < my $session_error = $@; < chomp $session_error; < $dbh->DBI::set_err(1, "Session Error: ${session_error}. Statement: ${statement}"); < return; < } < } < 621a612 Show quoted text
> $session->verify_statement($dbh, $sth->{Statement});
From: tnt [...] netsafe.cz
There is working patch for this bug. With this changes are all my DB unittest working.
Subject: fix-DBD-Mock-1.43-fetchrow_hashref.diff
diff -urNp DBD-Mock-1.43/lib/DBD/Mock/db.pm DBD-Mock-1.43-patched/lib/DBD/Mock/db.pm --- DBD-Mock-1.43/lib/DBD/Mock/db.pm 2011-08-29 03:12:03.000000000 +0000 +++ DBD-Mock-1.43-patched/lib/DBD/Mock/db.pm 2012-08-09 12:40:37.000000000 +0000 @@ -54,6 +54,21 @@ sub prepare { return; } + if (my $session = $dbh->FETCH('mock_session')) { + eval { + $session->verify_statement($dbh, $statement); + # copy the result sets so that + # we can re-use the session + $dbh->STORE( 'mock_add_resultset' => [ @{ $session->current_state->{results} } ] ); + }; + if ($@) { + my $session_error = $@; + chomp $session_error; + $dbh->DBI::set_err(1, "Session Error: ${session_error}. Statement: ${statement}"); + return; + } + } + my $sth = DBI::_new_sth( $dbh, { Statement => $statement } ); $sth->trace_msg( "Preparing statement '${statement}'\n", 1 ); diff -urNp DBD-Mock-1.43/lib/DBD/Mock/Session.pm DBD-Mock-1.43-patched/lib/DBD/Mock/Session.pm --- DBD-Mock-1.43/lib/DBD/Mock/Session.pm 2011-08-29 03:12:03.000000000 +0000 +++ DBD-Mock-1.43-patched/lib/DBD/Mock/Session.pm 2012-08-09 12:38:42.000000000 +0000 @@ -91,10 +91,6 @@ sub verify_statement { "Bad 'statement' value '$SQL' in current state in DBD::Mock::Session (" . $self->{name} . ")"; } - - # copy the result sets so that - # we can re-use the session - $dbh->STORE( 'mock_add_resultset' => [ @{ $current_state->{results} } ] ); } sub verify_bound_params { diff -urNp DBD-Mock-1.43/t/bug_71438_fetchrow_hashref.t DBD-Mock-1.43-patched/t/bug_71438_fetchrow_hashref.t --- DBD-Mock-1.43/t/bug_71438_fetchrow_hashref.t 1970-01-01 00:00:00.000000000 +0000 +++ DBD-Mock-1.43-patched/t/bug_71438_fetchrow_hashref.t 2012-08-09 12:23:12.000000000 +0000 @@ -0,0 +1,70 @@ +#!/usr/bin/perl +use Test::More tests => 6; +use strict; +use warnings; +use Test::Exception; +use DBI; +use DBD::Mock; + +my $dbh = DBI->connect('dbi:Mock:', '', '', { PrintError => 0, RaiseError => 1}); + +my $query = 'SELECT foo, bar FROM baz WHERE id=?'; +my @session = ( + { + statement => $query, + results => [ + ['foo', 'bar'], + [1, 'test1'], + [2, 'test2'] + ], + bound_params => [ 1 ], + }, + { + statement => $query, + results => [ + ['abc', 'xyz'], + [7, 'test7'], + [8, 'test8'] + ], + bound_params => [ 2 ], + }, +); +$dbh->{mock_session} = DBD::Mock::Session->new(@session); + +# First query +my $sth = $dbh->prepare($query); +$sth->execute(1); + +is_deeply( + $sth->fetchrow_hashref(), + {foo => 1, bar => 'test1'} +); + +is_deeply( + $sth->fetchrow_hashref(), + {foo => 2, bar => 'test2'} +); + +is_deeply( + $sth->fetchrow_hashref(), + undef +); + +# Second query +$sth = $dbh->prepare($query); +$sth->execute(2); + +is_deeply( + $sth->fetchrow_hashref(), + {abc => 7, xyz => 'test7'} +); + +is_deeply( + $sth->fetchrow_hashref(), + {abc => 8, xyz => 'test8'} +); + +is_deeply( + $sth->fetchrow_hashref(), + undef +);
What is the status of this bug? Looks like 1.45 fixed this but its not acknowledged in the (still) open bug.
Hi, It looks like this bug was resolved in either 1.44 or 1.45. I've added a test for the bug to the codebase (just to make sure it doesn't reappear in the future). Regards, Jason.