From: Brandon L. Black Date: Tue, 8 Aug 2006 05:02:31 +0000 (+0000) Subject: A few dbd-specific dbh_do usage improvements X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0680ac39a01672213cac78f8a740ec2acbc82a3e;p=dbsrgits%2FDBIx-Class-Historic.git A few dbd-specific dbh_do usage improvements --- diff --git a/lib/DBIx/Class/Storage/DBI/Oracle.pm b/lib/DBIx/Class/Storage/DBI/Oracle.pm index 94df0e6..29d6c65 100644 --- a/lib/DBIx/Class/Storage/DBI/Oracle.pm +++ b/lib/DBIx/Class/Storage/DBI/Oracle.pm @@ -9,14 +9,32 @@ use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/; # __PACKAGE__->load_components(qw/PK::Auto/); +sub _ora_last_insert_id { + my ($dbh, $sql) = @_; + $dbh->selectrow_array($sql); +} sub last_insert_id { my ($self,$source,$col) = @_; my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col)); - my $sql = "SELECT " . $seq . ".currval FROM DUAL"; - my ($id) = $self->dbh_do(sub { shift->selectrow_array($sql) }); + my $sql = 'SELECT ' . $seq . '.currval FROM DUAL'; + my ($id) = $self->dbh_do(\&_ora_last_insert_id($sql)); return $id; } +sub _ora_get_autoinc_seq { + my ($dbh, $source, $sql) = @_; + + # trigger_body is a LONG + $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024); + + my $sth = $dbh->prepare($sql); + $sth->execute( uc($source->name) ); + while (my ($insert_trigger) = $sth->fetchrow_array) { + return uc($1) if $insert_trigger =~ m!(\w+)\.nextval!i; # col name goes here??? + } + croak "Unable to find a sequence INSERT trigger on table '" . $source->name . "'."; +} + sub get_autoinc_seq { my ($self,$source,$col) = @_; @@ -28,17 +46,7 @@ sub get_autoinc_seq { AND t.status = 'ENABLED' }; - $self->dbh_do(sub { - my $dbh = shift; - # trigger_body is a LONG - $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024); - my $sth = $dbh->prepare($sql); - $sth->execute( uc($source->name) ); - while (my ($insert_trigger) = $sth->fetchrow_array) { - return uc($1) if $insert_trigger =~ m!(\w+)\.nextval!i; # col name goes here??? - } - croak "Unable to find a sequence INSERT trigger on table '" . $source->name . "'."; - }); + $self->dbh_do(\&_ora_get_autoinc_seq, $source, $sql); } sub columns_info_for { diff --git a/lib/DBIx/Class/Storage/DBI/Pg.pm b/lib/DBIx/Class/Storage/DBI/Pg.pm index f17831c..d8ed989 100644 --- a/lib/DBIx/Class/Storage/DBI/Pg.pm +++ b/lib/DBIx/Class/Storage/DBI/Pg.pm @@ -13,10 +13,30 @@ use base qw/DBIx::Class::Storage::DBI/; warn "DBD::Pg 1.49 is strongly recommended" if ($DBD::Pg::VERSION < 1.49); +sub _pg_last_insert_id { + my ($dbh, $seq) = @_; + $dbh->last_insert_id(undef,undef,undef,undef, {sequence => $seq}); +} + sub last_insert_id { my ($self,$source,$col) = @_; my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col)); - $self->dbh_do(sub { shift->last_insert_id(undef,undef,undef,undef, {sequence => $seq}) } ); + $self->dbh_do(\&_pg_last_insert_id, $seq); +} + +sub _pg_get_autoinc_seq { + my ($dbh, $schema, $table, @pri) = @_; + + while (my $col = shift @pri) { + my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref; + if(defined $info->{COLUMN_DEF} and + $info->{COLUMN_DEF} =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/) { + my $seq = $1; + # may need to strip quotes -- see if this works + return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq; + } + } + return; } sub get_autoinc_seq { @@ -26,19 +46,7 @@ sub get_autoinc_seq { my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2) : (undef,$source->name); - $self->dbh_do(sub { - my $dbh = shift; - while (my $col = shift @pri) { - my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref; - if (defined $info->{COLUMN_DEF} and $info->{COLUMN_DEF} =~ - /^nextval\(+'([^']+)'::(?:text|regclass)\)/) - { - my $seq = $1; - return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq; # may need to strip quotes -- see if this works - } - } - return; - }); + $self->dbh_do(\&_pg_get_autoinc_seq, $schema, $table, @pri); } sub sqlt_type {