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