line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::Abstract::Plugin::InsertReturning; |
2
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Augment SQL::Abstract->insert with support for returning data |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
91063
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
96
|
|
6
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
6153
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
|
|
26
|
use Sub::Exporter -setup => { |
9
|
|
|
|
|
|
|
into => 'SQL::Abstract', |
10
|
|
|
|
|
|
|
exports => [qw( insert_returning )], |
11
|
|
|
|
|
|
|
groups => { |
12
|
|
|
|
|
|
|
default => [qw( insert_returning )] |
13
|
|
|
|
|
|
|
} |
14
|
2
|
|
|
2
|
|
3108
|
}; |
|
2
|
|
|
|
|
41721
|
|
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. This functionality is now in L |
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 method into the L |
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 part and a C part. The |
46
|
|
|
|
|
|
|
C part is generated by L's C 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
|
|
|
|
|
|
|
sub insert_returning { |
59
|
5
|
|
|
5
|
1
|
4904
|
my ($self, $table, $fieldvals, $returning) = @_; |
60
|
5
|
|
|
|
|
24
|
return $self->insert($table, $fieldvals, { returning => $returning }); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
1; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|