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