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