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