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;
}
# __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{
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;
# __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)
if (defined $info->[12] and $info->[12] =~
/^nextval\('"?([^"']+)"?'::(?:text|regclass)\)/)
{
- $source->{_autoinc_seq} = $1;
- last;
+ return $1;
}
}
}