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