Author: turnstep
Date: Mon Feb  4 19:13:03 2008
New Revision: 10688

Modified:
   DBD-Pg/trunk/Pg.pm
   DBD-Pg/trunk/t/03dbmethod.t

Log:
Make sure we set the error and return undef consistently with last_insert_id().
Write a test for a non-existent table and clean up others for the function.
Thanks to Rolf Schaufelberger for spotting this.


Modified: DBD-Pg/trunk/Pg.pm
==============================================================================
--- DBD-Pg/trunk/Pg.pm  (original)
+++ DBD-Pg/trunk/Pg.pm  Mon Feb  4 19:13:03 2008
@@ -248,7 +248,8 @@
                                $attr = {sequence => $attr};
                        }
                        elsif (ref $attr ne 'HASH') {
-                               return $dbh->set_err(1, "last_insert_id must be 
passed a hashref as the final argument");
+                               $dbh->set_err(1, "last_insert_id must be passed 
a hashref as the final argument");
+                               return undef;
                        }
                        ## Named sequence overrides any table or schema settings
                        if (exists $attr->{sequence} and length 
$attr->{sequence}) {
@@ -265,10 +266,10 @@
                elsif (! defined $sequence) {
                        ## At this point, we must have a valid table name
                        if (! length $table) {
-                               return $dbh->set_err(1, "last_insert_id needs 
at least a sequence or table name");
+                               $dbh->set_err(1, "last_insert_id needs at least 
a sequence or table name");
+                               return undef;
                        }
                        my @args = ($table);
-
                        ## Make sure the table in question exists and grab its 
oid
                        my ($schemajoin,$schemawhere) = ('','');
                        if (length $schema) {
@@ -283,7 +284,8 @@
                                $sth->finish();
                                my $message = qq{Could not find the table 
"$table"};
                                length $schema and $message .= qq{ in the 
schema "$schema"};
-                               return $dbh->set_err(1, $message);
+                               $dbh->set_err(1, $message);
+                               return undef;
                        }
                        my $oid = $sth->fetchall_arrayref()->[0][0];
                        $oid =~ /(\d+)/ or die qq{OID was not numeric?!?\n};
@@ -300,6 +302,7 @@
                        if (!defined $count or $count eq '0E0') {
                                $sth->finish();
                                $dbh->set_err(1, qq{No suitable column found 
for last_insert_id of table "$table"});
+                               return undef;
                        }
                        my $info = $sth->fetchall_arrayref();
 
@@ -327,7 +330,8 @@
                }
 
                $sth = $dbh->prepare_cached("SELECT currval(?)");
-               $sth->execute($sequence);
+               $count = $sth->execute($sequence);
+               return undef if ! defined $count;
                return $sth->fetchall_arrayref()->[0][0];
 
        } ## end of last_insert_id

Modified: DBD-Pg/trunk/t/03dbmethod.t
==============================================================================
--- DBD-Pg/trunk/t/03dbmethod.t (original)
+++ DBD-Pg/trunk/t/03dbmethod.t Mon Feb  4 19:13:03 2008
@@ -23,7 +23,7 @@
 my $dbh = connect_database();
 
 if (defined $dbh) {
-       plan tests => 207;
+       plan tests => 208;
 }
 else {
        plan skip_all => 'Connection to database failed, cannot continue 
testing';
@@ -65,30 +65,37 @@
 eval {
        $dbh->last_insert_id(undef,undef,undef,undef);
 };
-ok( $@, 'DB handle method "last_insert_id" fails when no arguments are given');
+like( $@, qr{last_insert_id.*least}, 'DB handle method "last_insert_id" fails 
when no arguments are given');
 
 eval {
        
$dbh->last_insert_id(undef,undef,undef,undef,{sequence=>'dbd_pg_nonexistentsequence_test'});
 };
-ok( $@, 'DB handle method "last_insert_id" fails when given a non-existent 
sequence');
+like( $@, qr{ERROR}, 'DB handle method "last_insert_id" fails when given a 
non-existent sequence');
 $dbh->rollback();
 
 eval {
        $dbh->last_insert_id(undef,undef,'dbd_pg_nonexistenttable_test',undef);
 };
-ok( $@, 'DB handle method "last_insert_id" fails when given a non-existent 
table');
+like( $@, qr{not find}, 'DB handle method "last_insert_id" fails when given a 
non-existent table');
 $dbh->rollback();
 
 eval {
        
$dbh->last_insert_id(undef,undef,'dbd_pg_nonexistenttable_test',undef,[]);
 };
-ok($@, 'DB handle method "last_insert_id" fails when given an arrayref as last 
argument');
+like($@, qr{last_insert_id.*hashref}, 'DB handle method "last_insert_id" fails 
when given an arrayref as last argument');
 $dbh->rollback();
 
 eval {
        $dbh->last_insert_id(undef,undef,'dbd_pg_test',undef,{sequence=>''});
 };
-is($@, q{}, 'DB handle method "last_insert_id" fails when given an empty 
sequence argument');
+is($@, q{}, 'DB handle method "last_insert_id" works when given an empty 
sequence argument');
+$dbh->rollback();
+
+$dbh->do('CREATE TEMP TABLE dbd_pg_test_temp(a int)');
+eval {
+       $dbh->last_insert_id(undef,undef,'dbd_pg_test_temp',undef);
+};
+like($@, qr{last_insert_id}, 'DB handle method "last_insert_id" fails when 
given a table with no primary key');
 $dbh->rollback();
 
 eval {

Reply via email to