document order by madness
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract.pm
1 package SQL::Abstract; # see doc at end of file
2
3 use Carp ();
4 use List::Util ();
5 use Scalar::Util ();
6 use Module::Runtime qw(use_module);
7 use Moo;
8 use namespace::clean;
9
10 our $VERSION  = '1.72';
11
12 $VERSION = eval $VERSION;
13
14 sub belch (@) {
15   my($func) = (caller(1))[3];
16   Carp::carp "[$func] Warning: ", @_;
17 }
18
19 sub puke (@) {
20   my($func) = (caller(1))[3];
21   Carp::croak "[$func] Fatal: ", @_;
22 }
23
24 has converter => (is => 'lazy', clearer => 'clear_converter');
25
26 has case => (
27   is => 'ro', coerce => sub { $_[0] eq 'lower' ? 'lower' : undef }
28 );
29
30 has logic => (
31   is => 'ro', coerce => sub { uc($_[0]) }, default => sub { 'OR' }
32 );
33
34 has bindtype => (
35   is => 'ro', default => sub { 'normal' }
36 );
37
38 has cmp => (is => 'ro', default => sub { '=' });
39
40 has sqltrue => (is => 'ro', default => sub { '1=1' });
41 has sqlfalse => (is => 'ro', default => sub { '0=1' });
42
43 has special_ops => (is => 'ro', default => sub { [] });
44 has unary_ops => (is => 'ro', default => sub { [] });
45
46 # FIXME
47 # need to guard against ()'s in column names too, but this will break tons of
48 # hacks... ideas anyone?
49
50 has injection_guard => (
51   is => 'ro',
52   default => sub {
53     qr/
54       \;
55         |
56       ^ \s* go \s
57     /xmi;
58   }
59 );
60
61 has renderer => (is => 'lazy', clearer => 'clear_renderer');
62
63 has name_sep => (
64   is => 'rw', default => sub { '.' },
65   trigger => sub {
66     $_[0]->clear_renderer;
67     $_[0]->clear_converter;
68   },
69 );
70
71 has quote_char => (
72   is => 'rw',
73   trigger => sub {
74     $_[0]->clear_renderer;
75     $_[0]->clear_converter;
76   },
77 );
78
79 has collapse_aliases => (
80   is => 'ro',
81   default => sub { 0 }
82 );
83
84 has always_quote => (
85   is => 'rw', default => sub { 1 },
86   trigger => sub {
87     $_[0]->clear_renderer;
88     $_[0]->clear_converter;
89   },
90 );
91
92 has convert => (is => 'ro');
93
94 has array_datatypes => (is => 'ro');
95
96 has converter_class => (
97   is => 'rw', lazy => 1, builder => '_build_converter_class',
98   trigger => sub { shift->clear_converter },
99 );
100
101 sub _build_converter_class {
102   use_module('SQL::Abstract::Converter')
103 }
104
105 has renderer_class => (
106   is => 'rw', lazy => 1, builder => '_build_renderer_class',
107   trigger => sub { shift->clear_renderer },
108 );
109
110 sub _build_renderer_class {
111   use_module('Data::Query::Renderer::SQL::Naive')
112 }
113
114 sub _converter_args {
115   my ($self) = @_;
116   Scalar::Util::weaken($self);
117   +{
118     lower_case => $self->case,
119     default_logic => $self->logic,
120     bind_meta => not($self->bindtype eq 'normal'),
121     identifier_sep => $self->name_sep,
122     (map +($_ => $self->$_), qw(
123       cmp sqltrue sqlfalse injection_guard convert array_datatypes
124     )),
125     special_ops => [
126       map {
127         my $sub = $_->{handler};
128         +{
129           %$_,
130           handler => sub { $self->$sub(@_) }
131         }
132       } @{$self->special_ops}
133     ],
134     renderer_will_quote => (
135       defined($self->quote_char) and $self->always_quote
136     ),
137   }
138 }
139
140 sub _build_converter {
141   my ($self) = @_;
142   $self->converter_class->new($self->_converter_args);
143 }
144
145 sub _renderer_args {
146   my ($self) = @_;
147   my ($chars);
148   for ($self->quote_char) {
149     $chars = defined() ? (ref() ? $_ : [$_]) : ['',''];
150   }
151   +{
152     quote_chars => $chars, always_quote => $self->always_quote,
153     identifier_sep => $self->name_sep,
154     collapse_aliases => $self->collapse_aliases,
155     ($self->case ? (lc_keywords => 1) : ()), # always 'lower' if it exists
156   };
157 }
158
159 sub _build_renderer {
160   my ($self) = @_;
161   $self->renderer_class->new($self->_renderer_args);
162 }
163
164 sub _render_dq {
165   my ($self, $dq) = @_;
166   if (!$dq) {
167     return '';
168   }
169   my ($sql, @bind) = @{$self->renderer->render($dq)};
170   wantarray ?
171     ($self->{bindtype} eq 'normal'
172       ? ($sql, map $_->{value}, @bind)
173       : ($sql, map [ $_->{value_meta}, $_->{value} ], @bind)
174     )
175     : $sql;
176 }
177
178 sub _render_sqla {
179   my ($self, $type, @args) = @_;
180   $self->_render_dq($self->converter->${\"_${type}_to_dq"}(@args));
181 }
182
183 sub insert { shift->_render_sqla(insert => @_) }
184
185 sub update { shift->_render_sqla(update => @_) }
186
187 sub select { shift->_render_sqla(select => @_) }
188
189 sub delete { shift->_render_sqla(delete => @_) }
190
191 sub where {
192   my ($self, $where, $order) = @_;
193
194   my $sql = '';
195   my @bind;
196
197   # where ?
198   ($sql, @bind) = $self->_recurse_where($where) if defined($where);
199   $sql = $sql ? $self->_sqlcase(' where ') . "( $sql )" : '';
200
201   # order by?
202   if ($order) {
203     $sql .= $self->_order_by($order);
204   }
205
206   return wantarray ? ($sql, @bind) : $sql;
207 }
208
209 sub _recurse_where { shift->_render_sqla(where => @_) }
210
211 sub _order_by {
212   my ($self, $arg) = @_;
213   if (my $dq = $self->converter->_order_by_to_dq($arg)) {
214     # SQLA generates ' ORDER BY foo'. The hilarity.
215     wantarray
216       ? do { my @r = $self->_render_dq($dq); $r[0] = ' '.$r[0]; @r }
217       : ' '.$self->_render_dq($dq);
218   } else {
219     '';
220   }
221 }
222
223 # highly optimized, as it's called way too often
224 sub _quote {
225   # my ($self, $label) = @_;
226
227   return '' unless defined $_[1];
228   return ${$_[1]} if ref($_[1]) eq 'SCALAR';
229
230   unless ($_[0]->{quote_char}) {
231     $_[0]->_assert_pass_injection_guard($_[1]);
232     return $_[1];
233   }
234
235   my $qref = ref $_[0]->{quote_char};
236   my ($l, $r);
237   if (!$qref) {
238     ($l, $r) = ( $_[0]->{quote_char}, $_[0]->{quote_char} );
239   }
240   elsif ($qref eq 'ARRAY') {
241     ($l, $r) = @{$_[0]->{quote_char}};
242   }
243   else {
244     puke "Unsupported quote_char format: $_[0]->{quote_char}";
245   }
246
247   # parts containing * are naturally unquoted
248   return join( $_[0]->{name_sep}||'', map
249     { $_ eq '*' ? $_ : $l . $_ . $r }
250     ( $_[0]->{name_sep} ? split (/\Q$_[0]->{name_sep}\E/, $_[1] ) : $_[1] )
251   );
252 }
253
254 sub _assert_pass_injection_guard {
255   if ($_[1] =~ $_[0]->{injection_guard}) {
256     my $class = ref $_[0];
257     die "Possible SQL injection attempt '$_[1]'. If this is indeed a part of the
258  "
259      . "desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply your own 
260 "
261      . "{injection_guard} attribute to ${class}->new()"
262   }
263 }
264
265 # Conversion, if applicable
266 sub _convert ($) {
267   #my ($self, $arg) = @_;
268
269 # LDNOTE : modified the previous implementation below because
270 # it was not consistent : the first "return" is always an array,
271 # the second "return" is context-dependent. Anyway, _convert
272 # seems always used with just a single argument, so make it a
273 # scalar function.
274 #     return @_ unless $self->{convert};
275 #     my $conv = $self->_sqlcase($self->{convert});
276 #     my @ret = map { $conv.'('.$_.')' } @_;
277 #     return wantarray ? @ret : $ret[0];
278   if ($_[0]->{convert}) {
279     return $_[0]->_sqlcase($_[0]->{convert}) .'(' . $_[1] . ')';
280   }
281   return $_[1];
282 }
283
284 # And bindtype
285 sub _bindtype (@) {
286   #my ($self, $col, @vals) = @_;
287
288   #LDNOTE : changed original implementation below because it did not make
289   # sense when bindtype eq 'columns' and @vals > 1.
290 #  return $self->{bindtype} eq 'columns' ? [ $col, @vals ] : @vals;
291
292   # called often - tighten code
293   return $_[0]->{bindtype} eq 'columns'
294     ? map {[$_[1], $_]} @_[2 .. $#_]
295     : @_[2 .. $#_]
296   ;
297 }
298
299 # Dies if any element of @bind is not in [colname => value] format
300 # if bindtype is 'columns'.
301 sub _assert_bindval_matches_bindtype {
302 #  my ($self, @bind) = @_;
303   my $self = shift;
304   if ($self->{bindtype} eq 'columns') {
305     for (@_) {
306       if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
307         puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
308       }
309     }
310   }
311 }
312
313 # Fix SQL case, if so requested
314 sub _sqlcase {
315   # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
316   # don't touch the argument ... crooked logic, but let's not change it!
317   return $_[0]->{case} ? $_[1] : uc($_[1]);
318 }
319
320 sub values {
321     my $self = shift;
322     my $data = shift || return;
323     puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
324         unless ref $data eq 'HASH';
325
326     my @all_bind;
327     foreach my $k ( sort keys %$data ) {
328         my $v = $data->{$k};
329         local our $Cur_Col_Meta = $k;
330         my ($sql, @bind) = $self->_render_sqla(
331             mutation_rhs => $v
332         );
333         push @all_bind, @bind;
334     }
335
336     return @all_bind;
337 }
338
339 sub generate {
340     my $self  = shift;
341
342     my(@sql, @sqlq, @sqlv);
343
344     for (@_) {
345         my $ref = ref $_;
346         if ($ref eq 'HASH') {
347             for my $k (sort keys %$_) {
348                 my $v = $_->{$k};
349                 my $r = ref $v;
350                 my $label = $self->_quote($k);
351                 if ($r eq 'ARRAY') {
352                     # literal SQL with bind
353                     my ($sql, @bind) = @$v;
354                     $self->_assert_bindval_matches_bindtype(@bind);
355                     push @sqlq, "$label = $sql";
356                     push @sqlv, @bind;
357                 } elsif ($r eq 'SCALAR') {
358                     # literal SQL without bind
359                     push @sqlq, "$label = $$v";
360                 } else {
361                     push @sqlq, "$label = ?";
362                     push @sqlv, $self->_bindtype($k, $v);
363                 }
364             }
365             push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
366         } elsif ($ref eq 'ARRAY') {
367             # unlike insert(), assume these are ONLY the column names, i.e. for SQL
368             for my $v (@$_) {
369                 my $r = ref $v;
370                 if ($r eq 'ARRAY') {   # literal SQL with bind
371                     my ($sql, @bind) = @$v;
372                     $self->_assert_bindval_matches_bindtype(@bind);
373                     push @sqlq, $sql;
374                     push @sqlv, @bind;
375                 } elsif ($r eq 'SCALAR') {  # literal SQL without bind
376                     # embedded literal SQL
377                     push @sqlq, $$v;
378                 } else {
379                     push @sqlq, '?';
380                     push @sqlv, $v;
381                 }
382             }
383             push @sql, '(' . join(', ', @sqlq) . ')';
384         } elsif ($ref eq 'SCALAR') {
385             # literal SQL
386             push @sql, $$_;
387         } else {
388             # strings get case twiddled
389             push @sql, $self->_sqlcase($_);
390         }
391     }
392
393     my $sql = join ' ', @sql;
394
395     # this is pretty tricky
396     # if ask for an array, return ($stmt, @bind)
397     # otherwise, s/?/shift @sqlv/ to put it inline
398     if (wantarray) {
399         return ($sql, @sqlv);
400     } else {
401         1 while $sql =~ s/\?/my $d = shift(@sqlv);
402                              ref $d ? $d->[1] : $d/e;
403         return $sql;
404     }
405 }
406
407 1;
408
409
410 __END__
411
412 =head1 NAME
413
414 SQL::Abstract - Generate SQL from Perl data structures
415
416 =head1 SYNOPSIS
417
418     use SQL::Abstract;
419
420     my $sql = SQL::Abstract->new;
421
422     my($stmt, @bind) = $sql->select($table, \@fields, \%where, \@order);
423
424     my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
425
426     my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
427
428     my($stmt, @bind) = $sql->delete($table, \%where);
429
430     # Then, use these in your DBI statements
431     my $sth = $dbh->prepare($stmt);
432     $sth->execute(@bind);
433
434     # Just generate the WHERE clause
435     my($stmt, @bind) = $sql->where(\%where, \@order);
436
437     # Return values in the same order, for hashed queries
438     # See PERFORMANCE section for more details
439     my @bind = $sql->values(\%fieldvals);
440
441 =head1 DESCRIPTION
442
443 This module was inspired by the excellent L<DBIx::Abstract>.
444 However, in using that module I found that what I really wanted
445 to do was generate SQL, but still retain complete control over my
446 statement handles and use the DBI interface. So, I set out to
447 create an abstract SQL generation module.
448
449 While based on the concepts used by L<DBIx::Abstract>, there are
450 several important differences, especially when it comes to WHERE
451 clauses. I have modified the concepts used to make the SQL easier
452 to generate from Perl data structures and, IMO, more intuitive.
453 The underlying idea is for this module to do what you mean, based
454 on the data structures you provide it. The big advantage is that
455 you don't have to modify your code every time your data changes,
456 as this module figures it out.
457
458 To begin with, an SQL INSERT is as easy as just specifying a hash
459 of C<key=value> pairs:
460
461     my %data = (
462         name => 'Jimbo Bobson',
463         phone => '123-456-7890',
464         address => '42 Sister Lane',
465         city => 'St. Louis',
466         state => 'Louisiana',
467     );
468
469 The SQL can then be generated with this:
470
471     my($stmt, @bind) = $sql->insert('people', \%data);
472
473 Which would give you something like this:
474
475     $stmt = "INSERT INTO people
476                     (address, city, name, phone, state)
477                     VALUES (?, ?, ?, ?, ?)";
478     @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
479              '123-456-7890', 'Louisiana');
480
481 These are then used directly in your DBI code:
482
483     my $sth = $dbh->prepare($stmt);
484     $sth->execute(@bind);
485
486 =head2 Inserting and Updating Arrays
487
488 If your database has array types (like for example Postgres),
489 activate the special option C<< array_datatypes => 1 >>
490 when creating the C<SQL::Abstract> object.
491 Then you may use an arrayref to insert and update database array types:
492
493     my $sql = SQL::Abstract->new(array_datatypes => 1);
494     my %data = (
495         planets => [qw/Mercury Venus Earth Mars/]
496     );
497
498     my($stmt, @bind) = $sql->insert('solar_system', \%data);
499
500 This results in:
501
502     $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
503
504     @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
505
506
507 =head2 Inserting and Updating SQL
508
509 In order to apply SQL functions to elements of your C<%data> you may
510 specify a reference to an arrayref for the given hash value. For example,
511 if you need to execute the Oracle C<to_date> function on a value, you can
512 say something like this:
513
514     my %data = (
515         name => 'Bill',
516         date_entered => \["to_date(?,'MM/DD/YYYY')", "03/02/2003"],
517     );
518
519 The first value in the array is the actual SQL. Any other values are
520 optional and would be included in the bind values array. This gives
521 you:
522
523     my($stmt, @bind) = $sql->insert('people', \%data);
524
525     $stmt = "INSERT INTO people (name, date_entered)
526                 VALUES (?, to_date(?,'MM/DD/YYYY'))";
527     @bind = ('Bill', '03/02/2003');
528
529 An UPDATE is just as easy, all you change is the name of the function:
530
531     my($stmt, @bind) = $sql->update('people', \%data);
532
533 Notice that your C<%data> isn't touched; the module will generate
534 the appropriately quirky SQL for you automatically. Usually you'll
535 want to specify a WHERE clause for your UPDATE, though, which is
536 where handling C<%where> hashes comes in handy...
537
538 =head2 Complex where statements
539
540 This module can generate pretty complicated WHERE statements
541 easily. For example, simple C<key=value> pairs are taken to mean
542 equality, and if you want to see if a field is within a set
543 of values, you can use an arrayref. Let's say we wanted to
544 SELECT some data based on this criteria:
545
546     my %where = (
547        requestor => 'inna',
548        worker => ['nwiger', 'rcwe', 'sfz'],
549        status => { '!=', 'completed' }
550     );
551
552     my($stmt, @bind) = $sql->select('tickets', '*', \%where);
553
554 The above would give you something like this:
555
556     $stmt = "SELECT * FROM tickets WHERE
557                 ( requestor = ? ) AND ( status != ? )
558                 AND ( worker = ? OR worker = ? OR worker = ? )";
559     @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
560
561 Which you could then use in DBI code like so:
562
563     my $sth = $dbh->prepare($stmt);
564     $sth->execute(@bind);
565
566 Easy, eh?
567
568 =head1 FUNCTIONS
569
570 The functions are simple. There's one for each major SQL operation,
571 and a constructor you use first. The arguments are specified in a
572 similar order to each function (table, then fields, then a where
573 clause) to try and simplify things.
574
575
576
577
578 =head2 new(option => 'value')
579
580 The C<new()> function takes a list of options and values, and returns
581 a new B<SQL::Abstract> object which can then be used to generate SQL
582 through the methods below. The options accepted are:
583
584 =over
585
586 =item case
587
588 If set to 'lower', then SQL will be generated in all lowercase. By
589 default SQL is generated in "textbook" case meaning something like:
590
591     SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
592
593 Any setting other than 'lower' is ignored.
594
595 =item cmp
596
597 This determines what the default comparison operator is. By default
598 it is C<=>, meaning that a hash like this:
599
600     %where = (name => 'nwiger', email => 'nate@wiger.org');
601
602 Will generate SQL like this:
603
604     WHERE name = 'nwiger' AND email = 'nate@wiger.org'
605
606 However, you may want loose comparisons by default, so if you set
607 C<cmp> to C<like> you would get SQL such as:
608
609     WHERE name like 'nwiger' AND email like 'nate@wiger.org'
610
611 You can also override the comparsion on an individual basis - see
612 the huge section on L</"WHERE CLAUSES"> at the bottom.
613
614 =item sqltrue, sqlfalse
615
616 Expressions for inserting boolean values within SQL statements.
617 By default these are C<1=1> and C<1=0>. They are used
618 by the special operators C<-in> and C<-not_in> for generating
619 correct SQL even when the argument is an empty array (see below).
620
621 =item logic
622
623 This determines the default logical operator for multiple WHERE
624 statements in arrays or hashes. If absent, the default logic is "or"
625 for arrays, and "and" for hashes. This means that a WHERE
626 array of the form:
627
628     @where = (
629         event_date => {'>=', '2/13/99'},
630         event_date => {'<=', '4/24/03'},
631     );
632
633 will generate SQL like this:
634
635     WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
636
637 This is probably not what you want given this query, though (look
638 at the dates). To change the "OR" to an "AND", simply specify:
639
640     my $sql = SQL::Abstract->new(logic => 'and');
641
642 Which will change the above C<WHERE> to:
643
644     WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
645
646 The logic can also be changed locally by inserting
647 a modifier in front of an arrayref :
648
649     @where = (-and => [event_date => {'>=', '2/13/99'},
650                        event_date => {'<=', '4/24/03'} ]);
651
652 See the L</"WHERE CLAUSES"> section for explanations.
653
654 =item convert
655
656 This will automatically convert comparisons using the specified SQL
657 function for both column and value. This is mostly used with an argument
658 of C<upper> or C<lower>, so that the SQL will have the effect of
659 case-insensitive "searches". For example, this:
660
661     $sql = SQL::Abstract->new(convert => 'upper');
662     %where = (keywords => 'MaKe iT CAse inSeNSItive');
663
664 Will turn out the following SQL:
665
666     WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
667
668 The conversion can be C<upper()>, C<lower()>, or any other SQL function
669 that can be applied symmetrically to fields (actually B<SQL::Abstract> does
670 not validate this option; it will just pass through what you specify verbatim).
671
672 =item bindtype
673
674 This is a kludge because many databases suck. For example, you can't
675 just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
676 Instead, you have to use C<bind_param()>:
677
678     $sth->bind_param(1, 'reg data');
679     $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
680
681 The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
682 which loses track of which field each slot refers to. Fear not.
683
684 If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
685 Currently, you can specify either C<normal> (default) or C<columns>. If you
686 specify C<columns>, you will get an array that looks like this:
687
688     my $sql = SQL::Abstract->new(bindtype => 'columns');
689     my($stmt, @bind) = $sql->insert(...);
690
691     @bind = (
692         [ 'column1', 'value1' ],
693         [ 'column2', 'value2' ],
694         [ 'column3', 'value3' ],
695     );
696
697 You can then iterate through this manually, using DBI's C<bind_param()>.
698
699     $sth->prepare($stmt);
700     my $i = 1;
701     for (@bind) {
702         my($col, $data) = @$_;
703         if ($col eq 'details' || $col eq 'comments') {
704             $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
705         } elsif ($col eq 'image') {
706             $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
707         } else {
708             $sth->bind_param($i, $data);
709         }
710         $i++;
711     }
712     $sth->execute;      # execute without @bind now
713
714 Now, why would you still use B<SQL::Abstract> if you have to do this crap?
715 Basically, the advantage is still that you don't have to care which fields
716 are or are not included. You could wrap that above C<for> loop in a simple
717 sub called C<bind_fields()> or something and reuse it repeatedly. You still
718 get a layer of abstraction over manual SQL specification.
719
720 Note that if you set L</bindtype> to C<columns>, the C<\[$sql, @bind]>
721 construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
722 will expect the bind values in this format.
723
724 =item quote_char
725
726 This is the character that a table or column name will be quoted
727 with.  By default this is an empty string, but you could set it to
728 the character C<`>, to generate SQL like this:
729
730   SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
731
732 Alternatively, you can supply an array ref of two items, the first being the left
733 hand quote character, and the second the right hand quote character. For
734 example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
735 that generates SQL like this:
736
737   SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
738
739 Quoting is useful if you have tables or columns names that are reserved
740 words in your database's SQL dialect.
741
742 =item name_sep
743
744 This is the character that separates a table and column name.  It is
745 necessary to specify this when the C<quote_char> option is selected,
746 so that tables and column names can be individually quoted like this:
747
748   SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
749
750 =item injection_guard
751
752 A regular expression C<qr/.../> that is applied to any C<-function> and unquoted
753 column name specified in a query structure. This is a safety mechanism to avoid
754 injection attacks when mishandling user input e.g.:
755
756   my %condition_as_column_value_pairs = get_values_from_user();
757   $sqla->select( ... , \%condition_as_column_value_pairs );
758
759 If the expression matches an exception is thrown. Note that literal SQL
760 supplied via C<\'...'> or C<\['...']> is B<not> checked in any way.
761
762 Defaults to checking for C<;> and the C<GO> keyword (TransactSQL)
763
764 =item array_datatypes
765
766 When this option is true, arrayrefs in INSERT or UPDATE are
767 interpreted as array datatypes and are passed directly
768 to the DBI layer.
769 When this option is false, arrayrefs are interpreted
770 as literal SQL, just like refs to arrayrefs
771 (but this behavior is for backwards compatibility; when writing
772 new queries, use the "reference to arrayref" syntax
773 for literal SQL).
774
775
776 =item special_ops
777
778 Takes a reference to a list of "special operators"
779 to extend the syntax understood by L<SQL::Abstract>.
780 See section L</"SPECIAL OPERATORS"> for details.
781
782 =item unary_ops
783
784 Takes a reference to a list of "unary operators"
785 to extend the syntax understood by L<SQL::Abstract>.
786 See section L</"UNARY OPERATORS"> for details.
787
788
789
790 =back
791
792 =head2 insert($table, \@values || \%fieldvals, \%options)
793
794 This is the simplest function. You simply give it a table name
795 and either an arrayref of values or hashref of field/value pairs.
796 It returns an SQL INSERT statement and a list of bind values.
797 See the sections on L</"Inserting and Updating Arrays"> and
798 L</"Inserting and Updating SQL"> for information on how to insert
799 with those data types.
800
801 The optional C<\%options> hash reference may contain additional
802 options to generate the insert SQL. Currently supported options
803 are:
804
805 =over 4
806
807 =item returning
808
809 Takes either a scalar of raw SQL fields, or an array reference of
810 field names, and adds on an SQL C<RETURNING> statement at the end.
811 This allows you to return data generated by the insert statement
812 (such as row IDs) without performing another C<SELECT> statement.
813 Note, however, this is not part of the SQL standard and may not
814 be supported by all database engines.
815
816 =back
817
818 =head2 update($table, \%fieldvals, \%where)
819
820 This takes a table, hashref of field/value pairs, and an optional
821 hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
822 of bind values.
823 See the sections on L</"Inserting and Updating Arrays"> and
824 L</"Inserting and Updating SQL"> for information on how to insert
825 with those data types.
826
827 =head2 select($source, $fields, $where, $order)
828
829 This returns a SQL SELECT statement and associated list of bind values, as
830 specified by the arguments  :
831
832 =over
833
834 =item $source
835
836 Specification of the 'FROM' part of the statement.
837 The argument can be either a plain scalar (interpreted as a table
838 name, will be quoted), or an arrayref (interpreted as a list
839 of table names, joined by commas, quoted), or a scalarref
840 (literal table name, not quoted), or a ref to an arrayref
841 (list of literal table names, joined by commas, not quoted).
842
843 =item $fields
844
845 Specification of the list of fields to retrieve from
846 the source.
847 The argument can be either an arrayref (interpreted as a list
848 of field names, will be joined by commas and quoted), or a
849 plain scalar (literal SQL, not quoted).
850 Please observe that this API is not as flexible as for
851 the first argument C<$table>, for backwards compatibility reasons.
852
853 =item $where
854
855 Optional argument to specify the WHERE part of the query.
856 The argument is most often a hashref, but can also be
857 an arrayref or plain scalar --
858 see section L<WHERE clause|/"WHERE CLAUSES"> for details.
859
860 =item $order
861
862 Optional argument to specify the ORDER BY part of the query.
863 The argument can be a scalar, a hashref or an arrayref
864 -- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
865 for details.
866
867 =back
868
869
870 =head2 delete($table, \%where)
871
872 This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
873 It returns an SQL DELETE statement and list of bind values.
874
875 =head2 where(\%where, \@order)
876
877 This is used to generate just the WHERE clause. For example,
878 if you have an arbitrary data structure and know what the
879 rest of your SQL is going to look like, but want an easy way
880 to produce a WHERE clause, use this. It returns an SQL WHERE
881 clause and list of bind values.
882
883
884 =head2 values(\%data)
885
886 This just returns the values from the hash C<%data>, in the same
887 order that would be returned from any of the other above queries.
888 Using this allows you to markedly speed up your queries if you
889 are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
890
891 =head2 generate($any, 'number', $of, \@data, $struct, \%types)
892
893 Warning: This is an experimental method and subject to change.
894
895 This returns arbitrarily generated SQL. It's a really basic shortcut.
896 It will return two different things, depending on return context:
897
898     my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
899     my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
900
901 These would return the following:
902
903     # First calling form
904     $stmt = "CREATE TABLE test (?, ?)";
905     @bind = (field1, field2);
906
907     # Second calling form
908     $stmt_and_val = "CREATE TABLE test (field1, field2)";
909
910 Depending on what you're trying to do, it's up to you to choose the correct
911 format. In this example, the second form is what you would want.
912
913 By the same token:
914
915     $sql->generate('alter session', { nls_date_format => 'MM/YY' });
916
917 Might give you:
918
919     ALTER SESSION SET nls_date_format = 'MM/YY'
920
921 You get the idea. Strings get their case twiddled, but everything
922 else remains verbatim.
923
924 =head1 WHERE CLAUSES
925
926 =head2 Introduction
927
928 This module uses a variation on the idea from L<DBIx::Abstract>. It
929 is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
930 module is that things in arrays are OR'ed, and things in hashes
931 are AND'ed.>
932
933 The easiest way to explain is to show lots of examples. After
934 each C<%where> hash shown, it is assumed you used:
935
936     my($stmt, @bind) = $sql->where(\%where);
937
938 However, note that the C<%where> hash can be used directly in any
939 of the other functions as well, as described above.
940
941 =head2 Key-value pairs
942
943 So, let's get started. To begin, a simple hash:
944
945     my %where  = (
946         user   => 'nwiger',
947         status => 'completed'
948     );
949
950 Is converted to SQL C<key = val> statements:
951
952     $stmt = "WHERE user = ? AND status = ?";
953     @bind = ('nwiger', 'completed');
954
955 One common thing I end up doing is having a list of values that
956 a field can be in. To do this, simply specify a list inside of
957 an arrayref:
958
959     my %where  = (
960         user   => 'nwiger',
961         status => ['assigned', 'in-progress', 'pending'];
962     );
963
964 This simple code will create the following:
965
966     $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
967     @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
968
969 A field associated to an empty arrayref will be considered a
970 logical false and will generate 0=1.
971
972 =head2 Tests for NULL values
973
974 If the value part is C<undef> then this is converted to SQL <IS NULL>
975
976     my %where  = (
977         user   => 'nwiger',
978         status => undef,
979     );
980
981 becomes:
982
983     $stmt = "WHERE user = ? AND status IS NULL";
984     @bind = ('nwiger');
985
986 To test if a column IS NOT NULL:
987
988     my %where  = (
989         user   => 'nwiger',
990         status => { '!=', undef },
991     );
992
993 =head2 Specific comparison operators
994
995 If you want to specify a different type of operator for your comparison,
996 you can use a hashref for a given column:
997
998     my %where  = (
999         user   => 'nwiger',
1000         status => { '!=', 'completed' }
1001     );
1002
1003 Which would generate:
1004
1005     $stmt = "WHERE user = ? AND status != ?";
1006     @bind = ('nwiger', 'completed');
1007
1008 To test against multiple values, just enclose the values in an arrayref:
1009
1010     status => { '=', ['assigned', 'in-progress', 'pending'] };
1011
1012 Which would give you:
1013
1014     "WHERE status = ? OR status = ? OR status = ?"
1015
1016
1017 The hashref can also contain multiple pairs, in which case it is expanded
1018 into an C<AND> of its elements:
1019
1020     my %where  = (
1021         user   => 'nwiger',
1022         status => { '!=', 'completed', -not_like => 'pending%' }
1023     );
1024
1025     # Or more dynamically, like from a form
1026     $where{user} = 'nwiger';
1027     $where{status}{'!='} = 'completed';
1028     $where{status}{'-not_like'} = 'pending%';
1029
1030     # Both generate this
1031     $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
1032     @bind = ('nwiger', 'completed', 'pending%');
1033
1034
1035 To get an OR instead, you can combine it with the arrayref idea:
1036
1037     my %where => (
1038          user => 'nwiger',
1039          priority => [ { '=', 2 }, { '>', 5 } ]
1040     );
1041
1042 Which would generate:
1043
1044     $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?";
1045     @bind = ('2', '5', 'nwiger');
1046
1047 If you want to include literal SQL (with or without bind values), just use a
1048 scalar reference or array reference as the value:
1049
1050     my %where  = (
1051         date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
1052         date_expires => { '<' => \"now()" }
1053     );
1054
1055 Which would generate:
1056
1057     $stmt = "WHERE date_entered > "to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
1058     @bind = ('11/26/2008');
1059
1060
1061 =head2 Logic and nesting operators
1062
1063 In the example above,
1064 there is a subtle trap if you want to say something like
1065 this (notice the C<AND>):
1066
1067     WHERE priority != ? AND priority != ?
1068
1069 Because, in Perl you I<can't> do this:
1070
1071     priority => { '!=', 2, '!=', 1 }
1072
1073 As the second C<!=> key will obliterate the first. The solution
1074 is to use the special C<-modifier> form inside an arrayref:
1075
1076     priority => [ -and => {'!=', 2},
1077                           {'!=', 1} ]
1078
1079
1080 Normally, these would be joined by C<OR>, but the modifier tells it
1081 to use C<AND> instead. (Hint: You can use this in conjunction with the
1082 C<logic> option to C<new()> in order to change the way your queries
1083 work by default.) B<Important:> Note that the C<-modifier> goes
1084 B<INSIDE> the arrayref, as an extra first element. This will
1085 B<NOT> do what you think it might:
1086
1087     priority => -and => [{'!=', 2}, {'!=', 1}]   # WRONG!
1088
1089 Here is a quick list of equivalencies, since there is some overlap:
1090
1091     # Same
1092     status => {'!=', 'completed', 'not like', 'pending%' }
1093     status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
1094
1095     # Same
1096     status => {'=', ['assigned', 'in-progress']}
1097     status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
1098     status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
1099
1100
1101
1102 =head2 Special operators : IN, BETWEEN, etc.
1103
1104 You can also use the hashref format to compare a list of fields using the
1105 C<IN> comparison operator, by specifying the list as an arrayref:
1106
1107     my %where  = (
1108         status   => 'completed',
1109         reportid => { -in => [567, 2335, 2] }
1110     );
1111
1112 Which would generate:
1113
1114     $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
1115     @bind = ('completed', '567', '2335', '2');
1116
1117 The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
1118 the same way.
1119
1120 If the argument to C<-in> is an empty array, 'sqlfalse' is generated
1121 (by default : C<1=0>). Similarly, C<< -not_in => [] >> generates
1122 'sqltrue' (by default : C<1=1>).
1123
1124 In addition to the array you can supply a chunk of literal sql or
1125 literal sql with bind:
1126
1127     my %where = {
1128       customer => { -in => \[
1129         'SELECT cust_id FROM cust WHERE balance > ?',
1130         2000,
1131       ],
1132       status => { -in => \'SELECT status_codes FROM states' },
1133     };
1134
1135 would generate:
1136
1137     $stmt = "WHERE (
1138           customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
1139       AND status IN ( SELECT status_codes FROM states )
1140     )";
1141     @bind = ('2000');
1142
1143
1144
1145 Another pair of operators is C<-between> and C<-not_between>,
1146 used with an arrayref of two values:
1147
1148     my %where  = (
1149         user   => 'nwiger',
1150         completion_date => {
1151            -not_between => ['2002-10-01', '2003-02-06']
1152         }
1153     );
1154
1155 Would give you:
1156
1157     WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
1158
1159 Just like with C<-in> all plausible combinations of literal SQL
1160 are possible:
1161
1162     my %where = {
1163       start0 => { -between => [ 1, 2 ] },
1164       start1 => { -between => \["? AND ?", 1, 2] },
1165       start2 => { -between => \"lower(x) AND upper(y)" },
1166       start3 => { -between => [
1167         \"lower(x)",
1168         \["upper(?)", 'stuff' ],
1169       ] },
1170     };
1171
1172 Would give you:
1173
1174     $stmt = "WHERE (
1175           ( start0 BETWEEN ? AND ?                )
1176       AND ( start1 BETWEEN ? AND ?                )
1177       AND ( start2 BETWEEN lower(x) AND upper(y)  )
1178       AND ( start3 BETWEEN lower(x) AND upper(?)  )
1179     )";
1180     @bind = (1, 2, 1, 2, 'stuff');
1181
1182
1183 These are the two builtin "special operators"; but the
1184 list can be expanded : see section L</"SPECIAL OPERATORS"> below.
1185
1186 =head2 Unary operators: bool
1187
1188 If you wish to test against boolean columns or functions within your
1189 database you can use the C<-bool> and C<-not_bool> operators. For
1190 example to test the column C<is_user> being true and the column
1191 C<is_enabled> being false you would use:-
1192
1193     my %where  = (
1194         -bool       => 'is_user',
1195         -not_bool   => 'is_enabled',
1196     );
1197
1198 Would give you:
1199
1200     WHERE is_user AND NOT is_enabled
1201
1202 If a more complex combination is required, testing more conditions,
1203 then you should use the and/or operators:-
1204
1205     my %where  = (
1206         -and           => [
1207             -bool      => 'one',
1208             -bool      => 'two',
1209             -bool      => 'three',
1210             -not_bool  => 'four',
1211         ],
1212     );
1213
1214 Would give you:
1215
1216     WHERE one AND two AND three AND NOT four
1217
1218
1219 =head2 Nested conditions, -and/-or prefixes
1220
1221 So far, we've seen how multiple conditions are joined with a top-level
1222 C<AND>.  We can change this by putting the different conditions we want in
1223 hashes and then putting those hashes in an array. For example:
1224
1225     my @where = (
1226         {
1227             user   => 'nwiger',
1228             status => { -like => ['pending%', 'dispatched'] },
1229         },
1230         {
1231             user   => 'robot',
1232             status => 'unassigned',
1233         }
1234     );
1235
1236 This data structure would create the following:
1237
1238     $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
1239                 OR ( user = ? AND status = ? ) )";
1240     @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
1241
1242
1243 Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or>
1244 to change the logic inside :
1245
1246     my @where = (
1247          -and => [
1248             user => 'nwiger',
1249             [
1250                 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
1251                 -or => { workhrs => {'<', 50}, geo => 'EURO' },
1252             ],
1253         ],
1254     );
1255
1256 That would yield:
1257
1258     WHERE ( user = ? AND (
1259                ( workhrs > ? AND geo = ? )
1260             OR ( workhrs < ? OR geo = ? )
1261           ) )
1262
1263 =head3 Algebraic inconsistency, for historical reasons
1264
1265 C<Important note>: when connecting several conditions, the C<-and->|C<-or>
1266 operator goes C<outside> of the nested structure; whereas when connecting
1267 several constraints on one column, the C<-and> operator goes
1268 C<inside> the arrayref. Here is an example combining both features :
1269
1270    my @where = (
1271      -and => [a => 1, b => 2],
1272      -or  => [c => 3, d => 4],
1273       e   => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
1274    )
1275
1276 yielding
1277
1278   WHERE ( (    ( a = ? AND b = ? )
1279             OR ( c = ? OR d = ? )
1280             OR ( e LIKE ? AND e LIKE ? ) ) )
1281
1282 This difference in syntax is unfortunate but must be preserved for
1283 historical reasons. So be careful : the two examples below would
1284 seem algebraically equivalent, but they are not
1285
1286   {col => [-and => {-like => 'foo%'}, {-like => '%bar'}]}
1287   # yields : WHERE ( ( col LIKE ? AND col LIKE ? ) )
1288
1289   [-and => {col => {-like => 'foo%'}, {col => {-like => '%bar'}}]]
1290   # yields : WHERE ( ( col LIKE ? OR col LIKE ? ) )
1291
1292
1293 =head2 Literal SQL and value type operators
1294
1295 The basic premise of SQL::Abstract is that in WHERE specifications the "left
1296 side" is a column name and the "right side" is a value (normally rendered as
1297 a placeholder). This holds true for both hashrefs and arrayref pairs as you
1298 see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to
1299 alter this behavior. There are several ways of doing so.
1300
1301 =head3 -ident
1302
1303 This is a virtual operator that signals the string to its right side is an
1304 identifier (a column name) and not a value. For example to compare two
1305 columns you would write:
1306
1307     my %where = (
1308         priority => { '<', 2 },
1309         requestor => { -ident => 'submitter' },
1310     );
1311
1312 which creates:
1313
1314     $stmt = "WHERE priority < ? AND requestor = submitter";
1315     @bind = ('2');
1316
1317 If you are maintaining legacy code you may see a different construct as
1318 described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new
1319 code.
1320
1321 =head3 -value
1322
1323 This is a virtual operator that signals that the construct to its right side
1324 is a value to be passed to DBI. This is for example necessary when you want
1325 to write a where clause against an array (for RDBMS that support such
1326 datatypes). For example:
1327
1328     my %where = (
1329         array => { -value => [1, 2, 3] }
1330     );
1331
1332 will result in:
1333
1334     $stmt = 'WHERE array = ?';
1335     @bind = ([1, 2, 3]);
1336
1337 Note that if you were to simply say:
1338
1339     my %where = (
1340         array => [1, 2, 3]
1341     );
1342
1343 the result would porbably be not what you wanted:
1344
1345     $stmt = 'WHERE array = ? OR array = ? OR array = ?';
1346     @bind = (1, 2, 3);
1347
1348 =head3 Literal SQL
1349
1350 Finally, sometimes only literal SQL will do. To include a random snippet
1351 of SQL verbatim, you specify it as a scalar reference. Consider this only
1352 as a last resort. Usually there is a better way. For example:
1353
1354     my %where = (
1355         priority => { '<', 2 },
1356         requestor => { -in => \'(SELECT name FROM hitmen)' },
1357     );
1358
1359 Would create:
1360
1361     $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)"
1362     @bind = (2);
1363
1364 Note that in this example, you only get one bind parameter back, since
1365 the verbatim SQL is passed as part of the statement.
1366
1367 =head4 CAVEAT
1368
1369   Never use untrusted input as a literal SQL argument - this is a massive
1370   security risk (there is no way to check literal snippets for SQL
1371   injections and other nastyness). If you need to deal with untrusted input
1372   use literal SQL with placeholders as described next.
1373
1374 =head3 Literal SQL with placeholders and bind values (subqueries)
1375
1376 If the literal SQL to be inserted has placeholders and bind values,
1377 use a reference to an arrayref (yes this is a double reference --
1378 not so common, but perfectly legal Perl). For example, to find a date
1379 in Postgres you can use something like this:
1380
1381     my %where = (
1382        date_column => \[q/= date '2008-09-30' - ?::integer/, 10/]
1383     )
1384
1385 This would create:
1386
1387     $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
1388     @bind = ('10');
1389
1390 Note that you must pass the bind values in the same format as they are returned
1391 by L</where>. That means that if you set L</bindtype> to C<columns>, you must
1392 provide the bind values in the C<< [ column_meta => value ] >> format, where
1393 C<column_meta> is an opaque scalar value; most commonly the column name, but
1394 you can use any scalar value (including references and blessed references),
1395 L<SQL::Abstract> will simply pass it through intact. So if C<bindtype> is set
1396 to C<columns> the above example will look like:
1397
1398     my %where = (
1399        date_column => \[q/= date '2008-09-30' - ?::integer/, [ dummy => 10 ]/]
1400     )
1401
1402 Literal SQL is especially useful for nesting parenthesized clauses in the
1403 main SQL query. Here is a first example :
1404
1405   my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
1406                                100, "foo%");
1407   my %where = (
1408     foo => 1234,
1409     bar => \["IN ($sub_stmt)" => @sub_bind],
1410   );
1411
1412 This yields :
1413
1414   $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
1415                                              WHERE c2 < ? AND c3 LIKE ?))";
1416   @bind = (1234, 100, "foo%");
1417
1418 Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
1419 are expressed in the same way. Of course the C<$sub_stmt> and
1420 its associated bind values can be generated through a former call
1421 to C<select()> :
1422
1423   my ($sub_stmt, @sub_bind)
1424      = $sql->select("t1", "c1", {c2 => {"<" => 100},
1425                                  c3 => {-like => "foo%"}});
1426   my %where = (
1427     foo => 1234,
1428     bar => \["> ALL ($sub_stmt)" => @sub_bind],
1429   );
1430
1431 In the examples above, the subquery was used as an operator on a column;
1432 but the same principle also applies for a clause within the main C<%where>
1433 hash, like an EXISTS subquery :
1434
1435   my ($sub_stmt, @sub_bind)
1436      = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
1437   my %where = ( -and => [
1438     foo   => 1234,
1439     \["EXISTS ($sub_stmt)" => @sub_bind],
1440   ]);
1441
1442 which yields
1443
1444   $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
1445                                         WHERE c1 = ? AND c2 > t0.c0))";
1446   @bind = (1234, 1);
1447
1448
1449 Observe that the condition on C<c2> in the subquery refers to
1450 column C<t0.c0> of the main query : this is I<not> a bind
1451 value, so we have to express it through a scalar ref.
1452 Writing C<< c2 => {">" => "t0.c0"} >> would have generated
1453 C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
1454 what we wanted here.
1455
1456 Finally, here is an example where a subquery is used
1457 for expressing unary negation:
1458
1459   my ($sub_stmt, @sub_bind)
1460      = $sql->where({age => [{"<" => 10}, {">" => 20}]});
1461   $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
1462   my %where = (
1463         lname  => {like => '%son%'},
1464         \["NOT ($sub_stmt)" => @sub_bind],
1465     );
1466
1467 This yields
1468
1469   $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
1470   @bind = ('%son%', 10, 20)
1471
1472 =head3 Deprecated usage of Literal SQL
1473
1474 Below are some examples of archaic use of literal SQL. It is shown only as
1475 reference for those who deal with legacy code. Each example has a much
1476 better, cleaner and safer alternative that users should opt for in new code.
1477
1478 =over
1479
1480 =item *
1481
1482     my %where = ( requestor => \'IS NOT NULL' )
1483
1484     $stmt = "WHERE requestor IS NOT NULL"
1485
1486 This used to be the way of generating NULL comparisons, before the handling
1487 of C<undef> got formalized. For new code please use the superior syntax as
1488 described in L</Tests for NULL values>.
1489
1490 =item *
1491
1492     my %where = ( requestor => \'= submitter' )
1493
1494     $stmt = "WHERE requestor = submitter"
1495
1496 This used to be the only way to compare columns. Use the superior L</-ident>
1497 method for all new code. For example an identifier declared in such a way
1498 will be properly quoted if L</quote_char> is properly set, while the legacy
1499 form will remain as supplied.
1500
1501 =item *
1502
1503     my %where = ( is_ready  => \"", completed => { '>', '2012-12-21' } )
1504
1505     $stmt = "WHERE completed > ? AND is_ready"
1506     @bind = ('2012-12-21')
1507
1508 Using an empty string literal used to be the only way to express a boolean.
1509 For all new code please use the much more readable
1510 L<-bool|/Unary operators: bool> operator.
1511
1512 =back
1513
1514 =head2 Conclusion
1515
1516 These pages could go on for a while, since the nesting of the data
1517 structures this module can handle are pretty much unlimited (the
1518 module implements the C<WHERE> expansion as a recursive function
1519 internally). Your best bet is to "play around" with the module a
1520 little to see how the data structures behave, and choose the best
1521 format for your data based on that.
1522
1523 And of course, all the values above will probably be replaced with
1524 variables gotten from forms or the command line. After all, if you
1525 knew everything ahead of time, you wouldn't have to worry about
1526 dynamically-generating SQL and could just hardwire it into your
1527 script.
1528
1529 =head1 ORDER BY CLAUSES
1530
1531 Some functions take an order by clause. This can either be a scalar (just a
1532 column name,) a hash of C<< { -desc => 'col' } >> or C<< { -asc => 'col' } >>,
1533 or an array of either of the two previous forms. Examples:
1534
1535                Given            |         Will Generate
1536     ----------------------------------------------------------
1537                                 |
1538     \'colA DESC'                | ORDER BY colA DESC
1539                                 |
1540     'colA'                      | ORDER BY colA
1541                                 |
1542     [qw/colA colB/]             | ORDER BY colA, colB
1543                                 |
1544     {-asc  => 'colA'}           | ORDER BY colA ASC
1545                                 |
1546     {-desc => 'colB'}           | ORDER BY colB DESC
1547                                 |
1548     ['colA', {-asc => 'colB'}]  | ORDER BY colA, colB ASC
1549                                 |
1550     { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC
1551                                 |
1552     [                           |
1553       { -asc => 'colA' },       | ORDER BY colA ASC, colB DESC,
1554       { -desc => [qw/colB/],    |          colC ASC, colD ASC
1555       { -asc => [qw/colC colD/],|
1556     ]                           |
1557     ===========================================================
1558
1559
1560
1561 =head1 SPECIAL OPERATORS
1562
1563   my $sqlmaker = SQL::Abstract->new(special_ops => [
1564      {
1565       regex => qr/.../,
1566       handler => sub {
1567         my ($self, $field, $op, $arg) = @_;
1568         ...
1569       },
1570      },
1571      {
1572       regex => qr/.../,
1573       handler => 'method_name',
1574      },
1575    ]);
1576
1577 A "special operator" is a SQL syntactic clause that can be
1578 applied to a field, instead of a usual binary operator.
1579 For example :
1580
1581    WHERE field IN (?, ?, ?)
1582    WHERE field BETWEEN ? AND ?
1583    WHERE MATCH(field) AGAINST (?, ?)
1584
1585 Special operators IN and BETWEEN are fairly standard and therefore
1586 are builtin within C<SQL::Abstract> (as the overridable methods
1587 C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
1588 like the MATCH .. AGAINST example above which is specific to MySQL,
1589 you can write your own operator handlers - supply a C<special_ops>
1590 argument to the C<new> method. That argument takes an arrayref of
1591 operator definitions; each operator definition is a hashref with two
1592 entries:
1593
1594 =over
1595
1596 =item regex
1597
1598 the regular expression to match the operator
1599
1600 =item handler
1601
1602 Either a coderef or a plain scalar method name. In both cases
1603 the expected return is C<< ($sql, @bind) >>.
1604
1605 When supplied with a method name, it is simply called on the
1606 L<SQL::Abstract/> object as:
1607
1608  $self->$method_name ($field, $op, $arg)
1609
1610  Where:
1611
1612   $op is the part that matched the handler regex
1613   $field is the LHS of the operator
1614   $arg is the RHS
1615
1616 When supplied with a coderef, it is called as:
1617
1618  $coderef->($self, $field, $op, $arg)
1619
1620
1621 =back
1622
1623 For example, here is an implementation
1624 of the MATCH .. AGAINST syntax for MySQL
1625
1626   my $sqlmaker = SQL::Abstract->new(special_ops => [
1627
1628     # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
1629     {regex => qr/^match$/i,
1630      handler => sub {
1631        my ($self, $field, $op, $arg) = @_;
1632        $arg = [$arg] if not ref $arg;
1633        my $label         = $self->_quote($field);
1634        my ($placeholder) = $self->_convert('?');
1635        my $placeholders  = join ", ", (($placeholder) x @$arg);
1636        my $sql           = $self->_sqlcase('match') . " ($label) "
1637                          . $self->_sqlcase('against') . " ($placeholders) ";
1638        my @bind = $self->_bindtype($field, @$arg);
1639        return ($sql, @bind);
1640        }
1641      },
1642
1643   ]);
1644
1645
1646 =head1 UNARY OPERATORS
1647
1648   my $sqlmaker = SQL::Abstract->new(unary_ops => [
1649      {
1650       regex => qr/.../,
1651       handler => sub {
1652         my ($self, $op, $arg) = @_;
1653         ...
1654       },
1655      },
1656      {
1657       regex => qr/.../,
1658       handler => 'method_name',
1659      },
1660    ]);
1661
1662 A "unary operator" is a SQL syntactic clause that can be
1663 applied to a field - the operator goes before the field
1664
1665 You can write your own operator handlers - supply a C<unary_ops>
1666 argument to the C<new> method. That argument takes an arrayref of
1667 operator definitions; each operator definition is a hashref with two
1668 entries:
1669
1670 =over
1671
1672 =item regex
1673
1674 the regular expression to match the operator
1675
1676 =item handler
1677
1678 Either a coderef or a plain scalar method name. In both cases
1679 the expected return is C<< $sql >>.
1680
1681 When supplied with a method name, it is simply called on the
1682 L<SQL::Abstract/> object as:
1683
1684  $self->$method_name ($op, $arg)
1685
1686  Where:
1687
1688   $op is the part that matched the handler regex
1689   $arg is the RHS or argument of the operator
1690
1691 When supplied with a coderef, it is called as:
1692
1693  $coderef->($self, $op, $arg)
1694
1695
1696 =back
1697
1698
1699 =head1 PERFORMANCE
1700
1701 Thanks to some benchmarking by Mark Stosberg, it turns out that
1702 this module is many orders of magnitude faster than using C<DBIx::Abstract>.
1703 I must admit this wasn't an intentional design issue, but it's a
1704 byproduct of the fact that you get to control your C<DBI> handles
1705 yourself.
1706
1707 To maximize performance, use a code snippet like the following:
1708
1709     # prepare a statement handle using the first row
1710     # and then reuse it for the rest of the rows
1711     my($sth, $stmt);
1712     for my $href (@array_of_hashrefs) {
1713         $stmt ||= $sql->insert('table', $href);
1714         $sth  ||= $dbh->prepare($stmt);
1715         $sth->execute($sql->values($href));
1716     }
1717
1718 The reason this works is because the keys in your C<$href> are sorted
1719 internally by B<SQL::Abstract>. Thus, as long as your data retains
1720 the same structure, you only have to generate the SQL the first time
1721 around. On subsequent queries, simply use the C<values> function provided
1722 by this module to return your values in the correct order.
1723
1724 However this depends on the values having the same type - if, for
1725 example, the values of a where clause may either have values
1726 (resulting in sql of the form C<column = ?> with a single bind
1727 value), or alternatively the values might be C<undef> (resulting in
1728 sql of the form C<column IS NULL> with no bind value) then the
1729 caching technique suggested will not work.
1730
1731 =head1 FORMBUILDER
1732
1733 If you use my C<CGI::FormBuilder> module at all, you'll hopefully
1734 really like this part (I do, at least). Building up a complex query
1735 can be as simple as the following:
1736
1737     #!/usr/bin/perl
1738
1739     use CGI::FormBuilder;
1740     use SQL::Abstract;
1741
1742     my $form = CGI::FormBuilder->new(...);
1743     my $sql  = SQL::Abstract->new;
1744
1745     if ($form->submitted) {
1746         my $field = $form->field;
1747         my $id = delete $field->{id};
1748         my($stmt, @bind) = $sql->update('table', $field, {id => $id});
1749     }
1750
1751 Of course, you would still have to connect using C<DBI> to run the
1752 query, but the point is that if you make your form look like your
1753 table, the actual query script can be extremely simplistic.
1754
1755 If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
1756 a fast interface to returning and formatting data. I frequently
1757 use these three modules together to write complex database query
1758 apps in under 50 lines.
1759
1760 =head1 REPO
1761
1762 =over
1763
1764 =item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git>
1765
1766 =item * git: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git>
1767
1768 =back
1769
1770 =head1 CHANGES
1771
1772 Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
1773 Great care has been taken to preserve the I<published> behavior
1774 documented in previous versions in the 1.* family; however,
1775 some features that were previously undocumented, or behaved
1776 differently from the documentation, had to be changed in order
1777 to clarify the semantics. Hence, client code that was relying
1778 on some dark areas of C<SQL::Abstract> v1.*
1779 B<might behave differently> in v1.50.
1780
1781 The main changes are :
1782
1783 =over
1784
1785 =item *
1786
1787 support for literal SQL through the C<< \ [$sql, bind] >> syntax.
1788
1789 =item *
1790
1791 support for the { operator => \"..." } construct (to embed literal SQL)
1792
1793 =item *
1794
1795 support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
1796
1797 =item *
1798
1799 optional support for L<array datatypes|/"Inserting and Updating Arrays">
1800
1801 =item *
1802
1803 defensive programming : check arguments
1804
1805 =item *
1806
1807 fixed bug with global logic, which was previously implemented
1808 through global variables yielding side-effects. Prior versions would
1809 interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
1810 as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
1811 Now this is interpreted
1812 as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
1813
1814
1815 =item *
1816
1817 fixed semantics of  _bindtype on array args
1818
1819 =item *
1820
1821 dropped the C<_anoncopy> of the %where tree. No longer necessary,
1822 we just avoid shifting arrays within that tree.
1823
1824 =item *
1825
1826 dropped the C<_modlogic> function
1827
1828 =back
1829
1830 =head1 ACKNOWLEDGEMENTS
1831
1832 There are a number of individuals that have really helped out with
1833 this module. Unfortunately, most of them submitted bugs via CPAN
1834 so I have no idea who they are! But the people I do know are:
1835
1836     Ash Berlin (order_by hash term support)
1837     Matt Trout (DBIx::Class support)
1838     Mark Stosberg (benchmarking)
1839     Chas Owens (initial "IN" operator support)
1840     Philip Collins (per-field SQL functions)
1841     Eric Kolve (hashref "AND" support)
1842     Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
1843     Dan Kubb (support for "quote_char" and "name_sep")
1844     Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
1845     Laurent Dami (internal refactoring, extensible list of special operators, literal SQL)
1846     Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
1847     Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
1848     Oliver Charles (support for "RETURNING" after "INSERT")
1849
1850 Thanks!
1851
1852 =head1 SEE ALSO
1853
1854 L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
1855
1856 =head1 AUTHOR
1857
1858 Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
1859
1860 This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
1861
1862 For support, your best bet is to try the C<DBIx::Class> users mailing list.
1863 While not an official support venue, C<DBIx::Class> makes heavy use of
1864 C<SQL::Abstract>, and as such list members there are very familiar with
1865 how to create queries.
1866
1867 =head1 LICENSE
1868
1869 This module is free software; you may copy this under the same
1870 terms as perl itself (either the GNU General Public License or
1871 the Artistic License)
1872
1873 =cut
1874