| 1 | package SQL::Abstract::Plugin::InsertReturning; | ||
| 2 | 2 | our $VERSION = '0.05'; | |
| 3 | # ABSTRACT: Augment SQL::Abstract->insert with support for returning data | ||
| 4 | |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 2 | ▶ | use Sub::Exporter -setup => { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | into => 'SQL::Abstract', | ||
| 10 | exports => [qw( insert_returning )], | ||
| 11 | groups => { | ||
| 12 | default => [qw( insert_returning )] | ||
| 13 | } | ||
| 14 | 2 | }; | |
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use SQL::Abstract; | ||
| 19 | use SQL::Abstract::Plugin::InsertReturning; | ||
| 20 | |||
| 21 | my $sql = SQL::Abstract->new; | ||
| 22 | my ($query, @bind) = $sql->insert_returning('pets', { | ||
| 23 | name => 'Fluffy Munchkins', type => 'Kitty' | ||
| 24 | }, [qw( name type )]); | ||
| 25 | |||
| 26 | print $sql; | ||
| 27 | # INSERT INTO pets ( name, type ) VALUES ( ?, ? ) RETURNING name, type; | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | B<DEPRECATED>. This functionality is now in L<SQL::Abstract> | ||
| 32 | itself. This module just wraps around that. Please, stop using this! | ||
| 33 | |||
| 34 | Some databases have support for returning data after an insert query, which can | ||
| 35 | help gain performance when doing common operations such as inserting and then | ||
| 36 | returning the new objects ID. | ||
| 37 | |||
| 38 | This plugin exports the C<insert_returning> method into the L<SQL::Abstract> | ||
| 39 | namespace, allowing you to call it much like any other method. | ||
| 40 | |||
| 41 | =head1 METHODS | ||
| 42 | |||
| 43 | =head2 insert_returning($table, \@values || \%fieldvals, \@returning) | ||
| 44 | |||
| 45 | Forms an SQL query with both an C<INSERT> part and a C<RETURNING> part. The | ||
| 46 | C<INSERT> part is generated by L<SQL::Abstract>'s C<insert> method, and both the | ||
| 47 | C<$table> and C<$fieldvals> values are passed directly to it. The returning SQL | ||
| 48 | is then altered to have a returning statement. | ||
| 49 | |||
| 50 | C<\@returning> is an array reference of column names that should be | ||
| 51 | returned. | ||
| 52 | |||
| 53 | This method will return an array of the SQL generated, and then all bind | ||
| 54 | parameters. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | 5 | ▶ | sub insert_returning { |
|
Subroutine
insert_returning: called CC=1 SCAR=0.0
Pod
documented
| |||
| 59 | 5 | my ($self, $table, $fieldvals, $returning) = @_; | |
| 60 | 5 | return $self->insert($table, $fieldvals, { returning => $returning }); | |
| 61 | } | ||
| 62 | |||
| 63 | 2 | 1; | |
| 64 | |||
| 65 | |||