line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Fey::SQL::Update; |
2
|
|
|
|
|
|
|
|
3
|
27
|
|
|
27
|
|
143
|
use strict; |
|
27
|
|
|
|
|
46
|
|
|
27
|
|
|
|
|
1204
|
|
4
|
27
|
|
|
27
|
|
136
|
use warnings; |
|
27
|
|
|
|
|
43
|
|
|
27
|
|
|
|
|
929
|
|
5
|
27
|
|
|
27
|
|
135
|
use namespace::autoclean; |
|
27
|
|
|
|
|
42
|
|
|
27
|
|
|
|
|
293
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.43'; |
8
|
|
|
|
|
|
|
|
9
|
27
|
|
|
27
|
|
2505
|
use Fey::Exceptions qw( param_error ); |
|
27
|
|
|
|
|
50
|
|
|
27
|
|
|
|
|
1646
|
|
10
|
27
|
|
|
27
|
|
134
|
use Fey::Literal; |
|
27
|
|
|
|
|
43
|
|
|
27
|
|
|
|
|
621
|
|
11
|
27
|
|
|
|
|
264
|
use Fey::Types qw( ArrayRef CanQuote ColumnWithTable NonNullableUpdateValue |
12
|
27
|
|
|
27
|
|
119
|
NullableUpdateValue Table ); |
|
27
|
|
|
|
|
42
|
|
13
|
27
|
|
|
27
|
|
326034
|
use overload (); |
|
27
|
|
|
|
|
65
|
|
|
27
|
|
|
|
|
659
|
|
14
|
27
|
|
|
27
|
|
126
|
use Scalar::Util qw( blessed ); |
|
27
|
|
|
|
|
41
|
|
|
27
|
|
|
|
|
2188
|
|
15
|
|
|
|
|
|
|
|
16
|
27
|
|
|
27
|
|
148
|
use Moose 2.1200; |
|
27
|
|
|
|
|
836
|
|
|
27
|
|
|
|
|
333
|
|
17
|
27
|
|
|
27
|
|
157682
|
use MooseX::Params::Validate 0.21 qw( pos_validated_list ); |
|
27
|
|
|
|
|
813
|
|
|
27
|
|
|
|
|
196
|
|
18
|
27
|
|
|
27
|
|
5447
|
use MooseX::SemiAffordanceAccessor 0.03; |
|
27
|
|
|
|
|
469
|
|
|
27
|
|
|
|
|
183
|
|
19
|
27
|
|
|
27
|
|
91538
|
use MooseX::StrictConstructor 0.13; |
|
27
|
|
|
|
|
666
|
|
|
27
|
|
|
|
|
194
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
with 'Fey::Role::SQL::HasOrderByClause', 'Fey::Role::SQL::HasLimitClause'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
with 'Fey::Role::SQL::HasWhereClause' => { |
24
|
|
|
|
|
|
|
-excludes => 'bind_params', |
25
|
|
|
|
|
|
|
-alias => { bind_params => '_where_clause_bind_params' }, |
26
|
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
with 'Fey::Role::SQL::HasBindParams' => { |
29
|
|
|
|
|
|
|
-excludes => 'bind_params', |
30
|
|
|
|
|
|
|
-alias => { bind_params => '_update_bind_params' }, |
31
|
|
|
|
|
|
|
}; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has '_update' => ( |
34
|
|
|
|
|
|
|
is => 'rw', |
35
|
|
|
|
|
|
|
isa => ArrayRef, |
36
|
|
|
|
|
|
|
default => sub { [] }, |
37
|
|
|
|
|
|
|
init_arg => undef, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has '_set_pairs' => ( |
41
|
|
|
|
|
|
|
traits => ['Array'], |
42
|
|
|
|
|
|
|
is => 'bare', |
43
|
|
|
|
|
|
|
isa => ArrayRef [ArrayRef], |
44
|
|
|
|
|
|
|
default => sub { [] }, |
45
|
|
|
|
|
|
|
handles => { |
46
|
|
|
|
|
|
|
_add_set_pair => 'push', |
47
|
|
|
|
|
|
|
_set_pairs => 'elements', |
48
|
|
|
|
|
|
|
}, |
49
|
|
|
|
|
|
|
init_arg => undef, |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
with 'Fey::Role::SQL::Cloneable'; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub update { |
55
|
26
|
|
|
26
|
1
|
3912
|
my $self = shift; |
56
|
|
|
|
|
|
|
|
57
|
26
|
100
|
|
|
|
102
|
my $count = @_ ? @_ : 1; |
58
|
26
|
|
|
|
|
178
|
my (@tables) = pos_validated_list( |
59
|
|
|
|
|
|
|
\@_, |
60
|
|
|
|
|
|
|
( ( { isa => Table } ) x $count ), |
61
|
|
|
|
|
|
|
MX_PARAMS_VALIDATE_NO_CACHE => 1, |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
25
|
|
|
|
|
17483
|
$self->_set_update( \@tables ); |
65
|
|
|
|
|
|
|
|
66
|
25
|
|
|
|
|
96
|
return $self; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub set { |
70
|
23
|
|
|
23
|
1
|
1784
|
my $self = shift; |
71
|
|
|
|
|
|
|
|
72
|
23
|
100
|
100
|
|
|
199
|
if ( !@_ || @_ % 2 ) { |
73
|
2
|
|
|
|
|
6
|
my $count = @_; |
74
|
2
|
|
|
|
|
15
|
param_error |
75
|
|
|
|
|
|
|
"The set method expects a list of paired column objects and values but you passed $count parameters"; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
21
|
|
|
|
|
30
|
my @spec; |
79
|
21
|
|
|
|
|
94
|
for ( my $x = 0; $x < @_; $x += 2 ) { |
80
|
25
|
|
|
|
|
479
|
push @spec, { isa => ColumnWithTable }; |
81
|
25
|
100
|
66
|
|
|
3934
|
push @spec, |
82
|
|
|
|
|
|
|
blessed $_[$x] && $_[$x]->is_nullable() |
83
|
|
|
|
|
|
|
? { isa => NullableUpdateValue } |
84
|
|
|
|
|
|
|
: { isa => NonNullableUpdateValue }; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my @set |
88
|
21
|
|
|
|
|
2399
|
= pos_validated_list( \@_, @spec, MX_PARAMS_VALIDATE_NO_CACHE => 1 ); |
89
|
|
|
|
|
|
|
|
90
|
21
|
|
|
|
|
3556
|
for ( my $x = 0; $x < @_; $x += 2 ) { |
91
|
25
|
|
|
|
|
79
|
my $val = $_[ $x + 1 ]; |
92
|
|
|
|
|
|
|
|
93
|
25
|
100
|
100
|
|
|
170
|
$val .= '' |
94
|
|
|
|
|
|
|
if blessed $val && overload::Overloaded($val); |
95
|
|
|
|
|
|
|
|
96
|
25
|
100
|
|
|
|
867
|
if ( !blessed $val ) { |
97
|
16
|
100
|
100
|
|
|
563
|
if ( defined $val && $self->auto_placeholders() ) { |
98
|
8
|
|
|
|
|
270
|
$self->_add_bind_param($val); |
99
|
|
|
|
|
|
|
|
100
|
8
|
|
|
|
|
215
|
$val = Fey::Placeholder->new(); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
else { |
103
|
8
|
|
|
|
|
59
|
$val = Fey::Literal->new_from_scalar($val); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
25
|
|
|
|
|
1071
|
$self->_add_set_pair( [ $_[$x], $val ] ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
21
|
|
|
|
|
142
|
return $self; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub sql { |
114
|
4
|
|
|
4
|
1
|
84
|
my $self = shift; |
115
|
4
|
|
|
|
|
28
|
my ($dbh) = pos_validated_list( \@_, { isa => CanQuote } ); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
return ( |
118
|
4
|
|
|
|
|
208
|
join ' ', |
119
|
|
|
|
|
|
|
$self->update_clause($dbh), |
120
|
|
|
|
|
|
|
$self->set_clause($dbh), |
121
|
|
|
|
|
|
|
$self->where_clause($dbh), |
122
|
|
|
|
|
|
|
$self->order_by_clause($dbh), |
123
|
|
|
|
|
|
|
$self->limit_clause($dbh), |
124
|
|
|
|
|
|
|
); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub update_clause { |
128
|
6
|
|
|
6
|
1
|
43
|
return 'UPDATE ' . $_[0]->_tables_subclause( $_[1] ); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub _tables_subclause { |
132
|
|
|
|
|
|
|
return ( |
133
|
7
|
|
|
|
|
224
|
join ', ', |
134
|
6
|
|
|
6
|
|
15
|
map { $_[1]->quote_identifier( $_->name() ) } @{ $_[0]->_update() } |
|
6
|
|
|
|
|
179
|
|
135
|
|
|
|
|
|
|
); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub set_clause { |
139
|
21
|
|
|
21
|
1
|
518
|
my $self = shift; |
140
|
21
|
|
|
|
|
31
|
my $dbh = shift; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# SQLite objects when the table name is provided ("User"."email") |
143
|
|
|
|
|
|
|
# on the LHS of the set. I'm hoping that a DBMS which allows a |
144
|
|
|
|
|
|
|
# multi-table update also allows the table name in the LHS. |
145
|
21
|
100
|
|
|
|
30
|
my $col_quote = @{ $self->_update() } > 1 ? '_name_and_table' : '_name'; |
|
21
|
|
|
|
|
641
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
return ( |
148
|
24
|
|
|
|
|
297
|
'SET ' . ( |
149
|
|
|
|
|
|
|
join ', ', |
150
|
|
|
|
|
|
|
map { |
151
|
21
|
|
|
|
|
771
|
my $val = $_->[1]; |
152
|
24
|
|
|
|
|
135
|
my $val_sql = $val->sql($dbh); |
153
|
24
|
100
|
33
|
|
|
1134
|
$val_sql = "($val_sql)" |
|
|
|
66
|
|
|
|
|
154
|
|
|
|
|
|
|
if blessed $val |
155
|
|
|
|
|
|
|
&& $val->can('does') |
156
|
|
|
|
|
|
|
&& $val->does('Fey::Role::SQL::ReturnsData'); |
157
|
24
|
|
|
|
|
2460
|
$self->$col_quote( $_->[0], $dbh ) . ' = ' . $val_sql; |
158
|
|
|
|
|
|
|
} $self->_set_pairs() |
159
|
|
|
|
|
|
|
) |
160
|
|
|
|
|
|
|
); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub _name_and_table { |
164
|
1
|
|
|
1
|
|
5
|
return $_[1]->sql( $_[2] ); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub _name { |
168
|
23
|
|
|
23
|
|
654
|
return $_[2]->quote_identifier( $_[1]->name() ); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub bind_params { |
172
|
5
|
|
|
5
|
1
|
3749
|
my $self = shift; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
return ( |
175
|
5
|
|
|
|
|
31
|
$self->_update_bind_params(), |
176
|
|
|
|
|
|
|
$self->_where_clause_bind_params(), |
177
|
|
|
|
|
|
|
); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
__PACKAGE__->meta()->make_immutable(); |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
1; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# ABSTRACT: Represents a UPDATE query |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
__END__ |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=pod |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 NAME |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Fey::SQL::Update - Represents a UPDATE query |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 VERSION |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
version 0.43 |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 SYNOPSIS |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
my $sql = Fey::SQL->new_update(); |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# UPDATE Part |
203
|
|
|
|
|
|
|
# SET quantity = 10 |
204
|
|
|
|
|
|
|
# WHERE part_id IN (1, 5) |
205
|
|
|
|
|
|
|
$sql->update($Part); |
206
|
|
|
|
|
|
|
$sql->set( $quantity, 10 ); |
207
|
|
|
|
|
|
|
$sql->where( $part_id, 'IN', 1, 5 ); |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
print $sql->sql($dbh); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 DESCRIPTION |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
This class represents a C<UPDATE> query. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head1 METHODS |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
This class provides the following methods: |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head2 Constructor |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
To construct an object of this class, call C<< $query->update() >> on |
222
|
|
|
|
|
|
|
a C<Fey::SQL> object. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 $update->update() |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
This method specifies the C<UPDATE> clause of the query. It expects |
227
|
|
|
|
|
|
|
one or more L<Fey::Table> objects (not aliases). Most RDBMS |
228
|
|
|
|
|
|
|
implementations only allow for a single table here, but some (like |
229
|
|
|
|
|
|
|
MySQL) do allow for multi-table updates. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head2 $update->set(...) |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
This method takes a list of key/value pairs. The keys should be column |
234
|
|
|
|
|
|
|
objects, and the value can be one of the following: |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=over 4 |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=item * a plain scalar, including undef |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
This will be passed to C<< Fey::Literal->new_from_scalar() >>. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=item * C<Fey::Literal> object |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=item * C<Fey::Column> object |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
A column alias cannot be used. |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=item * C<Fey::Placeholder> object |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=back |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=head2 $update->where(...) |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
See the L<Fey::SQL section on WHERE Clauses|Fey::SQL/WHERE Clauses> |
255
|
|
|
|
|
|
|
for more details. |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head2 $update->order_by(...) |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
See the L<Fey::SQL section on ORDER BY Clauses|Fey::SQL/ORDER BY |
260
|
|
|
|
|
|
|
Clauses> for more details. |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head2 $update->limit(...) |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
See the L<Fey::SQL section on LIMIT Clauses|Fey::SQL/LIMIT Clauses> |
265
|
|
|
|
|
|
|
for more details. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=head2 $update->sql($dbh) |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
Returns the full SQL statement which this object represents. A DBI |
270
|
|
|
|
|
|
|
handle must be passed so that identifiers can be properly quoted. |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=head2 $update->bind_params() |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
See the L<Fey::SQL section on Bind Parameters|Fey::SQL/Bind |
275
|
|
|
|
|
|
|
Parameters> for more details. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head2 $update->update_clause() |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
Returns the C<UPDATE> clause portion of the SQL statement as a string. |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head2 $update->set_clause() |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
Returns the C<SET> clause portion of the SQL statement as a string. |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=head2 $update->where_clause() |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
Returns the C<WHERE> clause portion of the SQL statement as a string. |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=head2 $update->order_by_clause() |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
Returns the C<ORDER BY> clause portion of the SQL statement as a |
292
|
|
|
|
|
|
|
string. |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=head2 $update->limit_clause() |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
Returns the C<LIMIT> clause portion of the SQL statement as a string. |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=head1 ROLES |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=over 4 |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=item * L<Fey::Role::SQL::HasBindParams> |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=item * L<Fey::Role::SQL::HasWhereClause> |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=item * L<Fey::Role::SQL::HasOrderByClause> |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=item * L<Fey::Role::SQL::HasLimitClause> |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=item * L<Fey::Role::SQL::Cloneable> |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=back |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=head1 BUGS |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
See L<Fey> for details on how to report bugs. |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=head1 AUTHOR |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
Dave Rolsky <autarch@urth.org> |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
This software is Copyright (c) 2011 - 2015 by Dave Rolsky. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
This is free software, licensed under: |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=cut |