Skip Menu |

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

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

People
Owner: Nobody in particular
Requestors: EHUELS [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Single statement preparation, multiple execute() invocations -- advice needed
Hi, I'm executing something which looks like the following loop: sub some_sub { my $dbh = DBI->connect('dbi:Mock', '', ''); $sth = $dbh->prepare('select 1 from db_patches where sha = ?'); my $found = 0; for my $sha (@sha_values) { $sth->execute($sha); my $rv = $sth->fetchall_arrayref; $sth->finish; $found = scalar @$rv; last if $found; } return $found; } Now, I have a test case where I want the first execute to return no rows (zero rows), but the second execute should return a single row. From the docs on MetaCPAN, it's unclear to me how to achieve this. Please advise! Thanks in advance. Erik http://ledgersmb.org/ -- Open Source ERP in Perl
Subject: [rt.cpan.org #127074] Single statement preparation, multiple execute() invocations
Date: Thu, 20 Jun 2019 11:06:44 +0100
To: bug-DBD-Mock [...] rt.cpan.org
From: Jason Cooper <scrapheap [...] heckrothindustries.co.uk>
Hi, I appreciate this is an old ticket but hopefully this response will be helpful for other. To do what you want you'll need to use a mock session (DBD::Mock::Session). That lets you define a sequence of expected SQL statements and the results they should return. Something like the following should do the trick: use DBI; use DBD::Mock::Session; my $dbh = DBI->connect('DBI:Mock:', '', ''); $dbh->{mock_session} = DBD::Mock::Session->new( mySession => ( { statement => "select 1 from db_patches where sha = ?", results => [[]], }, { statement => "select 1 from db_patches where sha = ?", results => [[1],[1]], }, )); warn some_sub('a', 'b', 'c') . "\n"; sub some_sub { my @sha_values = @_; my $sth = $dbh->prepare('select 1 from db_patches where sha = ?'); my $found = 0; for my $sha (@sha_values) { warn "looking up $sha\n"; $sth->execute($sha); my $rv = $sth->fetchall_arrayref; $sth->finish; $found = scalar @$rv; last if $found; } return $found; } Regards, Jason.