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