summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgregor herrmann <gregoa@debian.org>2020-09-20 16:47:44 +0200
committergregor herrmann <gregoa@debian.org>2020-09-20 16:47:44 +0200
commit02ea73f6006623a01b6fea22146fd768b51993d9 (patch)
treeee7403853468bec81303e0e3a6ef92e86c84d093
parent713e12f8fd368ebf4a0ad45d146542d507a84724 (diff)
parenta44a00b4ae4d59a70d3b3126e36adbd43770efda (diff)
New upstream version 1.57
-rw-r--r--Changes7
-rw-r--r--META.json4
-rw-r--r--META.yml4
-rw-r--r--README.md23
-rw-r--r--lib/DBD/Mock.pm26
-rw-r--r--lib/DBD/Mock/StatementTrack.pm4
-rw-r--r--lib/DBD/Mock/st.pm10
-rw-r--r--t/016_mock_add_resultset_test.t51
-rw-r--r--t/025_mock_last_insert_id.t43
9 files changed, 160 insertions, 12 deletions
diff --git a/Changes b/Changes
index 1bf64be..f347f3f 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,12 @@
Revision history for Perl extension DBD::Mock.
+1.57 2020-09-18T06:57:48Z
+ - Fixed bug rt133358 t/016_mock_add_resultset_test.t fails (with older DBI)
+
+1.56 2020-09-17T14:35:10Z
+ - The DBD::st module now supports the last_insert_id method
+ - Result sets with callbacks can now specify a last_insert_id
+
1.55 2019-12-30T14:20:00Z
- Fixed bug rt131264 t/033_table_info.t fails (with older DBI)
diff --git a/META.json b/META.json
index c817a3d..b2996b8 100644
--- a/META.json
+++ b/META.json
@@ -61,7 +61,7 @@
"provides" : {
"DBD::Mock" : {
"file" : "lib/DBD/Mock.pm",
- "version" : "1.55"
+ "version" : "1.57"
},
"DBD::Mock::Pool" : {
"file" : "lib/DBD/Mock/Pool.pm"
@@ -96,7 +96,7 @@
"web" : "https://gitlab.com/scrapheap/DBD-Mock"
}
},
- "version" : "1.55",
+ "version" : "1.57",
"x_authority" : "cpan:JLCOOPER",
"x_contributors" : [
"Bernhard Graf <GRAF@cpan.org>",
diff --git a/META.yml b/META.yml
index 7313e00..41f7246 100644
--- a/META.yml
+++ b/META.yml
@@ -31,7 +31,7 @@ no_index:
provides:
DBD::Mock:
file: lib/DBD/Mock.pm
- version: '1.55'
+ version: '1.57'
DBD::Mock::Pool:
file: lib/DBD/Mock/Pool.pm
DBD::Mock::Pool::db:
@@ -55,7 +55,7 @@ requires:
resources:
homepage: https://gitlab.com/scrapheap/DBD-Mock
repository: git://gitlab.com/scrapheap/DBD-Mock.git
-version: '1.55'
+version: '1.57'
x_authority: cpan:JLCOOPER
x_contributors:
- 'Bernhard Graf <GRAF@cpan.org>'
diff --git a/README.md b/README.md
index 15acad7..d393816 100644
--- a/README.md
+++ b/README.md
@@ -1202,6 +1202,29 @@ great caution (if at all).
property to be undef in this situation then set the
`$DBD::Mock::DefaultFieldsToUndef` flag to `1`.
+ If you're mocking an INSERT statement with a callback and you want to
+ explicitly set the database's `last_insert_id` value then you can use the
+ `last_insert_id` key in the result set. If you don't specify a
+ `last_insert_id` then the standard `DBD::Mock` logic for generating an value
+ for the last inserted item will be followed. This will allow you to mock
+ MySQL/MariaDB INSERT queries that use `ON DUPLICATE KEY UPDATE` logic to set
+ the `last_insert_id`.
+
+ $dbh->{mock_add_resultset} = {
+ sql => 'INSERT INTO y ( x ) VALUES ( ? ) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID( id )',
+ callback => sub {
+ my @bound_params = @_;
+
+ my %result = (
+ fields => [],
+ rows => [],
+ last_insert_id => 99,
+ );
+
+ return %result;
+ },
+ };
+
# BUGS
- Odd `$dbh` attribute behavior
diff --git a/lib/DBD/Mock.pm b/lib/DBD/Mock.pm
index 3402c1e..c1cf14b 100644
--- a/lib/DBD/Mock.pm
+++ b/lib/DBD/Mock.pm
@@ -30,7 +30,7 @@ sub import {
if ( @_ && lc( $_[0] ) eq "pool" );
}
-our $VERSION = '1.55';
+our $VERSION = '1.57';
our $drh = undef; # will hold driver handle
our $err = 0; # will hold any error codes
@@ -1398,6 +1398,30 @@ stays compatible with previous versions. If you need the C<NUM_OF_FIELDS>
property to be undef in this situation then set the
C<$DBD::Mock::DefaultFieldsToUndef> flag to C<1>.
+If you're mocking an INSERT statement with a callback and you want to
+explicitly set the database's C<last_insert_id> value then you can use the
+C<last_insert_id> key in the result set. If you don't specify a
+C<last_insert_id> then the standard C<DBD::Mock> logic for generating an value
+for the last inserted item will be followed. This will allow you to mock
+MySQL/MariaDB INSERT queries that use C<ON DUPLICATE KEY UPDATE> logic to set
+the C<last_insert_id>.
+
+
+ $dbh->{mock_add_resultset} = {
+ sql => 'INSERT INTO y ( x ) VALUES ( ? ) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID( id )',
+ callback => sub {
+ my @bound_params = @_;
+
+ my %result = (
+ fields => [],
+ rows => [],
+ last_insert_id => 99,
+ );
+
+ return %result;
+ },
+ };
+
=back
diff --git a/lib/DBD/Mock/StatementTrack.pm b/lib/DBD/Mock/StatementTrack.pm
index 31dedda..60fae31 100644
--- a/lib/DBD/Mock/StatementTrack.pm
+++ b/lib/DBD/Mock/StatementTrack.pm
@@ -141,6 +141,10 @@ sub mark_executed {
if (ref $recordSet{rows} eq "ARRAY") {
$self->{return_data} = $recordSet{rows};
}
+
+ if (defined $recordSet{last_insert_id}) {
+ $self->{last_insert_id} = $recordSet{last_insert_id};
+ }
}
}
diff --git a/lib/DBD/Mock/st.pm b/lib/DBD/Mock/st.pm
index 66d5468..44229d3 100644
--- a/lib/DBD/Mock/st.pm
+++ b/lib/DBD/Mock/st.pm
@@ -131,7 +131,10 @@ sub execute {
#use Data::Dumper;warn Dumper $dbh->{mock_last_insert_ids};
if ( $dbh->{Statement} =~ /^\s*?insert(?:\s+ignore)?\s+into\s+(\S+)/i ) {
- if ( $dbh->{mock_last_insert_ids}
+ if ( $tracker->{last_insert_id} ) {
+ $dbh->{mock_last_insert_id} = $tracker->{last_insert_id};
+
+ } elsif ( $dbh->{mock_last_insert_ids}
&& exists $dbh->{mock_last_insert_ids}{$1} )
{
$dbh->{mock_last_insert_id} = $dbh->{mock_last_insert_ids}{$1}++;
@@ -298,6 +301,11 @@ sub fetchall_hashref {
return $rethash;
}
+sub last_insert_id {
+ my ( $sth, @params ) = @_;
+ return $sth->{Database}->last_insert_id( @params );
+}
+
sub finish {
my ($sth) = @_;
$sth->FETCH('mock_my_history')->is_finished('yes');
diff --git a/t/016_mock_add_resultset_test.t b/t/016_mock_add_resultset_test.t
index 6743a82..eac27e1 100644
--- a/t/016_mock_add_resultset_test.t
+++ b/t/016_mock_add_resultset_test.t
@@ -317,4 +317,55 @@ $dbh->{mock_add_resultset} = {
$sth->finish();
}
+$dbh->{mock_start_insert_id} = [ 'y', 4 ];
+
+$dbh->{mock_add_resultset} = {
+ sql => 'INSERT INTO y ( x ) VALUES ( ? )',
+ callback => sub {
+ my @bound_params = @_;
+
+ my %result = (
+ fields => [],
+ rows => []
+ );
+
+ return %result;
+ },
+};
+
+{
+ my $sth = $dbh->prepare( 'INSERT INTO y ( x ) VALUES ( ? )' );
+
+ $sth->execute( 'test' );
+
+ is( $dbh->last_insert_id( (undef) x 4 ), 4, "last_insert_id should return the next Id value after an insert as our callback doesn't override it")
+}
+
+
+$dbh->{mock_add_resultset} = {
+ sql => 'INSERT INTO y ( x ) VALUES ( ? )',
+ callback => sub {
+ my @bound_params = @_;
+
+ my %result = (
+ fields => [],
+ rows => [],
+ last_insert_id => 99,
+ );
+
+ return %result;
+ },
+};
+
+{
+ my $sth = $dbh->prepare( 'INSERT INTO y ( x ) VALUES ( ? )' );
+
+ $sth->execute( 'test' );
+
+ is( $dbh->last_insert_id( (undef) x 4 ), 99, "last_insert_id should return the id the callback has provided");
+
+ is( $dbh->{mock_last_insert_ids}{y}, 5, "If we provide a last_insert_id value then the one stored against the table shouldn't be updated");
+}
+
+
done_testing();
diff --git a/t/025_mock_last_insert_id.t b/t/025_mock_last_insert_id.t
index 264bab6..10b398c 100644
--- a/t/025_mock_last_insert_id.t
+++ b/t/025_mock_last_insert_id.t
@@ -18,15 +18,31 @@ $dbh->{mock_start_insert_id} = ['Baz', 345];
$sth->execute(15, 17);
is($dbh->{mock_last_insert_id}, 123, '... got the right insert id');
- is($dbh->last_insert_id((undef)x4), 123, '... got the right insert id from last_insert_id');
+ is($dbh->last_insert_id((undef)x4), 123, "... got the right insert id from the database's last_insert_id");
+
+ SKIP: {
+ skip "Version of DBI::st doesn't support last_insert_id" unless $sth->can('last_insert_id');
+
+ is($sth->last_insert_id((undef)x4), 123, "... got the right insert id from the statement handle's last_insert_id");
+ }
$sth->execute(16, 18);
is($dbh->{mock_last_insert_id}, 124, '... got the right insert id');
- is($dbh->last_insert_id((undef)x4), 124, '... got the right insert id from last_insert_id');
+ is($dbh->last_insert_id((undef)x4), 124, "... got the right insert id from the database handle's last_insert_id");
+ SKIP: {
+ skip "Version of DBI::st doesn't support last_insert_id" unless $sth->can('last_insert_id');
+
+ is($sth->last_insert_id((undef)x4), 124, "... got the right insert id from the statement handle's last_insert_id");
+ }
$sth->execute(19, 34);
is($dbh->{mock_last_insert_id}, 125, '... got the right insert id');
- is($dbh->last_insert_id((undef)x4), 125, '... got the right insert id from last_insert_id');
+ is($dbh->last_insert_id((undef)x4), 125, "... got the right insert id from the database handle's last_insert_id");
+ SKIP: {
+ skip "Version of DBI::st doesn't support last_insert_id" unless $sth->can('last_insert_id');
+
+ is($sth->last_insert_id((undef)x4), 125, "... got the right insert id from the statement handle's last_insert_id");
+ }
}
{
@@ -34,15 +50,30 @@ $dbh->{mock_start_insert_id} = ['Baz', 345];
$sth->execute(90, 41);
is($dbh->{mock_last_insert_id}, 345, '... got the right insert id');
- is($dbh->last_insert_id((undef)x4), 345, '... got the right insert id from last_insert_id');
+ is($dbh->last_insert_id((undef)x4), 345, "... got the right insert id from the database handle's last_insert_id");
+ SKIP: {
+ skip "Version of DBI::st doesn't support last_insert_id" unless $sth->can('last_insert_id');
+
+ is($sth->last_insert_id((undef)x4), 345, "... got the right insert id from the statement handle's last_insert_id");
+ }
$sth->execute(32, 71);
is($dbh->{mock_last_insert_id}, 346, '... got the right insert id');
- is($dbh->last_insert_id((undef)x4), 346, '... got the right insert id from last_insert_id');
+ is($dbh->last_insert_id((undef)x4), 346, "... got the right insert id from the database handle's last_insert_id");
+ SKIP: {
+ skip "Version of DBI::st doesn't support last_insert_id" unless $sth->can('last_insert_id');
+
+ is($sth->last_insert_id((undef)x4), 346, "... got the right insert id from the statement handle's last_insert_id");
+ }
$sth->execute(77, 42);
is($dbh->{mock_last_insert_id}, 347, '... got the right insert id');
- is($dbh->last_insert_id((undef)x4), 347, '... got the right insert id from last_insert_id');
+ is($dbh->last_insert_id((undef)x4), 347, "... got the right insert id from the database handle's last_insert_id");
+ SKIP: {
+ skip "Version of DBI::st doesn't support last_insert_id" unless $sth->can('last_insert_id');
+
+ is($sth->last_insert_id((undef)x4), 347, "... got the right insert id from the statement handle's last_insert_id");
+ }
}
done_testing();