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