From eefdae2ec33f00d95de3a1cdd7bc233dff6a1f9d Mon Sep 17 00:00:00 2001 From: Mike Slattery Date: Tue, 26 Mar 2013 12:50:46 -0300 Subject: [PATCH] Adding Pg support to autopk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Attempting to recognize autoincrementing via currval(pg_get_serial_sequence). Depends on consistent structure in the schema metadata for dbix_lite --- lib/DBIx/Lite.pm | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/DBIx/Lite.pm b/lib/DBIx/Lite.pm index 8399999..a1f7e60 100644 --- a/lib/DBIx/Lite.pm +++ b/lib/DBIx/Lite.pm @@ -123,6 +123,10 @@ sub _autopk { return $self->dbh_do(sub { +($_->selectrow_array('SELECT LAST_INSERT_ID()'))[0] }); } elsif ($driver_name eq 'SQLite') { return $self->dbh_do(sub { +($_->selectrow_array('SELECT LAST_INSERT_ROWID()'))[0] }); + } elsif ($driver_name eq 'Pg' ){ + return $self->dbh_do(sub { +($_->selectrow_array( + qq{SELECT currval(pg_get_serial_sequence('$table_name', '$self->{schema}->{tables}->{$table_name}->{autopk}'))} + ))[0] }); } else { croak "Autoincrementing ID is not supported on $driver_name"; }