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