X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FInsertReturning.pm;h=f110cb5bca02320df329e8a70f9daae8fa3ca388;hb=1e45aa874a9876ca6b9e576166724551a55a4c4b;hp=1948a2f14b39f532f50dc40b1e3aeee93109921b;hpb=be860760714f5933672f8913d742f6cde6931149;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI/InsertReturning.pm b/lib/DBIx/Class/Storage/DBI/InsertReturning.pm index 1948a2f..f110cb5 100644 --- a/lib/DBIx/Class/Storage/DBI/InsertReturning.pm +++ b/lib/DBIx/Class/Storage/DBI/InsertReturning.pm @@ -6,10 +6,6 @@ use warnings; use base qw/DBIx::Class::Storage::DBI/; use mro 'c3'; -__PACKAGE__->mk_group_accessors(simple => qw/ - _returning_cols -/); - =head1 NAME DBIx::Class::Storage::DBI::InsertReturning - Storage component for RDBMSes @@ -19,91 +15,42 @@ supporting INSERT ... RETURNING Provides Auto-PK and L support for -databases supporting the C syntax. Currently -L and -L. +databases supporting the C syntax. =cut -sub _prep_for_execute { +sub insert { my $self = shift; - my ($op, $extra_bind, $ident, $args) = @_; - - if ($op eq 'insert') { - $self->_returning_cols([]); - - my %pk; - @pk{$ident->primary_columns} = (); - - my @auto_inc_cols = grep { - my $inserting = $args->[0]{$_}; - - ($ident->column_info($_)->{is_auto_increment} - || exists $pk{$_}) - && ( - (not defined $inserting) - || - (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i) - ) - } $ident->columns; + my ($source, $to_insert, $opts) = @_; - if (@auto_inc_cols) { - $args->[1]{returning} = \@auto_inc_cols; + return $self->next::method (@_) unless ($opts && $opts->{returning}); - $self->_returning_cols->[0] = \@auto_inc_cols; - } - } - - return $self->next::method(@_); -} + my $updated_cols = $self->_prefetch_insert_auto_nextvals ($source, $to_insert); -sub _execute { - my $self = shift; - my ($op) = @_; + my $bind_attributes = $self->source_bind_attributes($source); + my ($rv, $sth) = $self->_execute (insert => [], $source, $bind_attributes, $to_insert, $opts); - my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_); + if (my @ret_cols = @{$opts->{returning}}) { - if ($op eq 'insert' && $self->_returning_cols) { - local $@; - my (@returning_cols) = eval { + my @ret_vals = eval { local $SIG{__WARN__} = sub {}; - $sth->fetchrow_array + my @r = $sth->fetchrow_array; + $sth->finish; + @r; }; - $self->_returning_cols->[1] = \@returning_cols; - $sth->finish; - } - return wantarray ? ($rv, $sth, @bind) : $rv; -} + my %ret; + @ret{@ret_cols} = @ret_vals if (@ret_vals); -sub insert { - my $self = shift; - - my $updated_cols = $self->next::method(@_); - - if ($self->_returning_cols->[0]) { - my %returning_cols; - @returning_cols{ @{ $self->_returning_cols->[0] } } = @{ $self->_returning_cols->[1] }; - - $updated_cols = { %$updated_cols, %returning_cols }; + $updated_cols = { + %$updated_cols, + %ret, + }; } return $updated_cols; } -sub last_insert_id { - my ($self, $source, @cols) = @_; - my @result; - - my %returning_cols; - @returning_cols{ @{ $self->_returning_cols->[0] } } = - @{ $self->_returning_cols->[1] }; - - push @result, $returning_cols{$_} for @cols; - - return @result; -} - =head1 AUTHOR See L and L