Commit | Line | Data |
be860760 |
1 | package DBIx::Class::Storage::DBI::InsertReturning; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use base qw/DBIx::Class::Storage::DBI/; |
7 | use mro 'c3'; |
8 | |
be860760 |
9 | =head1 NAME |
10 | |
11 | DBIx::Class::Storage::DBI::InsertReturning - Storage component for RDBMSes |
12 | supporting INSERT ... RETURNING |
13 | |
14 | =head1 DESCRIPTION |
15 | |
16 | Provides Auto-PK and |
17 | L<is_auto_increment|DBIx::Class::ResultSource/is_auto_increment> support for |
1e45aa87 |
18 | databases supporting the C<INSERT ... RETURNING> syntax. |
be860760 |
19 | |
20 | =cut |
21 | |
1e45aa87 |
22 | sub insert { |
be860760 |
23 | my $self = shift; |
1e45aa87 |
24 | my ($source, $to_insert, $opts) = @_; |
be860760 |
25 | |
1e45aa87 |
26 | return $self->next::method (@_) unless ($opts && $opts->{returning}); |
be860760 |
27 | |
1e45aa87 |
28 | my $updated_cols = $self->_prefetch_insert_auto_nextvals ($source, $to_insert); |
be860760 |
29 | |
1e45aa87 |
30 | my $bind_attributes = $self->source_bind_attributes($source); |
31 | my ($rv, $sth) = $self->_execute (insert => [], $source, $bind_attributes, $to_insert, $opts); |
be860760 |
32 | |
1e45aa87 |
33 | if (my @ret_cols = @{$opts->{returning}}) { |
be860760 |
34 | |
1e45aa87 |
35 | my @ret_vals = eval { |
be860760 |
36 | local $SIG{__WARN__} = sub {}; |
1e45aa87 |
37 | my @r = $sth->fetchrow_array; |
38 | $sth->finish; |
39 | @r; |
be860760 |
40 | }; |
be860760 |
41 | |
1e45aa87 |
42 | my %ret; |
43 | @ret{@ret_cols} = @ret_vals if (@ret_vals); |
be860760 |
44 | |
1e45aa87 |
45 | $updated_cols = { |
46 | %$updated_cols, |
47 | %ret, |
48 | }; |
be860760 |
49 | } |
50 | |
51 | return $updated_cols; |
52 | } |
53 | |
be860760 |
54 | =head1 AUTHOR |
55 | |
56 | See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS> |
57 | |
58 | =head1 LICENSE |
59 | |
60 | You may distribute this code under the same terms as Perl itself. |
61 | |
62 | =cut |
63 | |
64 | 1; |