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