line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Fey::SQL::Insert; |
2
|
|
|
|
|
|
|
|
3
|
27
|
|
|
27
|
|
152
|
use strict; |
|
27
|
|
|
|
|
56
|
|
|
27
|
|
|
|
|
1128
|
|
4
|
27
|
|
|
27
|
|
126
|
use warnings; |
|
27
|
|
|
|
|
41
|
|
|
27
|
|
|
|
|
841
|
|
5
|
27
|
|
|
27
|
|
129
|
use namespace::autoclean; |
|
27
|
|
|
|
|
41
|
|
|
27
|
|
|
|
|
227
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.42'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Fey::Types |
10
|
27
|
|
|
|
|
223
|
qw( ArrayRef HashRef CanQuote IntoElement NonNullableInsertValue |
11
|
27
|
|
|
27
|
|
2333
|
NullableInsertValue ); |
|
27
|
|
|
|
|
45
|
|
12
|
27
|
|
|
27
|
|
292861
|
use overload (); |
|
27
|
|
|
|
|
59
|
|
|
27
|
|
|
|
|
575
|
|
13
|
27
|
|
|
27
|
|
113
|
use Scalar::Util qw( blessed ); |
|
27
|
|
|
|
|
39
|
|
|
27
|
|
|
|
|
1746
|
|
14
|
|
|
|
|
|
|
|
15
|
27
|
|
|
27
|
|
123
|
use Moose 0.90; |
|
27
|
|
|
|
|
694
|
|
|
27
|
|
|
|
|
197
|
|
16
|
27
|
|
|
27
|
|
143886
|
use MooseX::Params::Validate 0.08 qw( validated_hash pos_validated_list ); |
|
27
|
|
|
|
|
734
|
|
|
27
|
|
|
|
|
184
|
|
17
|
27
|
|
|
27
|
|
6683
|
use MooseX::SemiAffordanceAccessor 0.03; |
|
27
|
|
|
|
|
457
|
|
|
27
|
|
|
|
|
176
|
|
18
|
27
|
|
|
27
|
|
81031
|
use MooseX::StrictConstructor 0.07; |
|
27
|
|
|
|
|
572
|
|
|
27
|
|
|
|
|
165
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
with 'Fey::Role::SQL::HasBindParams'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has '_into' => ( |
23
|
|
|
|
|
|
|
is => 'rw', |
24
|
|
|
|
|
|
|
isa => ArrayRef, |
25
|
|
|
|
|
|
|
init_arg => undef, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has '_values_spec' => ( |
29
|
|
|
|
|
|
|
is => 'rw', |
30
|
|
|
|
|
|
|
isa => HashRef, |
31
|
|
|
|
|
|
|
init_arg => undef, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has '_values' => ( |
35
|
|
|
|
|
|
|
traits => ['Array'], |
36
|
|
|
|
|
|
|
is => 'bare', |
37
|
|
|
|
|
|
|
isa => ArrayRef [HashRef], |
38
|
|
|
|
|
|
|
default => sub { [] }, |
39
|
|
|
|
|
|
|
handles => { |
40
|
|
|
|
|
|
|
_add_values => 'push', |
41
|
|
|
|
|
|
|
_values => 'elements', |
42
|
|
|
|
|
|
|
}, |
43
|
|
|
|
|
|
|
init_arg => undef, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
with 'Fey::Role::SQL::Cloneable'; |
47
|
|
|
|
|
|
|
|
48
|
26
|
|
|
26
|
1
|
56
|
sub insert { return $_[0] } |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub into { |
51
|
27
|
|
|
27
|
1
|
1905
|
my $self = shift; |
52
|
|
|
|
|
|
|
|
53
|
27
|
100
|
|
|
|
76
|
my $count = @_ ? scalar @_ : 1; |
54
|
27
|
|
|
|
|
108
|
my @into = pos_validated_list( |
55
|
|
|
|
|
|
|
\@_, |
56
|
|
|
|
|
|
|
( ( { isa => IntoElement } ) x $count ), |
57
|
|
|
|
|
|
|
MX_PARAMS_VALIDATE_NO_CACHE => 1, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
26
|
|
|
|
|
603
|
my @cols; |
61
|
26
|
|
|
|
|
46
|
for my $elt (@into) { |
62
|
43
|
100
|
|
|
|
221
|
push @cols, $elt->isa('Fey::Table') |
63
|
|
|
|
|
|
|
? $elt->columns |
64
|
|
|
|
|
|
|
: $elt; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
26
|
|
|
|
|
1018
|
$self->_set_into( \@cols ); |
68
|
|
|
|
|
|
|
|
69
|
26
|
|
|
|
|
46
|
my %spec; |
70
|
26
|
|
|
|
|
33
|
for my $col ( @{ $self->_into() } ) { |
|
26
|
|
|
|
|
605
|
|
71
|
46
|
100
|
|
|
|
978
|
$spec{ $col->name() } |
72
|
|
|
|
|
|
|
= $col->is_nullable() |
73
|
|
|
|
|
|
|
? { isa => NullableInsertValue } |
74
|
|
|
|
|
|
|
: { isa => NonNullableInsertValue }; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
26
|
|
|
|
|
749
|
$self->_set_values_spec( \%spec ); |
78
|
|
|
|
|
|
|
|
79
|
26
|
|
|
|
|
87
|
return $self; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub values { |
83
|
26
|
|
|
26
|
1
|
98
|
my $self = shift; |
84
|
|
|
|
|
|
|
|
85
|
26
|
|
|
|
|
640
|
my %vals = validated_hash( |
86
|
|
|
|
|
|
|
\@_, |
87
|
26
|
|
|
|
|
54
|
%{ $self->_values_spec() }, |
88
|
|
|
|
|
|
|
MX_PARAMS_VALIDATE_NO_CACHE => 1 |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
23
|
|
|
|
|
3155
|
for my $col_name ( |
|
37
|
|
|
|
|
92
|
|
92
|
37
|
|
|
|
|
791
|
grep { exists $vals{$_} } |
93
|
23
|
|
|
|
|
639
|
map { $_->name() } @{ $self->_into() } |
94
|
|
|
|
|
|
|
) { |
95
|
37
|
|
|
|
|
54
|
my $val = $vals{$col_name}; |
96
|
|
|
|
|
|
|
|
97
|
37
|
100
|
100
|
|
|
156
|
$val .= '' |
98
|
|
|
|
|
|
|
if blessed $val && overload::Overloaded($val); |
99
|
|
|
|
|
|
|
|
100
|
37
|
100
|
|
|
|
713
|
if ( !blessed $val ) { |
101
|
26
|
100
|
100
|
|
|
700
|
if ( defined $val && $self->auto_placeholders() ) { |
102
|
16
|
|
|
|
|
492
|
$self->_add_bind_param($val); |
103
|
|
|
|
|
|
|
|
104
|
16
|
|
|
|
|
361
|
$val = Fey::Placeholder->new(); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
else { |
107
|
10
|
|
|
|
|
44
|
$val = Fey::Literal->new_from_scalar($val); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
37
|
|
|
|
|
120
|
$vals{$col_name} = $val; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
23
|
|
|
|
|
815
|
$self->_add_values( \%vals ); |
115
|
|
|
|
|
|
|
|
116
|
23
|
|
|
|
|
57
|
return $self; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub sql { |
120
|
7
|
|
|
7
|
1
|
22
|
my $self = shift; |
121
|
7
|
|
|
|
|
30
|
my ($dbh) = pos_validated_list( \@_, { isa => CanQuote } ); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
return ( |
124
|
7
|
|
|
|
|
322
|
join ' ', |
125
|
|
|
|
|
|
|
$self->insert_clause($dbh), |
126
|
|
|
|
|
|
|
$self->columns_clause($dbh), |
127
|
|
|
|
|
|
|
$self->values_clause($dbh), |
128
|
|
|
|
|
|
|
); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub insert_clause { |
132
|
10
|
|
|
10
|
1
|
272
|
return ( 'INSERT INTO ' |
133
|
|
|
|
|
|
|
. $_[1]->quote_identifier( $_[0]->_into()->[0]->table()->name() ) |
134
|
|
|
|
|
|
|
); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub columns_clause { |
138
|
|
|
|
|
|
|
return ( |
139
|
25
|
|
|
|
|
1111
|
'(' |
140
|
|
|
|
|
|
|
. ( |
141
|
|
|
|
|
|
|
join ', ', |
142
|
12
|
|
|
12
|
1
|
2704
|
map { $_[1]->quote_identifier( $_->name() ) } @{ $_[0]->_into() } |
|
12
|
|
|
|
|
326
|
|
143
|
|
|
|
|
|
|
) |
144
|
|
|
|
|
|
|
. ')' |
145
|
|
|
|
|
|
|
); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub values_clause { |
149
|
20
|
|
|
20
|
1
|
900
|
my $self = shift; |
150
|
20
|
|
|
|
|
29
|
my $dbh = shift; |
151
|
|
|
|
|
|
|
|
152
|
20
|
|
|
|
|
27
|
my @cols = @{ $self->_into() }; |
|
20
|
|
|
|
|
511
|
|
153
|
|
|
|
|
|
|
|
154
|
20
|
|
|
|
|
23
|
my @v; |
155
|
20
|
|
|
|
|
646
|
for my $vals ( $self->_values() ) { |
156
|
22
|
|
|
|
|
27
|
my $v = '('; |
157
|
|
|
|
|
|
|
|
158
|
35
|
|
|
|
|
1339
|
$v .= ( |
159
|
|
|
|
|
|
|
join ', ', |
160
|
|
|
|
|
|
|
map { |
161
|
22
|
|
|
|
|
38
|
my $val = $vals->{ $_->name() }; |
162
|
35
|
|
|
|
|
118
|
my $sql = $val->sql($dbh); |
163
|
35
|
100
|
66
|
|
|
503
|
blessed $val |
164
|
|
|
|
|
|
|
&& $val->can('does') |
165
|
|
|
|
|
|
|
&& $val->does('Fey::Role::SQL::ReturnsData') |
166
|
|
|
|
|
|
|
? "($sql)" |
167
|
|
|
|
|
|
|
: $sql |
168
|
|
|
|
|
|
|
} @cols |
169
|
|
|
|
|
|
|
); |
170
|
|
|
|
|
|
|
|
171
|
22
|
|
|
|
|
1245
|
$v .= ')'; |
172
|
|
|
|
|
|
|
|
173
|
22
|
|
|
|
|
54
|
push @v, $v; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
20
|
|
|
|
|
149
|
return 'VALUES ' . join ',', @v; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
__PACKAGE__->meta()->make_immutable(); |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
1; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# ABSTRACT: Represents a INSERT query |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
__END__ |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=pod |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 NAME |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Fey::SQL::Insert - Represents a INSERT query |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 VERSION |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
version 0.42 |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head1 SYNOPSIS |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
my $sql = Fey::SQL->new_insert(); |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
# INSERT INTO Part |
202
|
|
|
|
|
|
|
# (part_id, name, quantity) |
203
|
|
|
|
|
|
|
# VALUES |
204
|
|
|
|
|
|
|
# (?, ?, ?) |
205
|
|
|
|
|
|
|
$sql->insert()->into($Part); |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
my $ph = Fey::Placeholder->new(); |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
$sql->values( part_id => $ph, |
210
|
|
|
|
|
|
|
name => $ph, |
211
|
|
|
|
|
|
|
quantity => $ph, |
212
|
|
|
|
|
|
|
); |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
print $sql->sql($dbh); |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head1 DESCRIPTION |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
This class represents a C<INSERT> query. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head1 METHODS |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
This class provides the following methods: |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 Constructor |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
To construct an object of this class, call C<< $query->insert() >> on |
227
|
|
|
|
|
|
|
a C<Fey::SQL> object. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 $insert->insert() |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
This method is basically a no-op that exists to so that L<Fey::SQL> |
232
|
|
|
|
|
|
|
has something to call after it constructs an object in this class. |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 $insert->into() |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
This method specifies the C<INTO> clause of the query. It expects a |
237
|
|
|
|
|
|
|
list of L<Fey::Column> and/or L<Fey::Table> objects, but not aliases. |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
If you pass a table object, then the C<INTO> will include all of that |
240
|
|
|
|
|
|
|
table's columns, in the order returned by the C<< $table->columns() >> |
241
|
|
|
|
|
|
|
method. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Most RDBMS implementations only allow for a single table here, but |
244
|
|
|
|
|
|
|
some (like MySQL) do allow for multi-table inserts. |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head2 $insert->values(...) |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
This method takes a hash where the keys are column names, and values are the |
249
|
|
|
|
|
|
|
value to be inserted for that column. Each value can be of the following: |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=over 4 |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=item * a plain scalar, including undef |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
This will be passed to C<< Fey::Literal->new_from_scalar() >>. |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=item * C<Fey::Literal> object |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=item * C<Fey::Placeholder> object |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=back |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
You can call this method multiple times in order to do a multi-row |
264
|
|
|
|
|
|
|
insert. |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=head2 $insert->sql() |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
Returns the full SQL statement which this object represents. A DBI |
269
|
|
|
|
|
|
|
handle must be passed so that identifiers can be properly quoted. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head2 $insert->bind_params() |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
See the L<Fey::SQL section on Bind Parameters|Fey::SQL/Bind |
274
|
|
|
|
|
|
|
Parameters> for more details. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head2 $insert->insert_clause() |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
Returns the C<INSERT INTO> clause portion of the SQL statement as a |
279
|
|
|
|
|
|
|
string (just the tables). |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head2 $insert->columns_clause() |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
Returns the portion of the SQL statement containing the columns for |
284
|
|
|
|
|
|
|
which values are being inserted as a string. |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=head2 $insert->values_clause() |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
Returns the C<VALUES> clause portion of the SQL statement as a string. |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
=head1 ROLES |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
=over 4 |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=item * L<Fey::Role::SQL::HasBindParams> |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=item * L<Fey::Role::SQL::Cloneable> |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=back |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=head1 BUGS |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
See L<Fey> for details on how to report bugs. |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=head1 AUTHOR |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
Dave Rolsky <autarch@urth.org> |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
This software is Copyright (c) 2011 - 2015 by Dave Rolsky. |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
This is free software, licensed under: |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=cut |