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