Subject: | mock_{last,start}_insert_id docs are wrong (and confusing) |
The docs for last_insert_id have a mistake in the code sample, and are
generally a bit confusing. I've attached a patch which fixes the code
and also improves clarity (IMO). I also mentioned that if you SQL has
quoted table names, you need to quote the names you pass to
mock_start_insert_id.
Subject: | mock.patch |
diff -ru ../DBD-Mock-1.36/lib/DBD/Mock.pm ./lib/DBD/Mock.pm
--- ../DBD-Mock-1.36/lib/DBD/Mock.pm 2007-10-19 21:47:10.000000000 -0500
+++ ./lib/DBD/Mock.pm 2008-02-13 16:44:08.000000000 -0600
@@ -1588,9 +1588,7 @@
This attribute is incremented each time an INSERT statement is passed to C<prepare> on a per-handle basis. It's starting value can be set with the 'mock_start_insert_id' attribute (see below).
-This attribute also can be used with an ARRAY ref parameter, it's behavior is slightly different in that instead of incrementing the value for every C<prepare> it will only increment for each C<execute>. This allows it to be used over multiple C<execute> calls in a single C<$sth>. It's usage looks like this:
-
- $dbh->{mock_last_insert_id} = [ 'Foo', 10 ];
+ $dbh->{mock_start_insert_id} = 10;
my $sth = $dbh->prepare('INSERT INTO Foo (foo, bar) VALUES(?, ?)');
@@ -1606,6 +1604,25 @@
This attribute can be used to set a start value for the 'mock_last_insert_id' attribute. It can also be used to effectively reset the 'mock_last_insert_id' attribute as well.
+This attribute also can be used with an ARRAY ref parameter, it's behavior is slightly different in that instead of incrementing the value for every C<prepare> it will only increment for each C<execute>. This allows it to be used over multiple C<execute> calls in a single C<$sth>. It's usage looks like this:
+
+ $dbh->{mock_start_insert_id} = [ 'Foo', 10 ];
+ $dbh->{mock_start_insert_id} = [ 'Baz', 20 ];
+
+ my $sth1 = $dbh->prepare('INSERT INTO Foo (foo, bar) VALUES(?, ?)');
+
+ my $sth2 = $dbh->prepare('INSERT INTO Baz (baz, buz) VALUES(?, ?)');
+
+ $sth1->execute(1, 2);
+ # $dbh->{mock_last_insert_id} == 10
+
+ $sth2->execute(3, 4);
+ # $dbh->{mock_last_insert_id} == 20
+
+Note that DBD::Mock's matching of table names in 'INSERT' statements is fairly simple, so if your table names are quoted in the insert statement (C<INSERT INTO "Foo">) then you need to quote the name for C<mock_start_insert_id>:
+
+ $dbh->{mock_start_insert_id} = [ q{"Foo"}, 10 ];
+
=item B<mock_add_parser>
DBI provides some simple parsing capabilities for 'SELECT' statements to ensure that placeholders are bound properly. And typically you may simply want to check after the fact that a statement is syntactically correct, or at least what you expect.