From: David Kamholz Date: Fri, 10 Feb 2006 18:59:33 +0000 (+0000) Subject: re-rewrite PK::Auto, Pg, Oracle bits X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ca48cd7ddaf84b1e9a37c975321f96f2e65e8198;p=dbsrgits%2FDBIx-Class-Historic.git re-rewrite PK::Auto, Pg, Oracle bits --- diff --git a/lib/DBIx/Class/PK/Auto.pm b/lib/DBIx/Class/PK/Auto.pm index e62dbc1..5bf069a 100644 --- a/lib/DBIx/Class/PK/Auto.pm +++ b/lib/DBIx/Class/PK/Auto.pm @@ -53,24 +53,15 @@ sub insert { my ($self, @rest) = @_; my $ret = $self->next::method(@rest); - # if all primaries are already populated, skip auto-inc - my $populated = 0; - map { $populated++ if defined $self->get_column($_) } $self->primary_columns; - return $ret if ( $populated == scalar $self->primary_columns ); - - my ($pri, $too_many) = - (grep { $self->column_info($_)->{'auto_increment'} } - $self->primary_columns) - || $self->primary_columns; + my ($pri, $too_many) = grep { !defined $self->get_column($_) } $self->primary_columns; + return $ret unless defined $pri; # if all primaries are already populated, skip auto-inc $self->throw_exception( "More than one possible key found for auto-inc on ".ref $self ) - if $too_many; - unless (defined $self->get_column($pri)) { -# $self->throw_exception( "Can't auto-inc for $pri on ".ref $self.": no _last_insert_id method" ) -# unless $self->can('last_insert_id'); - my $id = $self->result_source->storage->last_insert_id($self->result_source); - $self->throw_exception( "Can't get last insert id" ) unless $id; - $self->store_column($pri => $id); - } + if defined $too_many; + + my $id = $self->result_source->storage->last_insert_id($self->result_source,$pri); + $self->throw_exception( "Can't get last insert id" ) unless $id; + $self->store_column($pri => $id); + return $ret; } diff --git a/lib/DBIx/Class/Storage/DBI/Oracle.pm b/lib/DBIx/Class/Storage/DBI/Oracle.pm index 73859c5..063c88b 100644 --- a/lib/DBIx/Class/Storage/DBI/Oracle.pm +++ b/lib/DBIx/Class/Storage/DBI/Oracle.pm @@ -10,22 +10,16 @@ use base qw/DBIx::Class::Storage::DBI/; # __PACKAGE__->load_components(qw/PK::Auto/); sub last_insert_id { - my ($self, $source) = shift; - $self->get_autoinc_seq($source) unless $source->{_autoinc_seq}; - my $sql = "SELECT " . $source->{_autoinc_seq} . ".currval FROM DUAL"; + 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->selectrow_array($sql); return $id; } sub get_autoinc_seq { - my ($self, $source) = @_; - - # return the user-defined sequence if known - my $result_class = $source->result_class; - if ($result_class->can('sequence') and $result_class->sequence) { - return ($source->{_autoinc_seq} = $result_class->sequence); - } - + my ($self,$source,$col) = @_; + # look up the correct sequence automatically my $dbh = $self->_dbh; my $sql = qq{ @@ -39,13 +33,9 @@ sub get_autoinc_seq { my $sth = $dbh->prepare($sql); $sth->execute( uc($source->name) ); while (my ($insert_trigger) = $sth->fetchrow_array) { - if ($insert_trigger =~ m!(\w+)\.nextval!i ) { - $source->{_autoinc_seq} = uc($1); - } - } - unless ($source->{_autoinc_seq}) { - croak "Unable to find a sequence INSERT trigger on table '" . $self->_table_name . "'."; + 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 . "'."; } 1; diff --git a/lib/DBIx/Class/Storage/DBI/Pg.pm b/lib/DBIx/Class/Storage/DBI/Pg.pm index 6faaf4c..7fb6b8c 100644 --- a/lib/DBIx/Class/Storage/DBI/Pg.pm +++ b/lib/DBIx/Class/Storage/DBI/Pg.pm @@ -8,21 +8,14 @@ use base qw/DBIx::Class::Storage::DBI/; # __PACKAGE__->load_components(qw/PK::Auto/); sub last_insert_id { - my ($self, $source) = @_; - $self->get_autoinc_seq($source) unless $source->{_autoinc_seq}; - $self->_dbh->last_insert_id(undef,undef,undef,undef, - {sequence => $source->{_autoinc_seq}}); + my ($self,$source,$col) = @_; + my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col)); + $self->_dbh->last_insert_id(undef,undef,undef,undef, {sequence => $seq}); } sub get_autoinc_seq { - my ($self,$source) = @_; - - # return the user-defined sequence if known - my $result_class = $source->result_class; - if ($result_class->can('sequence') and $result_class->sequence) { - return ($source->{_autoinc_seq} = $result_class->sequence); - } - + my ($self,$source,$col) = @_; + my @pri = $source->primary_columns; my $dbh = $self->_dbh; my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2) @@ -32,8 +25,7 @@ sub get_autoinc_seq { if (defined $info->[12] and $info->[12] =~ /^nextval\('"?([^"']+)"?'::(?:text|regclass)\)/) { - $source->{_autoinc_seq} = $1; - last; + return $1; } } }