Class::DBI _auto_increment_value update/suggestion

[prev] [thread] [next] [Date index for 2005/06/22]

From: larodi
Subject: Class::DBI _auto_increment_value update/suggestion
Date: 19:42 on 22 Jun 2005
   hi all,

   I found out that even in the recent 0.96 version of Class::DBI a dirty hack exists to provide functionality for the _auto_increment_value subroutine. In the current codebase we have :

# the DBI will provide a standard attribute soon, meanwhile...
my $id = $dbh->{mysql_insertid}    # mysql
       || eval { $dbh->func('last_insert_rowid') };    # SQLite

  (~ line 540)

   DBI actually provides the last_insert_id method since v1.38 (nb: current DBI version is 1.48) and most DB drivers already handle it properly. That's why I believe the above two lines should be replaced with something like:

   my $id  = $dbh->last_insert_id(undef, undef, $self->table, undef );

   ( works for me, i'm still not sure if it's ok with schemas, but Class::DBI seems not to make any difference between tables in the public/general schema and others. let's hope that most drivers will handle the schema.table notation when used as table name ).

   moreover, some drivers allow passing additional parameters with information about the sequence to be checked (Pg for example). the best approach will of course be to ask all Class::DBI::DBDRIVER authors to update their code having last_insert_id in mind. meanwhile, the above solution could be used as temporary workaround.

   i'd suggest the error message on the line following is extended with some additional information about the table/sequence in question this way:

   $self->_croak("Can't get last insert id for table [
".$self->table." ] / sequence [ ".$self->sequence." ]" ) unless
defined $id;

   i hope this helps and I'll be looking forward to see some kind of solution in the next release (though hacking sweets me fine). I believe it will benefit Class::DBI and enable it to handle properly inserts when dealing with arbitrary databases (different than mysql and sqlite).

   good luck,

         stelf

Class::DBI _auto_increment_value update/suggestion
larodi 19:42 on 22 Jun 2005

Generated at 16:35 on 28 Jul 2005 by mariachi v0.52