Commit | Line | Data |
96449e8e |
1 | package SQL::Abstract; # see doc at end of file |
2 | |
96449e8e |
3 | use strict; |
4 | use warnings; |
9d9d5bd6 |
5 | use Carp (); |
312d830b |
6 | use List::Util (); |
7 | use Scalar::Util (); |
96449e8e |
8 | |
0da0fe34 |
9 | use Exporter 'import'; |
10 | our @EXPORT_OK = qw(is_plain_value is_literal_value); |
11 | |
12 | BEGIN { |
13 | if ($] < 5.009_005) { |
14 | require MRO::Compat; |
15 | } |
16 | else { |
17 | require mro; |
18 | } |
843a94b5 |
19 | |
20 | *SQL::Abstract::_ENV_::DETECT_AUTOGENERATED_STRINGIFICATION = $ENV{SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION} |
21 | ? sub () { 0 } |
22 | : sub () { 1 } |
23 | ; |
0da0fe34 |
24 | } |
25 | |
96449e8e |
26 | #====================================================================== |
27 | # GLOBALS |
28 | #====================================================================== |
29 | |
dc6afcf8 |
30 | our $VERSION = '1.86'; |
7479e27e |
31 | |
22f1a437 |
32 | # This would confuse some packagers |
c520207b |
33 | $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases |
96449e8e |
34 | |
35 | our $AUTOLOAD; |
36 | |
37 | # special operators (-in, -between). May be extended/overridden by user. |
38 | # See section WHERE: BUILTIN SPECIAL OPERATORS below for implementation |
39 | my @BUILTIN_SPECIAL_OPS = ( |
f663e672 |
40 | {regex => qr/^ (?: not \s )? between $/ix, handler => sub { die "NOPE" }}, |
41 | {regex => qr/^ (?: not \s )? in $/ix, handler => sub { die "NOPE" }}, |
87c8e45b |
42 | {regex => qr/^ is (?: \s+ not )? $/ix, handler => sub { die "NOPE" }}, |
96449e8e |
43 | ); |
44 | |
45 | #====================================================================== |
46 | # DEBUGGING AND ERROR REPORTING |
47 | #====================================================================== |
48 | |
49 | sub _debug { |
50 | return unless $_[0]->{debug}; shift; # a little faster |
51 | my $func = (caller(1))[3]; |
52 | warn "[$func] ", @_, "\n"; |
53 | } |
54 | |
55 | sub belch (@) { |
56 | my($func) = (caller(1))[3]; |
9d9d5bd6 |
57 | Carp::carp "[$func] Warning: ", @_; |
96449e8e |
58 | } |
59 | |
60 | sub puke (@) { |
61 | my($func) = (caller(1))[3]; |
9d9d5bd6 |
62 | Carp::croak "[$func] Fatal: ", @_; |
96449e8e |
63 | } |
64 | |
0da0fe34 |
65 | sub is_literal_value ($) { |
66 | ref $_[0] eq 'SCALAR' ? [ ${$_[0]} ] |
67 | : ( ref $_[0] eq 'REF' and ref ${$_[0]} eq 'ARRAY' ) ? [ @${ $_[0] } ] |
0da0fe34 |
68 | : undef; |
69 | } |
70 | |
71 | # FIXME XSify - this can be done so much more efficiently |
72 | sub is_plain_value ($) { |
73 | no strict 'refs'; |
966200cc |
74 | ! length ref $_[0] ? \($_[0]) |
0da0fe34 |
75 | : ( |
76 | ref $_[0] eq 'HASH' and keys %{$_[0]} == 1 |
77 | and |
78 | exists $_[0]->{-value} |
966200cc |
79 | ) ? \($_[0]->{-value}) |
0da0fe34 |
80 | : ( |
a1c9e0ff |
81 | # reuse @_ for even moar speedz |
82 | defined ( $_[1] = Scalar::Util::blessed $_[0] ) |
0da0fe34 |
83 | and |
84 | # deliberately not using Devel::OverloadInfo - the checks we are |
85 | # intersted in are much more limited than the fullblown thing, and |
86 | # this is a very hot piece of code |
87 | ( |
e8d729d4 |
88 | # simply using ->can('(""') can leave behind stub methods that |
89 | # break actually using the overload later (see L<perldiag/Stub |
90 | # found while resolving method "%s" overloading "%s" in package |
91 | # "%s"> and the source of overload::mycan()) |
44e54b41 |
92 | # |
0da0fe34 |
93 | # either has stringification which DBI SHOULD prefer out of the box |
a1c9e0ff |
94 | grep { *{ (qq[${_}::(""]) }{CODE} } @{ $_[2] = mro::get_linear_isa( $_[1] ) } |
0da0fe34 |
95 | or |
20e178a8 |
96 | # has nummification or boolification, AND fallback is *not* disabled |
0da0fe34 |
97 | ( |
843a94b5 |
98 | SQL::Abstract::_ENV_::DETECT_AUTOGENERATED_STRINGIFICATION |
99 | and |
20e178a8 |
100 | ( |
101 | grep { *{"${_}::(0+"}{CODE} } @{$_[2]} |
102 | or |
103 | grep { *{"${_}::(bool"}{CODE} } @{$_[2]} |
104 | ) |
0da0fe34 |
105 | and |
106 | ( |
107 | # no fallback specified at all |
a1c9e0ff |
108 | ! ( ($_[3]) = grep { *{"${_}::()"}{CODE} } @{$_[2]} ) |
0da0fe34 |
109 | or |
110 | # fallback explicitly undef |
a1c9e0ff |
111 | ! defined ${"$_[3]::()"} |
0da0fe34 |
112 | or |
113 | # explicitly true |
a1c9e0ff |
114 | !! ${"$_[3]::()"} |
0da0fe34 |
115 | ) |
116 | ) |
117 | ) |
966200cc |
118 | ) ? \($_[0]) |
0da0fe34 |
119 | : undef; |
120 | } |
121 | |
122 | |
96449e8e |
123 | |
124 | #====================================================================== |
125 | # NEW |
126 | #====================================================================== |
127 | |
128 | sub new { |
129 | my $self = shift; |
130 | my $class = ref($self) || $self; |
131 | my %opt = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_; |
132 | |
133 | # choose our case by keeping an option around |
134 | delete $opt{case} if $opt{case} && $opt{case} ne 'lower'; |
135 | |
136 | # default logic for interpreting arrayrefs |
ef559da3 |
137 | $opt{logic} = $opt{logic} ? uc $opt{logic} : 'OR'; |
96449e8e |
138 | |
139 | # how to return bind vars |
96449e8e |
140 | $opt{bindtype} ||= 'normal'; |
141 | |
142 | # default comparison is "=", but can be overridden |
143 | $opt{cmp} ||= '='; |
144 | |
3af02ccb |
145 | # try to recognize which are the 'equality' and 'inequality' ops |
3cdadcbe |
146 | # (temporary quickfix (in 2007), should go through a more seasoned API) |
147 | $opt{equality_op} = qr/^( \Q$opt{cmp}\E | \= )$/ix; |
148 | $opt{inequality_op} = qr/^( != | <> )$/ix; |
149 | |
150 | $opt{like_op} = qr/^ (is\s+)? r?like $/xi; |
151 | $opt{not_like_op} = qr/^ (is\s+)? not \s+ r?like $/xi; |
96449e8e |
152 | |
153 | # SQL booleans |
154 | $opt{sqltrue} ||= '1=1'; |
155 | $opt{sqlfalse} ||= '0=1'; |
156 | |
9d48860e |
157 | # special operators |
30af97c5 |
158 | $opt{special_ops} ||= []; |
159 | |
b6251592 |
160 | # regexes are applied in order, thus push after user-defines |
96449e8e |
161 | push @{$opt{special_ops}}, @BUILTIN_SPECIAL_OPS; |
162 | |
cf06e9dc |
163 | if ($class->isa('DBIx::Class::SQLMaker')) { |
dd1d0dbf |
164 | push @{$opt{special_ops}}, our $DBIC_Compat_Op ||= { |
165 | regex => qr/^(?:ident|value)$/i, handler => sub { die "NOPE" } |
166 | }; |
cf06e9dc |
167 | $opt{is_dbic_sqlmaker} = 1; |
dd1d0dbf |
168 | } |
169 | |
9d48860e |
170 | # unary operators |
59f23b3d |
171 | $opt{unary_ops} ||= []; |
59f23b3d |
172 | |
3af02ccb |
173 | # rudimentary sanity-check for user supplied bits treated as functions/operators |
b6251592 |
174 | # If a purported function matches this regular expression, an exception is thrown. |
175 | # Literal SQL is *NOT* subject to this check, only functions (and column names |
176 | # when quoting is not in effect) |
96449e8e |
177 | |
b6251592 |
178 | # FIXME |
179 | # need to guard against ()'s in column names too, but this will break tons of |
180 | # hacks... ideas anyone? |
181 | $opt{injection_guard} ||= qr/ |
182 | \; |
183 | | |
184 | ^ \s* go \s |
185 | /xmi; |
96449e8e |
186 | |
ec19b759 |
187 | $opt{expand_unary} = {}; |
188 | |
767b62e4 |
189 | $opt{expand} = { |
d1aabfe4 |
190 | -ident => '_expand_ident', |
71f1364b |
191 | -value => '_expand_value', |
2a238661 |
192 | -not => '_expand_not', |
05d37f9c |
193 | -bool => '_expand_bool', |
70f98e4b |
194 | -and => '_expand_andor', |
195 | -or => '_expand_andor', |
767b62e4 |
196 | }; |
197 | |
143f50df |
198 | $opt{expand_op} = { |
199 | 'between' => '_expand_between', |
200 | 'not between' => '_expand_between', |
416026a9 |
201 | 'in' => '_expand_in', |
202 | 'not in' => '_expand_in', |
2f57f4f0 |
203 | (map +($_ => sub { |
0eca8cb9 |
204 | my ($self, $op, $arg, $k) = @_; |
205 | return +{ -op => [ |
206 | $self->{cmp}, |
207 | $self->_expand_ident(-ident => $k), |
208 | $self->_expand_expr({ '-'.$op => $arg }), |
209 | ] }; |
2f57f4f0 |
210 | }), qw(ident value)), |
143f50df |
211 | }; |
ca158918 |
212 | |
213 | $opt{render} = { |
214 | (map +("-$_", "_render_$_"), qw(op func bind ident literal list)), |
215 | %{$opt{render}||{}} |
216 | }; |
217 | |
143f50df |
218 | $opt{render_op} = our $RENDER_OP; |
219 | |
b6251592 |
220 | return bless \%opt, $class; |
221 | } |
96449e8e |
222 | |
e175845b |
223 | sub sqltrue { +{ -literal => [ $_[0]->{sqltrue} ] } } |
224 | sub sqlfalse { +{ -literal => [ $_[0]->{sqlfalse} ] } } |
170e6c33 |
225 | |
226 | sub _assert_pass_injection_guard { |
227 | if ($_[1] =~ $_[0]->{injection_guard}) { |
228 | my $class = ref $_[0]; |
229 | puke "Possible SQL injection attempt '$_[1]'. If this is indeed a part of the " |
230 | . "desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply your own " |
231 | . "{injection_guard} attribute to ${class}->new()" |
232 | } |
233 | } |
234 | |
235 | |
96449e8e |
236 | #====================================================================== |
237 | # INSERT methods |
238 | #====================================================================== |
239 | |
240 | sub insert { |
02288357 |
241 | my $self = shift; |
242 | my $table = $self->_table(shift); |
243 | my $data = shift || return; |
244 | my $options = shift; |
96449e8e |
245 | |
246 | my $method = $self->_METHOD_FOR_refkind("_insert", $data); |
02288357 |
247 | my ($sql, @bind) = $self->$method($data); |
96449e8e |
248 | $sql = join " ", $self->_sqlcase('insert into'), $table, $sql; |
02288357 |
249 | |
e82e648a |
250 | if ($options->{returning}) { |
ca4f826a |
251 | my ($s, @b) = $self->_insert_returning($options); |
e82e648a |
252 | $sql .= $s; |
253 | push @bind, @b; |
02288357 |
254 | } |
255 | |
96449e8e |
256 | return wantarray ? ($sql, @bind) : $sql; |
257 | } |
258 | |
60f3fd3f |
259 | # So that subclasses can override INSERT ... RETURNING separately from |
260 | # UPDATE and DELETE (e.g. DBIx::Class::SQLMaker::Oracle does this) |
b17a3ece |
261 | sub _insert_returning { shift->_returning(@_) } |
262 | |
95904db5 |
263 | sub _returning { |
e82e648a |
264 | my ($self, $options) = @_; |
6b1fe79d |
265 | |
e82e648a |
266 | my $f = $options->{returning}; |
267 | |
79d310f2 |
268 | my ($sql, @bind) = $self->render_aqt( |
ff96fdd4 |
269 | $self->_expand_maybe_list_expr($f, undef, -ident) |
270 | ); |
271 | return wantarray |
272 | ? $self->_sqlcase(' returning ') . $sql |
273 | : ($self->_sqlcase(' returning ').$sql, @bind); |
6b1fe79d |
274 | } |
275 | |
96449e8e |
276 | sub _insert_HASHREF { # explicit list of fields and then values |
277 | my ($self, $data) = @_; |
278 | |
279 | my @fields = sort keys %$data; |
280 | |
fe3ae272 |
281 | my ($sql, @bind) = $self->_insert_values($data); |
96449e8e |
282 | |
283 | # assemble SQL |
284 | $_ = $self->_quote($_) foreach @fields; |
285 | $sql = "( ".join(", ", @fields).") ".$sql; |
286 | |
287 | return ($sql, @bind); |
288 | } |
289 | |
290 | sub _insert_ARRAYREF { # just generate values(?,?) part (no list of fields) |
291 | my ($self, $data) = @_; |
292 | |
293 | # no names (arrayref) so can't generate bindtype |
294 | $self->{bindtype} ne 'columns' |
295 | or belch "can't do 'columns' bindtype when called with arrayref"; |
296 | |
19b6ccce |
297 | my (@values, @all_bind); |
298 | foreach my $value (@$data) { |
299 | my ($values, @bind) = $self->_insert_value(undef, $value); |
300 | push @values, $values; |
301 | push @all_bind, @bind; |
302 | } |
303 | my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )"; |
304 | return ($sql, @all_bind); |
fe3ae272 |
305 | } |
306 | |
307 | sub _insert_ARRAYREFREF { # literal SQL with bind |
308 | my ($self, $data) = @_; |
309 | |
310 | my ($sql, @bind) = @${$data}; |
311 | $self->_assert_bindval_matches_bindtype(@bind); |
312 | |
313 | return ($sql, @bind); |
314 | } |
315 | |
316 | |
317 | sub _insert_SCALARREF { # literal SQL without bind |
318 | my ($self, $data) = @_; |
319 | |
320 | return ($$data); |
321 | } |
322 | |
323 | sub _insert_values { |
324 | my ($self, $data) = @_; |
325 | |
96449e8e |
326 | my (@values, @all_bind); |
fe3ae272 |
327 | foreach my $column (sort keys %$data) { |
19b6ccce |
328 | my ($values, @bind) = $self->_insert_value($column, $data->{$column}); |
329 | push @values, $values; |
330 | push @all_bind, @bind; |
331 | } |
332 | my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )"; |
333 | return ($sql, @all_bind); |
334 | } |
96449e8e |
335 | |
19b6ccce |
336 | sub _insert_value { |
337 | my ($self, $column, $v) = @_; |
96449e8e |
338 | |
79d310f2 |
339 | return $self->render_aqt( |
720ca4f7 |
340 | $self->_expand_insert_value($column, $v) |
341 | ); |
342 | } |
96449e8e |
343 | |
720ca4f7 |
344 | sub _expand_insert_value { |
345 | my ($self, $column, $v) = @_; |
96449e8e |
346 | |
720ca4f7 |
347 | if (ref($v) eq 'ARRAY') { |
348 | if ($self->{array_datatypes}) { |
349 | return +{ -bind => [ $column, $v ] }; |
350 | } |
351 | my ($sql, @bind) = @$v; |
352 | $self->_assert_bindval_matches_bindtype(@bind); |
353 | return +{ -literal => $v }; |
354 | } |
355 | if (ref($v) eq 'HASH') { |
356 | if (grep !/^-/, keys %$v) { |
357 | belch "HASH ref as bind value in insert is not supported"; |
358 | return +{ -bind => [ $column, $v ] }; |
359 | } |
360 | } |
361 | if (!defined($v)) { |
362 | return +{ -bind => [ $column, undef ] }; |
363 | } |
364 | local our $Cur_Col_Meta = $column; |
79d310f2 |
365 | return $self->expand_expr($v); |
96449e8e |
366 | } |
367 | |
368 | |
96449e8e |
369 | |
370 | #====================================================================== |
371 | # UPDATE methods |
372 | #====================================================================== |
373 | |
374 | |
375 | sub update { |
95904db5 |
376 | my $self = shift; |
377 | my $table = $self->_table(shift); |
378 | my $data = shift || return; |
379 | my $where = shift; |
380 | my $options = shift; |
96449e8e |
381 | |
382 | # first build the 'SET' part of the sql statement |
96449e8e |
383 | puke "Unsupported data type specified to \$sql->update" |
384 | unless ref $data eq 'HASH'; |
385 | |
9ade906e |
386 | my ($sql, @all_bind) = $self->_update_set_values($data); |
a9e94508 |
387 | $sql = $self->_sqlcase('update ') . $table . $self->_sqlcase(' set ') |
9ade906e |
388 | . $sql; |
389 | |
390 | if ($where) { |
391 | my($where_sql, @where_bind) = $self->where($where); |
392 | $sql .= $where_sql; |
393 | push @all_bind, @where_bind; |
394 | } |
395 | |
396 | if ($options->{returning}) { |
397 | my ($returning_sql, @returning_bind) = $self->_update_returning($options); |
398 | $sql .= $returning_sql; |
399 | push @all_bind, @returning_bind; |
400 | } |
401 | |
402 | return wantarray ? ($sql, @all_bind) : $sql; |
403 | } |
404 | |
405 | sub _update_set_values { |
406 | my ($self, $data) = @_; |
407 | |
79d310f2 |
408 | return $self->render_aqt( |
89690da2 |
409 | $self->_expand_update_set_values($data), |
410 | ); |
411 | } |
96449e8e |
412 | |
89690da2 |
413 | sub _expand_update_set_values { |
414 | my ($self, $data) = @_; |
415 | $self->_expand_maybe_list_expr( [ |
416 | map { |
417 | my ($k, $set) = @$_; |
c4ed66f4 |
418 | $set = { -bind => $_ } unless defined $set; |
9103bd14 |
419 | +{ -op => [ '=', $self->_expand_ident(-ident => $k), $set ] }; |
89690da2 |
420 | } |
421 | map { |
422 | my $k = $_; |
423 | my $v = $data->{$k}; |
424 | (ref($v) eq 'ARRAY' |
425 | ? ($self->{array_datatypes} |
426 | ? [ $k, +{ -bind => [ $k, $v ] } ] |
427 | : [ $k, +{ -literal => $v } ]) |
428 | : do { |
429 | local our $Cur_Col_Meta = $k; |
430 | [ $k, $self->_expand_expr($v) ] |
431 | } |
432 | ); |
433 | } sort keys %$data |
434 | ] ); |
96449e8e |
435 | } |
436 | |
60f3fd3f |
437 | # So that subclasses can override UPDATE ... RETURNING separately from |
438 | # INSERT and DELETE |
20bb2ad5 |
439 | sub _update_returning { shift->_returning(@_) } |
96449e8e |
440 | |
441 | |
442 | |
443 | #====================================================================== |
444 | # SELECT |
445 | #====================================================================== |
446 | |
447 | |
448 | sub select { |
449 | my $self = shift; |
450 | my $table = $self->_table(shift); |
451 | my $fields = shift || '*'; |
452 | my $where = shift; |
453 | my $order = shift; |
454 | |
daa4ccdd |
455 | my ($fields_sql, @bind) = $self->_select_fields($fields); |
96449e8e |
456 | |
daa4ccdd |
457 | my ($where_sql, @where_bind) = $self->where($where, $order); |
458 | push @bind, @where_bind; |
459 | |
460 | my $sql = join(' ', $self->_sqlcase('select'), $fields_sql, |
96449e8e |
461 | $self->_sqlcase('from'), $table) |
462 | . $where_sql; |
463 | |
9d48860e |
464 | return wantarray ? ($sql, @bind) : $sql; |
96449e8e |
465 | } |
466 | |
daa4ccdd |
467 | sub _select_fields { |
468 | my ($self, $fields) = @_; |
de63ce57 |
469 | return $fields unless ref($fields); |
79d310f2 |
470 | return $self->render_aqt( |
27592e2b |
471 | $self->_expand_maybe_list_expr($fields, undef, '-ident') |
472 | ); |
daa4ccdd |
473 | } |
474 | |
96449e8e |
475 | #====================================================================== |
476 | # DELETE |
477 | #====================================================================== |
478 | |
479 | |
480 | sub delete { |
85327cd5 |
481 | my $self = shift; |
482 | my $table = $self->_table(shift); |
483 | my $where = shift; |
484 | my $options = shift; |
96449e8e |
485 | |
486 | my($where_sql, @bind) = $self->where($where); |
a9e94508 |
487 | my $sql = $self->_sqlcase('delete from ') . $table . $where_sql; |
96449e8e |
488 | |
85327cd5 |
489 | if ($options->{returning}) { |
ca4f826a |
490 | my ($returning_sql, @returning_bind) = $self->_delete_returning($options); |
85327cd5 |
491 | $sql .= $returning_sql; |
492 | push @bind, @returning_bind; |
493 | } |
494 | |
9d48860e |
495 | return wantarray ? ($sql, @bind) : $sql; |
96449e8e |
496 | } |
497 | |
60f3fd3f |
498 | # So that subclasses can override DELETE ... RETURNING separately from |
499 | # INSERT and UPDATE |
85327cd5 |
500 | sub _delete_returning { shift->_returning(@_) } |
501 | |
502 | |
96449e8e |
503 | |
504 | #====================================================================== |
505 | # WHERE: entry point |
506 | #====================================================================== |
507 | |
508 | |
509 | |
510 | # Finally, a separate routine just to handle WHERE clauses |
511 | sub where { |
512 | my ($self, $where, $order) = @_; |
513 | |
7ad12721 |
514 | local $self->{convert_where} = $self->{convert}; |
515 | |
96449e8e |
516 | # where ? |
e175845b |
517 | my ($sql, @bind) = defined($where) |
518 | ? $self->_recurse_where($where) |
519 | : (undef); |
417dd15e |
520 | $sql = (defined $sql and length $sql) ? $self->_sqlcase(' where ') . "( $sql )" : ''; |
96449e8e |
521 | |
522 | # order by? |
523 | if ($order) { |
26fe4d30 |
524 | my ($order_sql, @order_bind) = $self->_order_by($order); |
525 | $sql .= $order_sql; |
526 | push @bind, @order_bind; |
96449e8e |
527 | } |
528 | |
9d48860e |
529 | return wantarray ? ($sql, @bind) : $sql; |
96449e8e |
530 | } |
531 | |
79d310f2 |
532 | sub expand_expr { |
54d3140f |
533 | my ($self, $expr, $default_scalar_to) = @_; |
534 | local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to; |
7d93797d |
535 | $self->_expand_expr($expr); |
79d310f2 |
536 | } |
537 | |
538 | sub render_aqt { |
539 | my ($self, $aqt) = @_; |
540 | my ($k, $v, @rest) = %$aqt; |
541 | die "No" if @rest; |
21225d32 |
542 | if (my $meth = $self->{render}{$k}) { |
79d310f2 |
543 | return $self->$meth($v); |
544 | } |
545 | die "notreached: $k"; |
546 | } |
547 | |
6082a464 |
548 | sub render_expr { |
549 | my ($self, $expr) = @_; |
550 | $self->render_aqt($self->expand_expr($expr)); |
551 | } |
552 | |
a2cd381d |
553 | sub _expand_expr { |
c7aeb9f5 |
554 | my ($self, $expr, $logic) = @_; |
3ae10d16 |
555 | our $Expand_Depth ||= 0; local $Expand_Depth = $Expand_Depth + 1; |
252518da |
556 | return undef unless defined($expr); |
59588695 |
557 | if (ref($expr) eq 'HASH') { |
767b62e4 |
558 | return undef unless my $kc = keys %$expr; |
559 | if ($kc > 1) { |
59588695 |
560 | $logic ||= 'and'; |
70f98e4b |
561 | return $self->_expand_andor("-${logic}", $expr); |
59588695 |
562 | } |
767b62e4 |
563 | my ($key, $value) = %$expr; |
70f98e4b |
564 | if ($key =~ /^-/ and $key =~ s/ [_\s]? \d+ $//x ) { |
565 | belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. ' |
566 | . "You probably wanted ...-and => [ $key => COND1, $key => COND2 ... ]"; |
567 | } |
767b62e4 |
568 | if (my $exp = $self->{expand}{$key}) { |
d1aabfe4 |
569 | return $self->$exp($key, $value); |
767b62e4 |
570 | } |
571 | return $self->_expand_expr_hashpair($key, $value, $logic); |
a2cd381d |
572 | } |
08264f40 |
573 | if (ref($expr) eq 'ARRAY') { |
99a65fa8 |
574 | my $logic = lc($logic || $self->{logic}); |
70f98e4b |
575 | return $self->_expand_andor("-${logic}", $expr); |
08264f40 |
576 | } |
ca3da680 |
577 | if (my $literal = is_literal_value($expr)) { |
578 | return +{ -literal => $literal }; |
579 | } |
99a65fa8 |
580 | if (!ref($expr) or Scalar::Util::blessed($expr)) { |
c7aeb9f5 |
581 | if (my $d = our $Default_Scalar_To) { |
a332e2cb |
582 | return $self->_expand_expr({ $d => $expr }); |
2558b622 |
583 | } |
24c9f930 |
584 | return $self->_expand_value(-value => $expr); |
252518da |
585 | } |
252518da |
586 | die "notreached"; |
a2cd381d |
587 | } |
96449e8e |
588 | |
59588695 |
589 | sub _expand_expr_hashpair { |
590 | my ($self, $k, $v, $logic) = @_; |
d13725da |
591 | unless (defined($k) and length($k)) { |
2d64004f |
592 | if (defined($k) and my $literal = is_literal_value($v)) { |
d13725da |
593 | belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead'; |
2d64004f |
594 | return { -literal => $literal }; |
d13725da |
595 | } |
596 | puke "Supplying an empty left hand side argument is not supported"; |
597 | } |
ef071fad |
598 | if ($k =~ /^-/) { |
99a65fa8 |
599 | $self->_assert_pass_injection_guard($k =~ /^-(.*)$/s); |
ef071fad |
600 | if ($k eq '-nest') { |
0fc68377 |
601 | # DBIx::Class requires a nest warning to be emitted once but the private |
602 | # method it overrode to do so no longer exists |
cf06e9dc |
603 | if ($self->{is_dbic_sqlmaker}) { |
604 | unless (our $Nest_Warned) { |
0fc68377 |
605 | belch( |
606 | "-nest in search conditions is deprecated, you most probably wanted:\n" |
607 | .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }| |
608 | ); |
609 | $Nest_Warned = 1; |
610 | } |
611 | } |
ef071fad |
612 | return $self->_expand_expr($v); |
613 | } |
ce3bc4b0 |
614 | if (my ($rest) = $k =~/^-not[_ ](.*)$/) { |
0c7e3af0 |
615 | return +{ -op => [ |
616 | 'not', |
767b62e4 |
617 | $self->_expand_expr({ "-${rest}", $v }, $logic) |
0c7e3af0 |
618 | ] }; |
ce3bc4b0 |
619 | } |
99a65fa8 |
620 | { |
621 | my $op = $k; |
622 | $op =~ s/^-// if length($op) > 1; |
ddc44894 |
623 | |
99a65fa8 |
624 | # top level special ops are illegal in general |
3ae10d16 |
625 | # note that, arguably, if it makes no sense at top level, it also |
626 | # makes no sense on the other side of an = sign or similar but DBIC |
627 | # gets disappointingly upset if I disallow it |
628 | if ( |
629 | (our $Expand_Depth) == 1 |
630 | and List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}} |
631 | ) { |
632 | puke "Illegal use of top-level '-$op'" |
633 | } |
96a8d74a |
634 | if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) { |
635 | return { -op => [ $op, $v ] }; |
636 | } |
99a65fa8 |
637 | } |
21225d32 |
638 | if ($self->{render}{$k}) { |
99a65fa8 |
639 | return { $k => $v }; |
d13725da |
640 | } |
711892b1 |
641 | if ( |
642 | ref($v) eq 'HASH' |
643 | and keys %$v == 1 |
644 | and (keys %$v)[0] =~ /^-/ |
645 | ) { |
646 | my ($func) = $k =~ /^-(.*)$/; |
3ae10d16 |
647 | if (List::Util::first { $func =~ $_->{regex} } @{$self->{special_ops}}) { |
648 | return +{ -op => [ $func, $self->_expand_expr($v) ] }; |
649 | } |
711892b1 |
650 | return +{ -func => [ $func, $self->_expand_expr($v) ] }; |
651 | } |
652 | if (!ref($v) or is_literal_value($v)) { |
99a65fa8 |
653 | return +{ -op => [ $k =~ /^-(.*)$/, $self->_expand_expr($v) ] }; |
59588695 |
654 | } |
99a65fa8 |
655 | } |
656 | if ( |
657 | !defined($v) |
658 | or ( |
659 | ref($v) eq 'HASH' |
660 | and exists $v->{-value} |
661 | and not defined $v->{-value} |
662 | ) |
663 | ) { |
213209dd |
664 | return $self->_expand_expr({ $k => { $self->{cmp} => undef } }); |
99a65fa8 |
665 | } |
666 | if (!ref($v) or Scalar::Util::blessed($v)) { |
ec19b759 |
667 | my $d = our $Default_Scalar_To; |
99a65fa8 |
668 | return +{ |
669 | -op => [ |
670 | $self->{cmp}, |
944f2c7b |
671 | $self->_expand_ident(-ident => $k), |
7d93797d |
672 | ($d |
673 | ? $self->_expand_expr($d => $v) |
674 | : { -bind => [ $k, $v ] } |
675 | ) |
99a65fa8 |
676 | ] |
677 | }; |
678 | } |
679 | if (ref($v) eq 'HASH') { |
680 | if (keys %$v > 1) { |
86997346 |
681 | return $self->_expand_andor(-and => [ |
682 | map +{ $k => { $_ => $v->{$_} } }, |
99a65fa8 |
683 | sort keys %$v |
86997346 |
684 | ]); |
99a65fa8 |
685 | } |
b18a95e4 |
686 | return undef unless keys %$v; |
99a65fa8 |
687 | my ($vk, $vv) = %$v; |
d455dcf1 |
688 | my $op = join ' ', split '_', (map lc, $vk =~ /^-?(.*)$/)[0]; |
689 | $self->_assert_pass_injection_guard($op); |
690 | if ($op =~ s/ [_\s]? \d+ $//x ) { |
99a65fa8 |
691 | belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. ' |
d455dcf1 |
692 | . "You probably wanted ...-and => [ -$op => COND1, -$op => COND2 ... ]"; |
99a65fa8 |
693 | } |
143f50df |
694 | if (my $x = $self->{expand_op}{$op}) { |
2f57f4f0 |
695 | local our $Cur_Col_Meta = $k; |
143f50df |
696 | return $self->$x($op, $vv, $k); |
99a65fa8 |
697 | } |
2f57f4f0 |
698 | if ($op eq 'value' and not defined($vv)) { |
7b3a5558 |
699 | return $self->_expand_expr({ $k, undef }) unless defined($vv); |
99a65fa8 |
700 | } |
d455dcf1 |
701 | if ($op =~ /^is(?: not)?$/) { |
702 | puke "$op can only take undef as argument" |
99a65fa8 |
703 | if defined($vv) |
704 | and not ( |
705 | ref($vv) eq 'HASH' |
706 | and exists($vv->{-value}) |
707 | and !defined($vv->{-value}) |
708 | ); |
d455dcf1 |
709 | return +{ -op => [ $op.' null', $self->_expand_ident(-ident => $k) ] }; |
99a65fa8 |
710 | } |
d455dcf1 |
711 | if ($op =~ /^(and|or)$/) { |
99a65fa8 |
712 | if (ref($vv) eq 'HASH') { |
e62fe58a |
713 | return +{ -op => [ |
d455dcf1 |
714 | $op, |
ff1b554a |
715 | map $self->_expand_expr({ $k, { $_ => $vv->{$_} } }), |
99a65fa8 |
716 | sort keys %$vv |
7d7868d1 |
717 | ] }; |
718 | } |
e28d9b13 |
719 | } |
d455dcf1 |
720 | if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}) { |
721 | return { -op => [ $op, $self->_expand_ident(-ident => $k), $vv ] }; |
7dbe1183 |
722 | } |
d455dcf1 |
723 | if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) { |
96a8d74a |
724 | return { -op => [ |
725 | $self->{cmp}, |
de8d04fb |
726 | $self->_expand_ident(-ident => $k), |
d455dcf1 |
727 | { -op => [ $op, $vv ] } |
96a8d74a |
728 | ] }; |
729 | } |
99a65fa8 |
730 | if (ref($vv) eq 'ARRAY') { |
731 | my ($logic, @values) = ( |
732 | (defined($vv->[0]) and $vv->[0] =~ /^-(and|or)$/i) |
733 | ? @$vv |
734 | : (-or => @$vv) |
735 | ); |
736 | if ( |
d455dcf1 |
737 | $op =~ $self->{inequality_op} |
738 | or $op =~ $self->{not_like_op} |
99a65fa8 |
739 | ) { |
740 | if (lc($logic) eq '-or' and @values > 1) { |
d455dcf1 |
741 | belch "A multi-element arrayref as an argument to the inequality op '${\uc($op)}' " |
99a65fa8 |
742 | . 'is technically equivalent to an always-true 1=1 (you probably wanted ' |
743 | . "to say ...{ \$inequality_op => [ -and => \@values ] }... instead)" |
744 | ; |
745 | } |
ef071fad |
746 | } |
b3cb13e8 |
747 | unless (@values) { |
748 | # try to DWIM on equality operators |
b3cb13e8 |
749 | return |
e175845b |
750 | $op =~ $self->{equality_op} ? $self->sqlfalse |
751 | : $op =~ $self->{like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqlfalse |
752 | : $op =~ $self->{inequality_op} ? $self->sqltrue |
753 | : $op =~ $self->{not_like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqltrue |
b3cb13e8 |
754 | : puke "operator '$op' applied on an empty array (field '$k')"; |
755 | } |
63c97a0b |
756 | return +{ -op => [ |
757 | $logic =~ /^-(.*)$/, |
85acf9c4 |
758 | map $self->_expand_expr({ $k => { $vk => $_ } }), |
99a65fa8 |
759 | @values |
760 | ] }; |
761 | } |
762 | if ( |
763 | !defined($vv) |
764 | or ( |
765 | ref($vv) eq 'HASH' |
766 | and exists $vv->{-value} |
767 | and not defined $vv->{-value} |
768 | ) |
769 | ) { |
99a65fa8 |
770 | my $is = |
771 | $op =~ /^not$/i ? 'is not' # legacy |
772 | : $op =~ $self->{equality_op} ? 'is' |
773 | : $op =~ $self->{like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is' |
774 | : $op =~ $self->{inequality_op} ? 'is not' |
775 | : $op =~ $self->{not_like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is not' |
776 | : puke "unexpected operator '$op' with undef operand"; |
de8d04fb |
777 | return +{ -op => [ $is.' null', $self->_expand_ident(-ident => $k) ] }; |
99a65fa8 |
778 | } |
779 | local our $Cur_Col_Meta = $k; |
780 | return +{ -op => [ |
d455dcf1 |
781 | $op, |
782 | $self->_expand_ident(-ident => $k), |
783 | $self->_expand_expr($vv) |
99a65fa8 |
784 | ] }; |
785 | } |
786 | if (ref($v) eq 'ARRAY') { |
e175845b |
787 | return $self->sqlfalse unless @$v; |
99a65fa8 |
788 | $self->_debug("ARRAY($k) means distribute over elements"); |
70f98e4b |
789 | my $this_logic = lc( |
790 | $v->[0] =~ /^-(and|or)$/i |
791 | ? shift(@{$v = [ @$v ]}) |
792 | : '-'.($self->{logic} || 'or') |
99a65fa8 |
793 | ); |
70f98e4b |
794 | return $self->_expand_expr({ |
795 | $this_logic => [ map +{ $k => $_ }, @$v ] |
796 | }); |
99a65fa8 |
797 | } |
798 | if (my $literal = is_literal_value($v)) { |
799 | unless (length $k) { |
800 | belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead'; |
801 | return \$literal; |
802 | } |
803 | my ($sql, @bind) = @$literal; |
804 | if ($self->{bindtype} eq 'columns') { |
805 | for (@bind) { |
6fb2bd90 |
806 | $self->_assert_bindval_matches_bindtype($_); |
aa8d7bdb |
807 | } |
331e2209 |
808 | } |
99a65fa8 |
809 | return +{ -literal => [ $self->_quote($k).' '.$sql, @bind ] }; |
331e2209 |
810 | } |
99a65fa8 |
811 | die "notreached"; |
59588695 |
812 | } |
813 | |
9103bd14 |
814 | sub _expand_ident { |
83de255f |
815 | my ($self, $op, $body) = @_; |
816 | unless (defined($body) or (ref($body) and ref($body) eq 'ARRAY')) { |
817 | puke "$op requires a single plain scalar argument (a quotable identifier) or an arrayref of identifier parts"; |
818 | } |
9103bd14 |
819 | my @parts = map split(/\Q${\($self->{name_sep}||'.')}\E/, $_), |
820 | ref($body) ? @$body : $body; |
944f2c7b |
821 | return { -ident => $parts[-1] } if $self->{_dequalify_idents}; |
822 | unless ($self->{quote_char}) { |
823 | $self->_assert_pass_injection_guard($_) for @parts; |
824 | } |
9103bd14 |
825 | return +{ -ident => \@parts }; |
826 | } |
827 | |
71f1364b |
828 | sub _expand_value { |
829 | +{ -bind => [ our $Cur_Col_Meta, $_[2] ] }; |
830 | } |
831 | |
2a238661 |
832 | sub _expand_not { |
833 | +{ -op => [ 'not', $_[0]->_expand_expr($_[2]) ] }; |
834 | } |
835 | |
05d37f9c |
836 | sub _expand_bool { |
837 | my ($self, undef, $v) = @_; |
838 | if (ref($v)) { |
839 | return $self->_expand_expr($v); |
840 | } |
841 | puke "-bool => undef not supported" unless defined($v); |
842 | return $self->_expand_ident(-ident => $v); |
843 | } |
844 | |
70f98e4b |
845 | sub _expand_andor { |
846 | my ($self, $k, $v) = @_; |
847 | my ($logic) = $k =~ /^-(.*)$/; |
848 | if (ref($v) eq 'HASH') { |
849 | return +{ -op => [ |
850 | $logic, |
851 | map $self->_expand_expr({ $_ => $v->{$_} }, $logic), |
852 | sort keys %$v |
853 | ] }; |
854 | } |
855 | if (ref($v) eq 'ARRAY') { |
856 | $logic eq 'and' or $logic eq 'or' or puke "unknown logic: $logic"; |
857 | |
858 | my @expr = grep { |
859 | (ref($_) eq 'ARRAY' and @$_) |
860 | or (ref($_) eq 'HASH' and %$_) |
861 | or 1 |
862 | } @$v; |
863 | |
864 | my @res; |
865 | |
866 | while (my ($el) = splice @expr, 0, 1) { |
867 | puke "Supplying an empty left hand side argument is not supported in array-pairs" |
868 | unless defined($el) and length($el); |
869 | my $elref = ref($el); |
870 | if (!$elref) { |
871 | local our $Expand_Depth = 0; |
872 | push(@res, grep defined, $self->_expand_expr({ $el, shift(@expr) })); |
873 | } elsif ($elref eq 'ARRAY') { |
874 | push(@res, grep defined, $self->_expand_expr($el)) if @$el; |
875 | } elsif (my $l = is_literal_value($el)) { |
876 | push @res, { -literal => $l }; |
877 | } elsif ($elref eq 'HASH') { |
878 | local our $Expand_Depth = 0; |
879 | push @res, grep defined, $self->_expand_expr($el) if %$el; |
880 | } else { |
881 | die "notreached"; |
882 | } |
883 | } |
884 | # ??? |
885 | # return $res[0] if @res == 1; |
886 | return { -op => [ $logic, @res ] }; |
887 | } |
888 | die "notreached"; |
889 | } |
890 | |
143f50df |
891 | sub _expand_between { |
892 | my ($self, $op, $vv, $k) = @_; |
893 | local our $Cur_Col_Meta = $k; |
894 | my @rhs = map $self->_expand_expr($_), |
895 | ref($vv) eq 'ARRAY' ? @$vv : $vv; |
896 | unless ( |
897 | (@rhs == 1 and ref($rhs[0]) eq 'HASH' and $rhs[0]->{-literal}) |
898 | or |
899 | (@rhs == 2 and defined($rhs[0]) and defined($rhs[1])) |
900 | ) { |
901 | puke "Operator '${\uc($op)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref"; |
902 | } |
903 | return +{ -op => [ |
904 | $op, |
905 | $self->_expand_ident(-ident => $k), |
906 | @rhs |
907 | ] } |
908 | } |
909 | |
416026a9 |
910 | sub _expand_in { |
911 | my ($self, $op, $vv, $k) = @_; |
912 | if (my $literal = is_literal_value($vv)) { |
913 | my ($sql, @bind) = @$literal; |
914 | my $opened_sql = $self->_open_outer_paren($sql); |
915 | return +{ -op => [ |
916 | $op, $self->_expand_ident(-ident => $k), |
917 | [ { -literal => [ $opened_sql, @bind ] } ] |
918 | ] }; |
919 | } |
920 | my $undef_err = |
921 | 'SQL::Abstract before v1.75 used to generate incorrect SQL when the ' |
922 | . "-${\uc($op)} operator was given an undef-containing list: !!!AUDIT YOUR CODE " |
923 | . 'AND DATA!!! (the upcoming Data::Query-based version of SQL::Abstract ' |
924 | . 'will emit the logically correct SQL instead of raising this exception)' |
925 | ; |
926 | puke("Argument passed to the '${\uc($op)}' operator can not be undefined") |
927 | if !defined($vv); |
928 | my @rhs = map $self->_expand_expr($_), |
929 | map { ref($_) ? $_ : { -bind => [ $k, $_ ] } } |
930 | map { defined($_) ? $_: puke($undef_err) } |
931 | (ref($vv) eq 'ARRAY' ? @$vv : $vv); |
932 | return $self->${\($op =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs; |
933 | |
934 | return +{ -op => [ |
935 | $op, |
936 | $self->_expand_ident(-ident => $k), |
937 | \@rhs |
938 | ] }; |
939 | } |
940 | |
96449e8e |
941 | sub _recurse_where { |
942 | my ($self, $where, $logic) = @_; |
943 | |
5492d4c2 |
944 | # Special case: top level simple string treated as literal |
945 | |
946 | my $where_exp = (ref($where) |
ae56a156 |
947 | ? $self->_expand_expr($where, $logic) |
5492d4c2 |
948 | : { -literal => [ $where ] }); |
e175845b |
949 | |
e3e27543 |
950 | # dispatch expanded expression |
311b2151 |
951 | |
79d310f2 |
952 | my ($sql, @bind) = defined($where_exp) ? $self->render_aqt($where_exp) : (undef); |
abe1a491 |
953 | # DBIx::Class used to call _recurse_where in scalar context |
954 | # something else might too... |
955 | if (wantarray) { |
956 | return ($sql, @bind); |
957 | } |
958 | else { |
959 | belch "Calling _recurse_where in scalar context is deprecated and will go away before 2.0"; |
960 | return $sql; |
961 | } |
96449e8e |
962 | } |
963 | |
181dcebf |
964 | sub _render_ident { |
965 | my ($self, $ident) = @_; |
cc422895 |
966 | |
9cf28dfb |
967 | return $self->_convert($self->_quote($ident)); |
cc422895 |
968 | } |
969 | |
ca158918 |
970 | sub _render_list { |
971 | my ($self, $list) = @_; |
972 | my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @$list; |
973 | return join(', ', map $_->[0], @parts), map @{$_}[1..$#$_], @parts; |
974 | } |
975 | |
976 | sub _render_func { |
977 | my ($self, $rest) = @_; |
978 | my ($func, @args) = @$rest; |
979 | my @arg_sql; |
980 | my @bind = map { |
981 | my @x = @$_; |
982 | push @arg_sql, shift @x; |
983 | @x |
984 | } map [ $self->render_aqt($_) ], @args; |
985 | return ($self->_sqlcase($func).'('.join(', ', @arg_sql).')', @bind); |
986 | } |
d13725da |
987 | |
ca158918 |
988 | sub _render_bind { |
989 | my ($self, $bind) = @_; |
990 | return ($self->_convert('?'), $self->_bindtype(@$bind)); |
991 | } |
992 | |
993 | sub _render_literal { |
994 | my ($self, $literal) = @_; |
995 | $self->_assert_bindval_matches_bindtype(@{$literal}[1..$#$literal]); |
996 | return @$literal; |
997 | } |
998 | |
999 | our $RENDER_OP = { |
2d653a7f |
1000 | (map +($_ => sub { |
1001 | my ($self, $op, $args) = @_; |
1002 | my ($left, $low, $high) = @$args; |
1003 | my ($rhsql, @rhbind) = do { |
1004 | if (@$args == 2) { |
1005 | puke "Single arg to between must be a literal" |
1006 | unless $low->{-literal}; |
1007 | @{$low->{-literal}} |
1008 | } else { |
1009 | my ($l, $h) = map [ $self->render_aqt($_) ], $low, $high; |
1010 | (join(' ', $l->[0], $self->_sqlcase('and'), $h->[0]), |
1011 | @{$l}[1..$#$l], @{$h}[1..$#$h]) |
1012 | } |
1013 | }; |
1014 | my ($lhsql, @lhbind) = $self->render_aqt($left); |
1015 | return ( |
1016 | join(' ', '(', $lhsql, $self->_sqlcase($op), $rhsql, ')'), |
1017 | @lhbind, @rhbind |
1018 | ); |
b5b18861 |
1019 | }), 'between', 'not between'), |
2d653a7f |
1020 | (map +($_ => sub { |
1021 | my ($self, $op, $args) = @_; |
1022 | my ($lhs, $rhs) = @$args; |
1023 | my @in_bind; |
1024 | my @in_sql = map { |
1025 | my ($sql, @bind) = $self->render_aqt($_); |
1026 | push @in_bind, @bind; |
1027 | $sql; |
1028 | } @$rhs; |
1029 | my ($lhsql, @lbind) = $self->render_aqt($lhs); |
1030 | return ( |
1031 | $lhsql.' '.$self->_sqlcase($op).' ( ' |
1032 | .join(', ', @in_sql) |
1033 | .' )', |
1034 | @lbind, @in_bind |
1035 | ); |
10d07c4e |
1036 | }), 'in', 'not in'), |
b23fd5ff |
1037 | (map +($_ => '_render_unop_postfix'), |
1038 | 'is null', 'is not null', 'asc', 'desc', |
1039 | ), |
2ddaa002 |
1040 | (not => '_render_op_not'), |
2d653a7f |
1041 | (map +($_ => sub { |
1042 | my ($self, $op, $args) = @_; |
1043 | my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @$args; |
1044 | return '' unless @parts; |
1045 | return @{$parts[0]} if @parts == 1; |
1046 | my ($final_sql) = join( |
1047 | ' '.$self->_sqlcase($op).' ', |
1048 | map $_->[0], @parts |
1049 | ); |
1050 | return ( |
1051 | '('.$final_sql.')', |
1052 | map @{$_}[1..$#$_], @parts |
1053 | ); |
1054 | }), qw(and or)), |
ca158918 |
1055 | }; |
1056 | |
181dcebf |
1057 | sub _render_op { |
1058 | my ($self, $v) = @_; |
d13725da |
1059 | my ($op, @args) = @$v; |
ca158918 |
1060 | if (my $r = $self->{render_op}{$op}) { |
b23fd5ff |
1061 | return $self->$r($op, \@args); |
b5b18861 |
1062 | } |
3ae10d16 |
1063 | my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}; |
1064 | if ($us and @args > 1) { |
99a65fa8 |
1065 | puke "Special op '${op}' requires first value to be identifier" |
7d93797d |
1066 | unless my ($ident) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0]; |
1067 | my $k = join(($self->{name_sep}||'.'), @$ident); |
dd1d0dbf |
1068 | local our $Expand_Depth = 1; |
99a65fa8 |
1069 | return $self->${\($us->{handler})}($k, $op, $args[1]); |
1070 | } |
96a8d74a |
1071 | if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) { |
1072 | return $self->${\($us->{handler})}($op, $args[0]); |
1073 | } |
2d653a7f |
1074 | if (@args == 1) { |
2ddaa002 |
1075 | return $self->_render_unop_prefix($op, \@args); |
16d9289c |
1076 | } else { |
79d310f2 |
1077 | my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @args; |
1c0c0f41 |
1078 | return '' unless @parts; |
2d653a7f |
1079 | my ($final_sql) = join( |
9e242c8c |
1080 | ' '.$self->_sqlcase($op).' ', |
77617257 |
1081 | map $_->[0], @parts |
1082 | ); |
99a65fa8 |
1083 | return ( |
77617257 |
1084 | $final_sql, |
16d9289c |
1085 | map @{$_}[1..$#$_], @parts |
99a65fa8 |
1086 | ); |
d13725da |
1087 | } |
1088 | die "unhandled"; |
1089 | } |
1090 | |
2ddaa002 |
1091 | sub _render_op_not { |
1092 | my ($self, $op, $v) = @_; |
1093 | my ($sql, @bind) = $self->_render_unop_prefix($op, $v); |
1094 | return "(${sql})", @bind; |
1095 | } |
1096 | |
1097 | sub _render_unop_prefix { |
1098 | my ($self, $op, $v) = @_; |
1099 | my ($expr_sql, @bind) = $self->render_aqt($v->[0]); |
1100 | my $op_sql = $self->_sqlcase($op); |
1101 | return ("${op_sql} ${expr_sql}", @bind); |
1102 | } |
1103 | |
b23fd5ff |
1104 | sub _render_unop_postfix { |
1105 | my ($self, $op, $v) = @_; |
715b4e6a |
1106 | my ($expr_sql, @bind) = $self->render_aqt($v->[0]); |
b23fd5ff |
1107 | my $op_sql = $self->_sqlcase($op); |
1108 | return ($expr_sql.' '.$op_sql, @bind); |
1109 | } |
1110 | |
4a1f01a3 |
1111 | # Some databases (SQLite) treat col IN (1, 2) different from |
1112 | # col IN ( (1, 2) ). Use this to strip all outer parens while |
1113 | # adding them back in the corresponding method |
1114 | sub _open_outer_paren { |
1115 | my ($self, $sql) = @_; |
a5f91feb |
1116 | |
ca4f826a |
1117 | while (my ($inner) = $sql =~ /^ \s* \( (.*) \) \s* $/xs) { |
a5f91feb |
1118 | |
1119 | # there are closing parens inside, need the heavy duty machinery |
1120 | # to reevaluate the extraction starting from $sql (full reevaluation) |
ca4f826a |
1121 | if ($inner =~ /\)/) { |
a5f91feb |
1122 | require Text::Balanced; |
1123 | |
1124 | my (undef, $remainder) = do { |
1125 | # idiotic design - writes to $@ but *DOES NOT* throw exceptions |
1126 | local $@; |
ca4f826a |
1127 | Text::Balanced::extract_bracketed($sql, '()', qr/\s*/); |
a5f91feb |
1128 | }; |
1129 | |
1130 | # the entire expression needs to be a balanced bracketed thing |
1131 | # (after an extract no remainder sans trailing space) |
1132 | last if defined $remainder and $remainder =~ /\S/; |
1133 | } |
1134 | |
1135 | $sql = $inner; |
1136 | } |
1137 | |
1138 | $sql; |
4a1f01a3 |
1139 | } |
1140 | |
96449e8e |
1141 | |
96449e8e |
1142 | #====================================================================== |
1143 | # ORDER BY |
1144 | #====================================================================== |
1145 | |
33177570 |
1146 | sub _expand_order_by { |
96449e8e |
1147 | my ($self, $arg) = @_; |
1148 | |
33177570 |
1149 | return unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg); |
2b6158af |
1150 | |
4325df6a |
1151 | my $expander = sub { |
1152 | my ($self, $dir, $expr) = @_; |
52ca537e |
1153 | my @to_expand = ref($expr) eq 'ARRAY' ? @$expr : $expr; |
1154 | foreach my $arg (@to_expand) { |
1155 | if ( |
1156 | ref($arg) eq 'HASH' |
1157 | and keys %$arg > 1 |
1158 | and grep /^-(asc|desc)$/, keys %$arg |
1159 | ) { |
1160 | puke "ordering direction hash passed to order by must have exactly one key (-asc or -desc)"; |
1161 | } |
1162 | } |
7384c311 |
1163 | my @exp = map +( |
1164 | defined($dir) ? { -op => [ $dir =~ /^-?(.*)$/ ,=> $_ ] } : $_ |
1165 | ), |
79d310f2 |
1166 | map $self->expand_expr($_, -ident), |
74156ee9 |
1167 | map ref($_) eq 'ARRAY' ? @$_ : $_, @to_expand; |
352d74bc |
1168 | return (@exp > 1 ? { -list => \@exp } : $exp[0]); |
4325df6a |
1169 | }; |
18c743c8 |
1170 | |
7384c311 |
1171 | local @{$self->{expand}}{qw(-asc -desc)} = (($expander) x 2); |
f267b646 |
1172 | |
33177570 |
1173 | return $self->$expander(undef, $arg); |
1174 | } |
1175 | |
1176 | sub _order_by { |
1177 | my ($self, $arg) = @_; |
1178 | |
1179 | return '' unless defined(my $expanded = $self->_expand_order_by($arg)); |
4325df6a |
1180 | |
79d310f2 |
1181 | my ($sql, @bind) = $self->render_aqt($expanded); |
4325df6a |
1182 | |
13cd9220 |
1183 | return '' unless length($sql); |
1184 | |
4325df6a |
1185 | my $final_sql = $self->_sqlcase(' order by ').$sql; |
1186 | |
1187 | return wantarray ? ($final_sql, @bind) : $final_sql; |
f267b646 |
1188 | } |
1189 | |
2e3cc357 |
1190 | # _order_by no longer needs to call this so doesn't but DBIC uses it. |
1191 | |
33177570 |
1192 | sub _order_by_chunks { |
1193 | my ($self, $arg) = @_; |
1194 | |
1195 | return () unless defined(my $expanded = $self->_expand_order_by($arg)); |
1196 | |
2e3cc357 |
1197 | return $self->_chunkify_order_by($expanded); |
1198 | } |
1199 | |
1200 | sub _chunkify_order_by { |
1201 | my ($self, $expanded) = @_; |
1b630cfe |
1202 | |
79d310f2 |
1203 | return grep length, $self->render_aqt($expanded) |
1b630cfe |
1204 | if $expanded->{-ident} or @{$expanded->{-literal}||[]} == 1; |
1205 | |
33177570 |
1206 | for ($expanded) { |
352d74bc |
1207 | if (ref() eq 'HASH' and my $l = $_->{-list}) { |
1208 | return map $self->_chunkify_order_by($_), @$l; |
33177570 |
1209 | } |
79d310f2 |
1210 | return [ $self->render_aqt($_) ]; |
33177570 |
1211 | } |
1212 | } |
1213 | |
96449e8e |
1214 | #====================================================================== |
1215 | # DATASOURCE (FOR NOW, JUST PLAIN TABLE OR LIST OF TABLES) |
1216 | #====================================================================== |
1217 | |
1218 | sub _table { |
1219 | my $self = shift; |
1220 | my $from = shift; |
79d310f2 |
1221 | ($self->render_aqt( |
8476c6a3 |
1222 | $self->_expand_maybe_list_expr($from, undef, -ident) |
7ad12721 |
1223 | ))[0]; |
96449e8e |
1224 | } |
1225 | |
1226 | |
1227 | #====================================================================== |
1228 | # UTILITY FUNCTIONS |
1229 | #====================================================================== |
1230 | |
8476c6a3 |
1231 | sub _expand_maybe_list_expr { |
1232 | my ($self, $expr, $logic, $default) = @_; |
bba04f52 |
1233 | my $e = do { |
1234 | if (ref($expr) eq 'ARRAY') { |
352d74bc |
1235 | return { -list => [ |
4ee3313d |
1236 | map $self->expand_expr($_, $default), @$expr |
bba04f52 |
1237 | ] } if @$expr > 1; |
1238 | $expr->[0] |
1239 | } else { |
1240 | $expr |
1241 | } |
1242 | }; |
4ee3313d |
1243 | return $self->expand_expr($e, $default); |
8476c6a3 |
1244 | } |
1245 | |
955e77ca |
1246 | # highly optimized, as it's called way too often |
96449e8e |
1247 | sub _quote { |
955e77ca |
1248 | # my ($self, $label) = @_; |
96449e8e |
1249 | |
955e77ca |
1250 | return '' unless defined $_[1]; |
955e77ca |
1251 | return ${$_[1]} if ref($_[1]) eq 'SCALAR'; |
d3162b5c |
1252 | puke 'Identifier cannot be hashref' if ref($_[1]) eq 'HASH'; |
96449e8e |
1253 | |
d3162b5c |
1254 | unless ($_[0]->{quote_char}) { |
1255 | if (ref($_[1]) eq 'ARRAY') { |
1256 | return join($_[0]->{name_sep}||'.', @{$_[1]}); |
1257 | } else { |
1258 | $_[0]->_assert_pass_injection_guard($_[1]); |
1259 | return $_[1]; |
1260 | } |
1261 | } |
96449e8e |
1262 | |
07d7c35c |
1263 | my $qref = ref $_[0]->{quote_char}; |
439834d3 |
1264 | my ($l, $r) = |
1265 | !$qref ? ($_[0]->{quote_char}, $_[0]->{quote_char}) |
1266 | : ($qref eq 'ARRAY') ? @{$_[0]->{quote_char}} |
1267 | : puke "Unsupported quote_char format: $_[0]->{quote_char}"; |
1268 | |
46be4313 |
1269 | my $esc = $_[0]->{escape_char} || $r; |
96449e8e |
1270 | |
07d7c35c |
1271 | # parts containing * are naturally unquoted |
d3162b5c |
1272 | return join( |
1273 | $_[0]->{name_sep}||'', |
1274 | map +( |
1275 | $_ eq '*' |
1276 | ? $_ |
1277 | : do { (my $n = $_) =~ s/(\Q$esc\E|\Q$r\E)/$esc$1/g; $l . $n . $r } |
1278 | ), |
1279 | (ref($_[1]) eq 'ARRAY' |
1280 | ? @{$_[1]} |
1281 | : ( |
1282 | $_[0]->{name_sep} |
1283 | ? split (/\Q$_[0]->{name_sep}\E/, $_[1] ) |
1284 | : $_[1] |
1285 | ) |
1286 | ) |
955e77ca |
1287 | ); |
96449e8e |
1288 | } |
1289 | |
1290 | |
1291 | # Conversion, if applicable |
d7c862e0 |
1292 | sub _convert { |
07d7c35c |
1293 | #my ($self, $arg) = @_; |
7ad12721 |
1294 | if ($_[0]->{convert_where}) { |
1295 | return $_[0]->_sqlcase($_[0]->{convert_where}) .'(' . $_[1] . ')'; |
96449e8e |
1296 | } |
07d7c35c |
1297 | return $_[1]; |
96449e8e |
1298 | } |
1299 | |
1300 | # And bindtype |
d7c862e0 |
1301 | sub _bindtype { |
07d7c35c |
1302 | #my ($self, $col, @vals) = @_; |
07d7c35c |
1303 | # called often - tighten code |
1304 | return $_[0]->{bindtype} eq 'columns' |
1305 | ? map {[$_[1], $_]} @_[2 .. $#_] |
1306 | : @_[2 .. $#_] |
1307 | ; |
96449e8e |
1308 | } |
1309 | |
fe3ae272 |
1310 | # Dies if any element of @bind is not in [colname => value] format |
1311 | # if bindtype is 'columns'. |
1312 | sub _assert_bindval_matches_bindtype { |
c94a6c93 |
1313 | # my ($self, @bind) = @_; |
1314 | my $self = shift; |
fe3ae272 |
1315 | if ($self->{bindtype} eq 'columns') { |
c94a6c93 |
1316 | for (@_) { |
1317 | if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) { |
3a06278c |
1318 | puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]" |
fe3ae272 |
1319 | } |
1320 | } |
1321 | } |
1322 | } |
1323 | |
96449e8e |
1324 | sub _join_sql_clauses { |
1325 | my ($self, $logic, $clauses_aref, $bind_aref) = @_; |
1326 | |
1327 | if (@$clauses_aref > 1) { |
1328 | my $join = " " . $self->_sqlcase($logic) . " "; |
1329 | my $sql = '( ' . join($join, @$clauses_aref) . ' )'; |
1330 | return ($sql, @$bind_aref); |
1331 | } |
1332 | elsif (@$clauses_aref) { |
1333 | return ($clauses_aref->[0], @$bind_aref); # no parentheses |
1334 | } |
1335 | else { |
1336 | return (); # if no SQL, ignore @$bind_aref |
1337 | } |
1338 | } |
1339 | |
1340 | |
1341 | # Fix SQL case, if so requested |
1342 | sub _sqlcase { |
96449e8e |
1343 | # LDNOTE: if $self->{case} is true, then it contains 'lower', so we |
1344 | # don't touch the argument ... crooked logic, but let's not change it! |
07d7c35c |
1345 | return $_[0]->{case} ? $_[1] : uc($_[1]); |
96449e8e |
1346 | } |
1347 | |
1348 | |
1349 | #====================================================================== |
1350 | # DISPATCHING FROM REFKIND |
1351 | #====================================================================== |
1352 | |
1353 | sub _refkind { |
1354 | my ($self, $data) = @_; |
96449e8e |
1355 | |
955e77ca |
1356 | return 'UNDEF' unless defined $data; |
1357 | |
1358 | # blessed objects are treated like scalars |
1359 | my $ref = (Scalar::Util::blessed $data) ? '' : ref $data; |
1360 | |
1361 | return 'SCALAR' unless $ref; |
1362 | |
1363 | my $n_steps = 1; |
1364 | while ($ref eq 'REF') { |
96449e8e |
1365 | $data = $$data; |
955e77ca |
1366 | $ref = (Scalar::Util::blessed $data) ? '' : ref $data; |
1367 | $n_steps++ if $ref; |
96449e8e |
1368 | } |
1369 | |
848556bc |
1370 | return ($ref||'SCALAR') . ('REF' x $n_steps); |
96449e8e |
1371 | } |
1372 | |
1373 | sub _try_refkind { |
1374 | my ($self, $data) = @_; |
1375 | my @try = ($self->_refkind($data)); |
1376 | push @try, 'SCALAR_or_UNDEF' if $try[0] eq 'SCALAR' || $try[0] eq 'UNDEF'; |
1377 | push @try, 'FALLBACK'; |
955e77ca |
1378 | return \@try; |
96449e8e |
1379 | } |
1380 | |
1381 | sub _METHOD_FOR_refkind { |
1382 | my ($self, $meth_prefix, $data) = @_; |
f39eaa60 |
1383 | |
1384 | my $method; |
955e77ca |
1385 | for (@{$self->_try_refkind($data)}) { |
f39eaa60 |
1386 | $method = $self->can($meth_prefix."_".$_) |
1387 | and last; |
1388 | } |
1389 | |
1390 | return $method || puke "cannot dispatch on '$meth_prefix' for ".$self->_refkind($data); |
96449e8e |
1391 | } |
1392 | |
1393 | |
1394 | sub _SWITCH_refkind { |
1395 | my ($self, $data, $dispatch_table) = @_; |
1396 | |
f39eaa60 |
1397 | my $coderef; |
955e77ca |
1398 | for (@{$self->_try_refkind($data)}) { |
f39eaa60 |
1399 | $coderef = $dispatch_table->{$_} |
1400 | and last; |
1401 | } |
1402 | |
1403 | puke "no dispatch entry for ".$self->_refkind($data) |
1404 | unless $coderef; |
1405 | |
96449e8e |
1406 | $coderef->(); |
1407 | } |
1408 | |
1409 | |
1410 | |
1411 | |
1412 | #====================================================================== |
1413 | # VALUES, GENERATE, AUTOLOAD |
1414 | #====================================================================== |
1415 | |
1416 | # LDNOTE: original code from nwiger, didn't touch code in that section |
1417 | # I feel the AUTOLOAD stuff should not be the default, it should |
1418 | # only be activated on explicit demand by user. |
1419 | |
1420 | sub values { |
1421 | my $self = shift; |
1422 | my $data = shift || return; |
1423 | puke "Argument to ", __PACKAGE__, "->values must be a \\%hash" |
1424 | unless ref $data eq 'HASH'; |
bab725ce |
1425 | |
1426 | my @all_bind; |
ca4f826a |
1427 | foreach my $k (sort keys %$data) { |
bab725ce |
1428 | my $v = $data->{$k}; |
1429 | $self->_SWITCH_refkind($v, { |
9d48860e |
1430 | ARRAYREF => sub { |
bab725ce |
1431 | if ($self->{array_datatypes}) { # array datatype |
1432 | push @all_bind, $self->_bindtype($k, $v); |
1433 | } |
1434 | else { # literal SQL with bind |
1435 | my ($sql, @bind) = @$v; |
1436 | $self->_assert_bindval_matches_bindtype(@bind); |
1437 | push @all_bind, @bind; |
1438 | } |
1439 | }, |
1440 | ARRAYREFREF => sub { # literal SQL with bind |
1441 | my ($sql, @bind) = @${$v}; |
1442 | $self->_assert_bindval_matches_bindtype(@bind); |
1443 | push @all_bind, @bind; |
1444 | }, |
1445 | SCALARREF => sub { # literal SQL without bind |
1446 | }, |
1447 | SCALAR_or_UNDEF => sub { |
1448 | push @all_bind, $self->_bindtype($k, $v); |
1449 | }, |
1450 | }); |
1451 | } |
1452 | |
1453 | return @all_bind; |
96449e8e |
1454 | } |
1455 | |
1456 | sub generate { |
1457 | my $self = shift; |
1458 | |
1459 | my(@sql, @sqlq, @sqlv); |
1460 | |
1461 | for (@_) { |
1462 | my $ref = ref $_; |
1463 | if ($ref eq 'HASH') { |
1464 | for my $k (sort keys %$_) { |
1465 | my $v = $_->{$k}; |
1466 | my $r = ref $v; |
1467 | my $label = $self->_quote($k); |
1468 | if ($r eq 'ARRAY') { |
fe3ae272 |
1469 | # literal SQL with bind |
1470 | my ($sql, @bind) = @$v; |
1471 | $self->_assert_bindval_matches_bindtype(@bind); |
96449e8e |
1472 | push @sqlq, "$label = $sql"; |
fe3ae272 |
1473 | push @sqlv, @bind; |
96449e8e |
1474 | } elsif ($r eq 'SCALAR') { |
fe3ae272 |
1475 | # literal SQL without bind |
96449e8e |
1476 | push @sqlq, "$label = $$v"; |
9d48860e |
1477 | } else { |
96449e8e |
1478 | push @sqlq, "$label = ?"; |
1479 | push @sqlv, $self->_bindtype($k, $v); |
1480 | } |
1481 | } |
1482 | push @sql, $self->_sqlcase('set'), join ', ', @sqlq; |
1483 | } elsif ($ref eq 'ARRAY') { |
1484 | # unlike insert(), assume these are ONLY the column names, i.e. for SQL |
1485 | for my $v (@$_) { |
1486 | my $r = ref $v; |
fe3ae272 |
1487 | if ($r eq 'ARRAY') { # literal SQL with bind |
1488 | my ($sql, @bind) = @$v; |
1489 | $self->_assert_bindval_matches_bindtype(@bind); |
1490 | push @sqlq, $sql; |
1491 | push @sqlv, @bind; |
1492 | } elsif ($r eq 'SCALAR') { # literal SQL without bind |
96449e8e |
1493 | # embedded literal SQL |
1494 | push @sqlq, $$v; |
9d48860e |
1495 | } else { |
96449e8e |
1496 | push @sqlq, '?'; |
1497 | push @sqlv, $v; |
1498 | } |
1499 | } |
1500 | push @sql, '(' . join(', ', @sqlq) . ')'; |
1501 | } elsif ($ref eq 'SCALAR') { |
1502 | # literal SQL |
1503 | push @sql, $$_; |
1504 | } else { |
1505 | # strings get case twiddled |
1506 | push @sql, $self->_sqlcase($_); |
1507 | } |
1508 | } |
1509 | |
1510 | my $sql = join ' ', @sql; |
1511 | |
1512 | # this is pretty tricky |
1513 | # if ask for an array, return ($stmt, @bind) |
1514 | # otherwise, s/?/shift @sqlv/ to put it inline |
1515 | if (wantarray) { |
1516 | return ($sql, @sqlv); |
1517 | } else { |
1518 | 1 while $sql =~ s/\?/my $d = shift(@sqlv); |
1519 | ref $d ? $d->[1] : $d/e; |
1520 | return $sql; |
1521 | } |
1522 | } |
1523 | |
1524 | |
1525 | sub DESTROY { 1 } |
1526 | |
1527 | sub AUTOLOAD { |
1528 | # This allows us to check for a local, then _form, attr |
1529 | my $self = shift; |
1530 | my($name) = $AUTOLOAD =~ /.*::(.+)/; |
1531 | return $self->generate($name, @_); |
1532 | } |
1533 | |
1534 | 1; |
1535 | |
1536 | |
1537 | |
1538 | __END__ |
32eab2da |
1539 | |
1540 | =head1 NAME |
1541 | |
1542 | SQL::Abstract - Generate SQL from Perl data structures |
1543 | |
1544 | =head1 SYNOPSIS |
1545 | |
1546 | use SQL::Abstract; |
1547 | |
1548 | my $sql = SQL::Abstract->new; |
1549 | |
85783f3c |
1550 | my($stmt, @bind) = $sql->select($source, \@fields, \%where, $order); |
32eab2da |
1551 | |
1552 | my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values); |
1553 | |
1554 | my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where); |
1555 | |
1556 | my($stmt, @bind) = $sql->delete($table, \%where); |
1557 | |
1558 | # Then, use these in your DBI statements |
1559 | my $sth = $dbh->prepare($stmt); |
1560 | $sth->execute(@bind); |
1561 | |
1562 | # Just generate the WHERE clause |
85783f3c |
1563 | my($stmt, @bind) = $sql->where(\%where, $order); |
32eab2da |
1564 | |
1565 | # Return values in the same order, for hashed queries |
1566 | # See PERFORMANCE section for more details |
1567 | my @bind = $sql->values(\%fieldvals); |
1568 | |
1569 | =head1 DESCRIPTION |
1570 | |
1571 | This module was inspired by the excellent L<DBIx::Abstract>. |
1572 | However, in using that module I found that what I really wanted |
1573 | to do was generate SQL, but still retain complete control over my |
1574 | statement handles and use the DBI interface. So, I set out to |
1575 | create an abstract SQL generation module. |
1576 | |
1577 | While based on the concepts used by L<DBIx::Abstract>, there are |
1578 | several important differences, especially when it comes to WHERE |
1579 | clauses. I have modified the concepts used to make the SQL easier |
1580 | to generate from Perl data structures and, IMO, more intuitive. |
1581 | The underlying idea is for this module to do what you mean, based |
1582 | on the data structures you provide it. The big advantage is that |
1583 | you don't have to modify your code every time your data changes, |
1584 | as this module figures it out. |
1585 | |
1586 | To begin with, an SQL INSERT is as easy as just specifying a hash |
1587 | of C<key=value> pairs: |
1588 | |
1589 | my %data = ( |
1590 | name => 'Jimbo Bobson', |
1591 | phone => '123-456-7890', |
1592 | address => '42 Sister Lane', |
1593 | city => 'St. Louis', |
1594 | state => 'Louisiana', |
1595 | ); |
1596 | |
1597 | The SQL can then be generated with this: |
1598 | |
1599 | my($stmt, @bind) = $sql->insert('people', \%data); |
1600 | |
1601 | Which would give you something like this: |
1602 | |
1603 | $stmt = "INSERT INTO people |
1604 | (address, city, name, phone, state) |
1605 | VALUES (?, ?, ?, ?, ?)"; |
1606 | @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson', |
1607 | '123-456-7890', 'Louisiana'); |
1608 | |
1609 | These are then used directly in your DBI code: |
1610 | |
1611 | my $sth = $dbh->prepare($stmt); |
1612 | $sth->execute(@bind); |
1613 | |
96449e8e |
1614 | =head2 Inserting and Updating Arrays |
1615 | |
1616 | If your database has array types (like for example Postgres), |
1617 | activate the special option C<< array_datatypes => 1 >> |
9d48860e |
1618 | when creating the C<SQL::Abstract> object. |
96449e8e |
1619 | Then you may use an arrayref to insert and update database array types: |
1620 | |
1621 | my $sql = SQL::Abstract->new(array_datatypes => 1); |
1622 | my %data = ( |
1623 | planets => [qw/Mercury Venus Earth Mars/] |
1624 | ); |
9d48860e |
1625 | |
96449e8e |
1626 | my($stmt, @bind) = $sql->insert('solar_system', \%data); |
1627 | |
1628 | This results in: |
1629 | |
1630 | $stmt = "INSERT INTO solar_system (planets) VALUES (?)" |
1631 | |
1632 | @bind = (['Mercury', 'Venus', 'Earth', 'Mars']); |
1633 | |
1634 | |
1635 | =head2 Inserting and Updating SQL |
1636 | |
1637 | In order to apply SQL functions to elements of your C<%data> you may |
1638 | specify a reference to an arrayref for the given hash value. For example, |
1639 | if you need to execute the Oracle C<to_date> function on a value, you can |
1640 | say something like this: |
32eab2da |
1641 | |
1642 | my %data = ( |
1643 | name => 'Bill', |
3ae1c5e2 |
1644 | date_entered => \[ "to_date(?,'MM/DD/YYYY')", "03/02/2003" ], |
9d48860e |
1645 | ); |
32eab2da |
1646 | |
1647 | The first value in the array is the actual SQL. Any other values are |
1648 | optional and would be included in the bind values array. This gives |
1649 | you: |
1650 | |
1651 | my($stmt, @bind) = $sql->insert('people', \%data); |
1652 | |
9d48860e |
1653 | $stmt = "INSERT INTO people (name, date_entered) |
32eab2da |
1654 | VALUES (?, to_date(?,'MM/DD/YYYY'))"; |
1655 | @bind = ('Bill', '03/02/2003'); |
1656 | |
1657 | An UPDATE is just as easy, all you change is the name of the function: |
1658 | |
1659 | my($stmt, @bind) = $sql->update('people', \%data); |
1660 | |
1661 | Notice that your C<%data> isn't touched; the module will generate |
1662 | the appropriately quirky SQL for you automatically. Usually you'll |
1663 | want to specify a WHERE clause for your UPDATE, though, which is |
1664 | where handling C<%where> hashes comes in handy... |
1665 | |
96449e8e |
1666 | =head2 Complex where statements |
1667 | |
32eab2da |
1668 | This module can generate pretty complicated WHERE statements |
1669 | easily. For example, simple C<key=value> pairs are taken to mean |
1670 | equality, and if you want to see if a field is within a set |
1671 | of values, you can use an arrayref. Let's say we wanted to |
1672 | SELECT some data based on this criteria: |
1673 | |
1674 | my %where = ( |
1675 | requestor => 'inna', |
1676 | worker => ['nwiger', 'rcwe', 'sfz'], |
1677 | status => { '!=', 'completed' } |
1678 | ); |
1679 | |
1680 | my($stmt, @bind) = $sql->select('tickets', '*', \%where); |
1681 | |
1682 | The above would give you something like this: |
1683 | |
1684 | $stmt = "SELECT * FROM tickets WHERE |
1685 | ( requestor = ? ) AND ( status != ? ) |
1686 | AND ( worker = ? OR worker = ? OR worker = ? )"; |
1687 | @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz'); |
1688 | |
1689 | Which you could then use in DBI code like so: |
1690 | |
1691 | my $sth = $dbh->prepare($stmt); |
1692 | $sth->execute(@bind); |
1693 | |
1694 | Easy, eh? |
1695 | |
0da0fe34 |
1696 | =head1 METHODS |
32eab2da |
1697 | |
13cc86af |
1698 | The methods are simple. There's one for every major SQL operation, |
32eab2da |
1699 | and a constructor you use first. The arguments are specified in a |
13cc86af |
1700 | similar order for each method (table, then fields, then a where |
32eab2da |
1701 | clause) to try and simplify things. |
1702 | |
32eab2da |
1703 | =head2 new(option => 'value') |
1704 | |
1705 | The C<new()> function takes a list of options and values, and returns |
1706 | a new B<SQL::Abstract> object which can then be used to generate SQL |
1707 | through the methods below. The options accepted are: |
1708 | |
1709 | =over |
1710 | |
1711 | =item case |
1712 | |
1713 | If set to 'lower', then SQL will be generated in all lowercase. By |
1714 | default SQL is generated in "textbook" case meaning something like: |
1715 | |
1716 | SELECT a_field FROM a_table WHERE some_field LIKE '%someval%' |
1717 | |
96449e8e |
1718 | Any setting other than 'lower' is ignored. |
1719 | |
32eab2da |
1720 | =item cmp |
1721 | |
1722 | This determines what the default comparison operator is. By default |
1723 | it is C<=>, meaning that a hash like this: |
1724 | |
1725 | %where = (name => 'nwiger', email => 'nate@wiger.org'); |
1726 | |
1727 | Will generate SQL like this: |
1728 | |
1729 | WHERE name = 'nwiger' AND email = 'nate@wiger.org' |
1730 | |
1731 | However, you may want loose comparisons by default, so if you set |
1732 | C<cmp> to C<like> you would get SQL such as: |
1733 | |
1734 | WHERE name like 'nwiger' AND email like 'nate@wiger.org' |
1735 | |
3af02ccb |
1736 | You can also override the comparison on an individual basis - see |
32eab2da |
1737 | the huge section on L</"WHERE CLAUSES"> at the bottom. |
1738 | |
96449e8e |
1739 | =item sqltrue, sqlfalse |
1740 | |
1741 | Expressions for inserting boolean values within SQL statements. |
6e0c6552 |
1742 | By default these are C<1=1> and C<1=0>. They are used |
1743 | by the special operators C<-in> and C<-not_in> for generating |
1744 | correct SQL even when the argument is an empty array (see below). |
96449e8e |
1745 | |
32eab2da |
1746 | =item logic |
1747 | |
1748 | This determines the default logical operator for multiple WHERE |
7cac25e6 |
1749 | statements in arrays or hashes. If absent, the default logic is "or" |
1750 | for arrays, and "and" for hashes. This means that a WHERE |
32eab2da |
1751 | array of the form: |
1752 | |
1753 | @where = ( |
9d48860e |
1754 | event_date => {'>=', '2/13/99'}, |
1755 | event_date => {'<=', '4/24/03'}, |
32eab2da |
1756 | ); |
1757 | |
7cac25e6 |
1758 | will generate SQL like this: |
32eab2da |
1759 | |
1760 | WHERE event_date >= '2/13/99' OR event_date <= '4/24/03' |
1761 | |
1762 | This is probably not what you want given this query, though (look |
1763 | at the dates). To change the "OR" to an "AND", simply specify: |
1764 | |
1765 | my $sql = SQL::Abstract->new(logic => 'and'); |
1766 | |
1767 | Which will change the above C<WHERE> to: |
1768 | |
1769 | WHERE event_date >= '2/13/99' AND event_date <= '4/24/03' |
1770 | |
96449e8e |
1771 | The logic can also be changed locally by inserting |
be21dde3 |
1772 | a modifier in front of an arrayref: |
96449e8e |
1773 | |
9d48860e |
1774 | @where = (-and => [event_date => {'>=', '2/13/99'}, |
7cac25e6 |
1775 | event_date => {'<=', '4/24/03'} ]); |
96449e8e |
1776 | |
1777 | See the L</"WHERE CLAUSES"> section for explanations. |
1778 | |
32eab2da |
1779 | =item convert |
1780 | |
1781 | This will automatically convert comparisons using the specified SQL |
1782 | function for both column and value. This is mostly used with an argument |
1783 | of C<upper> or C<lower>, so that the SQL will have the effect of |
1784 | case-insensitive "searches". For example, this: |
1785 | |
1786 | $sql = SQL::Abstract->new(convert => 'upper'); |
1787 | %where = (keywords => 'MaKe iT CAse inSeNSItive'); |
1788 | |
1789 | Will turn out the following SQL: |
1790 | |
1791 | WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive') |
1792 | |
1793 | The conversion can be C<upper()>, C<lower()>, or any other SQL function |
1794 | that can be applied symmetrically to fields (actually B<SQL::Abstract> does |
1795 | not validate this option; it will just pass through what you specify verbatim). |
1796 | |
1797 | =item bindtype |
1798 | |
1799 | This is a kludge because many databases suck. For example, you can't |
1800 | just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields. |
1801 | Instead, you have to use C<bind_param()>: |
1802 | |
1803 | $sth->bind_param(1, 'reg data'); |
1804 | $sth->bind_param(2, $lots, {ora_type => ORA_CLOB}); |
1805 | |
1806 | The problem is, B<SQL::Abstract> will normally just return a C<@bind> array, |
1807 | which loses track of which field each slot refers to. Fear not. |
1808 | |
1809 | If you specify C<bindtype> in new, you can determine how C<@bind> is returned. |
1810 | Currently, you can specify either C<normal> (default) or C<columns>. If you |
1811 | specify C<columns>, you will get an array that looks like this: |
1812 | |
1813 | my $sql = SQL::Abstract->new(bindtype => 'columns'); |
1814 | my($stmt, @bind) = $sql->insert(...); |
1815 | |
1816 | @bind = ( |
1817 | [ 'column1', 'value1' ], |
1818 | [ 'column2', 'value2' ], |
1819 | [ 'column3', 'value3' ], |
1820 | ); |
1821 | |
1822 | You can then iterate through this manually, using DBI's C<bind_param()>. |
e3f9dff4 |
1823 | |
32eab2da |
1824 | $sth->prepare($stmt); |
1825 | my $i = 1; |
1826 | for (@bind) { |
1827 | my($col, $data) = @$_; |
1828 | if ($col eq 'details' || $col eq 'comments') { |
1829 | $sth->bind_param($i, $data, {ora_type => ORA_CLOB}); |
1830 | } elsif ($col eq 'image') { |
1831 | $sth->bind_param($i, $data, {ora_type => ORA_BLOB}); |
1832 | } else { |
1833 | $sth->bind_param($i, $data); |
1834 | } |
1835 | $i++; |
1836 | } |
1837 | $sth->execute; # execute without @bind now |
1838 | |
1839 | Now, why would you still use B<SQL::Abstract> if you have to do this crap? |
1840 | Basically, the advantage is still that you don't have to care which fields |
1841 | are or are not included. You could wrap that above C<for> loop in a simple |
1842 | sub called C<bind_fields()> or something and reuse it repeatedly. You still |
1843 | get a layer of abstraction over manual SQL specification. |
1844 | |
3ae1c5e2 |
1845 | Note that if you set L</bindtype> to C<columns>, the C<\[ $sql, @bind ]> |
deb148a2 |
1846 | construct (see L</Literal SQL with placeholders and bind values (subqueries)>) |
1847 | will expect the bind values in this format. |
1848 | |
32eab2da |
1849 | =item quote_char |
1850 | |
1851 | This is the character that a table or column name will be quoted |
9d48860e |
1852 | with. By default this is an empty string, but you could set it to |
32eab2da |
1853 | the character C<`>, to generate SQL like this: |
1854 | |
1855 | SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%' |
1856 | |
96449e8e |
1857 | Alternatively, you can supply an array ref of two items, the first being the left |
1858 | hand quote character, and the second the right hand quote character. For |
1859 | example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes |
1860 | that generates SQL like this: |
1861 | |
1862 | SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%' |
1863 | |
9d48860e |
1864 | Quoting is useful if you have tables or columns names that are reserved |
96449e8e |
1865 | words in your database's SQL dialect. |
32eab2da |
1866 | |
46be4313 |
1867 | =item escape_char |
1868 | |
1869 | This is the character that will be used to escape L</quote_char>s appearing |
1870 | in an identifier before it has been quoted. |
1871 | |
80790166 |
1872 | The parameter default in case of a single L</quote_char> character is the quote |
46be4313 |
1873 | character itself. |
1874 | |
1875 | When opening-closing-style quoting is used (L</quote_char> is an arrayref) |
9de2bd86 |
1876 | this parameter defaults to the B<closing (right)> L</quote_char>. Occurrences |
46be4313 |
1877 | of the B<opening (left)> L</quote_char> within the identifier are currently left |
1878 | untouched. The default for opening-closing-style quotes may change in future |
1879 | versions, thus you are B<strongly encouraged> to specify the escape character |
1880 | explicitly. |
1881 | |
32eab2da |
1882 | =item name_sep |
1883 | |
1884 | This is the character that separates a table and column name. It is |
1885 | necessary to specify this when the C<quote_char> option is selected, |
1886 | so that tables and column names can be individually quoted like this: |
1887 | |
1888 | SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1 |
1889 | |
b6251592 |
1890 | =item injection_guard |
1891 | |
1892 | A regular expression C<qr/.../> that is applied to any C<-function> and unquoted |
1893 | column name specified in a query structure. This is a safety mechanism to avoid |
1894 | injection attacks when mishandling user input e.g.: |
1895 | |
1896 | my %condition_as_column_value_pairs = get_values_from_user(); |
1897 | $sqla->select( ... , \%condition_as_column_value_pairs ); |
1898 | |
1899 | If the expression matches an exception is thrown. Note that literal SQL |
1900 | supplied via C<\'...'> or C<\['...']> is B<not> checked in any way. |
1901 | |
1902 | Defaults to checking for C<;> and the C<GO> keyword (TransactSQL) |
1903 | |
96449e8e |
1904 | =item array_datatypes |
32eab2da |
1905 | |
9d48860e |
1906 | When this option is true, arrayrefs in INSERT or UPDATE are |
1907 | interpreted as array datatypes and are passed directly |
96449e8e |
1908 | to the DBI layer. |
1909 | When this option is false, arrayrefs are interpreted |
1910 | as literal SQL, just like refs to arrayrefs |
1911 | (but this behavior is for backwards compatibility; when writing |
1912 | new queries, use the "reference to arrayref" syntax |
1913 | for literal SQL). |
32eab2da |
1914 | |
32eab2da |
1915 | |
96449e8e |
1916 | =item special_ops |
32eab2da |
1917 | |
9d48860e |
1918 | Takes a reference to a list of "special operators" |
96449e8e |
1919 | to extend the syntax understood by L<SQL::Abstract>. |
1920 | See section L</"SPECIAL OPERATORS"> for details. |
32eab2da |
1921 | |
59f23b3d |
1922 | =item unary_ops |
1923 | |
9d48860e |
1924 | Takes a reference to a list of "unary operators" |
59f23b3d |
1925 | to extend the syntax understood by L<SQL::Abstract>. |
1926 | See section L</"UNARY OPERATORS"> for details. |
1927 | |
32eab2da |
1928 | |
32eab2da |
1929 | |
96449e8e |
1930 | =back |
32eab2da |
1931 | |
02288357 |
1932 | =head2 insert($table, \@values || \%fieldvals, \%options) |
32eab2da |
1933 | |
1934 | This is the simplest function. You simply give it a table name |
1935 | and either an arrayref of values or hashref of field/value pairs. |
1936 | It returns an SQL INSERT statement and a list of bind values. |
96449e8e |
1937 | See the sections on L</"Inserting and Updating Arrays"> and |
1938 | L</"Inserting and Updating SQL"> for information on how to insert |
1939 | with those data types. |
32eab2da |
1940 | |
02288357 |
1941 | The optional C<\%options> hash reference may contain additional |
1942 | options to generate the insert SQL. Currently supported options |
1943 | are: |
1944 | |
1945 | =over 4 |
1946 | |
1947 | =item returning |
1948 | |
1949 | Takes either a scalar of raw SQL fields, or an array reference of |
1950 | field names, and adds on an SQL C<RETURNING> statement at the end. |
1951 | This allows you to return data generated by the insert statement |
1952 | (such as row IDs) without performing another C<SELECT> statement. |
1953 | Note, however, this is not part of the SQL standard and may not |
1954 | be supported by all database engines. |
1955 | |
1956 | =back |
1957 | |
95904db5 |
1958 | =head2 update($table, \%fieldvals, \%where, \%options) |
32eab2da |
1959 | |
1960 | This takes a table, hashref of field/value pairs, and an optional |
86298391 |
1961 | hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list |
32eab2da |
1962 | of bind values. |
96449e8e |
1963 | See the sections on L</"Inserting and Updating Arrays"> and |
1964 | L</"Inserting and Updating SQL"> for information on how to insert |
1965 | with those data types. |
32eab2da |
1966 | |
95904db5 |
1967 | The optional C<\%options> hash reference may contain additional |
1968 | options to generate the update SQL. Currently supported options |
1969 | are: |
1970 | |
1971 | =over 4 |
1972 | |
1973 | =item returning |
1974 | |
1975 | See the C<returning> option to |
1976 | L<insert|/insert($table, \@values || \%fieldvals, \%options)>. |
1977 | |
1978 | =back |
1979 | |
96449e8e |
1980 | =head2 select($source, $fields, $where, $order) |
32eab2da |
1981 | |
9d48860e |
1982 | This returns a SQL SELECT statement and associated list of bind values, as |
be21dde3 |
1983 | specified by the arguments: |
32eab2da |
1984 | |
96449e8e |
1985 | =over |
32eab2da |
1986 | |
96449e8e |
1987 | =item $source |
32eab2da |
1988 | |
9d48860e |
1989 | Specification of the 'FROM' part of the statement. |
96449e8e |
1990 | The argument can be either a plain scalar (interpreted as a table |
1991 | name, will be quoted), or an arrayref (interpreted as a list |
1992 | of table names, joined by commas, quoted), or a scalarref |
063097a3 |
1993 | (literal SQL, not quoted). |
32eab2da |
1994 | |
96449e8e |
1995 | =item $fields |
32eab2da |
1996 | |
9d48860e |
1997 | Specification of the list of fields to retrieve from |
96449e8e |
1998 | the source. |
1999 | The argument can be either an arrayref (interpreted as a list |
9d48860e |
2000 | of field names, will be joined by commas and quoted), or a |
96449e8e |
2001 | plain scalar (literal SQL, not quoted). |
521647e7 |
2002 | Please observe that this API is not as flexible as that of |
2003 | the first argument C<$source>, for backwards compatibility reasons. |
32eab2da |
2004 | |
96449e8e |
2005 | =item $where |
32eab2da |
2006 | |
96449e8e |
2007 | Optional argument to specify the WHERE part of the query. |
2008 | The argument is most often a hashref, but can also be |
9d48860e |
2009 | an arrayref or plain scalar -- |
96449e8e |
2010 | see section L<WHERE clause|/"WHERE CLAUSES"> for details. |
32eab2da |
2011 | |
96449e8e |
2012 | =item $order |
32eab2da |
2013 | |
96449e8e |
2014 | Optional argument to specify the ORDER BY part of the query. |
9d48860e |
2015 | The argument can be a scalar, a hashref or an arrayref |
96449e8e |
2016 | -- see section L<ORDER BY clause|/"ORDER BY CLAUSES"> |
2017 | for details. |
32eab2da |
2018 | |
96449e8e |
2019 | =back |
32eab2da |
2020 | |
32eab2da |
2021 | |
85327cd5 |
2022 | =head2 delete($table, \%where, \%options) |
32eab2da |
2023 | |
86298391 |
2024 | This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>. |
32eab2da |
2025 | It returns an SQL DELETE statement and list of bind values. |
2026 | |
85327cd5 |
2027 | The optional C<\%options> hash reference may contain additional |
2028 | options to generate the delete SQL. Currently supported options |
2029 | are: |
2030 | |
2031 | =over 4 |
2032 | |
2033 | =item returning |
2034 | |
2035 | See the C<returning> option to |
2036 | L<insert|/insert($table, \@values || \%fieldvals, \%options)>. |
2037 | |
2038 | =back |
2039 | |
85783f3c |
2040 | =head2 where(\%where, $order) |
32eab2da |
2041 | |
2042 | This is used to generate just the WHERE clause. For example, |
2043 | if you have an arbitrary data structure and know what the |
2044 | rest of your SQL is going to look like, but want an easy way |
2045 | to produce a WHERE clause, use this. It returns an SQL WHERE |
2046 | clause and list of bind values. |
2047 | |
32eab2da |
2048 | |
2049 | =head2 values(\%data) |
2050 | |
2051 | This just returns the values from the hash C<%data>, in the same |
2052 | order that would be returned from any of the other above queries. |
2053 | Using this allows you to markedly speed up your queries if you |
2054 | are affecting lots of rows. See below under the L</"PERFORMANCE"> section. |
2055 | |
32eab2da |
2056 | =head2 generate($any, 'number', $of, \@data, $struct, \%types) |
2057 | |
2058 | Warning: This is an experimental method and subject to change. |
2059 | |
2060 | This returns arbitrarily generated SQL. It's a really basic shortcut. |
2061 | It will return two different things, depending on return context: |
2062 | |
2063 | my($stmt, @bind) = $sql->generate('create table', \$table, \@fields); |
2064 | my $stmt_and_val = $sql->generate('create table', \$table, \@fields); |
2065 | |
2066 | These would return the following: |
2067 | |
2068 | # First calling form |
2069 | $stmt = "CREATE TABLE test (?, ?)"; |
2070 | @bind = (field1, field2); |
2071 | |
2072 | # Second calling form |
2073 | $stmt_and_val = "CREATE TABLE test (field1, field2)"; |
2074 | |
2075 | Depending on what you're trying to do, it's up to you to choose the correct |
2076 | format. In this example, the second form is what you would want. |
2077 | |
2078 | By the same token: |
2079 | |
2080 | $sql->generate('alter session', { nls_date_format => 'MM/YY' }); |
2081 | |
2082 | Might give you: |
2083 | |
2084 | ALTER SESSION SET nls_date_format = 'MM/YY' |
2085 | |
2086 | You get the idea. Strings get their case twiddled, but everything |
2087 | else remains verbatim. |
2088 | |
0da0fe34 |
2089 | =head1 EXPORTABLE FUNCTIONS |
2090 | |
2091 | =head2 is_plain_value |
2092 | |
2093 | Determines if the supplied argument is a plain value as understood by this |
2094 | module: |
2095 | |
2096 | =over |
2097 | |
2098 | =item * The value is C<undef> |
2099 | |
2100 | =item * The value is a non-reference |
2101 | |
2102 | =item * The value is an object with stringification overloading |
2103 | |
2104 | =item * The value is of the form C<< { -value => $anything } >> |
2105 | |
2106 | =back |
2107 | |
9de2bd86 |
2108 | On failure returns C<undef>, on success returns a B<scalar> reference |
966200cc |
2109 | to the original supplied argument. |
0da0fe34 |
2110 | |
843a94b5 |
2111 | =over |
2112 | |
2113 | =item * Note |
2114 | |
2115 | The stringification overloading detection is rather advanced: it takes |
2116 | into consideration not only the presence of a C<""> overload, but if that |
2117 | fails also checks for enabled |
2118 | L<autogenerated versions of C<"">|overload/Magic Autogeneration>, based |
2119 | on either C<0+> or C<bool>. |
2120 | |
2121 | Unfortunately testing in the field indicates that this |
2122 | detection B<< may tickle a latent bug in perl versions before 5.018 >>, |
2123 | but only when very large numbers of stringifying objects are involved. |
2124 | At the time of writing ( Sep 2014 ) there is no clear explanation of |
2125 | the direct cause, nor is there a manageably small test case that reliably |
2126 | reproduces the problem. |
2127 | |
2128 | If you encounter any of the following exceptions in B<random places within |
2129 | your application stack> - this module may be to blame: |
2130 | |
2131 | Operation "ne": no method found, |
2132 | left argument in overloaded package <something>, |
2133 | right argument in overloaded package <something> |
2134 | |
2135 | or perhaps even |
2136 | |
2137 | Stub found while resolving method "???" overloading """" in package <something> |
2138 | |
2139 | If you fall victim to the above - please attempt to reduce the problem |
2140 | to something that could be sent to the L<SQL::Abstract developers |
1f490ae4 |
2141 | |DBIx::Class/GETTING HELP/SUPPORT> |
843a94b5 |
2142 | (either publicly or privately). As a workaround in the meantime you can |
2143 | set C<$ENV{SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION}> to a true |
2144 | value, which will most likely eliminate your problem (at the expense of |
2145 | not being able to properly detect exotic forms of stringification). |
2146 | |
2147 | This notice and environment variable will be removed in a future version, |
2148 | as soon as the underlying problem is found and a reliable workaround is |
2149 | devised. |
2150 | |
2151 | =back |
2152 | |
0da0fe34 |
2153 | =head2 is_literal_value |
2154 | |
2155 | Determines if the supplied argument is a literal value as understood by this |
2156 | module: |
2157 | |
2158 | =over |
2159 | |
2160 | =item * C<\$sql_string> |
2161 | |
2162 | =item * C<\[ $sql_string, @bind_values ]> |
2163 | |
0da0fe34 |
2164 | =back |
2165 | |
9de2bd86 |
2166 | On failure returns C<undef>, on success returns an B<array> reference |
966200cc |
2167 | containing the unpacked version of the supplied literal SQL and bind values. |
0da0fe34 |
2168 | |
32eab2da |
2169 | =head1 WHERE CLAUSES |
2170 | |
96449e8e |
2171 | =head2 Introduction |
2172 | |
32eab2da |
2173 | This module uses a variation on the idea from L<DBIx::Abstract>. It |
2174 | is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this |
2175 | module is that things in arrays are OR'ed, and things in hashes |
2176 | are AND'ed.> |
2177 | |
2178 | The easiest way to explain is to show lots of examples. After |
2179 | each C<%where> hash shown, it is assumed you used: |
2180 | |
2181 | my($stmt, @bind) = $sql->where(\%where); |
2182 | |
2183 | However, note that the C<%where> hash can be used directly in any |
2184 | of the other functions as well, as described above. |
2185 | |
96449e8e |
2186 | =head2 Key-value pairs |
2187 | |
32eab2da |
2188 | So, let's get started. To begin, a simple hash: |
2189 | |
2190 | my %where = ( |
2191 | user => 'nwiger', |
2192 | status => 'completed' |
2193 | ); |
2194 | |
2195 | Is converted to SQL C<key = val> statements: |
2196 | |
2197 | $stmt = "WHERE user = ? AND status = ?"; |
2198 | @bind = ('nwiger', 'completed'); |
2199 | |
2200 | One common thing I end up doing is having a list of values that |
2201 | a field can be in. To do this, simply specify a list inside of |
2202 | an arrayref: |
2203 | |
2204 | my %where = ( |
2205 | user => 'nwiger', |
2206 | status => ['assigned', 'in-progress', 'pending']; |
2207 | ); |
2208 | |
2209 | This simple code will create the following: |
9d48860e |
2210 | |
32eab2da |
2211 | $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )"; |
2212 | @bind = ('nwiger', 'assigned', 'in-progress', 'pending'); |
2213 | |
9d48860e |
2214 | A field associated to an empty arrayref will be considered a |
7cac25e6 |
2215 | logical false and will generate 0=1. |
8a68b5be |
2216 | |
b864ba9b |
2217 | =head2 Tests for NULL values |
2218 | |
2219 | If the value part is C<undef> then this is converted to SQL <IS NULL> |
2220 | |
2221 | my %where = ( |
2222 | user => 'nwiger', |
2223 | status => undef, |
2224 | ); |
2225 | |
2226 | becomes: |
2227 | |
2228 | $stmt = "WHERE user = ? AND status IS NULL"; |
2229 | @bind = ('nwiger'); |
2230 | |
e9614080 |
2231 | To test if a column IS NOT NULL: |
2232 | |
2233 | my %where = ( |
2234 | user => 'nwiger', |
2235 | status => { '!=', undef }, |
2236 | ); |
cc422895 |
2237 | |
6e0c6552 |
2238 | =head2 Specific comparison operators |
96449e8e |
2239 | |
32eab2da |
2240 | If you want to specify a different type of operator for your comparison, |
2241 | you can use a hashref for a given column: |
2242 | |
2243 | my %where = ( |
2244 | user => 'nwiger', |
2245 | status => { '!=', 'completed' } |
2246 | ); |
2247 | |
2248 | Which would generate: |
2249 | |
2250 | $stmt = "WHERE user = ? AND status != ?"; |
2251 | @bind = ('nwiger', 'completed'); |
2252 | |
2253 | To test against multiple values, just enclose the values in an arrayref: |
2254 | |
96449e8e |
2255 | status => { '=', ['assigned', 'in-progress', 'pending'] }; |
2256 | |
f2d5020d |
2257 | Which would give you: |
96449e8e |
2258 | |
2259 | "WHERE status = ? OR status = ? OR status = ?" |
2260 | |
2261 | |
2262 | The hashref can also contain multiple pairs, in which case it is expanded |
32eab2da |
2263 | into an C<AND> of its elements: |
2264 | |
2265 | my %where = ( |
2266 | user => 'nwiger', |
2267 | status => { '!=', 'completed', -not_like => 'pending%' } |
2268 | ); |
2269 | |
2270 | # Or more dynamically, like from a form |
2271 | $where{user} = 'nwiger'; |
2272 | $where{status}{'!='} = 'completed'; |
2273 | $where{status}{'-not_like'} = 'pending%'; |
2274 | |
2275 | # Both generate this |
2276 | $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?"; |
2277 | @bind = ('nwiger', 'completed', 'pending%'); |
2278 | |
96449e8e |
2279 | |
32eab2da |
2280 | To get an OR instead, you can combine it with the arrayref idea: |
2281 | |
2282 | my %where => ( |
2283 | user => 'nwiger', |
1a6f2a03 |
2284 | priority => [ { '=', 2 }, { '>', 5 } ] |
32eab2da |
2285 | ); |
2286 | |
2287 | Which would generate: |
2288 | |
1a6f2a03 |
2289 | $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?"; |
2290 | @bind = ('2', '5', 'nwiger'); |
32eab2da |
2291 | |
44b9e502 |
2292 | If you want to include literal SQL (with or without bind values), just use a |
13cc86af |
2293 | scalar reference or reference to an arrayref as the value: |
44b9e502 |
2294 | |
2295 | my %where = ( |
2296 | date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] }, |
2297 | date_expires => { '<' => \"now()" } |
2298 | ); |
2299 | |
2300 | Which would generate: |
2301 | |
13cc86af |
2302 | $stmt = "WHERE date_entered > to_date(?, 'MM/DD/YYYY') AND date_expires < now()"; |
44b9e502 |
2303 | @bind = ('11/26/2008'); |
2304 | |
96449e8e |
2305 | |
2306 | =head2 Logic and nesting operators |
2307 | |
2308 | In the example above, |
2309 | there is a subtle trap if you want to say something like |
32eab2da |
2310 | this (notice the C<AND>): |
2311 | |
2312 | WHERE priority != ? AND priority != ? |
2313 | |
2314 | Because, in Perl you I<can't> do this: |
2315 | |
13cc86af |
2316 | priority => { '!=' => 2, '!=' => 1 } |
32eab2da |
2317 | |
2318 | As the second C<!=> key will obliterate the first. The solution |
2319 | is to use the special C<-modifier> form inside an arrayref: |
2320 | |
9d48860e |
2321 | priority => [ -and => {'!=', 2}, |
96449e8e |
2322 | {'!=', 1} ] |
2323 | |
32eab2da |
2324 | |
2325 | Normally, these would be joined by C<OR>, but the modifier tells it |
2326 | to use C<AND> instead. (Hint: You can use this in conjunction with the |
2327 | C<logic> option to C<new()> in order to change the way your queries |
2328 | work by default.) B<Important:> Note that the C<-modifier> goes |
2329 | B<INSIDE> the arrayref, as an extra first element. This will |
2330 | B<NOT> do what you think it might: |
2331 | |
2332 | priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG! |
2333 | |
2334 | Here is a quick list of equivalencies, since there is some overlap: |
2335 | |
2336 | # Same |
2337 | status => {'!=', 'completed', 'not like', 'pending%' } |
2338 | status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}] |
2339 | |
2340 | # Same |
2341 | status => {'=', ['assigned', 'in-progress']} |
2342 | status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}] |
2343 | status => [ {'=', 'assigned'}, {'=', 'in-progress'} ] |
2344 | |
e3f9dff4 |
2345 | |
2346 | |
be21dde3 |
2347 | =head2 Special operators: IN, BETWEEN, etc. |
96449e8e |
2348 | |
32eab2da |
2349 | You can also use the hashref format to compare a list of fields using the |
2350 | C<IN> comparison operator, by specifying the list as an arrayref: |
2351 | |
2352 | my %where = ( |
2353 | status => 'completed', |
2354 | reportid => { -in => [567, 2335, 2] } |
2355 | ); |
2356 | |
2357 | Which would generate: |
2358 | |
2359 | $stmt = "WHERE status = ? AND reportid IN (?,?,?)"; |
2360 | @bind = ('completed', '567', '2335', '2'); |
2361 | |
9d48860e |
2362 | The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in |
96449e8e |
2363 | the same way. |
2364 | |
6e0c6552 |
2365 | If the argument to C<-in> is an empty array, 'sqlfalse' is generated |
be21dde3 |
2366 | (by default: C<1=0>). Similarly, C<< -not_in => [] >> generates |
2367 | 'sqltrue' (by default: C<1=1>). |
6e0c6552 |
2368 | |
e41c3bdd |
2369 | In addition to the array you can supply a chunk of literal sql or |
2370 | literal sql with bind: |
6e0c6552 |
2371 | |
e41c3bdd |
2372 | my %where = { |
2373 | customer => { -in => \[ |
2374 | 'SELECT cust_id FROM cust WHERE balance > ?', |
2375 | 2000, |
2376 | ], |
2377 | status => { -in => \'SELECT status_codes FROM states' }, |
2378 | }; |
6e0c6552 |
2379 | |
e41c3bdd |
2380 | would generate: |
2381 | |
2382 | $stmt = "WHERE ( |
2383 | customer IN ( SELECT cust_id FROM cust WHERE balance > ? ) |
2384 | AND status IN ( SELECT status_codes FROM states ) |
2385 | )"; |
2386 | @bind = ('2000'); |
2387 | |
0dfd2442 |
2388 | Finally, if the argument to C<-in> is not a reference, it will be |
2389 | treated as a single-element array. |
e41c3bdd |
2390 | |
2391 | Another pair of operators is C<-between> and C<-not_between>, |
96449e8e |
2392 | used with an arrayref of two values: |
32eab2da |
2393 | |
2394 | my %where = ( |
2395 | user => 'nwiger', |
2396 | completion_date => { |
2397 | -not_between => ['2002-10-01', '2003-02-06'] |
2398 | } |
2399 | ); |
2400 | |
2401 | Would give you: |
2402 | |
2403 | WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? ) |
2404 | |
e41c3bdd |
2405 | Just like with C<-in> all plausible combinations of literal SQL |
2406 | are possible: |
2407 | |
2408 | my %where = { |
2409 | start0 => { -between => [ 1, 2 ] }, |
2410 | start1 => { -between => \["? AND ?", 1, 2] }, |
2411 | start2 => { -between => \"lower(x) AND upper(y)" }, |
9d48860e |
2412 | start3 => { -between => [ |
e41c3bdd |
2413 | \"lower(x)", |
2414 | \["upper(?)", 'stuff' ], |
2415 | ] }, |
2416 | }; |
2417 | |
2418 | Would give you: |
2419 | |
2420 | $stmt = "WHERE ( |
2421 | ( start0 BETWEEN ? AND ? ) |
2422 | AND ( start1 BETWEEN ? AND ? ) |
2423 | AND ( start2 BETWEEN lower(x) AND upper(y) ) |
2424 | AND ( start3 BETWEEN lower(x) AND upper(?) ) |
2425 | )"; |
2426 | @bind = (1, 2, 1, 2, 'stuff'); |
2427 | |
2428 | |
9d48860e |
2429 | These are the two builtin "special operators"; but the |
be21dde3 |
2430 | list can be expanded: see section L</"SPECIAL OPERATORS"> below. |
96449e8e |
2431 | |
59f23b3d |
2432 | =head2 Unary operators: bool |
97a920ef |
2433 | |
2434 | If you wish to test against boolean columns or functions within your |
2435 | database you can use the C<-bool> and C<-not_bool> operators. For |
2436 | example to test the column C<is_user> being true and the column |
827bb0eb |
2437 | C<is_enabled> being false you would use:- |
97a920ef |
2438 | |
2439 | my %where = ( |
2440 | -bool => 'is_user', |
2441 | -not_bool => 'is_enabled', |
2442 | ); |
2443 | |
2444 | Would give you: |
2445 | |
277b5d3f |
2446 | WHERE is_user AND NOT is_enabled |
97a920ef |
2447 | |
0b604e9d |
2448 | If a more complex combination is required, testing more conditions, |
2449 | then you should use the and/or operators:- |
2450 | |
2451 | my %where = ( |
2452 | -and => [ |
2453 | -bool => 'one', |
23401b81 |
2454 | -not_bool => { two=> { -rlike => 'bar' } }, |
2455 | -not_bool => { three => [ { '=', 2 }, { '>', 5 } ] }, |
0b604e9d |
2456 | ], |
2457 | ); |
2458 | |
2459 | Would give you: |
2460 | |
23401b81 |
2461 | WHERE |
2462 | one |
2463 | AND |
2464 | (NOT two RLIKE ?) |
2465 | AND |
2466 | (NOT ( three = ? OR three > ? )) |
97a920ef |
2467 | |
2468 | |
107b72f1 |
2469 | =head2 Nested conditions, -and/-or prefixes |
96449e8e |
2470 | |
32eab2da |
2471 | So far, we've seen how multiple conditions are joined with a top-level |
2472 | C<AND>. We can change this by putting the different conditions we want in |
2473 | hashes and then putting those hashes in an array. For example: |
2474 | |
2475 | my @where = ( |
2476 | { |
2477 | user => 'nwiger', |
2478 | status => { -like => ['pending%', 'dispatched'] }, |
2479 | }, |
2480 | { |
2481 | user => 'robot', |
2482 | status => 'unassigned', |
2483 | } |
2484 | ); |
2485 | |
2486 | This data structure would create the following: |
2487 | |
2488 | $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) ) |
2489 | OR ( user = ? AND status = ? ) )"; |
2490 | @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned'); |
2491 | |
107b72f1 |
2492 | |
48d9f5f8 |
2493 | Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or> |
be21dde3 |
2494 | to change the logic inside: |
32eab2da |
2495 | |
2496 | my @where = ( |
2497 | -and => [ |
2498 | user => 'nwiger', |
48d9f5f8 |
2499 | [ |
2500 | -and => [ workhrs => {'>', 20}, geo => 'ASIA' ], |
2501 | -or => { workhrs => {'<', 50}, geo => 'EURO' }, |
32eab2da |
2502 | ], |
2503 | ], |
2504 | ); |
2505 | |
2506 | That would yield: |
2507 | |
13cc86af |
2508 | $stmt = "WHERE ( user = ? |
2509 | AND ( ( workhrs > ? AND geo = ? ) |
2510 | OR ( workhrs < ? OR geo = ? ) ) )"; |
2511 | @bind = ('nwiger', '20', 'ASIA', '50', 'EURO'); |
107b72f1 |
2512 | |
cc422895 |
2513 | =head3 Algebraic inconsistency, for historical reasons |
107b72f1 |
2514 | |
7cac25e6 |
2515 | C<Important note>: when connecting several conditions, the C<-and->|C<-or> |
2516 | operator goes C<outside> of the nested structure; whereas when connecting |
2517 | several constraints on one column, the C<-and> operator goes |
be21dde3 |
2518 | C<inside> the arrayref. Here is an example combining both features: |
7cac25e6 |
2519 | |
2520 | my @where = ( |
2521 | -and => [a => 1, b => 2], |
2522 | -or => [c => 3, d => 4], |
2523 | e => [-and => {-like => 'foo%'}, {-like => '%bar'} ] |
2524 | ) |
2525 | |
2526 | yielding |
2527 | |
9d48860e |
2528 | WHERE ( ( ( a = ? AND b = ? ) |
2529 | OR ( c = ? OR d = ? ) |
7cac25e6 |
2530 | OR ( e LIKE ? AND e LIKE ? ) ) ) |
2531 | |
107b72f1 |
2532 | This difference in syntax is unfortunate but must be preserved for |
be21dde3 |
2533 | historical reasons. So be careful: the two examples below would |
107b72f1 |
2534 | seem algebraically equivalent, but they are not |
2535 | |
a948b1fe |
2536 | { col => [ -and => |
2537 | { -like => 'foo%' }, |
2538 | { -like => '%bar' }, |
2539 | ] } |
be21dde3 |
2540 | # yields: WHERE ( ( col LIKE ? AND col LIKE ? ) ) |
107b72f1 |
2541 | |
a948b1fe |
2542 | [ -and => |
2543 | { col => { -like => 'foo%' } }, |
2544 | { col => { -like => '%bar' } }, |
2545 | ] |
be21dde3 |
2546 | # yields: WHERE ( ( col LIKE ? OR col LIKE ? ) ) |
107b72f1 |
2547 | |
7cac25e6 |
2548 | |
cc422895 |
2549 | =head2 Literal SQL and value type operators |
96449e8e |
2550 | |
cc422895 |
2551 | The basic premise of SQL::Abstract is that in WHERE specifications the "left |
2552 | side" is a column name and the "right side" is a value (normally rendered as |
2553 | a placeholder). This holds true for both hashrefs and arrayref pairs as you |
2554 | see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to |
2555 | alter this behavior. There are several ways of doing so. |
e9614080 |
2556 | |
cc422895 |
2557 | =head3 -ident |
2558 | |
2559 | This is a virtual operator that signals the string to its right side is an |
2560 | identifier (a column name) and not a value. For example to compare two |
2561 | columns you would write: |
32eab2da |
2562 | |
e9614080 |
2563 | my %where = ( |
2564 | priority => { '<', 2 }, |
cc422895 |
2565 | requestor => { -ident => 'submitter' }, |
e9614080 |
2566 | ); |
2567 | |
2568 | which creates: |
2569 | |
2570 | $stmt = "WHERE priority < ? AND requestor = submitter"; |
2571 | @bind = ('2'); |
2572 | |
cc422895 |
2573 | If you are maintaining legacy code you may see a different construct as |
2574 | described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new |
2575 | code. |
2576 | |
2577 | =head3 -value |
e9614080 |
2578 | |
cc422895 |
2579 | This is a virtual operator that signals that the construct to its right side |
2580 | is a value to be passed to DBI. This is for example necessary when you want |
2581 | to write a where clause against an array (for RDBMS that support such |
2582 | datatypes). For example: |
e9614080 |
2583 | |
32eab2da |
2584 | my %where = ( |
cc422895 |
2585 | array => { -value => [1, 2, 3] } |
32eab2da |
2586 | ); |
2587 | |
cc422895 |
2588 | will result in: |
32eab2da |
2589 | |
cc422895 |
2590 | $stmt = 'WHERE array = ?'; |
2591 | @bind = ([1, 2, 3]); |
32eab2da |
2592 | |
cc422895 |
2593 | Note that if you were to simply say: |
32eab2da |
2594 | |
2595 | my %where = ( |
cc422895 |
2596 | array => [1, 2, 3] |
32eab2da |
2597 | ); |
2598 | |
3af02ccb |
2599 | the result would probably not be what you wanted: |
cc422895 |
2600 | |
2601 | $stmt = 'WHERE array = ? OR array = ? OR array = ?'; |
2602 | @bind = (1, 2, 3); |
2603 | |
2604 | =head3 Literal SQL |
96449e8e |
2605 | |
cc422895 |
2606 | Finally, sometimes only literal SQL will do. To include a random snippet |
2607 | of SQL verbatim, you specify it as a scalar reference. Consider this only |
2608 | as a last resort. Usually there is a better way. For example: |
96449e8e |
2609 | |
2610 | my %where = ( |
cc422895 |
2611 | priority => { '<', 2 }, |
2612 | requestor => { -in => \'(SELECT name FROM hitmen)' }, |
96449e8e |
2613 | ); |
2614 | |
cc422895 |
2615 | Would create: |
96449e8e |
2616 | |
cc422895 |
2617 | $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)" |
2618 | @bind = (2); |
2619 | |
2620 | Note that in this example, you only get one bind parameter back, since |
2621 | the verbatim SQL is passed as part of the statement. |
2622 | |
2623 | =head4 CAVEAT |
2624 | |
2625 | Never use untrusted input as a literal SQL argument - this is a massive |
2626 | security risk (there is no way to check literal snippets for SQL |
2627 | injections and other nastyness). If you need to deal with untrusted input |
2628 | use literal SQL with placeholders as described next. |
96449e8e |
2629 | |
cc422895 |
2630 | =head3 Literal SQL with placeholders and bind values (subqueries) |
96449e8e |
2631 | |
2632 | If the literal SQL to be inserted has placeholders and bind values, |
2633 | use a reference to an arrayref (yes this is a double reference -- |
2634 | not so common, but perfectly legal Perl). For example, to find a date |
2635 | in Postgres you can use something like this: |
2636 | |
2637 | my %where = ( |
3ae1c5e2 |
2638 | date_column => \[ "= date '2008-09-30' - ?::integer", 10 ] |
96449e8e |
2639 | ) |
2640 | |
2641 | This would create: |
2642 | |
d2a8fe1a |
2643 | $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )" |
96449e8e |
2644 | @bind = ('10'); |
2645 | |
deb148a2 |
2646 | Note that you must pass the bind values in the same format as they are returned |
85783f3c |
2647 | by L<where|/where(\%where, $order)>. This means that if you set L</bindtype> |
1f490ae4 |
2648 | to C<columns>, you must provide the bind values in the |
2649 | C<< [ column_meta => value ] >> format, where C<column_meta> is an opaque |
2650 | scalar value; most commonly the column name, but you can use any scalar value |
2651 | (including references and blessed references), L<SQL::Abstract> will simply |
2652 | pass it through intact. So if C<bindtype> is set to C<columns> the above |
2653 | example will look like: |
deb148a2 |
2654 | |
2655 | my %where = ( |
3ae1c5e2 |
2656 | date_column => \[ "= date '2008-09-30' - ?::integer", [ {} => 10 ] ] |
deb148a2 |
2657 | ) |
96449e8e |
2658 | |
2659 | Literal SQL is especially useful for nesting parenthesized clauses in the |
be21dde3 |
2660 | main SQL query. Here is a first example: |
96449e8e |
2661 | |
2662 | my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?", |
2663 | 100, "foo%"); |
2664 | my %where = ( |
2665 | foo => 1234, |
2666 | bar => \["IN ($sub_stmt)" => @sub_bind], |
2667 | ); |
2668 | |
be21dde3 |
2669 | This yields: |
96449e8e |
2670 | |
9d48860e |
2671 | $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1 |
96449e8e |
2672 | WHERE c2 < ? AND c3 LIKE ?))"; |
2673 | @bind = (1234, 100, "foo%"); |
2674 | |
9d48860e |
2675 | Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">, |
96449e8e |
2676 | are expressed in the same way. Of course the C<$sub_stmt> and |
9d48860e |
2677 | its associated bind values can be generated through a former call |
96449e8e |
2678 | to C<select()> : |
2679 | |
2680 | my ($sub_stmt, @sub_bind) |
9d48860e |
2681 | = $sql->select("t1", "c1", {c2 => {"<" => 100}, |
96449e8e |
2682 | c3 => {-like => "foo%"}}); |
2683 | my %where = ( |
2684 | foo => 1234, |
2685 | bar => \["> ALL ($sub_stmt)" => @sub_bind], |
2686 | ); |
2687 | |
2688 | In the examples above, the subquery was used as an operator on a column; |
9d48860e |
2689 | but the same principle also applies for a clause within the main C<%where> |
be21dde3 |
2690 | hash, like an EXISTS subquery: |
96449e8e |
2691 | |
9d48860e |
2692 | my ($sub_stmt, @sub_bind) |
96449e8e |
2693 | = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"}); |
48d9f5f8 |
2694 | my %where = ( -and => [ |
96449e8e |
2695 | foo => 1234, |
48d9f5f8 |
2696 | \["EXISTS ($sub_stmt)" => @sub_bind], |
2697 | ]); |
96449e8e |
2698 | |
2699 | which yields |
2700 | |
9d48860e |
2701 | $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1 |
96449e8e |
2702 | WHERE c1 = ? AND c2 > t0.c0))"; |
2703 | @bind = (1234, 1); |
2704 | |
2705 | |
9d48860e |
2706 | Observe that the condition on C<c2> in the subquery refers to |
be21dde3 |
2707 | column C<t0.c0> of the main query: this is I<not> a bind |
9d48860e |
2708 | value, so we have to express it through a scalar ref. |
96449e8e |
2709 | Writing C<< c2 => {">" => "t0.c0"} >> would have generated |
2710 | C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly |
2711 | what we wanted here. |
2712 | |
96449e8e |
2713 | Finally, here is an example where a subquery is used |
2714 | for expressing unary negation: |
2715 | |
9d48860e |
2716 | my ($sub_stmt, @sub_bind) |
96449e8e |
2717 | = $sql->where({age => [{"<" => 10}, {">" => 20}]}); |
2718 | $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause |
2719 | my %where = ( |
2720 | lname => {like => '%son%'}, |
48d9f5f8 |
2721 | \["NOT ($sub_stmt)" => @sub_bind], |
96449e8e |
2722 | ); |
2723 | |
2724 | This yields |
2725 | |
2726 | $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )" |
2727 | @bind = ('%son%', 10, 20) |
2728 | |
cc422895 |
2729 | =head3 Deprecated usage of Literal SQL |
2730 | |
2731 | Below are some examples of archaic use of literal SQL. It is shown only as |
2732 | reference for those who deal with legacy code. Each example has a much |
2733 | better, cleaner and safer alternative that users should opt for in new code. |
2734 | |
2735 | =over |
2736 | |
2737 | =item * |
2738 | |
2739 | my %where = ( requestor => \'IS NOT NULL' ) |
2740 | |
2741 | $stmt = "WHERE requestor IS NOT NULL" |
2742 | |
2743 | This used to be the way of generating NULL comparisons, before the handling |
2744 | of C<undef> got formalized. For new code please use the superior syntax as |
2745 | described in L</Tests for NULL values>. |
96449e8e |
2746 | |
cc422895 |
2747 | =item * |
2748 | |
2749 | my %where = ( requestor => \'= submitter' ) |
2750 | |
2751 | $stmt = "WHERE requestor = submitter" |
2752 | |
2753 | This used to be the only way to compare columns. Use the superior L</-ident> |
2754 | method for all new code. For example an identifier declared in such a way |
2755 | will be properly quoted if L</quote_char> is properly set, while the legacy |
2756 | form will remain as supplied. |
2757 | |
2758 | =item * |
2759 | |
2760 | my %where = ( is_ready => \"", completed => { '>', '2012-12-21' } ) |
2761 | |
2762 | $stmt = "WHERE completed > ? AND is_ready" |
2763 | @bind = ('2012-12-21') |
2764 | |
2765 | Using an empty string literal used to be the only way to express a boolean. |
2766 | For all new code please use the much more readable |
2767 | L<-bool|/Unary operators: bool> operator. |
2768 | |
2769 | =back |
96449e8e |
2770 | |
2771 | =head2 Conclusion |
2772 | |
32eab2da |
2773 | These pages could go on for a while, since the nesting of the data |
2774 | structures this module can handle are pretty much unlimited (the |
2775 | module implements the C<WHERE> expansion as a recursive function |
2776 | internally). Your best bet is to "play around" with the module a |
2777 | little to see how the data structures behave, and choose the best |
2778 | format for your data based on that. |
2779 | |
2780 | And of course, all the values above will probably be replaced with |
2781 | variables gotten from forms or the command line. After all, if you |
2782 | knew everything ahead of time, you wouldn't have to worry about |
2783 | dynamically-generating SQL and could just hardwire it into your |
2784 | script. |
2785 | |
86298391 |
2786 | =head1 ORDER BY CLAUSES |
2787 | |
9d48860e |
2788 | Some functions take an order by clause. This can either be a scalar (just a |
18710f60 |
2789 | column name), a hashref of C<< { -desc => 'col' } >> or C<< { -asc => 'col' } |
2790 | >>, a scalarref, an arrayref-ref, or an arrayref of any of the previous |
2791 | forms. Examples: |
1cfa1db3 |
2792 | |
8c15b421 |
2793 | Given | Will Generate |
18710f60 |
2794 | --------------------------------------------------------------- |
8c15b421 |
2795 | | |
2796 | 'colA' | ORDER BY colA |
2797 | | |
2798 | [qw/colA colB/] | ORDER BY colA, colB |
2799 | | |
2800 | {-asc => 'colA'} | ORDER BY colA ASC |
2801 | | |
2802 | {-desc => 'colB'} | ORDER BY colB DESC |
2803 | | |
2804 | ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC |
2805 | | |
2806 | { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC |
2807 | | |
2808 | \'colA DESC' | ORDER BY colA DESC |
2809 | | |
2810 | \[ 'FUNC(colA, ?)', $x ] | ORDER BY FUNC(colA, ?) |
2811 | | /* ...with $x bound to ? */ |
2812 | | |
bd805d85 |
2813 | [ | ORDER BY |
2814 | { -asc => 'colA' }, | colA ASC, |
2815 | { -desc => [qw/colB/] }, | colB DESC, |
2816 | { -asc => [qw/colC colD/] },| colC ASC, colD ASC, |
2817 | \'colE DESC', | colE DESC, |
2818 | \[ 'FUNC(colF, ?)', $x ], | FUNC(colF, ?) |
2819 | ] | /* ...with $x bound to ? */ |
18710f60 |
2820 | =============================================================== |
86298391 |
2821 | |
96449e8e |
2822 | |
2823 | |
2824 | =head1 SPECIAL OPERATORS |
2825 | |
e3f9dff4 |
2826 | my $sqlmaker = SQL::Abstract->new(special_ops => [ |
3a2e1a5e |
2827 | { |
2828 | regex => qr/.../, |
e3f9dff4 |
2829 | handler => sub { |
2830 | my ($self, $field, $op, $arg) = @_; |
2831 | ... |
3a2e1a5e |
2832 | }, |
2833 | }, |
2834 | { |
2835 | regex => qr/.../, |
2836 | handler => 'method_name', |
e3f9dff4 |
2837 | }, |
2838 | ]); |
2839 | |
9d48860e |
2840 | A "special operator" is a SQL syntactic clause that can be |
e3f9dff4 |
2841 | applied to a field, instead of a usual binary operator. |
be21dde3 |
2842 | For example: |
e3f9dff4 |
2843 | |
2844 | WHERE field IN (?, ?, ?) |
2845 | WHERE field BETWEEN ? AND ? |
2846 | WHERE MATCH(field) AGAINST (?, ?) |
96449e8e |
2847 | |
e3f9dff4 |
2848 | Special operators IN and BETWEEN are fairly standard and therefore |
3a2e1a5e |
2849 | are builtin within C<SQL::Abstract> (as the overridable methods |
2850 | C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators, |
2851 | like the MATCH .. AGAINST example above which is specific to MySQL, |
2852 | you can write your own operator handlers - supply a C<special_ops> |
2853 | argument to the C<new> method. That argument takes an arrayref of |
2854 | operator definitions; each operator definition is a hashref with two |
2855 | entries: |
96449e8e |
2856 | |
e3f9dff4 |
2857 | =over |
2858 | |
2859 | =item regex |
2860 | |
2861 | the regular expression to match the operator |
96449e8e |
2862 | |
e3f9dff4 |
2863 | =item handler |
2864 | |
3a2e1a5e |
2865 | Either a coderef or a plain scalar method name. In both cases |
2866 | the expected return is C<< ($sql, @bind) >>. |
2867 | |
2868 | When supplied with a method name, it is simply called on the |
13cc86af |
2869 | L<SQL::Abstract> object as: |
3a2e1a5e |
2870 | |
ca4f826a |
2871 | $self->$method_name($field, $op, $arg) |
3a2e1a5e |
2872 | |
2873 | Where: |
2874 | |
3a2e1a5e |
2875 | $field is the LHS of the operator |
13cc86af |
2876 | $op is the part that matched the handler regex |
3a2e1a5e |
2877 | $arg is the RHS |
2878 | |
2879 | When supplied with a coderef, it is called as: |
2880 | |
2881 | $coderef->($self, $field, $op, $arg) |
2882 | |
e3f9dff4 |
2883 | |
2884 | =back |
2885 | |
9d48860e |
2886 | For example, here is an implementation |
e3f9dff4 |
2887 | of the MATCH .. AGAINST syntax for MySQL |
2888 | |
2889 | my $sqlmaker = SQL::Abstract->new(special_ops => [ |
9d48860e |
2890 | |
e3f9dff4 |
2891 | # special op for MySql MATCH (field) AGAINST(word1, word2, ...) |
9d48860e |
2892 | {regex => qr/^match$/i, |
e3f9dff4 |
2893 | handler => sub { |
2894 | my ($self, $field, $op, $arg) = @_; |
2895 | $arg = [$arg] if not ref $arg; |
2896 | my $label = $self->_quote($field); |
2897 | my ($placeholder) = $self->_convert('?'); |
2898 | my $placeholders = join ", ", (($placeholder) x @$arg); |
2899 | my $sql = $self->_sqlcase('match') . " ($label) " |
2900 | . $self->_sqlcase('against') . " ($placeholders) "; |
2901 | my @bind = $self->_bindtype($field, @$arg); |
2902 | return ($sql, @bind); |
2903 | } |
2904 | }, |
9d48860e |
2905 | |
e3f9dff4 |
2906 | ]); |
96449e8e |
2907 | |
2908 | |
59f23b3d |
2909 | =head1 UNARY OPERATORS |
2910 | |
112b5232 |
2911 | my $sqlmaker = SQL::Abstract->new(unary_ops => [ |
59f23b3d |
2912 | { |
2913 | regex => qr/.../, |
2914 | handler => sub { |
2915 | my ($self, $op, $arg) = @_; |
2916 | ... |
2917 | }, |
2918 | }, |
2919 | { |
2920 | regex => qr/.../, |
2921 | handler => 'method_name', |
2922 | }, |
2923 | ]); |
2924 | |
9d48860e |
2925 | A "unary operator" is a SQL syntactic clause that can be |
59f23b3d |
2926 | applied to a field - the operator goes before the field |
2927 | |
2928 | You can write your own operator handlers - supply a C<unary_ops> |
2929 | argument to the C<new> method. That argument takes an arrayref of |
2930 | operator definitions; each operator definition is a hashref with two |
2931 | entries: |
2932 | |
2933 | =over |
2934 | |
2935 | =item regex |
2936 | |
2937 | the regular expression to match the operator |
2938 | |
2939 | =item handler |
2940 | |
2941 | Either a coderef or a plain scalar method name. In both cases |
2942 | the expected return is C<< $sql >>. |
2943 | |
2944 | When supplied with a method name, it is simply called on the |
13cc86af |
2945 | L<SQL::Abstract> object as: |
59f23b3d |
2946 | |
ca4f826a |
2947 | $self->$method_name($op, $arg) |
59f23b3d |
2948 | |
2949 | Where: |
2950 | |
2951 | $op is the part that matched the handler regex |
2952 | $arg is the RHS or argument of the operator |
2953 | |
2954 | When supplied with a coderef, it is called as: |
2955 | |
2956 | $coderef->($self, $op, $arg) |
2957 | |
2958 | |
2959 | =back |
2960 | |
2961 | |
32eab2da |
2962 | =head1 PERFORMANCE |
2963 | |
2964 | Thanks to some benchmarking by Mark Stosberg, it turns out that |
2965 | this module is many orders of magnitude faster than using C<DBIx::Abstract>. |
2966 | I must admit this wasn't an intentional design issue, but it's a |
2967 | byproduct of the fact that you get to control your C<DBI> handles |
2968 | yourself. |
2969 | |
2970 | To maximize performance, use a code snippet like the following: |
2971 | |
2972 | # prepare a statement handle using the first row |
2973 | # and then reuse it for the rest of the rows |
2974 | my($sth, $stmt); |
2975 | for my $href (@array_of_hashrefs) { |
2976 | $stmt ||= $sql->insert('table', $href); |
2977 | $sth ||= $dbh->prepare($stmt); |
2978 | $sth->execute($sql->values($href)); |
2979 | } |
2980 | |
2981 | The reason this works is because the keys in your C<$href> are sorted |
2982 | internally by B<SQL::Abstract>. Thus, as long as your data retains |
2983 | the same structure, you only have to generate the SQL the first time |
2984 | around. On subsequent queries, simply use the C<values> function provided |
2985 | by this module to return your values in the correct order. |
2986 | |
b864ba9b |
2987 | However this depends on the values having the same type - if, for |
2988 | example, the values of a where clause may either have values |
2989 | (resulting in sql of the form C<column = ?> with a single bind |
2990 | value), or alternatively the values might be C<undef> (resulting in |
2991 | sql of the form C<column IS NULL> with no bind value) then the |
2992 | caching technique suggested will not work. |
96449e8e |
2993 | |
32eab2da |
2994 | =head1 FORMBUILDER |
2995 | |
2996 | If you use my C<CGI::FormBuilder> module at all, you'll hopefully |
2997 | really like this part (I do, at least). Building up a complex query |
2998 | can be as simple as the following: |
2999 | |
3000 | #!/usr/bin/perl |
3001 | |
46dc2f3e |
3002 | use warnings; |
3003 | use strict; |
3004 | |
32eab2da |
3005 | use CGI::FormBuilder; |
3006 | use SQL::Abstract; |
3007 | |
3008 | my $form = CGI::FormBuilder->new(...); |
3009 | my $sql = SQL::Abstract->new; |
3010 | |
3011 | if ($form->submitted) { |
3012 | my $field = $form->field; |
3013 | my $id = delete $field->{id}; |
3014 | my($stmt, @bind) = $sql->update('table', $field, {id => $id}); |
3015 | } |
3016 | |
3017 | Of course, you would still have to connect using C<DBI> to run the |
3018 | query, but the point is that if you make your form look like your |
3019 | table, the actual query script can be extremely simplistic. |
3020 | |
3021 | If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for |
9d48860e |
3022 | a fast interface to returning and formatting data. I frequently |
32eab2da |
3023 | use these three modules together to write complex database query |
3024 | apps in under 50 lines. |
3025 | |
af733667 |
3026 | =head1 HOW TO CONTRIBUTE |
3027 | |
3028 | Contributions are always welcome, in all usable forms (we especially |
3029 | welcome documentation improvements). The delivery methods include git- |
3030 | or unified-diff formatted patches, GitHub pull requests, or plain bug |
3031 | reports either via RT or the Mailing list. Contributors are generally |
3032 | granted full access to the official repository after their first several |
3033 | patches pass successful review. |
3034 | |
3035 | This project is maintained in a git repository. The code and related tools are |
3036 | accessible at the following locations: |
d8cc1792 |
3037 | |
3038 | =over |
3039 | |
af733667 |
3040 | =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git> |
3041 | |
3042 | =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git> |
3043 | |
3044 | =item * GitHub mirror: L<https://github.com/dbsrgits/sql-abstract> |
d8cc1792 |
3045 | |
af733667 |
3046 | =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/SQL-Abstract.git> |
d8cc1792 |
3047 | |
3048 | =back |
32eab2da |
3049 | |
96449e8e |
3050 | =head1 CHANGES |
3051 | |
3052 | Version 1.50 was a major internal refactoring of C<SQL::Abstract>. |
3053 | Great care has been taken to preserve the I<published> behavior |
3054 | documented in previous versions in the 1.* family; however, |
9d48860e |
3055 | some features that were previously undocumented, or behaved |
96449e8e |
3056 | differently from the documentation, had to be changed in order |
3057 | to clarify the semantics. Hence, client code that was relying |
9d48860e |
3058 | on some dark areas of C<SQL::Abstract> v1.* |
96449e8e |
3059 | B<might behave differently> in v1.50. |
32eab2da |
3060 | |
be21dde3 |
3061 | The main changes are: |
d2a8fe1a |
3062 | |
96449e8e |
3063 | =over |
32eab2da |
3064 | |
9d48860e |
3065 | =item * |
32eab2da |
3066 | |
3ae1c5e2 |
3067 | support for literal SQL through the C<< \ [ $sql, @bind ] >> syntax. |
96449e8e |
3068 | |
3069 | =item * |
3070 | |
145fbfc8 |
3071 | support for the { operator => \"..." } construct (to embed literal SQL) |
3072 | |
3073 | =item * |
3074 | |
9c37b9c0 |
3075 | support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values) |
3076 | |
3077 | =item * |
3078 | |
96449e8e |
3079 | optional support for L<array datatypes|/"Inserting and Updating Arrays"> |
3080 | |
9d48860e |
3081 | =item * |
96449e8e |
3082 | |
be21dde3 |
3083 | defensive programming: check arguments |
96449e8e |
3084 | |
3085 | =item * |
3086 | |
3087 | fixed bug with global logic, which was previously implemented |
7cac25e6 |
3088 | through global variables yielding side-effects. Prior versions would |
96449e8e |
3089 | interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >> |
3090 | as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>. |
3091 | Now this is interpreted |
3092 | as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>. |
3093 | |
96449e8e |
3094 | |
3095 | =item * |
3096 | |
3097 | fixed semantics of _bindtype on array args |
3098 | |
9d48860e |
3099 | =item * |
96449e8e |
3100 | |
3101 | dropped the C<_anoncopy> of the %where tree. No longer necessary, |
3102 | we just avoid shifting arrays within that tree. |
3103 | |
3104 | =item * |
3105 | |
3106 | dropped the C<_modlogic> function |
3107 | |
3108 | =back |
32eab2da |
3109 | |
32eab2da |
3110 | =head1 ACKNOWLEDGEMENTS |
3111 | |
3112 | There are a number of individuals that have really helped out with |
3113 | this module. Unfortunately, most of them submitted bugs via CPAN |
3114 | so I have no idea who they are! But the people I do know are: |
3115 | |
9d48860e |
3116 | Ash Berlin (order_by hash term support) |
b643abe1 |
3117 | Matt Trout (DBIx::Class support) |
32eab2da |
3118 | Mark Stosberg (benchmarking) |
3119 | Chas Owens (initial "IN" operator support) |
3120 | Philip Collins (per-field SQL functions) |
3121 | Eric Kolve (hashref "AND" support) |
3122 | Mike Fragassi (enhancements to "BETWEEN" and "LIKE") |
3123 | Dan Kubb (support for "quote_char" and "name_sep") |
f5aab26e |
3124 | Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by) |
48d9f5f8 |
3125 | Laurent Dami (internal refactoring, extensible list of special operators, literal SQL) |
dbdf7648 |
3126 | Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests) |
e96c510a |
3127 | Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests) |
02288357 |
3128 | Oliver Charles (support for "RETURNING" after "INSERT") |
32eab2da |
3129 | |
3130 | Thanks! |
3131 | |
32eab2da |
3132 | =head1 SEE ALSO |
3133 | |
86298391 |
3134 | L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>. |
32eab2da |
3135 | |
32eab2da |
3136 | =head1 AUTHOR |
3137 | |
b643abe1 |
3138 | Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved. |
3139 | |
3140 | This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk> |
32eab2da |
3141 | |
abe72f94 |
3142 | For support, your best bet is to try the C<DBIx::Class> users mailing list. |
3143 | While not an official support venue, C<DBIx::Class> makes heavy use of |
3144 | C<SQL::Abstract>, and as such list members there are very familiar with |
3145 | how to create queries. |
3146 | |
0d067ded |
3147 | =head1 LICENSE |
3148 | |
d988ab87 |
3149 | This module is free software; you may copy this under the same |
3150 | terms as perl itself (either the GNU General Public License or |
3151 | the Artistic License) |
32eab2da |
3152 | |
3153 | =cut |