[Bps-public-commit] r13745 - in DBIx-SearchBuilder/trunk: SearchBuilder/Handle

clkao at bestpractical.com clkao at bestpractical.com
Wed Jul 2 21:40:27 EDT 2008


Author: clkao
Date: Wed Jul  2 21:40:27 2008
New Revision: 13745

Modified:
   DBIx-SearchBuilder/trunk/   (props changed)
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm

Log:
 r31977 at mtl:  clkao | 2008-07-02 19:02:54 +0100
 last_insert_rowid bug workaround for sqlite, though not enabled.
 


Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm	Wed Jul  2 21:40:27 2008
@@ -43,17 +43,37 @@
 
 =cut
 
+sub _last_insert_rowid {
+    my $self = shift;
+    my $table = shift;
+
+    return $self->dbh->func('last_insert_rowid');
+
+    # XXX: this is workaround nesty sqlite problem that
+    # last_insert_rowid in transaction is inaccurrate with multiple
+    # inserts.
+
+    return $self->dbh->func('last_insert_rowid')
+        unless $self->TransactionDepth;
+
+    # XXX: is the name of the column always id ?
+
+    my $ret = $self->FetchResult("select max(id) from $table");
+    return $ret;
+}
+
 sub Insert  {
     my $self = shift;
     my $table = shift;
+
     my %args = ( id => undef, @_);
     # We really don't want an empty id
-    
+
     my $sth = $self->SUPER::Insert($table, %args);
     return unless $sth;
 
     # If we have set an id, then we want to use that, otherwise, we want to lookup the last _new_ rowid
-    $self->{'id'}= $args{'id'} || $self->dbh->func('last_insert_rowid');
+    $self->{'id'}= $args{'id'} || $self->_last_insert_rowid($table);
 
     warn "$self no row id returned on row creation" unless ($self->{'id'});
     return( $self->{'id'}); #Add Succeded. return the id



More information about the Bps-public-commit mailing list