Subject: | Line 271 of lib/DBIx/Simple.pm erroneously calls DBI statement handle object's finish() method, destroying first query error message returned by DBMS |
Date: | Mon, 3 Aug 2015 01:45:56 +0100 |
To: | bug-DBIx-Simple [...] rt.cpan.org |
From: | Chris Pibbs <chrispbs.rt [...] gmail.com> |
Line 271 of lib/DBIx/Simple.pm makes a call to the finish() method on a DBI
statement handle object. For whatever reason, DBIx::Simple::Statement's
_die() method is called whenever a DBMS error is encountered for the first
time while executing a query, and because finish() is not a method that
preserves the values of DBI's err and errstr, the call nulls DBI's error
message before the calling code has a chance to read it (e.g., via
DBIx::Simple's error() method). The end result is that the calling code
knows an error occurred on the DBMS when a query fails for the first time,
but has no way of knowing what the nature of the error was. Error messages
for future queries are retained as expected.
Minimum working example demonstrating the problem (this shows the bug
happening with an SQLite backend, although I initially ran into it on a
PostgreSQL backend using DBD::Pg):
use DBIx::Simple;
my $db = DBIx::Simple->connect("dbi:SQLite:dbname=test.db", "", "", {
RaiseError => 0 });
$db->query("CREATE TABLE test(a NUMERIC PRIMARY KEY)");
print "1 ";
$db->query("INSERT INTO test (a) VALUES (42)") or print $db->error;
print "\n";
print "2 ";
$db->query("INSERT INTO test (a) VALUES (42)") or print $db->error;
print "\n";
print "3 ";
$db->query("INSERT INTO test (a) VALUES (42)") or print $db->error;
print "\n";
Expected output:
1
2 DBI error: UNIQUE constraint failed: test.a
3 DBI error: UNIQUE constraint failed: test.a
Actual output:
1
2 DBI error:
3 DBI error: UNIQUE constraint failed: test.a
The DBI documentation [1] states that finish() should not be called on
statement handle objects outside of driver code without an extremely good
reason. Removing the call to finish() on line 271 (e.g., by commenting out
the line altogether) fixes the bug and produces the expected output in the
MWE above, with no other apparent side-effects.
Perl 5.18.1, Linux x86_64
Relevant modules: DBIx-Simple-1.35, DBI-1.633, DBD-SQLite-1.48 (or
DBD-Pg-3.5.1)
[1] http://search.cpan.org/~timb/DBI-1.633/DBI.pm#finish