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