nuke old Data-Query symlink
[scpubgit/Q-Branch.git] / lib / SQL / Abstract.pm
CommitLineData
96449e8e 1package SQL::Abstract; # see doc at end of file
2
96449e8e 3use strict;
4use warnings;
9d9d5bd6 5use Carp ();
312d830b 6use List::Util ();
7use Scalar::Util ();
96449e8e 8
0da0fe34 9use Exporter 'import';
10our @EXPORT_OK = qw(is_plain_value is_literal_value);
11
12BEGIN {
13 if ($] < 5.009_005) {
14 require MRO::Compat;
15 }
16 else {
17 require mro;
18 }
843a94b5 19
20 *SQL::Abstract::_ENV_::DETECT_AUTOGENERATED_STRINGIFICATION = $ENV{SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION}
21 ? sub () { 0 }
22 : sub () { 1 }
23 ;
0da0fe34 24}
25
96449e8e 26#======================================================================
27# GLOBALS
28#======================================================================
29
dc6afcf8 30our $VERSION = '1.86';
7479e27e 31
22f1a437 32# This would confuse some packagers
c520207b 33$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
96449e8e 34
35our $AUTOLOAD;
36
37# special operators (-in, -between). May be extended/overridden by user.
38# See section WHERE: BUILTIN SPECIAL OPERATORS below for implementation
39my @BUILTIN_SPECIAL_OPS = (
f663e672 40 {regex => qr/^ (?: not \s )? between $/ix, handler => sub { die "NOPE" }},
41 {regex => qr/^ (?: not \s )? in $/ix, handler => sub { die "NOPE" }},
87c8e45b 42 {regex => qr/^ is (?: \s+ not )? $/ix, handler => sub { die "NOPE" }},
96449e8e 43);
44
45#======================================================================
46# DEBUGGING AND ERROR REPORTING
47#======================================================================
48
49sub _debug {
50 return unless $_[0]->{debug}; shift; # a little faster
51 my $func = (caller(1))[3];
52 warn "[$func] ", @_, "\n";
53}
54
55sub belch (@) {
56 my($func) = (caller(1))[3];
9d9d5bd6 57 Carp::carp "[$func] Warning: ", @_;
96449e8e 58}
59
60sub puke (@) {
61 my($func) = (caller(1))[3];
9d9d5bd6 62 Carp::croak "[$func] Fatal: ", @_;
96449e8e 63}
64
0da0fe34 65sub is_literal_value ($) {
66 ref $_[0] eq 'SCALAR' ? [ ${$_[0]} ]
67 : ( ref $_[0] eq 'REF' and ref ${$_[0]} eq 'ARRAY' ) ? [ @${ $_[0] } ]
0da0fe34 68 : undef;
69}
70
71# FIXME XSify - this can be done so much more efficiently
72sub is_plain_value ($) {
73 no strict 'refs';
966200cc 74 ! length ref $_[0] ? \($_[0])
0da0fe34 75 : (
76 ref $_[0] eq 'HASH' and keys %{$_[0]} == 1
77 and
78 exists $_[0]->{-value}
966200cc 79 ) ? \($_[0]->{-value})
0da0fe34 80 : (
a1c9e0ff 81 # reuse @_ for even moar speedz
82 defined ( $_[1] = Scalar::Util::blessed $_[0] )
0da0fe34 83 and
84 # deliberately not using Devel::OverloadInfo - the checks we are
85 # intersted in are much more limited than the fullblown thing, and
86 # this is a very hot piece of code
87 (
e8d729d4 88 # simply using ->can('(""') can leave behind stub methods that
89 # break actually using the overload later (see L<perldiag/Stub
90 # found while resolving method "%s" overloading "%s" in package
91 # "%s"> and the source of overload::mycan())
44e54b41 92 #
0da0fe34 93 # either has stringification which DBI SHOULD prefer out of the box
a1c9e0ff 94 grep { *{ (qq[${_}::(""]) }{CODE} } @{ $_[2] = mro::get_linear_isa( $_[1] ) }
0da0fe34 95 or
20e178a8 96 # has nummification or boolification, AND fallback is *not* disabled
0da0fe34 97 (
843a94b5 98 SQL::Abstract::_ENV_::DETECT_AUTOGENERATED_STRINGIFICATION
99 and
20e178a8 100 (
101 grep { *{"${_}::(0+"}{CODE} } @{$_[2]}
102 or
103 grep { *{"${_}::(bool"}{CODE} } @{$_[2]}
104 )
0da0fe34 105 and
106 (
107 # no fallback specified at all
a1c9e0ff 108 ! ( ($_[3]) = grep { *{"${_}::()"}{CODE} } @{$_[2]} )
0da0fe34 109 or
110 # fallback explicitly undef
a1c9e0ff 111 ! defined ${"$_[3]::()"}
0da0fe34 112 or
113 # explicitly true
a1c9e0ff 114 !! ${"$_[3]::()"}
0da0fe34 115 )
116 )
117 )
966200cc 118 ) ? \($_[0])
0da0fe34 119 : undef;
120}
121
122
96449e8e 123
124#======================================================================
125# NEW
126#======================================================================
127
128sub new {
129 my $self = shift;
130 my $class = ref($self) || $self;
131 my %opt = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
132
133 # choose our case by keeping an option around
134 delete $opt{case} if $opt{case} && $opt{case} ne 'lower';
135
136 # default logic for interpreting arrayrefs
ef559da3 137 $opt{logic} = $opt{logic} ? uc $opt{logic} : 'OR';
96449e8e 138
139 # how to return bind vars
96449e8e 140 $opt{bindtype} ||= 'normal';
141
142 # default comparison is "=", but can be overridden
143 $opt{cmp} ||= '=';
144
3af02ccb 145 # try to recognize which are the 'equality' and 'inequality' ops
3cdadcbe 146 # (temporary quickfix (in 2007), should go through a more seasoned API)
147 $opt{equality_op} = qr/^( \Q$opt{cmp}\E | \= )$/ix;
148 $opt{inequality_op} = qr/^( != | <> )$/ix;
149
150 $opt{like_op} = qr/^ (is\s+)? r?like $/xi;
151 $opt{not_like_op} = qr/^ (is\s+)? not \s+ r?like $/xi;
96449e8e 152
153 # SQL booleans
154 $opt{sqltrue} ||= '1=1';
155 $opt{sqlfalse} ||= '0=1';
156
9d48860e 157 # special operators
30af97c5 158 $opt{special_ops} ||= [];
159
b6251592 160 # regexes are applied in order, thus push after user-defines
96449e8e 161 push @{$opt{special_ops}}, @BUILTIN_SPECIAL_OPS;
162
cf06e9dc 163 if ($class->isa('DBIx::Class::SQLMaker')) {
dd1d0dbf 164 push @{$opt{special_ops}}, our $DBIC_Compat_Op ||= {
165 regex => qr/^(?:ident|value)$/i, handler => sub { die "NOPE" }
166 };
cf06e9dc 167 $opt{is_dbic_sqlmaker} = 1;
dd1d0dbf 168 }
169
9d48860e 170 # unary operators
59f23b3d 171 $opt{unary_ops} ||= [];
59f23b3d 172
3af02ccb 173 # rudimentary sanity-check for user supplied bits treated as functions/operators
b6251592 174 # If a purported function matches this regular expression, an exception is thrown.
175 # Literal SQL is *NOT* subject to this check, only functions (and column names
176 # when quoting is not in effect)
96449e8e 177
b6251592 178 # FIXME
179 # need to guard against ()'s in column names too, but this will break tons of
180 # hacks... ideas anyone?
181 $opt{injection_guard} ||= qr/
182 \;
183 |
184 ^ \s* go \s
185 /xmi;
96449e8e 186
ec19b759 187 $opt{expand_unary} = {};
188
767b62e4 189 $opt{expand} = {
d1aabfe4 190 -ident => '_expand_ident',
71f1364b 191 -value => '_expand_value',
2a238661 192 -not => '_expand_not',
05d37f9c 193 -bool => '_expand_bool',
70f98e4b 194 -and => '_expand_andor',
195 -or => '_expand_andor',
5c633220 196 -nest => '_expand_nest',
767b62e4 197 };
198
143f50df 199 $opt{expand_op} = {
200 'between' => '_expand_between',
201 'not between' => '_expand_between',
416026a9 202 'in' => '_expand_in',
203 'not in' => '_expand_in',
2f57f4f0 204 (map +($_ => sub {
0eca8cb9 205 my ($self, $op, $arg, $k) = @_;
206 return +{ -op => [
207 $self->{cmp},
208 $self->_expand_ident(-ident => $k),
209 $self->_expand_expr({ '-'.$op => $arg }),
210 ] };
2f57f4f0 211 }), qw(ident value)),
5c633220 212 'nest' => '_expand_nest',
143f50df 213 };
ca158918 214
215 $opt{render} = {
216 (map +("-$_", "_render_$_"), qw(op func bind ident literal list)),
217 %{$opt{render}||{}}
218 };
219
143f50df 220 $opt{render_op} = our $RENDER_OP;
221
b6251592 222 return bless \%opt, $class;
223}
96449e8e 224
e175845b 225sub sqltrue { +{ -literal => [ $_[0]->{sqltrue} ] } }
226sub sqlfalse { +{ -literal => [ $_[0]->{sqlfalse} ] } }
170e6c33 227
228sub _assert_pass_injection_guard {
229 if ($_[1] =~ $_[0]->{injection_guard}) {
230 my $class = ref $_[0];
231 puke "Possible SQL injection attempt '$_[1]'. If this is indeed a part of the "
232 . "desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply your own "
233 . "{injection_guard} attribute to ${class}->new()"
234 }
235}
236
237
96449e8e 238#======================================================================
239# INSERT methods
240#======================================================================
241
242sub insert {
02288357 243 my $self = shift;
244 my $table = $self->_table(shift);
245 my $data = shift || return;
246 my $options = shift;
96449e8e 247
248 my $method = $self->_METHOD_FOR_refkind("_insert", $data);
02288357 249 my ($sql, @bind) = $self->$method($data);
96449e8e 250 $sql = join " ", $self->_sqlcase('insert into'), $table, $sql;
02288357 251
e82e648a 252 if ($options->{returning}) {
ca4f826a 253 my ($s, @b) = $self->_insert_returning($options);
e82e648a 254 $sql .= $s;
255 push @bind, @b;
02288357 256 }
257
96449e8e 258 return wantarray ? ($sql, @bind) : $sql;
259}
260
60f3fd3f 261# So that subclasses can override INSERT ... RETURNING separately from
262# UPDATE and DELETE (e.g. DBIx::Class::SQLMaker::Oracle does this)
b17a3ece 263sub _insert_returning { shift->_returning(@_) }
264
95904db5 265sub _returning {
e82e648a 266 my ($self, $options) = @_;
6b1fe79d 267
e82e648a 268 my $f = $options->{returning};
269
79d310f2 270 my ($sql, @bind) = $self->render_aqt(
ff96fdd4 271 $self->_expand_maybe_list_expr($f, undef, -ident)
272 );
273 return wantarray
274 ? $self->_sqlcase(' returning ') . $sql
275 : ($self->_sqlcase(' returning ').$sql, @bind);
6b1fe79d 276}
277
96449e8e 278sub _insert_HASHREF { # explicit list of fields and then values
279 my ($self, $data) = @_;
280
281 my @fields = sort keys %$data;
282
fe3ae272 283 my ($sql, @bind) = $self->_insert_values($data);
96449e8e 284
285 # assemble SQL
286 $_ = $self->_quote($_) foreach @fields;
287 $sql = "( ".join(", ", @fields).") ".$sql;
288
289 return ($sql, @bind);
290}
291
292sub _insert_ARRAYREF { # just generate values(?,?) part (no list of fields)
293 my ($self, $data) = @_;
294
295 # no names (arrayref) so can't generate bindtype
296 $self->{bindtype} ne 'columns'
297 or belch "can't do 'columns' bindtype when called with arrayref";
298
19b6ccce 299 my (@values, @all_bind);
300 foreach my $value (@$data) {
301 my ($values, @bind) = $self->_insert_value(undef, $value);
302 push @values, $values;
303 push @all_bind, @bind;
304 }
305 my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
306 return ($sql, @all_bind);
fe3ae272 307}
308
309sub _insert_ARRAYREFREF { # literal SQL with bind
310 my ($self, $data) = @_;
311
312 my ($sql, @bind) = @${$data};
313 $self->_assert_bindval_matches_bindtype(@bind);
314
315 return ($sql, @bind);
316}
317
318
319sub _insert_SCALARREF { # literal SQL without bind
320 my ($self, $data) = @_;
321
322 return ($$data);
323}
324
325sub _insert_values {
326 my ($self, $data) = @_;
327
96449e8e 328 my (@values, @all_bind);
fe3ae272 329 foreach my $column (sort keys %$data) {
19b6ccce 330 my ($values, @bind) = $self->_insert_value($column, $data->{$column});
331 push @values, $values;
332 push @all_bind, @bind;
333 }
334 my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
335 return ($sql, @all_bind);
336}
96449e8e 337
19b6ccce 338sub _insert_value {
339 my ($self, $column, $v) = @_;
96449e8e 340
79d310f2 341 return $self->render_aqt(
720ca4f7 342 $self->_expand_insert_value($column, $v)
343 );
344}
96449e8e 345
720ca4f7 346sub _expand_insert_value {
347 my ($self, $column, $v) = @_;
96449e8e 348
720ca4f7 349 if (ref($v) eq 'ARRAY') {
350 if ($self->{array_datatypes}) {
351 return +{ -bind => [ $column, $v ] };
352 }
353 my ($sql, @bind) = @$v;
354 $self->_assert_bindval_matches_bindtype(@bind);
355 return +{ -literal => $v };
356 }
357 if (ref($v) eq 'HASH') {
358 if (grep !/^-/, keys %$v) {
359 belch "HASH ref as bind value in insert is not supported";
360 return +{ -bind => [ $column, $v ] };
361 }
362 }
363 if (!defined($v)) {
364 return +{ -bind => [ $column, undef ] };
365 }
366 local our $Cur_Col_Meta = $column;
79d310f2 367 return $self->expand_expr($v);
96449e8e 368}
369
370
96449e8e 371
372#======================================================================
373# UPDATE methods
374#======================================================================
375
376
377sub update {
95904db5 378 my $self = shift;
379 my $table = $self->_table(shift);
380 my $data = shift || return;
381 my $where = shift;
382 my $options = shift;
96449e8e 383
384 # first build the 'SET' part of the sql statement
96449e8e 385 puke "Unsupported data type specified to \$sql->update"
386 unless ref $data eq 'HASH';
387
9ade906e 388 my ($sql, @all_bind) = $self->_update_set_values($data);
a9e94508 389 $sql = $self->_sqlcase('update ') . $table . $self->_sqlcase(' set ')
9ade906e 390 . $sql;
391
392 if ($where) {
393 my($where_sql, @where_bind) = $self->where($where);
394 $sql .= $where_sql;
395 push @all_bind, @where_bind;
396 }
397
398 if ($options->{returning}) {
399 my ($returning_sql, @returning_bind) = $self->_update_returning($options);
400 $sql .= $returning_sql;
401 push @all_bind, @returning_bind;
402 }
403
404 return wantarray ? ($sql, @all_bind) : $sql;
405}
406
407sub _update_set_values {
408 my ($self, $data) = @_;
409
79d310f2 410 return $self->render_aqt(
89690da2 411 $self->_expand_update_set_values($data),
412 );
413}
96449e8e 414
89690da2 415sub _expand_update_set_values {
416 my ($self, $data) = @_;
417 $self->_expand_maybe_list_expr( [
418 map {
419 my ($k, $set) = @$_;
c4ed66f4 420 $set = { -bind => $_ } unless defined $set;
9103bd14 421 +{ -op => [ '=', $self->_expand_ident(-ident => $k), $set ] };
89690da2 422 }
423 map {
424 my $k = $_;
425 my $v = $data->{$k};
426 (ref($v) eq 'ARRAY'
427 ? ($self->{array_datatypes}
428 ? [ $k, +{ -bind => [ $k, $v ] } ]
429 : [ $k, +{ -literal => $v } ])
430 : do {
431 local our $Cur_Col_Meta = $k;
432 [ $k, $self->_expand_expr($v) ]
433 }
434 );
435 } sort keys %$data
436 ] );
96449e8e 437}
438
60f3fd3f 439# So that subclasses can override UPDATE ... RETURNING separately from
440# INSERT and DELETE
20bb2ad5 441sub _update_returning { shift->_returning(@_) }
96449e8e 442
443
444
445#======================================================================
446# SELECT
447#======================================================================
448
449
450sub select {
451 my $self = shift;
452 my $table = $self->_table(shift);
453 my $fields = shift || '*';
454 my $where = shift;
455 my $order = shift;
456
daa4ccdd 457 my ($fields_sql, @bind) = $self->_select_fields($fields);
96449e8e 458
daa4ccdd 459 my ($where_sql, @where_bind) = $self->where($where, $order);
460 push @bind, @where_bind;
461
462 my $sql = join(' ', $self->_sqlcase('select'), $fields_sql,
96449e8e 463 $self->_sqlcase('from'), $table)
464 . $where_sql;
465
9d48860e 466 return wantarray ? ($sql, @bind) : $sql;
96449e8e 467}
468
daa4ccdd 469sub _select_fields {
470 my ($self, $fields) = @_;
de63ce57 471 return $fields unless ref($fields);
79d310f2 472 return $self->render_aqt(
27592e2b 473 $self->_expand_maybe_list_expr($fields, undef, '-ident')
474 );
daa4ccdd 475}
476
96449e8e 477#======================================================================
478# DELETE
479#======================================================================
480
481
482sub delete {
85327cd5 483 my $self = shift;
484 my $table = $self->_table(shift);
485 my $where = shift;
486 my $options = shift;
96449e8e 487
488 my($where_sql, @bind) = $self->where($where);
a9e94508 489 my $sql = $self->_sqlcase('delete from ') . $table . $where_sql;
96449e8e 490
85327cd5 491 if ($options->{returning}) {
ca4f826a 492 my ($returning_sql, @returning_bind) = $self->_delete_returning($options);
85327cd5 493 $sql .= $returning_sql;
494 push @bind, @returning_bind;
495 }
496
9d48860e 497 return wantarray ? ($sql, @bind) : $sql;
96449e8e 498}
499
60f3fd3f 500# So that subclasses can override DELETE ... RETURNING separately from
501# INSERT and UPDATE
85327cd5 502sub _delete_returning { shift->_returning(@_) }
503
504
96449e8e 505
506#======================================================================
507# WHERE: entry point
508#======================================================================
509
510
511
512# Finally, a separate routine just to handle WHERE clauses
513sub where {
514 my ($self, $where, $order) = @_;
515
7ad12721 516 local $self->{convert_where} = $self->{convert};
517
96449e8e 518 # where ?
e175845b 519 my ($sql, @bind) = defined($where)
520 ? $self->_recurse_where($where)
521 : (undef);
417dd15e 522 $sql = (defined $sql and length $sql) ? $self->_sqlcase(' where ') . "( $sql )" : '';
96449e8e 523
524 # order by?
525 if ($order) {
26fe4d30 526 my ($order_sql, @order_bind) = $self->_order_by($order);
527 $sql .= $order_sql;
528 push @bind, @order_bind;
96449e8e 529 }
530
9d48860e 531 return wantarray ? ($sql, @bind) : $sql;
96449e8e 532}
533
79d310f2 534sub expand_expr {
54d3140f 535 my ($self, $expr, $default_scalar_to) = @_;
536 local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to;
7d93797d 537 $self->_expand_expr($expr);
79d310f2 538}
539
540sub render_aqt {
541 my ($self, $aqt) = @_;
542 my ($k, $v, @rest) = %$aqt;
543 die "No" if @rest;
21225d32 544 if (my $meth = $self->{render}{$k}) {
79d310f2 545 return $self->$meth($v);
546 }
547 die "notreached: $k";
548}
549
6082a464 550sub render_expr {
551 my ($self, $expr) = @_;
552 $self->render_aqt($self->expand_expr($expr));
553}
554
a2cd381d 555sub _expand_expr {
c7aeb9f5 556 my ($self, $expr, $logic) = @_;
3ae10d16 557 our $Expand_Depth ||= 0; local $Expand_Depth = $Expand_Depth + 1;
252518da 558 return undef unless defined($expr);
59588695 559 if (ref($expr) eq 'HASH') {
767b62e4 560 return undef unless my $kc = keys %$expr;
561 if ($kc > 1) {
59588695 562 $logic ||= 'and';
70f98e4b 563 return $self->_expand_andor("-${logic}", $expr);
59588695 564 }
767b62e4 565 my ($key, $value) = %$expr;
70f98e4b 566 if ($key =~ /^-/ and $key =~ s/ [_\s]? \d+ $//x ) {
567 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
568 . "You probably wanted ...-and => [ $key => COND1, $key => COND2 ... ]";
569 }
767b62e4 570 if (my $exp = $self->{expand}{$key}) {
d1aabfe4 571 return $self->$exp($key, $value);
767b62e4 572 }
573 return $self->_expand_expr_hashpair($key, $value, $logic);
a2cd381d 574 }
08264f40 575 if (ref($expr) eq 'ARRAY') {
99a65fa8 576 my $logic = lc($logic || $self->{logic});
70f98e4b 577 return $self->_expand_andor("-${logic}", $expr);
08264f40 578 }
ca3da680 579 if (my $literal = is_literal_value($expr)) {
580 return +{ -literal => $literal };
581 }
99a65fa8 582 if (!ref($expr) or Scalar::Util::blessed($expr)) {
c7aeb9f5 583 if (my $d = our $Default_Scalar_To) {
a332e2cb 584 return $self->_expand_expr({ $d => $expr });
2558b622 585 }
24c9f930 586 return $self->_expand_value(-value => $expr);
252518da 587 }
252518da 588 die "notreached";
a2cd381d 589}
96449e8e 590
59588695 591sub _expand_expr_hashpair {
592 my ($self, $k, $v, $logic) = @_;
d13725da 593 unless (defined($k) and length($k)) {
2d64004f 594 if (defined($k) and my $literal = is_literal_value($v)) {
d13725da 595 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
2d64004f 596 return { -literal => $literal };
d13725da 597 }
598 puke "Supplying an empty left hand side argument is not supported";
599 }
1d04e75a 600 $self->_assert_pass_injection_guard($k =~ /^-(.*)$/s) if $k =~ /^-/;
ef071fad 601 if ($k =~ /^-/) {
ce3bc4b0 602 if (my ($rest) = $k =~/^-not[_ ](.*)$/) {
0c7e3af0 603 return +{ -op => [
604 'not',
767b62e4 605 $self->_expand_expr({ "-${rest}", $v }, $logic)
0c7e3af0 606 ] };
ce3bc4b0 607 }
99a65fa8 608 {
609 my $op = $k;
610 $op =~ s/^-// if length($op) > 1;
ddc44894 611
99a65fa8 612 # top level special ops are illegal in general
3ae10d16 613 # note that, arguably, if it makes no sense at top level, it also
614 # makes no sense on the other side of an = sign or similar but DBIC
615 # gets disappointingly upset if I disallow it
616 if (
617 (our $Expand_Depth) == 1
618 and List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}
619 ) {
620 puke "Illegal use of top-level '-$op'"
621 }
96a8d74a 622 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
623 return { -op => [ $op, $v ] };
624 }
99a65fa8 625 }
21225d32 626 if ($self->{render}{$k}) {
99a65fa8 627 return { $k => $v };
d13725da 628 }
711892b1 629 if (
630 ref($v) eq 'HASH'
631 and keys %$v == 1
632 and (keys %$v)[0] =~ /^-/
633 ) {
634 my ($func) = $k =~ /^-(.*)$/;
3ae10d16 635 if (List::Util::first { $func =~ $_->{regex} } @{$self->{special_ops}}) {
636 return +{ -op => [ $func, $self->_expand_expr($v) ] };
637 }
711892b1 638 return +{ -func => [ $func, $self->_expand_expr($v) ] };
639 }
640 if (!ref($v) or is_literal_value($v)) {
99a65fa8 641 return +{ -op => [ $k =~ /^-(.*)$/, $self->_expand_expr($v) ] };
59588695 642 }
99a65fa8 643 }
644 if (
645 !defined($v)
646 or (
647 ref($v) eq 'HASH'
648 and exists $v->{-value}
649 and not defined $v->{-value}
650 )
651 ) {
213209dd 652 return $self->_expand_expr({ $k => { $self->{cmp} => undef } });
99a65fa8 653 }
654 if (!ref($v) or Scalar::Util::blessed($v)) {
ec19b759 655 my $d = our $Default_Scalar_To;
99a65fa8 656 return +{
657 -op => [
658 $self->{cmp},
944f2c7b 659 $self->_expand_ident(-ident => $k),
7d93797d 660 ($d
661 ? $self->_expand_expr($d => $v)
662 : { -bind => [ $k, $v ] }
663 )
99a65fa8 664 ]
665 };
666 }
667 if (ref($v) eq 'HASH') {
668 if (keys %$v > 1) {
86997346 669 return $self->_expand_andor(-and => [
670 map +{ $k => { $_ => $v->{$_} } },
99a65fa8 671 sort keys %$v
86997346 672 ]);
99a65fa8 673 }
b18a95e4 674 return undef unless keys %$v;
99a65fa8 675 my ($vk, $vv) = %$v;
d455dcf1 676 my $op = join ' ', split '_', (map lc, $vk =~ /^-?(.*)$/)[0];
677 $self->_assert_pass_injection_guard($op);
678 if ($op =~ s/ [_\s]? \d+ $//x ) {
99a65fa8 679 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
d455dcf1 680 . "You probably wanted ...-and => [ -$op => COND1, -$op => COND2 ... ]";
99a65fa8 681 }
143f50df 682 if (my $x = $self->{expand_op}{$op}) {
2f57f4f0 683 local our $Cur_Col_Meta = $k;
143f50df 684 return $self->$x($op, $vv, $k);
99a65fa8 685 }
2f57f4f0 686 if ($op eq 'value' and not defined($vv)) {
7b3a5558 687 return $self->_expand_expr({ $k, undef }) unless defined($vv);
99a65fa8 688 }
d455dcf1 689 if ($op =~ /^is(?: not)?$/) {
690 puke "$op can only take undef as argument"
99a65fa8 691 if defined($vv)
692 and not (
693 ref($vv) eq 'HASH'
694 and exists($vv->{-value})
695 and !defined($vv->{-value})
696 );
d455dcf1 697 return +{ -op => [ $op.' null', $self->_expand_ident(-ident => $k) ] };
99a65fa8 698 }
d455dcf1 699 if ($op =~ /^(and|or)$/) {
fb7d8ea6 700 return $self->_expand_andor('-'.$op, [
701 map +{ $k, { $_ => $vv->{$_} } },
702 sort keys %$vv
703 ]);
e28d9b13 704 }
d455dcf1 705 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}) {
706 return { -op => [ $op, $self->_expand_ident(-ident => $k), $vv ] };
7dbe1183 707 }
d455dcf1 708 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
96a8d74a 709 return { -op => [
710 $self->{cmp},
de8d04fb 711 $self->_expand_ident(-ident => $k),
d455dcf1 712 { -op => [ $op, $vv ] }
96a8d74a 713 ] };
714 }
99a65fa8 715 if (ref($vv) eq 'ARRAY') {
716 my ($logic, @values) = (
717 (defined($vv->[0]) and $vv->[0] =~ /^-(and|or)$/i)
718 ? @$vv
719 : (-or => @$vv)
720 );
721 if (
d455dcf1 722 $op =~ $self->{inequality_op}
723 or $op =~ $self->{not_like_op}
99a65fa8 724 ) {
725 if (lc($logic) eq '-or' and @values > 1) {
d455dcf1 726 belch "A multi-element arrayref as an argument to the inequality op '${\uc($op)}' "
99a65fa8 727 . 'is technically equivalent to an always-true 1=1 (you probably wanted '
728 . "to say ...{ \$inequality_op => [ -and => \@values ] }... instead)"
729 ;
730 }
ef071fad 731 }
b3cb13e8 732 unless (@values) {
733 # try to DWIM on equality operators
b3cb13e8 734 return
e175845b 735 $op =~ $self->{equality_op} ? $self->sqlfalse
736 : $op =~ $self->{like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqlfalse
737 : $op =~ $self->{inequality_op} ? $self->sqltrue
738 : $op =~ $self->{not_like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqltrue
b3cb13e8 739 : puke "operator '$op' applied on an empty array (field '$k')";
740 }
5ca42385 741 return $self->_expand_andor($logic => [
742 map +{ $k => { $vk => $_ } },
99a65fa8 743 @values
5ca42385 744 ]);
99a65fa8 745 }
746 if (
747 !defined($vv)
748 or (
749 ref($vv) eq 'HASH'
750 and exists $vv->{-value}
751 and not defined $vv->{-value}
752 )
753 ) {
99a65fa8 754 my $is =
755 $op =~ /^not$/i ? 'is not' # legacy
756 : $op =~ $self->{equality_op} ? 'is'
757 : $op =~ $self->{like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is'
758 : $op =~ $self->{inequality_op} ? 'is not'
759 : $op =~ $self->{not_like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is not'
760 : puke "unexpected operator '$op' with undef operand";
de8d04fb 761 return +{ -op => [ $is.' null', $self->_expand_ident(-ident => $k) ] };
99a65fa8 762 }
763 local our $Cur_Col_Meta = $k;
764 return +{ -op => [
d455dcf1 765 $op,
766 $self->_expand_ident(-ident => $k),
767 $self->_expand_expr($vv)
99a65fa8 768 ] };
769 }
770 if (ref($v) eq 'ARRAY') {
e175845b 771 return $self->sqlfalse unless @$v;
99a65fa8 772 $self->_debug("ARRAY($k) means distribute over elements");
70f98e4b 773 my $this_logic = lc(
774 $v->[0] =~ /^-(and|or)$/i
775 ? shift(@{$v = [ @$v ]})
776 : '-'.($self->{logic} || 'or')
99a65fa8 777 );
66e362e4 778 return $self->_expand_andor(
70f98e4b 779 $this_logic => [ map +{ $k => $_ }, @$v ]
66e362e4 780 );
99a65fa8 781 }
782 if (my $literal = is_literal_value($v)) {
783 unless (length $k) {
784 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
785 return \$literal;
786 }
787 my ($sql, @bind) = @$literal;
788 if ($self->{bindtype} eq 'columns') {
789 for (@bind) {
6fb2bd90 790 $self->_assert_bindval_matches_bindtype($_);
aa8d7bdb 791 }
331e2209 792 }
99a65fa8 793 return +{ -literal => [ $self->_quote($k).' '.$sql, @bind ] };
331e2209 794 }
99a65fa8 795 die "notreached";
59588695 796}
797
9103bd14 798sub _expand_ident {
83de255f 799 my ($self, $op, $body) = @_;
800 unless (defined($body) or (ref($body) and ref($body) eq 'ARRAY')) {
801 puke "$op requires a single plain scalar argument (a quotable identifier) or an arrayref of identifier parts";
802 }
9103bd14 803 my @parts = map split(/\Q${\($self->{name_sep}||'.')}\E/, $_),
804 ref($body) ? @$body : $body;
944f2c7b 805 return { -ident => $parts[-1] } if $self->{_dequalify_idents};
806 unless ($self->{quote_char}) {
807 $self->_assert_pass_injection_guard($_) for @parts;
808 }
9103bd14 809 return +{ -ident => \@parts };
810}
811
71f1364b 812sub _expand_value {
813 +{ -bind => [ our $Cur_Col_Meta, $_[2] ] };
814}
815
2a238661 816sub _expand_not {
817 +{ -op => [ 'not', $_[0]->_expand_expr($_[2]) ] };
818}
819
05d37f9c 820sub _expand_bool {
821 my ($self, undef, $v) = @_;
822 if (ref($v)) {
823 return $self->_expand_expr($v);
824 }
825 puke "-bool => undef not supported" unless defined($v);
826 return $self->_expand_ident(-ident => $v);
827}
828
70f98e4b 829sub _expand_andor {
830 my ($self, $k, $v) = @_;
831 my ($logic) = $k =~ /^-(.*)$/;
832 if (ref($v) eq 'HASH') {
833 return +{ -op => [
834 $logic,
835 map $self->_expand_expr({ $_ => $v->{$_} }, $logic),
836 sort keys %$v
837 ] };
838 }
839 if (ref($v) eq 'ARRAY') {
840 $logic eq 'and' or $logic eq 'or' or puke "unknown logic: $logic";
841
842 my @expr = grep {
843 (ref($_) eq 'ARRAY' and @$_)
844 or (ref($_) eq 'HASH' and %$_)
845 or 1
846 } @$v;
847
848 my @res;
849
850 while (my ($el) = splice @expr, 0, 1) {
851 puke "Supplying an empty left hand side argument is not supported in array-pairs"
852 unless defined($el) and length($el);
853 my $elref = ref($el);
854 if (!$elref) {
855 local our $Expand_Depth = 0;
856 push(@res, grep defined, $self->_expand_expr({ $el, shift(@expr) }));
857 } elsif ($elref eq 'ARRAY') {
858 push(@res, grep defined, $self->_expand_expr($el)) if @$el;
859 } elsif (my $l = is_literal_value($el)) {
860 push @res, { -literal => $l };
861 } elsif ($elref eq 'HASH') {
862 local our $Expand_Depth = 0;
863 push @res, grep defined, $self->_expand_expr($el) if %$el;
864 } else {
865 die "notreached";
866 }
867 }
868 # ???
869 # return $res[0] if @res == 1;
870 return { -op => [ $logic, @res ] };
871 }
872 die "notreached";
873}
874
143f50df 875sub _expand_between {
876 my ($self, $op, $vv, $k) = @_;
877 local our $Cur_Col_Meta = $k;
878 my @rhs = map $self->_expand_expr($_),
879 ref($vv) eq 'ARRAY' ? @$vv : $vv;
880 unless (
881 (@rhs == 1 and ref($rhs[0]) eq 'HASH' and $rhs[0]->{-literal})
882 or
883 (@rhs == 2 and defined($rhs[0]) and defined($rhs[1]))
884 ) {
885 puke "Operator '${\uc($op)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref";
886 }
887 return +{ -op => [
888 $op,
889 $self->_expand_ident(-ident => $k),
890 @rhs
891 ] }
892}
893
416026a9 894sub _expand_in {
895 my ($self, $op, $vv, $k) = @_;
896 if (my $literal = is_literal_value($vv)) {
897 my ($sql, @bind) = @$literal;
898 my $opened_sql = $self->_open_outer_paren($sql);
899 return +{ -op => [
900 $op, $self->_expand_ident(-ident => $k),
901 [ { -literal => [ $opened_sql, @bind ] } ]
902 ] };
903 }
904 my $undef_err =
905 'SQL::Abstract before v1.75 used to generate incorrect SQL when the '
906 . "-${\uc($op)} operator was given an undef-containing list: !!!AUDIT YOUR CODE "
907 . 'AND DATA!!! (the upcoming Data::Query-based version of SQL::Abstract '
908 . 'will emit the logically correct SQL instead of raising this exception)'
909 ;
910 puke("Argument passed to the '${\uc($op)}' operator can not be undefined")
911 if !defined($vv);
912 my @rhs = map $self->_expand_expr($_),
913 map { ref($_) ? $_ : { -bind => [ $k, $_ ] } }
914 map { defined($_) ? $_: puke($undef_err) }
915 (ref($vv) eq 'ARRAY' ? @$vv : $vv);
916 return $self->${\($op =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs;
917
918 return +{ -op => [
919 $op,
920 $self->_expand_ident(-ident => $k),
921 \@rhs
922 ] };
923}
924
5c633220 925sub _expand_nest {
926 my ($self, $op, $v) = @_;
927 # DBIx::Class requires a nest warning to be emitted once but the private
928 # method it overrode to do so no longer exists
929 if ($self->{is_dbic_sqlmaker}) {
930 unless (our $Nest_Warned) {
931 belch(
932 "-nest in search conditions is deprecated, you most probably wanted:\n"
933 .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }|
934 );
935 $Nest_Warned = 1;
936 }
937 }
938 return $self->_expand_expr($v);
939}
940
96449e8e 941sub _recurse_where {
942 my ($self, $where, $logic) = @_;
943
5492d4c2 944 # Special case: top level simple string treated as literal
945
946 my $where_exp = (ref($where)
ae56a156 947 ? $self->_expand_expr($where, $logic)
5492d4c2 948 : { -literal => [ $where ] });
e175845b 949
e3e27543 950 # dispatch expanded expression
311b2151 951
79d310f2 952 my ($sql, @bind) = defined($where_exp) ? $self->render_aqt($where_exp) : (undef);
abe1a491 953 # DBIx::Class used to call _recurse_where in scalar context
954 # something else might too...
955 if (wantarray) {
956 return ($sql, @bind);
957 }
958 else {
959 belch "Calling _recurse_where in scalar context is deprecated and will go away before 2.0";
960 return $sql;
961 }
96449e8e 962}
963
181dcebf 964sub _render_ident {
965 my ($self, $ident) = @_;
cc422895 966
9cf28dfb 967 return $self->_convert($self->_quote($ident));
cc422895 968}
969
ca158918 970sub _render_list {
971 my ($self, $list) = @_;
972 my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @$list;
973 return join(', ', map $_->[0], @parts), map @{$_}[1..$#$_], @parts;
974}
975
976sub _render_func {
977 my ($self, $rest) = @_;
978 my ($func, @args) = @$rest;
979 my @arg_sql;
980 my @bind = map {
981 my @x = @$_;
982 push @arg_sql, shift @x;
983 @x
984 } map [ $self->render_aqt($_) ], @args;
985 return ($self->_sqlcase($func).'('.join(', ', @arg_sql).')', @bind);
986}
d13725da 987
ca158918 988sub _render_bind {
989 my ($self, $bind) = @_;
990 return ($self->_convert('?'), $self->_bindtype(@$bind));
991}
992
993sub _render_literal {
994 my ($self, $literal) = @_;
995 $self->_assert_bindval_matches_bindtype(@{$literal}[1..$#$literal]);
996 return @$literal;
997}
998
999our $RENDER_OP = {
2d653a7f 1000 (map +($_ => sub {
1001 my ($self, $op, $args) = @_;
1002 my ($left, $low, $high) = @$args;
1003 my ($rhsql, @rhbind) = do {
1004 if (@$args == 2) {
1005 puke "Single arg to between must be a literal"
1006 unless $low->{-literal};
1007 @{$low->{-literal}}
1008 } else {
1009 my ($l, $h) = map [ $self->render_aqt($_) ], $low, $high;
1010 (join(' ', $l->[0], $self->_sqlcase('and'), $h->[0]),
1011 @{$l}[1..$#$l], @{$h}[1..$#$h])
1012 }
1013 };
1014 my ($lhsql, @lhbind) = $self->render_aqt($left);
1015 return (
1016 join(' ', '(', $lhsql, $self->_sqlcase($op), $rhsql, ')'),
1017 @lhbind, @rhbind
1018 );
b5b18861 1019 }), 'between', 'not between'),
2d653a7f 1020 (map +($_ => sub {
1021 my ($self, $op, $args) = @_;
1022 my ($lhs, $rhs) = @$args;
1023 my @in_bind;
1024 my @in_sql = map {
1025 my ($sql, @bind) = $self->render_aqt($_);
1026 push @in_bind, @bind;
1027 $sql;
1028 } @$rhs;
1029 my ($lhsql, @lbind) = $self->render_aqt($lhs);
1030 return (
1031 $lhsql.' '.$self->_sqlcase($op).' ( '
1032 .join(', ', @in_sql)
1033 .' )',
1034 @lbind, @in_bind
1035 );
10d07c4e 1036 }), 'in', 'not in'),
b23fd5ff 1037 (map +($_ => '_render_unop_postfix'),
1038 'is null', 'is not null', 'asc', 'desc',
1039 ),
2ddaa002 1040 (not => '_render_op_not'),
2d653a7f 1041 (map +($_ => sub {
1042 my ($self, $op, $args) = @_;
1043 my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @$args;
1044 return '' unless @parts;
1045 return @{$parts[0]} if @parts == 1;
1046 my ($final_sql) = join(
1047 ' '.$self->_sqlcase($op).' ',
1048 map $_->[0], @parts
1049 );
1050 return (
1051 '('.$final_sql.')',
1052 map @{$_}[1..$#$_], @parts
1053 );
1054 }), qw(and or)),
ca158918 1055};
1056
181dcebf 1057sub _render_op {
1058 my ($self, $v) = @_;
d13725da 1059 my ($op, @args) = @$v;
ca158918 1060 if (my $r = $self->{render_op}{$op}) {
b23fd5ff 1061 return $self->$r($op, \@args);
b5b18861 1062 }
3ae10d16 1063 my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
1064 if ($us and @args > 1) {
99a65fa8 1065 puke "Special op '${op}' requires first value to be identifier"
7d93797d 1066 unless my ($ident) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0];
1067 my $k = join(($self->{name_sep}||'.'), @$ident);
dd1d0dbf 1068 local our $Expand_Depth = 1;
99a65fa8 1069 return $self->${\($us->{handler})}($k, $op, $args[1]);
1070 }
96a8d74a 1071 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
1072 return $self->${\($us->{handler})}($op, $args[0]);
1073 }
2d653a7f 1074 if (@args == 1) {
2ddaa002 1075 return $self->_render_unop_prefix($op, \@args);
16d9289c 1076 } else {
79d310f2 1077 my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @args;
1c0c0f41 1078 return '' unless @parts;
2d653a7f 1079 my ($final_sql) = join(
9e242c8c 1080 ' '.$self->_sqlcase($op).' ',
77617257 1081 map $_->[0], @parts
1082 );
99a65fa8 1083 return (
77617257 1084 $final_sql,
16d9289c 1085 map @{$_}[1..$#$_], @parts
99a65fa8 1086 );
d13725da 1087 }
1088 die "unhandled";
1089}
1090
2ddaa002 1091sub _render_op_not {
1092 my ($self, $op, $v) = @_;
1093 my ($sql, @bind) = $self->_render_unop_prefix($op, $v);
1094 return "(${sql})", @bind;
1095}
1096
1097sub _render_unop_prefix {
1098 my ($self, $op, $v) = @_;
1099 my ($expr_sql, @bind) = $self->render_aqt($v->[0]);
1100 my $op_sql = $self->_sqlcase($op);
1101 return ("${op_sql} ${expr_sql}", @bind);
1102}
1103
b23fd5ff 1104sub _render_unop_postfix {
1105 my ($self, $op, $v) = @_;
715b4e6a 1106 my ($expr_sql, @bind) = $self->render_aqt($v->[0]);
b23fd5ff 1107 my $op_sql = $self->_sqlcase($op);
1108 return ($expr_sql.' '.$op_sql, @bind);
1109}
1110
4a1f01a3 1111# Some databases (SQLite) treat col IN (1, 2) different from
1112# col IN ( (1, 2) ). Use this to strip all outer parens while
1113# adding them back in the corresponding method
1114sub _open_outer_paren {
1115 my ($self, $sql) = @_;
a5f91feb 1116
ca4f826a 1117 while (my ($inner) = $sql =~ /^ \s* \( (.*) \) \s* $/xs) {
a5f91feb 1118
1119 # there are closing parens inside, need the heavy duty machinery
1120 # to reevaluate the extraction starting from $sql (full reevaluation)
ca4f826a 1121 if ($inner =~ /\)/) {
a5f91feb 1122 require Text::Balanced;
1123
1124 my (undef, $remainder) = do {
1125 # idiotic design - writes to $@ but *DOES NOT* throw exceptions
1126 local $@;
ca4f826a 1127 Text::Balanced::extract_bracketed($sql, '()', qr/\s*/);
a5f91feb 1128 };
1129
1130 # the entire expression needs to be a balanced bracketed thing
1131 # (after an extract no remainder sans trailing space)
1132 last if defined $remainder and $remainder =~ /\S/;
1133 }
1134
1135 $sql = $inner;
1136 }
1137
1138 $sql;
4a1f01a3 1139}
1140
96449e8e 1141
96449e8e 1142#======================================================================
1143# ORDER BY
1144#======================================================================
1145
33177570 1146sub _expand_order_by {
96449e8e 1147 my ($self, $arg) = @_;
1148
33177570 1149 return unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg);
2b6158af 1150
4325df6a 1151 my $expander = sub {
1152 my ($self, $dir, $expr) = @_;
52ca537e 1153 my @to_expand = ref($expr) eq 'ARRAY' ? @$expr : $expr;
1154 foreach my $arg (@to_expand) {
1155 if (
1156 ref($arg) eq 'HASH'
1157 and keys %$arg > 1
1158 and grep /^-(asc|desc)$/, keys %$arg
1159 ) {
1160 puke "ordering direction hash passed to order by must have exactly one key (-asc or -desc)";
1161 }
1162 }
7384c311 1163 my @exp = map +(
1164 defined($dir) ? { -op => [ $dir =~ /^-?(.*)$/ ,=> $_ ] } : $_
1165 ),
79d310f2 1166 map $self->expand_expr($_, -ident),
74156ee9 1167 map ref($_) eq 'ARRAY' ? @$_ : $_, @to_expand;
352d74bc 1168 return (@exp > 1 ? { -list => \@exp } : $exp[0]);
4325df6a 1169 };
18c743c8 1170
7384c311 1171 local @{$self->{expand}}{qw(-asc -desc)} = (($expander) x 2);
f267b646 1172
33177570 1173 return $self->$expander(undef, $arg);
1174}
1175
1176sub _order_by {
1177 my ($self, $arg) = @_;
1178
1179 return '' unless defined(my $expanded = $self->_expand_order_by($arg));
4325df6a 1180
79d310f2 1181 my ($sql, @bind) = $self->render_aqt($expanded);
4325df6a 1182
13cd9220 1183 return '' unless length($sql);
1184
4325df6a 1185 my $final_sql = $self->_sqlcase(' order by ').$sql;
1186
1187 return wantarray ? ($final_sql, @bind) : $final_sql;
f267b646 1188}
1189
2e3cc357 1190# _order_by no longer needs to call this so doesn't but DBIC uses it.
1191
33177570 1192sub _order_by_chunks {
1193 my ($self, $arg) = @_;
1194
1195 return () unless defined(my $expanded = $self->_expand_order_by($arg));
1196
2e3cc357 1197 return $self->_chunkify_order_by($expanded);
1198}
1199
1200sub _chunkify_order_by {
1201 my ($self, $expanded) = @_;
1b630cfe 1202
79d310f2 1203 return grep length, $self->render_aqt($expanded)
1b630cfe 1204 if $expanded->{-ident} or @{$expanded->{-literal}||[]} == 1;
1205
33177570 1206 for ($expanded) {
352d74bc 1207 if (ref() eq 'HASH' and my $l = $_->{-list}) {
1208 return map $self->_chunkify_order_by($_), @$l;
33177570 1209 }
79d310f2 1210 return [ $self->render_aqt($_) ];
33177570 1211 }
1212}
1213
96449e8e 1214#======================================================================
1215# DATASOURCE (FOR NOW, JUST PLAIN TABLE OR LIST OF TABLES)
1216#======================================================================
1217
1218sub _table {
1219 my $self = shift;
1220 my $from = shift;
79d310f2 1221 ($self->render_aqt(
8476c6a3 1222 $self->_expand_maybe_list_expr($from, undef, -ident)
7ad12721 1223 ))[0];
96449e8e 1224}
1225
1226
1227#======================================================================
1228# UTILITY FUNCTIONS
1229#======================================================================
1230
8476c6a3 1231sub _expand_maybe_list_expr {
1232 my ($self, $expr, $logic, $default) = @_;
bba04f52 1233 my $e = do {
1234 if (ref($expr) eq 'ARRAY') {
352d74bc 1235 return { -list => [
4ee3313d 1236 map $self->expand_expr($_, $default), @$expr
bba04f52 1237 ] } if @$expr > 1;
1238 $expr->[0]
1239 } else {
1240 $expr
1241 }
1242 };
4ee3313d 1243 return $self->expand_expr($e, $default);
8476c6a3 1244}
1245
955e77ca 1246# highly optimized, as it's called way too often
96449e8e 1247sub _quote {
955e77ca 1248 # my ($self, $label) = @_;
96449e8e 1249
955e77ca 1250 return '' unless defined $_[1];
955e77ca 1251 return ${$_[1]} if ref($_[1]) eq 'SCALAR';
d3162b5c 1252 puke 'Identifier cannot be hashref' if ref($_[1]) eq 'HASH';
96449e8e 1253
d3162b5c 1254 unless ($_[0]->{quote_char}) {
1255 if (ref($_[1]) eq 'ARRAY') {
1256 return join($_[0]->{name_sep}||'.', @{$_[1]});
1257 } else {
1258 $_[0]->_assert_pass_injection_guard($_[1]);
1259 return $_[1];
1260 }
1261 }
96449e8e 1262
07d7c35c 1263 my $qref = ref $_[0]->{quote_char};
439834d3 1264 my ($l, $r) =
1265 !$qref ? ($_[0]->{quote_char}, $_[0]->{quote_char})
1266 : ($qref eq 'ARRAY') ? @{$_[0]->{quote_char}}
1267 : puke "Unsupported quote_char format: $_[0]->{quote_char}";
1268
46be4313 1269 my $esc = $_[0]->{escape_char} || $r;
96449e8e 1270
07d7c35c 1271 # parts containing * are naturally unquoted
d3162b5c 1272 return join(
1273 $_[0]->{name_sep}||'',
1274 map +(
1275 $_ eq '*'
1276 ? $_
1277 : do { (my $n = $_) =~ s/(\Q$esc\E|\Q$r\E)/$esc$1/g; $l . $n . $r }
1278 ),
1279 (ref($_[1]) eq 'ARRAY'
1280 ? @{$_[1]}
1281 : (
1282 $_[0]->{name_sep}
1283 ? split (/\Q$_[0]->{name_sep}\E/, $_[1] )
1284 : $_[1]
1285 )
1286 )
955e77ca 1287 );
96449e8e 1288}
1289
1290
1291# Conversion, if applicable
d7c862e0 1292sub _convert {
07d7c35c 1293 #my ($self, $arg) = @_;
7ad12721 1294 if ($_[0]->{convert_where}) {
1295 return $_[0]->_sqlcase($_[0]->{convert_where}) .'(' . $_[1] . ')';
96449e8e 1296 }
07d7c35c 1297 return $_[1];
96449e8e 1298}
1299
1300# And bindtype
d7c862e0 1301sub _bindtype {
07d7c35c 1302 #my ($self, $col, @vals) = @_;
07d7c35c 1303 # called often - tighten code
1304 return $_[0]->{bindtype} eq 'columns'
1305 ? map {[$_[1], $_]} @_[2 .. $#_]
1306 : @_[2 .. $#_]
1307 ;
96449e8e 1308}
1309
fe3ae272 1310# Dies if any element of @bind is not in [colname => value] format
1311# if bindtype is 'columns'.
1312sub _assert_bindval_matches_bindtype {
c94a6c93 1313# my ($self, @bind) = @_;
1314 my $self = shift;
fe3ae272 1315 if ($self->{bindtype} eq 'columns') {
c94a6c93 1316 for (@_) {
1317 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
3a06278c 1318 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
fe3ae272 1319 }
1320 }
1321 }
1322}
1323
96449e8e 1324sub _join_sql_clauses {
1325 my ($self, $logic, $clauses_aref, $bind_aref) = @_;
1326
1327 if (@$clauses_aref > 1) {
1328 my $join = " " . $self->_sqlcase($logic) . " ";
1329 my $sql = '( ' . join($join, @$clauses_aref) . ' )';
1330 return ($sql, @$bind_aref);
1331 }
1332 elsif (@$clauses_aref) {
1333 return ($clauses_aref->[0], @$bind_aref); # no parentheses
1334 }
1335 else {
1336 return (); # if no SQL, ignore @$bind_aref
1337 }
1338}
1339
1340
1341# Fix SQL case, if so requested
1342sub _sqlcase {
96449e8e 1343 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
1344 # don't touch the argument ... crooked logic, but let's not change it!
07d7c35c 1345 return $_[0]->{case} ? $_[1] : uc($_[1]);
96449e8e 1346}
1347
1348
1349#======================================================================
1350# DISPATCHING FROM REFKIND
1351#======================================================================
1352
1353sub _refkind {
1354 my ($self, $data) = @_;
96449e8e 1355
955e77ca 1356 return 'UNDEF' unless defined $data;
1357
1358 # blessed objects are treated like scalars
1359 my $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
1360
1361 return 'SCALAR' unless $ref;
1362
1363 my $n_steps = 1;
1364 while ($ref eq 'REF') {
96449e8e 1365 $data = $$data;
955e77ca 1366 $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
1367 $n_steps++ if $ref;
96449e8e 1368 }
1369
848556bc 1370 return ($ref||'SCALAR') . ('REF' x $n_steps);
96449e8e 1371}
1372
1373sub _try_refkind {
1374 my ($self, $data) = @_;
1375 my @try = ($self->_refkind($data));
1376 push @try, 'SCALAR_or_UNDEF' if $try[0] eq 'SCALAR' || $try[0] eq 'UNDEF';
1377 push @try, 'FALLBACK';
955e77ca 1378 return \@try;
96449e8e 1379}
1380
1381sub _METHOD_FOR_refkind {
1382 my ($self, $meth_prefix, $data) = @_;
f39eaa60 1383
1384 my $method;
955e77ca 1385 for (@{$self->_try_refkind($data)}) {
f39eaa60 1386 $method = $self->can($meth_prefix."_".$_)
1387 and last;
1388 }
1389
1390 return $method || puke "cannot dispatch on '$meth_prefix' for ".$self->_refkind($data);
96449e8e 1391}
1392
1393
1394sub _SWITCH_refkind {
1395 my ($self, $data, $dispatch_table) = @_;
1396
f39eaa60 1397 my $coderef;
955e77ca 1398 for (@{$self->_try_refkind($data)}) {
f39eaa60 1399 $coderef = $dispatch_table->{$_}
1400 and last;
1401 }
1402
1403 puke "no dispatch entry for ".$self->_refkind($data)
1404 unless $coderef;
1405
96449e8e 1406 $coderef->();
1407}
1408
1409
1410
1411
1412#======================================================================
1413# VALUES, GENERATE, AUTOLOAD
1414#======================================================================
1415
1416# LDNOTE: original code from nwiger, didn't touch code in that section
1417# I feel the AUTOLOAD stuff should not be the default, it should
1418# only be activated on explicit demand by user.
1419
1420sub values {
1421 my $self = shift;
1422 my $data = shift || return;
1423 puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
1424 unless ref $data eq 'HASH';
bab725ce 1425
1426 my @all_bind;
ca4f826a 1427 foreach my $k (sort keys %$data) {
bab725ce 1428 my $v = $data->{$k};
1429 $self->_SWITCH_refkind($v, {
9d48860e 1430 ARRAYREF => sub {
bab725ce 1431 if ($self->{array_datatypes}) { # array datatype
1432 push @all_bind, $self->_bindtype($k, $v);
1433 }
1434 else { # literal SQL with bind
1435 my ($sql, @bind) = @$v;
1436 $self->_assert_bindval_matches_bindtype(@bind);
1437 push @all_bind, @bind;
1438 }
1439 },
1440 ARRAYREFREF => sub { # literal SQL with bind
1441 my ($sql, @bind) = @${$v};
1442 $self->_assert_bindval_matches_bindtype(@bind);
1443 push @all_bind, @bind;
1444 },
1445 SCALARREF => sub { # literal SQL without bind
1446 },
1447 SCALAR_or_UNDEF => sub {
1448 push @all_bind, $self->_bindtype($k, $v);
1449 },
1450 });
1451 }
1452
1453 return @all_bind;
96449e8e 1454}
1455
1456sub generate {
1457 my $self = shift;
1458
1459 my(@sql, @sqlq, @sqlv);
1460
1461 for (@_) {
1462 my $ref = ref $_;
1463 if ($ref eq 'HASH') {
1464 for my $k (sort keys %$_) {
1465 my $v = $_->{$k};
1466 my $r = ref $v;
1467 my $label = $self->_quote($k);
1468 if ($r eq 'ARRAY') {
fe3ae272 1469 # literal SQL with bind
1470 my ($sql, @bind) = @$v;
1471 $self->_assert_bindval_matches_bindtype(@bind);
96449e8e 1472 push @sqlq, "$label = $sql";
fe3ae272 1473 push @sqlv, @bind;
96449e8e 1474 } elsif ($r eq 'SCALAR') {
fe3ae272 1475 # literal SQL without bind
96449e8e 1476 push @sqlq, "$label = $$v";
9d48860e 1477 } else {
96449e8e 1478 push @sqlq, "$label = ?";
1479 push @sqlv, $self->_bindtype($k, $v);
1480 }
1481 }
1482 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
1483 } elsif ($ref eq 'ARRAY') {
1484 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
1485 for my $v (@$_) {
1486 my $r = ref $v;
fe3ae272 1487 if ($r eq 'ARRAY') { # literal SQL with bind
1488 my ($sql, @bind) = @$v;
1489 $self->_assert_bindval_matches_bindtype(@bind);
1490 push @sqlq, $sql;
1491 push @sqlv, @bind;
1492 } elsif ($r eq 'SCALAR') { # literal SQL without bind
96449e8e 1493 # embedded literal SQL
1494 push @sqlq, $$v;
9d48860e 1495 } else {
96449e8e 1496 push @sqlq, '?';
1497 push @sqlv, $v;
1498 }
1499 }
1500 push @sql, '(' . join(', ', @sqlq) . ')';
1501 } elsif ($ref eq 'SCALAR') {
1502 # literal SQL
1503 push @sql, $$_;
1504 } else {
1505 # strings get case twiddled
1506 push @sql, $self->_sqlcase($_);
1507 }
1508 }
1509
1510 my $sql = join ' ', @sql;
1511
1512 # this is pretty tricky
1513 # if ask for an array, return ($stmt, @bind)
1514 # otherwise, s/?/shift @sqlv/ to put it inline
1515 if (wantarray) {
1516 return ($sql, @sqlv);
1517 } else {
1518 1 while $sql =~ s/\?/my $d = shift(@sqlv);
1519 ref $d ? $d->[1] : $d/e;
1520 return $sql;
1521 }
1522}
1523
1524
1525sub DESTROY { 1 }
1526
1527sub AUTOLOAD {
1528 # This allows us to check for a local, then _form, attr
1529 my $self = shift;
1530 my($name) = $AUTOLOAD =~ /.*::(.+)/;
1531 return $self->generate($name, @_);
1532}
1533
15341;
1535
1536
1537
1538__END__
32eab2da 1539
1540=head1 NAME
1541
1542SQL::Abstract - Generate SQL from Perl data structures
1543
1544=head1 SYNOPSIS
1545
1546 use SQL::Abstract;
1547
1548 my $sql = SQL::Abstract->new;
1549
85783f3c 1550 my($stmt, @bind) = $sql->select($source, \@fields, \%where, $order);
32eab2da 1551
1552 my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
1553
1554 my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
1555
1556 my($stmt, @bind) = $sql->delete($table, \%where);
1557
1558 # Then, use these in your DBI statements
1559 my $sth = $dbh->prepare($stmt);
1560 $sth->execute(@bind);
1561
1562 # Just generate the WHERE clause
85783f3c 1563 my($stmt, @bind) = $sql->where(\%where, $order);
32eab2da 1564
1565 # Return values in the same order, for hashed queries
1566 # See PERFORMANCE section for more details
1567 my @bind = $sql->values(\%fieldvals);
1568
1569=head1 DESCRIPTION
1570
1571This module was inspired by the excellent L<DBIx::Abstract>.
1572However, in using that module I found that what I really wanted
1573to do was generate SQL, but still retain complete control over my
1574statement handles and use the DBI interface. So, I set out to
1575create an abstract SQL generation module.
1576
1577While based on the concepts used by L<DBIx::Abstract>, there are
1578several important differences, especially when it comes to WHERE
1579clauses. I have modified the concepts used to make the SQL easier
1580to generate from Perl data structures and, IMO, more intuitive.
1581The underlying idea is for this module to do what you mean, based
1582on the data structures you provide it. The big advantage is that
1583you don't have to modify your code every time your data changes,
1584as this module figures it out.
1585
1586To begin with, an SQL INSERT is as easy as just specifying a hash
1587of C<key=value> pairs:
1588
1589 my %data = (
1590 name => 'Jimbo Bobson',
1591 phone => '123-456-7890',
1592 address => '42 Sister Lane',
1593 city => 'St. Louis',
1594 state => 'Louisiana',
1595 );
1596
1597The SQL can then be generated with this:
1598
1599 my($stmt, @bind) = $sql->insert('people', \%data);
1600
1601Which would give you something like this:
1602
1603 $stmt = "INSERT INTO people
1604 (address, city, name, phone, state)
1605 VALUES (?, ?, ?, ?, ?)";
1606 @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
1607 '123-456-7890', 'Louisiana');
1608
1609These are then used directly in your DBI code:
1610
1611 my $sth = $dbh->prepare($stmt);
1612 $sth->execute(@bind);
1613
96449e8e 1614=head2 Inserting and Updating Arrays
1615
1616If your database has array types (like for example Postgres),
1617activate the special option C<< array_datatypes => 1 >>
9d48860e 1618when creating the C<SQL::Abstract> object.
96449e8e 1619Then you may use an arrayref to insert and update database array types:
1620
1621 my $sql = SQL::Abstract->new(array_datatypes => 1);
1622 my %data = (
1623 planets => [qw/Mercury Venus Earth Mars/]
1624 );
9d48860e 1625
96449e8e 1626 my($stmt, @bind) = $sql->insert('solar_system', \%data);
1627
1628This results in:
1629
1630 $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
1631
1632 @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
1633
1634
1635=head2 Inserting and Updating SQL
1636
1637In order to apply SQL functions to elements of your C<%data> you may
1638specify a reference to an arrayref for the given hash value. For example,
1639if you need to execute the Oracle C<to_date> function on a value, you can
1640say something like this:
32eab2da 1641
1642 my %data = (
1643 name => 'Bill',
3ae1c5e2 1644 date_entered => \[ "to_date(?,'MM/DD/YYYY')", "03/02/2003" ],
9d48860e 1645 );
32eab2da 1646
1647The first value in the array is the actual SQL. Any other values are
1648optional and would be included in the bind values array. This gives
1649you:
1650
1651 my($stmt, @bind) = $sql->insert('people', \%data);
1652
9d48860e 1653 $stmt = "INSERT INTO people (name, date_entered)
32eab2da 1654 VALUES (?, to_date(?,'MM/DD/YYYY'))";
1655 @bind = ('Bill', '03/02/2003');
1656
1657An UPDATE is just as easy, all you change is the name of the function:
1658
1659 my($stmt, @bind) = $sql->update('people', \%data);
1660
1661Notice that your C<%data> isn't touched; the module will generate
1662the appropriately quirky SQL for you automatically. Usually you'll
1663want to specify a WHERE clause for your UPDATE, though, which is
1664where handling C<%where> hashes comes in handy...
1665
96449e8e 1666=head2 Complex where statements
1667
32eab2da 1668This module can generate pretty complicated WHERE statements
1669easily. For example, simple C<key=value> pairs are taken to mean
1670equality, and if you want to see if a field is within a set
1671of values, you can use an arrayref. Let's say we wanted to
1672SELECT some data based on this criteria:
1673
1674 my %where = (
1675 requestor => 'inna',
1676 worker => ['nwiger', 'rcwe', 'sfz'],
1677 status => { '!=', 'completed' }
1678 );
1679
1680 my($stmt, @bind) = $sql->select('tickets', '*', \%where);
1681
1682The above would give you something like this:
1683
1684 $stmt = "SELECT * FROM tickets WHERE
1685 ( requestor = ? ) AND ( status != ? )
1686 AND ( worker = ? OR worker = ? OR worker = ? )";
1687 @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
1688
1689Which you could then use in DBI code like so:
1690
1691 my $sth = $dbh->prepare($stmt);
1692 $sth->execute(@bind);
1693
1694Easy, eh?
1695
0da0fe34 1696=head1 METHODS
32eab2da 1697
13cc86af 1698The methods are simple. There's one for every major SQL operation,
32eab2da 1699and a constructor you use first. The arguments are specified in a
13cc86af 1700similar order for each method (table, then fields, then a where
32eab2da 1701clause) to try and simplify things.
1702
32eab2da 1703=head2 new(option => 'value')
1704
1705The C<new()> function takes a list of options and values, and returns
1706a new B<SQL::Abstract> object which can then be used to generate SQL
1707through the methods below. The options accepted are:
1708
1709=over
1710
1711=item case
1712
1713If set to 'lower', then SQL will be generated in all lowercase. By
1714default SQL is generated in "textbook" case meaning something like:
1715
1716 SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
1717
96449e8e 1718Any setting other than 'lower' is ignored.
1719
32eab2da 1720=item cmp
1721
1722This determines what the default comparison operator is. By default
1723it is C<=>, meaning that a hash like this:
1724
1725 %where = (name => 'nwiger', email => 'nate@wiger.org');
1726
1727Will generate SQL like this:
1728
1729 WHERE name = 'nwiger' AND email = 'nate@wiger.org'
1730
1731However, you may want loose comparisons by default, so if you set
1732C<cmp> to C<like> you would get SQL such as:
1733
1734 WHERE name like 'nwiger' AND email like 'nate@wiger.org'
1735
3af02ccb 1736You can also override the comparison on an individual basis - see
32eab2da 1737the huge section on L</"WHERE CLAUSES"> at the bottom.
1738
96449e8e 1739=item sqltrue, sqlfalse
1740
1741Expressions for inserting boolean values within SQL statements.
6e0c6552 1742By default these are C<1=1> and C<1=0>. They are used
1743by the special operators C<-in> and C<-not_in> for generating
1744correct SQL even when the argument is an empty array (see below).
96449e8e 1745
32eab2da 1746=item logic
1747
1748This determines the default logical operator for multiple WHERE
7cac25e6 1749statements in arrays or hashes. If absent, the default logic is "or"
1750for arrays, and "and" for hashes. This means that a WHERE
32eab2da 1751array of the form:
1752
1753 @where = (
9d48860e 1754 event_date => {'>=', '2/13/99'},
1755 event_date => {'<=', '4/24/03'},
32eab2da 1756 );
1757
7cac25e6 1758will generate SQL like this:
32eab2da 1759
1760 WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
1761
1762This is probably not what you want given this query, though (look
1763at the dates). To change the "OR" to an "AND", simply specify:
1764
1765 my $sql = SQL::Abstract->new(logic => 'and');
1766
1767Which will change the above C<WHERE> to:
1768
1769 WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
1770
96449e8e 1771The logic can also be changed locally by inserting
be21dde3 1772a modifier in front of an arrayref:
96449e8e 1773
9d48860e 1774 @where = (-and => [event_date => {'>=', '2/13/99'},
7cac25e6 1775 event_date => {'<=', '4/24/03'} ]);
96449e8e 1776
1777See the L</"WHERE CLAUSES"> section for explanations.
1778
32eab2da 1779=item convert
1780
1781This will automatically convert comparisons using the specified SQL
1782function for both column and value. This is mostly used with an argument
1783of C<upper> or C<lower>, so that the SQL will have the effect of
1784case-insensitive "searches". For example, this:
1785
1786 $sql = SQL::Abstract->new(convert => 'upper');
1787 %where = (keywords => 'MaKe iT CAse inSeNSItive');
1788
1789Will turn out the following SQL:
1790
1791 WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
1792
1793The conversion can be C<upper()>, C<lower()>, or any other SQL function
1794that can be applied symmetrically to fields (actually B<SQL::Abstract> does
1795not validate this option; it will just pass through what you specify verbatim).
1796
1797=item bindtype
1798
1799This is a kludge because many databases suck. For example, you can't
1800just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
1801Instead, you have to use C<bind_param()>:
1802
1803 $sth->bind_param(1, 'reg data');
1804 $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
1805
1806The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
1807which loses track of which field each slot refers to. Fear not.
1808
1809If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
1810Currently, you can specify either C<normal> (default) or C<columns>. If you
1811specify C<columns>, you will get an array that looks like this:
1812
1813 my $sql = SQL::Abstract->new(bindtype => 'columns');
1814 my($stmt, @bind) = $sql->insert(...);
1815
1816 @bind = (
1817 [ 'column1', 'value1' ],
1818 [ 'column2', 'value2' ],
1819 [ 'column3', 'value3' ],
1820 );
1821
1822You can then iterate through this manually, using DBI's C<bind_param()>.
e3f9dff4 1823
32eab2da 1824 $sth->prepare($stmt);
1825 my $i = 1;
1826 for (@bind) {
1827 my($col, $data) = @$_;
1828 if ($col eq 'details' || $col eq 'comments') {
1829 $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
1830 } elsif ($col eq 'image') {
1831 $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
1832 } else {
1833 $sth->bind_param($i, $data);
1834 }
1835 $i++;
1836 }
1837 $sth->execute; # execute without @bind now
1838
1839Now, why would you still use B<SQL::Abstract> if you have to do this crap?
1840Basically, the advantage is still that you don't have to care which fields
1841are or are not included. You could wrap that above C<for> loop in a simple
1842sub called C<bind_fields()> or something and reuse it repeatedly. You still
1843get a layer of abstraction over manual SQL specification.
1844
3ae1c5e2 1845Note that if you set L</bindtype> to C<columns>, the C<\[ $sql, @bind ]>
deb148a2 1846construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
1847will expect the bind values in this format.
1848
32eab2da 1849=item quote_char
1850
1851This is the character that a table or column name will be quoted
9d48860e 1852with. By default this is an empty string, but you could set it to
32eab2da 1853the character C<`>, to generate SQL like this:
1854
1855 SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
1856
96449e8e 1857Alternatively, you can supply an array ref of two items, the first being the left
1858hand quote character, and the second the right hand quote character. For
1859example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
1860that generates SQL like this:
1861
1862 SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
1863
9d48860e 1864Quoting is useful if you have tables or columns names that are reserved
96449e8e 1865words in your database's SQL dialect.
32eab2da 1866
46be4313 1867=item escape_char
1868
1869This is the character that will be used to escape L</quote_char>s appearing
1870in an identifier before it has been quoted.
1871
80790166 1872The parameter default in case of a single L</quote_char> character is the quote
46be4313 1873character itself.
1874
1875When opening-closing-style quoting is used (L</quote_char> is an arrayref)
9de2bd86 1876this parameter defaults to the B<closing (right)> L</quote_char>. Occurrences
46be4313 1877of the B<opening (left)> L</quote_char> within the identifier are currently left
1878untouched. The default for opening-closing-style quotes may change in future
1879versions, thus you are B<strongly encouraged> to specify the escape character
1880explicitly.
1881
32eab2da 1882=item name_sep
1883
1884This is the character that separates a table and column name. It is
1885necessary to specify this when the C<quote_char> option is selected,
1886so that tables and column names can be individually quoted like this:
1887
1888 SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
1889
b6251592 1890=item injection_guard
1891
1892A regular expression C<qr/.../> that is applied to any C<-function> and unquoted
1893column name specified in a query structure. This is a safety mechanism to avoid
1894injection attacks when mishandling user input e.g.:
1895
1896 my %condition_as_column_value_pairs = get_values_from_user();
1897 $sqla->select( ... , \%condition_as_column_value_pairs );
1898
1899If the expression matches an exception is thrown. Note that literal SQL
1900supplied via C<\'...'> or C<\['...']> is B<not> checked in any way.
1901
1902Defaults to checking for C<;> and the C<GO> keyword (TransactSQL)
1903
96449e8e 1904=item array_datatypes
32eab2da 1905
9d48860e 1906When this option is true, arrayrefs in INSERT or UPDATE are
1907interpreted as array datatypes and are passed directly
96449e8e 1908to the DBI layer.
1909When this option is false, arrayrefs are interpreted
1910as literal SQL, just like refs to arrayrefs
1911(but this behavior is for backwards compatibility; when writing
1912new queries, use the "reference to arrayref" syntax
1913for literal SQL).
32eab2da 1914
32eab2da 1915
96449e8e 1916=item special_ops
32eab2da 1917
9d48860e 1918Takes a reference to a list of "special operators"
96449e8e 1919to extend the syntax understood by L<SQL::Abstract>.
1920See section L</"SPECIAL OPERATORS"> for details.
32eab2da 1921
59f23b3d 1922=item unary_ops
1923
9d48860e 1924Takes a reference to a list of "unary operators"
59f23b3d 1925to extend the syntax understood by L<SQL::Abstract>.
1926See section L</"UNARY OPERATORS"> for details.
1927
32eab2da 1928
32eab2da 1929
96449e8e 1930=back
32eab2da 1931
02288357 1932=head2 insert($table, \@values || \%fieldvals, \%options)
32eab2da 1933
1934This is the simplest function. You simply give it a table name
1935and either an arrayref of values or hashref of field/value pairs.
1936It returns an SQL INSERT statement and a list of bind values.
96449e8e 1937See the sections on L</"Inserting and Updating Arrays"> and
1938L</"Inserting and Updating SQL"> for information on how to insert
1939with those data types.
32eab2da 1940
02288357 1941The optional C<\%options> hash reference may contain additional
1942options to generate the insert SQL. Currently supported options
1943are:
1944
1945=over 4
1946
1947=item returning
1948
1949Takes either a scalar of raw SQL fields, or an array reference of
1950field names, and adds on an SQL C<RETURNING> statement at the end.
1951This allows you to return data generated by the insert statement
1952(such as row IDs) without performing another C<SELECT> statement.
1953Note, however, this is not part of the SQL standard and may not
1954be supported by all database engines.
1955
1956=back
1957
95904db5 1958=head2 update($table, \%fieldvals, \%where, \%options)
32eab2da 1959
1960This takes a table, hashref of field/value pairs, and an optional
86298391 1961hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
32eab2da 1962of bind values.
96449e8e 1963See the sections on L</"Inserting and Updating Arrays"> and
1964L</"Inserting and Updating SQL"> for information on how to insert
1965with those data types.
32eab2da 1966
95904db5 1967The optional C<\%options> hash reference may contain additional
1968options to generate the update SQL. Currently supported options
1969are:
1970
1971=over 4
1972
1973=item returning
1974
1975See the C<returning> option to
1976L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
1977
1978=back
1979
96449e8e 1980=head2 select($source, $fields, $where, $order)
32eab2da 1981
9d48860e 1982This returns a SQL SELECT statement and associated list of bind values, as
be21dde3 1983specified by the arguments:
32eab2da 1984
96449e8e 1985=over
32eab2da 1986
96449e8e 1987=item $source
32eab2da 1988
9d48860e 1989Specification of the 'FROM' part of the statement.
96449e8e 1990The argument can be either a plain scalar (interpreted as a table
1991name, will be quoted), or an arrayref (interpreted as a list
1992of table names, joined by commas, quoted), or a scalarref
063097a3 1993(literal SQL, not quoted).
32eab2da 1994
96449e8e 1995=item $fields
32eab2da 1996
9d48860e 1997Specification of the list of fields to retrieve from
96449e8e 1998the source.
1999The argument can be either an arrayref (interpreted as a list
9d48860e 2000of field names, will be joined by commas and quoted), or a
96449e8e 2001plain scalar (literal SQL, not quoted).
521647e7 2002Please observe that this API is not as flexible as that of
2003the first argument C<$source>, for backwards compatibility reasons.
32eab2da 2004
96449e8e 2005=item $where
32eab2da 2006
96449e8e 2007Optional argument to specify the WHERE part of the query.
2008The argument is most often a hashref, but can also be
9d48860e 2009an arrayref or plain scalar --
96449e8e 2010see section L<WHERE clause|/"WHERE CLAUSES"> for details.
32eab2da 2011
96449e8e 2012=item $order
32eab2da 2013
96449e8e 2014Optional argument to specify the ORDER BY part of the query.
9d48860e 2015The argument can be a scalar, a hashref or an arrayref
96449e8e 2016-- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
2017for details.
32eab2da 2018
96449e8e 2019=back
32eab2da 2020
32eab2da 2021
85327cd5 2022=head2 delete($table, \%where, \%options)
32eab2da 2023
86298391 2024This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
32eab2da 2025It returns an SQL DELETE statement and list of bind values.
2026
85327cd5 2027The optional C<\%options> hash reference may contain additional
2028options to generate the delete SQL. Currently supported options
2029are:
2030
2031=over 4
2032
2033=item returning
2034
2035See the C<returning> option to
2036L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
2037
2038=back
2039
85783f3c 2040=head2 where(\%where, $order)
32eab2da 2041
2042This is used to generate just the WHERE clause. For example,
2043if you have an arbitrary data structure and know what the
2044rest of your SQL is going to look like, but want an easy way
2045to produce a WHERE clause, use this. It returns an SQL WHERE
2046clause and list of bind values.
2047
32eab2da 2048
2049=head2 values(\%data)
2050
2051This just returns the values from the hash C<%data>, in the same
2052order that would be returned from any of the other above queries.
2053Using this allows you to markedly speed up your queries if you
2054are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
2055
32eab2da 2056=head2 generate($any, 'number', $of, \@data, $struct, \%types)
2057
2058Warning: This is an experimental method and subject to change.
2059
2060This returns arbitrarily generated SQL. It's a really basic shortcut.
2061It will return two different things, depending on return context:
2062
2063 my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
2064 my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
2065
2066These would return the following:
2067
2068 # First calling form
2069 $stmt = "CREATE TABLE test (?, ?)";
2070 @bind = (field1, field2);
2071
2072 # Second calling form
2073 $stmt_and_val = "CREATE TABLE test (field1, field2)";
2074
2075Depending on what you're trying to do, it's up to you to choose the correct
2076format. In this example, the second form is what you would want.
2077
2078By the same token:
2079
2080 $sql->generate('alter session', { nls_date_format => 'MM/YY' });
2081
2082Might give you:
2083
2084 ALTER SESSION SET nls_date_format = 'MM/YY'
2085
2086You get the idea. Strings get their case twiddled, but everything
2087else remains verbatim.
2088
0da0fe34 2089=head1 EXPORTABLE FUNCTIONS
2090
2091=head2 is_plain_value
2092
2093Determines if the supplied argument is a plain value as understood by this
2094module:
2095
2096=over
2097
2098=item * The value is C<undef>
2099
2100=item * The value is a non-reference
2101
2102=item * The value is an object with stringification overloading
2103
2104=item * The value is of the form C<< { -value => $anything } >>
2105
2106=back
2107
9de2bd86 2108On failure returns C<undef>, on success returns a B<scalar> reference
966200cc 2109to the original supplied argument.
0da0fe34 2110
843a94b5 2111=over
2112
2113=item * Note
2114
2115The stringification overloading detection is rather advanced: it takes
2116into consideration not only the presence of a C<""> overload, but if that
2117fails also checks for enabled
2118L<autogenerated versions of C<"">|overload/Magic Autogeneration>, based
2119on either C<0+> or C<bool>.
2120
2121Unfortunately testing in the field indicates that this
2122detection B<< may tickle a latent bug in perl versions before 5.018 >>,
2123but only when very large numbers of stringifying objects are involved.
2124At the time of writing ( Sep 2014 ) there is no clear explanation of
2125the direct cause, nor is there a manageably small test case that reliably
2126reproduces the problem.
2127
2128If you encounter any of the following exceptions in B<random places within
2129your application stack> - this module may be to blame:
2130
2131 Operation "ne": no method found,
2132 left argument in overloaded package <something>,
2133 right argument in overloaded package <something>
2134
2135or perhaps even
2136
2137 Stub found while resolving method "???" overloading """" in package <something>
2138
2139If you fall victim to the above - please attempt to reduce the problem
2140to something that could be sent to the L<SQL::Abstract developers
1f490ae4 2141|DBIx::Class/GETTING HELP/SUPPORT>
843a94b5 2142(either publicly or privately). As a workaround in the meantime you can
2143set C<$ENV{SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION}> to a true
2144value, which will most likely eliminate your problem (at the expense of
2145not being able to properly detect exotic forms of stringification).
2146
2147This notice and environment variable will be removed in a future version,
2148as soon as the underlying problem is found and a reliable workaround is
2149devised.
2150
2151=back
2152
0da0fe34 2153=head2 is_literal_value
2154
2155Determines if the supplied argument is a literal value as understood by this
2156module:
2157
2158=over
2159
2160=item * C<\$sql_string>
2161
2162=item * C<\[ $sql_string, @bind_values ]>
2163
0da0fe34 2164=back
2165
9de2bd86 2166On failure returns C<undef>, on success returns an B<array> reference
966200cc 2167containing the unpacked version of the supplied literal SQL and bind values.
0da0fe34 2168
32eab2da 2169=head1 WHERE CLAUSES
2170
96449e8e 2171=head2 Introduction
2172
32eab2da 2173This module uses a variation on the idea from L<DBIx::Abstract>. It
2174is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
2175module is that things in arrays are OR'ed, and things in hashes
2176are AND'ed.>
2177
2178The easiest way to explain is to show lots of examples. After
2179each C<%where> hash shown, it is assumed you used:
2180
2181 my($stmt, @bind) = $sql->where(\%where);
2182
2183However, note that the C<%where> hash can be used directly in any
2184of the other functions as well, as described above.
2185
96449e8e 2186=head2 Key-value pairs
2187
32eab2da 2188So, let's get started. To begin, a simple hash:
2189
2190 my %where = (
2191 user => 'nwiger',
2192 status => 'completed'
2193 );
2194
2195Is converted to SQL C<key = val> statements:
2196
2197 $stmt = "WHERE user = ? AND status = ?";
2198 @bind = ('nwiger', 'completed');
2199
2200One common thing I end up doing is having a list of values that
2201a field can be in. To do this, simply specify a list inside of
2202an arrayref:
2203
2204 my %where = (
2205 user => 'nwiger',
2206 status => ['assigned', 'in-progress', 'pending'];
2207 );
2208
2209This simple code will create the following:
9d48860e 2210
32eab2da 2211 $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
2212 @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
2213
9d48860e 2214A field associated to an empty arrayref will be considered a
7cac25e6 2215logical false and will generate 0=1.
8a68b5be 2216
b864ba9b 2217=head2 Tests for NULL values
2218
2219If the value part is C<undef> then this is converted to SQL <IS NULL>
2220
2221 my %where = (
2222 user => 'nwiger',
2223 status => undef,
2224 );
2225
2226becomes:
2227
2228 $stmt = "WHERE user = ? AND status IS NULL";
2229 @bind = ('nwiger');
2230
e9614080 2231To test if a column IS NOT NULL:
2232
2233 my %where = (
2234 user => 'nwiger',
2235 status => { '!=', undef },
2236 );
cc422895 2237
6e0c6552 2238=head2 Specific comparison operators
96449e8e 2239
32eab2da 2240If you want to specify a different type of operator for your comparison,
2241you can use a hashref for a given column:
2242
2243 my %where = (
2244 user => 'nwiger',
2245 status => { '!=', 'completed' }
2246 );
2247
2248Which would generate:
2249
2250 $stmt = "WHERE user = ? AND status != ?";
2251 @bind = ('nwiger', 'completed');
2252
2253To test against multiple values, just enclose the values in an arrayref:
2254
96449e8e 2255 status => { '=', ['assigned', 'in-progress', 'pending'] };
2256
f2d5020d 2257Which would give you:
96449e8e 2258
2259 "WHERE status = ? OR status = ? OR status = ?"
2260
2261
2262The hashref can also contain multiple pairs, in which case it is expanded
32eab2da 2263into an C<AND> of its elements:
2264
2265 my %where = (
2266 user => 'nwiger',
2267 status => { '!=', 'completed', -not_like => 'pending%' }
2268 );
2269
2270 # Or more dynamically, like from a form
2271 $where{user} = 'nwiger';
2272 $where{status}{'!='} = 'completed';
2273 $where{status}{'-not_like'} = 'pending%';
2274
2275 # Both generate this
2276 $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
2277 @bind = ('nwiger', 'completed', 'pending%');
2278
96449e8e 2279
32eab2da 2280To get an OR instead, you can combine it with the arrayref idea:
2281
2282 my %where => (
2283 user => 'nwiger',
1a6f2a03 2284 priority => [ { '=', 2 }, { '>', 5 } ]
32eab2da 2285 );
2286
2287Which would generate:
2288
1a6f2a03 2289 $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?";
2290 @bind = ('2', '5', 'nwiger');
32eab2da 2291
44b9e502 2292If you want to include literal SQL (with or without bind values), just use a
13cc86af 2293scalar reference or reference to an arrayref as the value:
44b9e502 2294
2295 my %where = (
2296 date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
2297 date_expires => { '<' => \"now()" }
2298 );
2299
2300Which would generate:
2301
13cc86af 2302 $stmt = "WHERE date_entered > to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
44b9e502 2303 @bind = ('11/26/2008');
2304
96449e8e 2305
2306=head2 Logic and nesting operators
2307
2308In the example above,
2309there is a subtle trap if you want to say something like
32eab2da 2310this (notice the C<AND>):
2311
2312 WHERE priority != ? AND priority != ?
2313
2314Because, in Perl you I<can't> do this:
2315
13cc86af 2316 priority => { '!=' => 2, '!=' => 1 }
32eab2da 2317
2318As the second C<!=> key will obliterate the first. The solution
2319is to use the special C<-modifier> form inside an arrayref:
2320
9d48860e 2321 priority => [ -and => {'!=', 2},
96449e8e 2322 {'!=', 1} ]
2323
32eab2da 2324
2325Normally, these would be joined by C<OR>, but the modifier tells it
2326to use C<AND> instead. (Hint: You can use this in conjunction with the
2327C<logic> option to C<new()> in order to change the way your queries
2328work by default.) B<Important:> Note that the C<-modifier> goes
2329B<INSIDE> the arrayref, as an extra first element. This will
2330B<NOT> do what you think it might:
2331
2332 priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG!
2333
2334Here is a quick list of equivalencies, since there is some overlap:
2335
2336 # Same
2337 status => {'!=', 'completed', 'not like', 'pending%' }
2338 status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
2339
2340 # Same
2341 status => {'=', ['assigned', 'in-progress']}
2342 status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
2343 status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
2344
e3f9dff4 2345
2346
be21dde3 2347=head2 Special operators: IN, BETWEEN, etc.
96449e8e 2348
32eab2da 2349You can also use the hashref format to compare a list of fields using the
2350C<IN> comparison operator, by specifying the list as an arrayref:
2351
2352 my %where = (
2353 status => 'completed',
2354 reportid => { -in => [567, 2335, 2] }
2355 );
2356
2357Which would generate:
2358
2359 $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
2360 @bind = ('completed', '567', '2335', '2');
2361
9d48860e 2362The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
96449e8e 2363the same way.
2364
6e0c6552 2365If the argument to C<-in> is an empty array, 'sqlfalse' is generated
be21dde3 2366(by default: C<1=0>). Similarly, C<< -not_in => [] >> generates
2367'sqltrue' (by default: C<1=1>).
6e0c6552 2368
e41c3bdd 2369In addition to the array you can supply a chunk of literal sql or
2370literal sql with bind:
6e0c6552 2371
e41c3bdd 2372 my %where = {
2373 customer => { -in => \[
2374 'SELECT cust_id FROM cust WHERE balance > ?',
2375 2000,
2376 ],
2377 status => { -in => \'SELECT status_codes FROM states' },
2378 };
6e0c6552 2379
e41c3bdd 2380would generate:
2381
2382 $stmt = "WHERE (
2383 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
2384 AND status IN ( SELECT status_codes FROM states )
2385 )";
2386 @bind = ('2000');
2387
0dfd2442 2388Finally, if the argument to C<-in> is not a reference, it will be
2389treated as a single-element array.
e41c3bdd 2390
2391Another pair of operators is C<-between> and C<-not_between>,
96449e8e 2392used with an arrayref of two values:
32eab2da 2393
2394 my %where = (
2395 user => 'nwiger',
2396 completion_date => {
2397 -not_between => ['2002-10-01', '2003-02-06']
2398 }
2399 );
2400
2401Would give you:
2402
2403 WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
2404
e41c3bdd 2405Just like with C<-in> all plausible combinations of literal SQL
2406are possible:
2407
2408 my %where = {
2409 start0 => { -between => [ 1, 2 ] },
2410 start1 => { -between => \["? AND ?", 1, 2] },
2411 start2 => { -between => \"lower(x) AND upper(y)" },
9d48860e 2412 start3 => { -between => [
e41c3bdd 2413 \"lower(x)",
2414 \["upper(?)", 'stuff' ],
2415 ] },
2416 };
2417
2418Would give you:
2419
2420 $stmt = "WHERE (
2421 ( start0 BETWEEN ? AND ? )
2422 AND ( start1 BETWEEN ? AND ? )
2423 AND ( start2 BETWEEN lower(x) AND upper(y) )
2424 AND ( start3 BETWEEN lower(x) AND upper(?) )
2425 )";
2426 @bind = (1, 2, 1, 2, 'stuff');
2427
2428
9d48860e 2429These are the two builtin "special operators"; but the
be21dde3 2430list can be expanded: see section L</"SPECIAL OPERATORS"> below.
96449e8e 2431
59f23b3d 2432=head2 Unary operators: bool
97a920ef 2433
2434If you wish to test against boolean columns or functions within your
2435database you can use the C<-bool> and C<-not_bool> operators. For
2436example to test the column C<is_user> being true and the column
827bb0eb 2437C<is_enabled> being false you would use:-
97a920ef 2438
2439 my %where = (
2440 -bool => 'is_user',
2441 -not_bool => 'is_enabled',
2442 );
2443
2444Would give you:
2445
277b5d3f 2446 WHERE is_user AND NOT is_enabled
97a920ef 2447
0b604e9d 2448If a more complex combination is required, testing more conditions,
2449then you should use the and/or operators:-
2450
2451 my %where = (
2452 -and => [
2453 -bool => 'one',
23401b81 2454 -not_bool => { two=> { -rlike => 'bar' } },
2455 -not_bool => { three => [ { '=', 2 }, { '>', 5 } ] },
0b604e9d 2456 ],
2457 );
2458
2459Would give you:
2460
23401b81 2461 WHERE
2462 one
2463 AND
2464 (NOT two RLIKE ?)
2465 AND
2466 (NOT ( three = ? OR three > ? ))
97a920ef 2467
2468
107b72f1 2469=head2 Nested conditions, -and/-or prefixes
96449e8e 2470
32eab2da 2471So far, we've seen how multiple conditions are joined with a top-level
2472C<AND>. We can change this by putting the different conditions we want in
2473hashes and then putting those hashes in an array. For example:
2474
2475 my @where = (
2476 {
2477 user => 'nwiger',
2478 status => { -like => ['pending%', 'dispatched'] },
2479 },
2480 {
2481 user => 'robot',
2482 status => 'unassigned',
2483 }
2484 );
2485
2486This data structure would create the following:
2487
2488 $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
2489 OR ( user = ? AND status = ? ) )";
2490 @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
2491
107b72f1 2492
48d9f5f8 2493Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or>
be21dde3 2494to change the logic inside:
32eab2da 2495
2496 my @where = (
2497 -and => [
2498 user => 'nwiger',
48d9f5f8 2499 [
2500 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
2501 -or => { workhrs => {'<', 50}, geo => 'EURO' },
32eab2da 2502 ],
2503 ],
2504 );
2505
2506That would yield:
2507
13cc86af 2508 $stmt = "WHERE ( user = ?
2509 AND ( ( workhrs > ? AND geo = ? )
2510 OR ( workhrs < ? OR geo = ? ) ) )";
2511 @bind = ('nwiger', '20', 'ASIA', '50', 'EURO');
107b72f1 2512
cc422895 2513=head3 Algebraic inconsistency, for historical reasons
107b72f1 2514
7cac25e6 2515C<Important note>: when connecting several conditions, the C<-and->|C<-or>
2516operator goes C<outside> of the nested structure; whereas when connecting
2517several constraints on one column, the C<-and> operator goes
be21dde3 2518C<inside> the arrayref. Here is an example combining both features:
7cac25e6 2519
2520 my @where = (
2521 -and => [a => 1, b => 2],
2522 -or => [c => 3, d => 4],
2523 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
2524 )
2525
2526yielding
2527
9d48860e 2528 WHERE ( ( ( a = ? AND b = ? )
2529 OR ( c = ? OR d = ? )
7cac25e6 2530 OR ( e LIKE ? AND e LIKE ? ) ) )
2531
107b72f1 2532This difference in syntax is unfortunate but must be preserved for
be21dde3 2533historical reasons. So be careful: the two examples below would
107b72f1 2534seem algebraically equivalent, but they are not
2535
a948b1fe 2536 { col => [ -and =>
2537 { -like => 'foo%' },
2538 { -like => '%bar' },
2539 ] }
be21dde3 2540 # yields: WHERE ( ( col LIKE ? AND col LIKE ? ) )
107b72f1 2541
a948b1fe 2542 [ -and =>
2543 { col => { -like => 'foo%' } },
2544 { col => { -like => '%bar' } },
2545 ]
be21dde3 2546 # yields: WHERE ( ( col LIKE ? OR col LIKE ? ) )
107b72f1 2547
7cac25e6 2548
cc422895 2549=head2 Literal SQL and value type operators
96449e8e 2550
cc422895 2551The basic premise of SQL::Abstract is that in WHERE specifications the "left
2552side" is a column name and the "right side" is a value (normally rendered as
2553a placeholder). This holds true for both hashrefs and arrayref pairs as you
2554see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to
2555alter this behavior. There are several ways of doing so.
e9614080 2556
cc422895 2557=head3 -ident
2558
2559This is a virtual operator that signals the string to its right side is an
2560identifier (a column name) and not a value. For example to compare two
2561columns you would write:
32eab2da 2562
e9614080 2563 my %where = (
2564 priority => { '<', 2 },
cc422895 2565 requestor => { -ident => 'submitter' },
e9614080 2566 );
2567
2568which creates:
2569
2570 $stmt = "WHERE priority < ? AND requestor = submitter";
2571 @bind = ('2');
2572
cc422895 2573If you are maintaining legacy code you may see a different construct as
2574described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new
2575code.
2576
2577=head3 -value
e9614080 2578
cc422895 2579This is a virtual operator that signals that the construct to its right side
2580is a value to be passed to DBI. This is for example necessary when you want
2581to write a where clause against an array (for RDBMS that support such
2582datatypes). For example:
e9614080 2583
32eab2da 2584 my %where = (
cc422895 2585 array => { -value => [1, 2, 3] }
32eab2da 2586 );
2587
cc422895 2588will result in:
32eab2da 2589
cc422895 2590 $stmt = 'WHERE array = ?';
2591 @bind = ([1, 2, 3]);
32eab2da 2592
cc422895 2593Note that if you were to simply say:
32eab2da 2594
2595 my %where = (
cc422895 2596 array => [1, 2, 3]
32eab2da 2597 );
2598
3af02ccb 2599the result would probably not be what you wanted:
cc422895 2600
2601 $stmt = 'WHERE array = ? OR array = ? OR array = ?';
2602 @bind = (1, 2, 3);
2603
2604=head3 Literal SQL
96449e8e 2605
cc422895 2606Finally, sometimes only literal SQL will do. To include a random snippet
2607of SQL verbatim, you specify it as a scalar reference. Consider this only
2608as a last resort. Usually there is a better way. For example:
96449e8e 2609
2610 my %where = (
cc422895 2611 priority => { '<', 2 },
2612 requestor => { -in => \'(SELECT name FROM hitmen)' },
96449e8e 2613 );
2614
cc422895 2615Would create:
96449e8e 2616
cc422895 2617 $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)"
2618 @bind = (2);
2619
2620Note that in this example, you only get one bind parameter back, since
2621the verbatim SQL is passed as part of the statement.
2622
2623=head4 CAVEAT
2624
2625 Never use untrusted input as a literal SQL argument - this is a massive
2626 security risk (there is no way to check literal snippets for SQL
2627 injections and other nastyness). If you need to deal with untrusted input
2628 use literal SQL with placeholders as described next.
96449e8e 2629
cc422895 2630=head3 Literal SQL with placeholders and bind values (subqueries)
96449e8e 2631
2632If the literal SQL to be inserted has placeholders and bind values,
2633use a reference to an arrayref (yes this is a double reference --
2634not so common, but perfectly legal Perl). For example, to find a date
2635in Postgres you can use something like this:
2636
2637 my %where = (
3ae1c5e2 2638 date_column => \[ "= date '2008-09-30' - ?::integer", 10 ]
96449e8e 2639 )
2640
2641This would create:
2642
d2a8fe1a 2643 $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
96449e8e 2644 @bind = ('10');
2645
deb148a2 2646Note that you must pass the bind values in the same format as they are returned
85783f3c 2647by L<where|/where(\%where, $order)>. This means that if you set L</bindtype>
1f490ae4 2648to C<columns>, you must provide the bind values in the
2649C<< [ column_meta => value ] >> format, where C<column_meta> is an opaque
2650scalar value; most commonly the column name, but you can use any scalar value
2651(including references and blessed references), L<SQL::Abstract> will simply
2652pass it through intact. So if C<bindtype> is set to C<columns> the above
2653example will look like:
deb148a2 2654
2655 my %where = (
3ae1c5e2 2656 date_column => \[ "= date '2008-09-30' - ?::integer", [ {} => 10 ] ]
deb148a2 2657 )
96449e8e 2658
2659Literal SQL is especially useful for nesting parenthesized clauses in the
be21dde3 2660main SQL query. Here is a first example:
96449e8e 2661
2662 my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
2663 100, "foo%");
2664 my %where = (
2665 foo => 1234,
2666 bar => \["IN ($sub_stmt)" => @sub_bind],
2667 );
2668
be21dde3 2669This yields:
96449e8e 2670
9d48860e 2671 $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
96449e8e 2672 WHERE c2 < ? AND c3 LIKE ?))";
2673 @bind = (1234, 100, "foo%");
2674
9d48860e 2675Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
96449e8e 2676are expressed in the same way. Of course the C<$sub_stmt> and
9d48860e 2677its associated bind values can be generated through a former call
96449e8e 2678to C<select()> :
2679
2680 my ($sub_stmt, @sub_bind)
9d48860e 2681 = $sql->select("t1", "c1", {c2 => {"<" => 100},
96449e8e 2682 c3 => {-like => "foo%"}});
2683 my %where = (
2684 foo => 1234,
2685 bar => \["> ALL ($sub_stmt)" => @sub_bind],
2686 );
2687
2688In the examples above, the subquery was used as an operator on a column;
9d48860e 2689but the same principle also applies for a clause within the main C<%where>
be21dde3 2690hash, like an EXISTS subquery:
96449e8e 2691
9d48860e 2692 my ($sub_stmt, @sub_bind)
96449e8e 2693 = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
48d9f5f8 2694 my %where = ( -and => [
96449e8e 2695 foo => 1234,
48d9f5f8 2696 \["EXISTS ($sub_stmt)" => @sub_bind],
2697 ]);
96449e8e 2698
2699which yields
2700
9d48860e 2701 $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
96449e8e 2702 WHERE c1 = ? AND c2 > t0.c0))";
2703 @bind = (1234, 1);
2704
2705
9d48860e 2706Observe that the condition on C<c2> in the subquery refers to
be21dde3 2707column C<t0.c0> of the main query: this is I<not> a bind
9d48860e 2708value, so we have to express it through a scalar ref.
96449e8e 2709Writing C<< c2 => {">" => "t0.c0"} >> would have generated
2710C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
2711what we wanted here.
2712
96449e8e 2713Finally, here is an example where a subquery is used
2714for expressing unary negation:
2715
9d48860e 2716 my ($sub_stmt, @sub_bind)
96449e8e 2717 = $sql->where({age => [{"<" => 10}, {">" => 20}]});
2718 $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
2719 my %where = (
2720 lname => {like => '%son%'},
48d9f5f8 2721 \["NOT ($sub_stmt)" => @sub_bind],
96449e8e 2722 );
2723
2724This yields
2725
2726 $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
2727 @bind = ('%son%', 10, 20)
2728
cc422895 2729=head3 Deprecated usage of Literal SQL
2730
2731Below are some examples of archaic use of literal SQL. It is shown only as
2732reference for those who deal with legacy code. Each example has a much
2733better, cleaner and safer alternative that users should opt for in new code.
2734
2735=over
2736
2737=item *
2738
2739 my %where = ( requestor => \'IS NOT NULL' )
2740
2741 $stmt = "WHERE requestor IS NOT NULL"
2742
2743This used to be the way of generating NULL comparisons, before the handling
2744of C<undef> got formalized. For new code please use the superior syntax as
2745described in L</Tests for NULL values>.
96449e8e 2746
cc422895 2747=item *
2748
2749 my %where = ( requestor => \'= submitter' )
2750
2751 $stmt = "WHERE requestor = submitter"
2752
2753This used to be the only way to compare columns. Use the superior L</-ident>
2754method for all new code. For example an identifier declared in such a way
2755will be properly quoted if L</quote_char> is properly set, while the legacy
2756form will remain as supplied.
2757
2758=item *
2759
2760 my %where = ( is_ready => \"", completed => { '>', '2012-12-21' } )
2761
2762 $stmt = "WHERE completed > ? AND is_ready"
2763 @bind = ('2012-12-21')
2764
2765Using an empty string literal used to be the only way to express a boolean.
2766For all new code please use the much more readable
2767L<-bool|/Unary operators: bool> operator.
2768
2769=back
96449e8e 2770
2771=head2 Conclusion
2772
32eab2da 2773These pages could go on for a while, since the nesting of the data
2774structures this module can handle are pretty much unlimited (the
2775module implements the C<WHERE> expansion as a recursive function
2776internally). Your best bet is to "play around" with the module a
2777little to see how the data structures behave, and choose the best
2778format for your data based on that.
2779
2780And of course, all the values above will probably be replaced with
2781variables gotten from forms or the command line. After all, if you
2782knew everything ahead of time, you wouldn't have to worry about
2783dynamically-generating SQL and could just hardwire it into your
2784script.
2785
86298391 2786=head1 ORDER BY CLAUSES
2787
9d48860e 2788Some functions take an order by clause. This can either be a scalar (just a
18710f60 2789column name), a hashref of C<< { -desc => 'col' } >> or C<< { -asc => 'col' }
2790>>, a scalarref, an arrayref-ref, or an arrayref of any of the previous
2791forms. Examples:
1cfa1db3 2792
8c15b421 2793 Given | Will Generate
18710f60 2794 ---------------------------------------------------------------
8c15b421 2795 |
2796 'colA' | ORDER BY colA
2797 |
2798 [qw/colA colB/] | ORDER BY colA, colB
2799 |
2800 {-asc => 'colA'} | ORDER BY colA ASC
2801 |
2802 {-desc => 'colB'} | ORDER BY colB DESC
2803 |
2804 ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC
2805 |
2806 { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC
2807 |
2808 \'colA DESC' | ORDER BY colA DESC
2809 |
2810 \[ 'FUNC(colA, ?)', $x ] | ORDER BY FUNC(colA, ?)
2811 | /* ...with $x bound to ? */
2812 |
bd805d85 2813 [ | ORDER BY
2814 { -asc => 'colA' }, | colA ASC,
2815 { -desc => [qw/colB/] }, | colB DESC,
2816 { -asc => [qw/colC colD/] },| colC ASC, colD ASC,
2817 \'colE DESC', | colE DESC,
2818 \[ 'FUNC(colF, ?)', $x ], | FUNC(colF, ?)
2819 ] | /* ...with $x bound to ? */
18710f60 2820 ===============================================================
86298391 2821
96449e8e 2822
2823
2824=head1 SPECIAL OPERATORS
2825
e3f9dff4 2826 my $sqlmaker = SQL::Abstract->new(special_ops => [
3a2e1a5e 2827 {
2828 regex => qr/.../,
e3f9dff4 2829 handler => sub {
2830 my ($self, $field, $op, $arg) = @_;
2831 ...
3a2e1a5e 2832 },
2833 },
2834 {
2835 regex => qr/.../,
2836 handler => 'method_name',
e3f9dff4 2837 },
2838 ]);
2839
9d48860e 2840A "special operator" is a SQL syntactic clause that can be
e3f9dff4 2841applied to a field, instead of a usual binary operator.
be21dde3 2842For example:
e3f9dff4 2843
2844 WHERE field IN (?, ?, ?)
2845 WHERE field BETWEEN ? AND ?
2846 WHERE MATCH(field) AGAINST (?, ?)
96449e8e 2847
e3f9dff4 2848Special operators IN and BETWEEN are fairly standard and therefore
3a2e1a5e 2849are builtin within C<SQL::Abstract> (as the overridable methods
2850C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
2851like the MATCH .. AGAINST example above which is specific to MySQL,
2852you can write your own operator handlers - supply a C<special_ops>
2853argument to the C<new> method. That argument takes an arrayref of
2854operator definitions; each operator definition is a hashref with two
2855entries:
96449e8e 2856
e3f9dff4 2857=over
2858
2859=item regex
2860
2861the regular expression to match the operator
96449e8e 2862
e3f9dff4 2863=item handler
2864
3a2e1a5e 2865Either a coderef or a plain scalar method name. In both cases
2866the expected return is C<< ($sql, @bind) >>.
2867
2868When supplied with a method name, it is simply called on the
13cc86af 2869L<SQL::Abstract> object as:
3a2e1a5e 2870
ca4f826a 2871 $self->$method_name($field, $op, $arg)
3a2e1a5e 2872
2873 Where:
2874
3a2e1a5e 2875 $field is the LHS of the operator
13cc86af 2876 $op is the part that matched the handler regex
3a2e1a5e 2877 $arg is the RHS
2878
2879When supplied with a coderef, it is called as:
2880
2881 $coderef->($self, $field, $op, $arg)
2882
e3f9dff4 2883
2884=back
2885
9d48860e 2886For example, here is an implementation
e3f9dff4 2887of the MATCH .. AGAINST syntax for MySQL
2888
2889 my $sqlmaker = SQL::Abstract->new(special_ops => [
9d48860e 2890
e3f9dff4 2891 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
9d48860e 2892 {regex => qr/^match$/i,
e3f9dff4 2893 handler => sub {
2894 my ($self, $field, $op, $arg) = @_;
2895 $arg = [$arg] if not ref $arg;
2896 my $label = $self->_quote($field);
2897 my ($placeholder) = $self->_convert('?');
2898 my $placeholders = join ", ", (($placeholder) x @$arg);
2899 my $sql = $self->_sqlcase('match') . " ($label) "
2900 . $self->_sqlcase('against') . " ($placeholders) ";
2901 my @bind = $self->_bindtype($field, @$arg);
2902 return ($sql, @bind);
2903 }
2904 },
9d48860e 2905
e3f9dff4 2906 ]);
96449e8e 2907
2908
59f23b3d 2909=head1 UNARY OPERATORS
2910
112b5232 2911 my $sqlmaker = SQL::Abstract->new(unary_ops => [
59f23b3d 2912 {
2913 regex => qr/.../,
2914 handler => sub {
2915 my ($self, $op, $arg) = @_;
2916 ...
2917 },
2918 },
2919 {
2920 regex => qr/.../,
2921 handler => 'method_name',
2922 },
2923 ]);
2924
9d48860e 2925A "unary operator" is a SQL syntactic clause that can be
59f23b3d 2926applied to a field - the operator goes before the field
2927
2928You can write your own operator handlers - supply a C<unary_ops>
2929argument to the C<new> method. That argument takes an arrayref of
2930operator definitions; each operator definition is a hashref with two
2931entries:
2932
2933=over
2934
2935=item regex
2936
2937the regular expression to match the operator
2938
2939=item handler
2940
2941Either a coderef or a plain scalar method name. In both cases
2942the expected return is C<< $sql >>.
2943
2944When supplied with a method name, it is simply called on the
13cc86af 2945L<SQL::Abstract> object as:
59f23b3d 2946
ca4f826a 2947 $self->$method_name($op, $arg)
59f23b3d 2948
2949 Where:
2950
2951 $op is the part that matched the handler regex
2952 $arg is the RHS or argument of the operator
2953
2954When supplied with a coderef, it is called as:
2955
2956 $coderef->($self, $op, $arg)
2957
2958
2959=back
2960
2961
32eab2da 2962=head1 PERFORMANCE
2963
2964Thanks to some benchmarking by Mark Stosberg, it turns out that
2965this module is many orders of magnitude faster than using C<DBIx::Abstract>.
2966I must admit this wasn't an intentional design issue, but it's a
2967byproduct of the fact that you get to control your C<DBI> handles
2968yourself.
2969
2970To maximize performance, use a code snippet like the following:
2971
2972 # prepare a statement handle using the first row
2973 # and then reuse it for the rest of the rows
2974 my($sth, $stmt);
2975 for my $href (@array_of_hashrefs) {
2976 $stmt ||= $sql->insert('table', $href);
2977 $sth ||= $dbh->prepare($stmt);
2978 $sth->execute($sql->values($href));
2979 }
2980
2981The reason this works is because the keys in your C<$href> are sorted
2982internally by B<SQL::Abstract>. Thus, as long as your data retains
2983the same structure, you only have to generate the SQL the first time
2984around. On subsequent queries, simply use the C<values> function provided
2985by this module to return your values in the correct order.
2986
b864ba9b 2987However this depends on the values having the same type - if, for
2988example, the values of a where clause may either have values
2989(resulting in sql of the form C<column = ?> with a single bind
2990value), or alternatively the values might be C<undef> (resulting in
2991sql of the form C<column IS NULL> with no bind value) then the
2992caching technique suggested will not work.
96449e8e 2993
32eab2da 2994=head1 FORMBUILDER
2995
2996If you use my C<CGI::FormBuilder> module at all, you'll hopefully
2997really like this part (I do, at least). Building up a complex query
2998can be as simple as the following:
2999
3000 #!/usr/bin/perl
3001
46dc2f3e 3002 use warnings;
3003 use strict;
3004
32eab2da 3005 use CGI::FormBuilder;
3006 use SQL::Abstract;
3007
3008 my $form = CGI::FormBuilder->new(...);
3009 my $sql = SQL::Abstract->new;
3010
3011 if ($form->submitted) {
3012 my $field = $form->field;
3013 my $id = delete $field->{id};
3014 my($stmt, @bind) = $sql->update('table', $field, {id => $id});
3015 }
3016
3017Of course, you would still have to connect using C<DBI> to run the
3018query, but the point is that if you make your form look like your
3019table, the actual query script can be extremely simplistic.
3020
3021If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
9d48860e 3022a fast interface to returning and formatting data. I frequently
32eab2da 3023use these three modules together to write complex database query
3024apps in under 50 lines.
3025
af733667 3026=head1 HOW TO CONTRIBUTE
3027
3028Contributions are always welcome, in all usable forms (we especially
3029welcome documentation improvements). The delivery methods include git-
3030or unified-diff formatted patches, GitHub pull requests, or plain bug
3031reports either via RT or the Mailing list. Contributors are generally
3032granted full access to the official repository after their first several
3033patches pass successful review.
3034
3035This project is maintained in a git repository. The code and related tools are
3036accessible at the following locations:
d8cc1792 3037
3038=over
3039
af733667 3040=item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git>
3041
3042=item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git>
3043
3044=item * GitHub mirror: L<https://github.com/dbsrgits/sql-abstract>
d8cc1792 3045
af733667 3046=item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/SQL-Abstract.git>
d8cc1792 3047
3048=back
32eab2da 3049
96449e8e 3050=head1 CHANGES
3051
3052Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
3053Great care has been taken to preserve the I<published> behavior
3054documented in previous versions in the 1.* family; however,
9d48860e 3055some features that were previously undocumented, or behaved
96449e8e 3056differently from the documentation, had to be changed in order
3057to clarify the semantics. Hence, client code that was relying
9d48860e 3058on some dark areas of C<SQL::Abstract> v1.*
96449e8e 3059B<might behave differently> in v1.50.
32eab2da 3060
be21dde3 3061The main changes are:
d2a8fe1a 3062
96449e8e 3063=over
32eab2da 3064
9d48860e 3065=item *
32eab2da 3066
3ae1c5e2 3067support for literal SQL through the C<< \ [ $sql, @bind ] >> syntax.
96449e8e 3068
3069=item *
3070
145fbfc8 3071support for the { operator => \"..." } construct (to embed literal SQL)
3072
3073=item *
3074
9c37b9c0 3075support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
3076
3077=item *
3078
96449e8e 3079optional support for L<array datatypes|/"Inserting and Updating Arrays">
3080
9d48860e 3081=item *
96449e8e 3082
be21dde3 3083defensive programming: check arguments
96449e8e 3084
3085=item *
3086
3087fixed bug with global logic, which was previously implemented
7cac25e6 3088through global variables yielding side-effects. Prior versions would
96449e8e 3089interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
3090as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
3091Now this is interpreted
3092as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
3093
96449e8e 3094
3095=item *
3096
3097fixed semantics of _bindtype on array args
3098
9d48860e 3099=item *
96449e8e 3100
3101dropped the C<_anoncopy> of the %where tree. No longer necessary,
3102we just avoid shifting arrays within that tree.
3103
3104=item *
3105
3106dropped the C<_modlogic> function
3107
3108=back
32eab2da 3109
32eab2da 3110=head1 ACKNOWLEDGEMENTS
3111
3112There are a number of individuals that have really helped out with
3113this module. Unfortunately, most of them submitted bugs via CPAN
3114so I have no idea who they are! But the people I do know are:
3115
9d48860e 3116 Ash Berlin (order_by hash term support)
b643abe1 3117 Matt Trout (DBIx::Class support)
32eab2da 3118 Mark Stosberg (benchmarking)
3119 Chas Owens (initial "IN" operator support)
3120 Philip Collins (per-field SQL functions)
3121 Eric Kolve (hashref "AND" support)
3122 Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
3123 Dan Kubb (support for "quote_char" and "name_sep")
f5aab26e 3124 Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
48d9f5f8 3125 Laurent Dami (internal refactoring, extensible list of special operators, literal SQL)
dbdf7648 3126 Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
e96c510a 3127 Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
02288357 3128 Oliver Charles (support for "RETURNING" after "INSERT")
32eab2da 3129
3130Thanks!
3131
32eab2da 3132=head1 SEE ALSO
3133
86298391 3134L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
32eab2da 3135
32eab2da 3136=head1 AUTHOR
3137
b643abe1 3138Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
3139
3140This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
32eab2da 3141
abe72f94 3142For support, your best bet is to try the C<DBIx::Class> users mailing list.
3143While not an official support venue, C<DBIx::Class> makes heavy use of
3144C<SQL::Abstract>, and as such list members there are very familiar with
3145how to create queries.
3146
0d067ded 3147=head1 LICENSE
3148
d988ab87 3149This module is free software; you may copy this under the same
3150terms as perl itself (either the GNU General Public License or
3151the Artistic License)
32eab2da 3152
3153=cut