1 package DBIx::Class::Storage::DBI::InsertReturning;
6 use base qw/DBIx::Class::Storage::DBI/;
9 __PACKAGE__->mk_group_accessors(simple => qw/
15 DBIx::Class::Storage::DBI::InsertReturning - Storage component for RDBMSes
16 supporting INSERT ... RETURNING
21 L<is_auto_increment|DBIx::Class::ResultSource/is_auto_increment> support for
22 databases supporting the C<INSERT ... RETURNING> syntax. Currently
23 L<PostgreSQL|DBIx::Class::Storage::DBI::Pg> and
24 L<Firebird|DBIx::Class::Storage::DBI::InterBase>.
28 sub _prep_for_execute {
30 my ($op, $extra_bind, $ident, $args) = @_;
32 if ($op eq 'insert') {
33 $self->_returning_cols([]);
36 @pk{$ident->primary_columns} = ();
38 my @auto_inc_cols = grep {
39 my $inserting = $args->[0]{$_};
41 ($ident->column_info($_)->{is_auto_increment}
44 (not defined $inserting)
46 (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i)
51 $args->[1]{returning} = \@auto_inc_cols;
53 $self->_returning_cols->[0] = \@auto_inc_cols;
57 return $self->next::method(@_);
64 my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
66 if ($op eq 'insert' && $self->_returning_cols) {
68 my (@returning_cols) = eval {
69 local $SIG{__WARN__} = sub {};
72 $self->_returning_cols->[1] = \@returning_cols;
76 return wantarray ? ($rv, $sth, @bind) : $rv;
82 my $updated_cols = $self->next::method(@_);
84 if ($self->_returning_cols->[0]) {
86 @returning_cols{ @{ $self->_returning_cols->[0] } } = @{ $self->_returning_cols->[1] };
88 $updated_cols = { %$updated_cols, %returning_cols };
95 my ($self, $source, @cols) = @_;
99 @returning_cols{ @{ $self->_returning_cols->[0] } } =
100 @{ $self->_returning_cols->[1] };
102 push @result, $returning_cols{$_} for @cols;
109 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>
113 You may distribute this code under the same terms as Perl itself.