1 package SQL::Abstract; # see doc at end of file
10 our @EXPORT_OK = qw(is_plain_value is_literal_value);
20 *SQL::Abstract::_ENV_::DETECT_AUTOGENERATED_STRINGIFICATION = $ENV{SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION}
26 #======================================================================
28 #======================================================================
30 our $VERSION = '1.87';
32 # This would confuse some packagers
33 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
37 # special operators (-in, -between). May be extended/overridden by user.
38 # See section WHERE: BUILTIN SPECIAL OPERATORS below for implementation
39 my @BUILTIN_SPECIAL_OPS = (
40 {regex => qr/^ (?: not \s )? between $/ix, handler => sub { die "NOPE" }},
41 {regex => qr/^ (?: not \s )? in $/ix, handler => sub { die "NOPE" }},
42 {regex => qr/^ is (?: \s+ not )? $/ix, handler => sub { die "NOPE" }},
45 #======================================================================
46 # DEBUGGING AND ERROR REPORTING
47 #======================================================================
50 return unless $_[0]->{debug}; shift; # a little faster
51 my $func = (caller(1))[3];
52 warn "[$func] ", @_, "\n";
56 my($func) = (caller(1))[3];
57 Carp::carp "[$func] Warning: ", @_;
61 my($func) = (caller(1))[3];
62 Carp::croak "[$func] Fatal: ", @_;
65 sub is_literal_value ($) {
66 ref $_[0] eq 'SCALAR' ? [ ${$_[0]} ]
67 : ( ref $_[0] eq 'REF' and ref ${$_[0]} eq 'ARRAY' ) ? [ @${ $_[0] } ]
71 # FIXME XSify - this can be done so much more efficiently
72 sub is_plain_value ($) {
74 ! length ref $_[0] ? \($_[0])
76 ref $_[0] eq 'HASH' and keys %{$_[0]} == 1
78 exists $_[0]->{-value}
79 ) ? \($_[0]->{-value})
81 # reuse @_ for even moar speedz
82 defined ( $_[1] = Scalar::Util::blessed $_[0] )
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
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())
93 # either has stringification which DBI SHOULD prefer out of the box
94 grep { *{ (qq[${_}::(""]) }{CODE} } @{ $_[2] = mro::get_linear_isa( $_[1] ) }
96 # has nummification or boolification, AND fallback is *not* disabled
98 SQL::Abstract::_ENV_::DETECT_AUTOGENERATED_STRINGIFICATION
101 grep { *{"${_}::(0+"}{CODE} } @{$_[2]}
103 grep { *{"${_}::(bool"}{CODE} } @{$_[2]}
107 # no fallback specified at all
108 ! ( ($_[3]) = grep { *{"${_}::()"}{CODE} } @{$_[2]} )
110 # fallback explicitly undef
111 ! defined ${"$_[3]::()"}
124 #======================================================================
126 #======================================================================
130 my $class = ref($self) || $self;
131 my %opt = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
133 # choose our case by keeping an option around
134 delete $opt{case} if $opt{case} && $opt{case} ne 'lower';
136 # default logic for interpreting arrayrefs
137 $opt{logic} = $opt{logic} ? uc $opt{logic} : 'OR';
139 # how to return bind vars
140 $opt{bindtype} ||= 'normal';
142 # default comparison is "=", but can be overridden
145 # try to recognize which are the 'equality' and 'inequality' ops
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;
150 $opt{like_op} = qr/^ (is\s+)? r?like $/xi;
151 $opt{not_like_op} = qr/^ (is\s+)? not \s+ r?like $/xi;
154 $opt{sqltrue} ||= '1=1';
155 $opt{sqlfalse} ||= '0=1';
158 $opt{special_ops} ||= [];
160 # regexes are applied in order, thus push after user-defines
161 push @{$opt{special_ops}}, @BUILTIN_SPECIAL_OPS;
163 if ($class->isa('DBIx::Class::SQLMaker')) {
164 push @{$opt{special_ops}}, our $DBIC_Compat_Op ||= {
165 regex => qr/^(?:ident|value)$/i, handler => sub { die "NOPE" }
167 $opt{is_dbic_sqlmaker} = 1;
171 $opt{unary_ops} ||= [];
173 # rudimentary sanity-check for user supplied bits treated as functions/operators
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)
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/
188 (map +("-$_", "_render_$_"), qw(op func bind ident literal list)),
192 $opt{expand_unary} = {};
195 -ident => '_expand_ident',
196 -value => sub { +{ -bind => [ our $Cur_Col_Meta, $_[2] ] } },
197 -not => sub { +{ -op => [ 'not', $_[0]->_expand_expr($_[2]) ] } },
200 return bless \%opt, $class;
203 sub sqltrue { +{ -literal => [ $_[0]->{sqltrue} ] } }
204 sub sqlfalse { +{ -literal => [ $_[0]->{sqlfalse} ] } }
206 sub _assert_pass_injection_guard {
207 if ($_[1] =~ $_[0]->{injection_guard}) {
208 my $class = ref $_[0];
209 puke "Possible SQL injection attempt '$_[1]'. If this is indeed a part of the "
210 . "desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply your own "
211 . "{injection_guard} attribute to ${class}->new()"
216 #======================================================================
218 #======================================================================
222 my $table = $self->_table(shift);
223 my $data = shift || return;
226 my $method = $self->_METHOD_FOR_refkind("_insert", $data);
227 my ($sql, @bind) = $self->$method($data);
228 $sql = join " ", $self->_sqlcase('insert into'), $table, $sql;
230 if ($options->{returning}) {
231 my ($s, @b) = $self->_insert_returning($options);
236 return wantarray ? ($sql, @bind) : $sql;
239 # So that subclasses can override INSERT ... RETURNING separately from
240 # UPDATE and DELETE (e.g. DBIx::Class::SQLMaker::Oracle does this)
241 sub _insert_returning { shift->_returning(@_) }
244 my ($self, $options) = @_;
246 my $f = $options->{returning};
248 my ($sql, @bind) = $self->render_aqt(
249 $self->_expand_maybe_list_expr($f, undef, -ident)
252 ? $self->_sqlcase(' returning ') . $sql
253 : ($self->_sqlcase(' returning ').$sql, @bind);
256 sub _insert_HASHREF { # explicit list of fields and then values
257 my ($self, $data) = @_;
259 my @fields = sort keys %$data;
261 my ($sql, @bind) = $self->_insert_values($data);
264 $_ = $self->_quote($_) foreach @fields;
265 $sql = "( ".join(", ", @fields).") ".$sql;
267 return ($sql, @bind);
270 sub _insert_ARRAYREF { # just generate values(?,?) part (no list of fields)
271 my ($self, $data) = @_;
273 # no names (arrayref) so can't generate bindtype
274 $self->{bindtype} ne 'columns'
275 or belch "can't do 'columns' bindtype when called with arrayref";
277 my (@values, @all_bind);
278 foreach my $value (@$data) {
279 my ($values, @bind) = $self->_insert_value(undef, $value);
280 push @values, $values;
281 push @all_bind, @bind;
283 my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
284 return ($sql, @all_bind);
287 sub _insert_ARRAYREFREF { # literal SQL with bind
288 my ($self, $data) = @_;
290 my ($sql, @bind) = @${$data};
291 $self->_assert_bindval_matches_bindtype(@bind);
293 return ($sql, @bind);
297 sub _insert_SCALARREF { # literal SQL without bind
298 my ($self, $data) = @_;
304 my ($self, $data) = @_;
306 my (@values, @all_bind);
307 foreach my $column (sort keys %$data) {
308 my ($values, @bind) = $self->_insert_value($column, $data->{$column});
309 push @values, $values;
310 push @all_bind, @bind;
312 my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
313 return ($sql, @all_bind);
317 my ($self, $column, $v) = @_;
319 return $self->render_aqt(
320 $self->_expand_insert_value($column, $v)
324 sub _expand_insert_value {
325 my ($self, $column, $v) = @_;
327 if (ref($v) eq 'ARRAY') {
328 if ($self->{array_datatypes}) {
329 return +{ -bind => [ $column, $v ] };
331 my ($sql, @bind) = @$v;
332 $self->_assert_bindval_matches_bindtype(@bind);
333 return +{ -literal => $v };
335 if (ref($v) eq 'HASH') {
336 if (grep !/^-/, keys %$v) {
337 belch "HASH ref as bind value in insert is not supported";
338 return +{ -bind => [ $column, $v ] };
342 return +{ -bind => [ $column, undef ] };
344 local our $Cur_Col_Meta = $column;
345 return $self->expand_expr($v);
350 #======================================================================
352 #======================================================================
357 my $table = $self->_table(shift);
358 my $data = shift || return;
362 # first build the 'SET' part of the sql statement
363 puke "Unsupported data type specified to \$sql->update"
364 unless ref $data eq 'HASH';
366 my ($sql, @all_bind) = $self->_update_set_values($data);
367 $sql = $self->_sqlcase('update ') . $table . $self->_sqlcase(' set ')
371 my($where_sql, @where_bind) = $self->where($where);
373 push @all_bind, @where_bind;
376 if ($options->{returning}) {
377 my ($returning_sql, @returning_bind) = $self->_update_returning($options);
378 $sql .= $returning_sql;
379 push @all_bind, @returning_bind;
382 return wantarray ? ($sql, @all_bind) : $sql;
385 sub _update_set_values {
386 my ($self, $data) = @_;
388 return $self->render_aqt(
389 $self->_expand_update_set_values($data),
393 sub _expand_update_set_values {
394 my ($self, $data) = @_;
395 $self->_expand_maybe_list_expr( [
398 $set = { -bind => $_ } unless defined $set;
399 +{ -op => [ '=', $self->_expand_ident(-ident => $k), $set ] };
405 ? ($self->{array_datatypes}
406 ? [ $k, +{ -bind => [ $k, $v ] } ]
407 : [ $k, +{ -literal => $v } ])
409 local our $Cur_Col_Meta = $k;
410 [ $k, $self->_expand_expr($v) ]
417 # So that subclasses can override UPDATE ... RETURNING separately from
419 sub _update_returning { shift->_returning(@_) }
423 #======================================================================
425 #======================================================================
430 my $table = $self->_table(shift);
431 my $fields = shift || '*';
435 my ($fields_sql, @bind) = $self->_select_fields($fields);
437 my ($where_sql, @where_bind) = $self->where($where, $order);
438 push @bind, @where_bind;
440 my $sql = join(' ', $self->_sqlcase('select'), $fields_sql,
441 $self->_sqlcase('from'), $table)
444 return wantarray ? ($sql, @bind) : $sql;
448 my ($self, $fields) = @_;
449 return $fields unless ref($fields);
450 return $self->render_aqt(
451 $self->_expand_maybe_list_expr($fields, undef, '-ident')
455 #======================================================================
457 #======================================================================
462 my $table = $self->_table(shift);
466 my($where_sql, @bind) = $self->where($where);
467 my $sql = $self->_sqlcase('delete from ') . $table . $where_sql;
469 if ($options->{returning}) {
470 my ($returning_sql, @returning_bind) = $self->_delete_returning($options);
471 $sql .= $returning_sql;
472 push @bind, @returning_bind;
475 return wantarray ? ($sql, @bind) : $sql;
478 # So that subclasses can override DELETE ... RETURNING separately from
480 sub _delete_returning { shift->_returning(@_) }
484 #======================================================================
486 #======================================================================
490 # Finally, a separate routine just to handle WHERE clauses
492 my ($self, $where, $order) = @_;
494 local $self->{convert_where} = $self->{convert};
497 my ($sql, @bind) = defined($where)
498 ? $self->_recurse_where($where)
500 $sql = (defined $sql and length $sql) ? $self->_sqlcase(' where ') . "( $sql )" : '';
504 my ($order_sql, @order_bind) = $self->_order_by($order);
506 push @bind, @order_bind;
509 return wantarray ? ($sql, @bind) : $sql;
513 my ($self, $expr, $default_scalar_to) = @_;
514 local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to;
515 $self->_expand_expr($expr);
519 my ($self, $aqt) = @_;
520 my ($k, $v, @rest) = %$aqt;
522 if (my $meth = $self->{render}{$k}) {
523 return $self->$meth($v);
525 die "notreached: $k";
529 my ($self, $expr) = @_;
530 $self->render_aqt($self->expand_expr($expr));
534 my ($self, $expr, $logic) = @_;
535 our $Expand_Depth ||= 0; local $Expand_Depth = $Expand_Depth + 1;
536 return undef unless defined($expr);
537 if (ref($expr) eq 'HASH') {
538 return undef unless my $kc = keys %$expr;
543 map $self->_expand_expr({ $_ => $expr->{$_} }, $logic),
547 my ($key, $value) = %$expr;
548 if (my $exp = $self->{expand}{$key}) {
549 return $self->$exp($key, $value);
551 return $self->_expand_expr_hashpair($key, $value, $logic);
553 if (ref($expr) eq 'ARRAY') {
554 my $logic = lc($logic || $self->{logic});
555 $logic eq 'and' or $logic eq 'or' or puke "unknown logic: $logic";
558 (ref($_) eq 'ARRAY' and @$_)
559 or (ref($_) eq 'HASH' and %$_)
565 while (my ($el) = splice @expr, 0, 1) {
566 puke "Supplying an empty left hand side argument is not supported in array-pairs"
567 unless defined($el) and length($el);
568 my $elref = ref($el);
570 local $Expand_Depth = 0;
571 push(@res, grep defined, $self->_expand_expr({ $el, shift(@expr) }));
572 } elsif ($elref eq 'ARRAY') {
573 push(@res, grep defined, $self->_expand_expr($el)) if @$el;
574 } elsif (my $l = is_literal_value($el)) {
575 push @res, { -literal => $l };
576 } elsif ($elref eq 'HASH') {
577 local $Expand_Depth = 0;
578 push @res, grep defined, $self->_expand_expr($el) if %$el;
584 # return $res[0] if @res == 1;
585 return { -op => [ $logic, @res ] };
587 if (my $literal = is_literal_value($expr)) {
588 return +{ -literal => $literal };
590 if (!ref($expr) or Scalar::Util::blessed($expr)) {
591 if (my $d = our $Default_Scalar_To) {
592 return $self->_expand_expr({ $d => $expr });
594 if (my $m = our $Cur_Col_Meta) {
595 return +{ -bind => [ $m, $expr ] };
597 return +{ -bind => [ undef, $expr ] };
602 sub _expand_expr_hashpair {
603 my ($self, $k, $v, $logic) = @_;
604 unless (defined($k) and length($k)) {
605 if (defined($k) and my $literal = is_literal_value($v)) {
606 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
607 return { -literal => $literal };
609 puke "Supplying an empty left hand side argument is not supported";
612 $self->_assert_pass_injection_guard($k =~ /^-(.*)$/s);
613 if ($k =~ s/ [_\s]? \d+ $//x ) {
614 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
615 . "You probably wanted ...-and => [ $k => COND1, $k => COND2 ... ]";
618 # DBIx::Class requires a nest warning to be emitted once but the private
619 # method it overrode to do so no longer exists
620 if ($self->{is_dbic_sqlmaker}) {
621 unless (our $Nest_Warned) {
623 "-nest in search conditions is deprecated, you most probably wanted:\n"
624 .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }|
629 return $self->_expand_expr($v);
633 return $self->_expand_expr($v);
635 puke "-bool => undef not supported" unless defined($v);
636 return $self->_expand_ident(-ident => $v);
638 if (my ($rest) = $k =~/^-not[_ ](.*)$/) {
641 $self->_expand_expr({ "-${rest}", $v }, $logic)
644 if (my ($logic) = $k =~ /^-(and|or)$/i) {
645 if (ref($v) eq 'HASH') {
646 return $self->_expand_expr($v, $logic);
648 if (ref($v) eq 'ARRAY') {
649 return $self->_expand_expr($v, $logic);
654 $op =~ s/^-// if length($op) > 1;
656 # top level special ops are illegal in general
657 # note that, arguably, if it makes no sense at top level, it also
658 # makes no sense on the other side of an = sign or similar but DBIC
659 # gets disappointingly upset if I disallow it
661 (our $Expand_Depth) == 1
662 and List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}
664 puke "Illegal use of top-level '-$op'"
666 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
667 return { -op => [ $op, $v ] };
670 if (my $custom = $self->{expand_unary}{$k}) {
671 return $self->$custom($v);
673 if ($self->{render}{$k}) {
679 and (keys %$v)[0] =~ /^-/
681 my ($func) = $k =~ /^-(.*)$/;
682 if (List::Util::first { $func =~ $_->{regex} } @{$self->{special_ops}}) {
683 return +{ -op => [ $func, $self->_expand_expr($v) ] };
685 return +{ -func => [ $func, $self->_expand_expr($v) ] };
687 if (!ref($v) or is_literal_value($v)) {
688 return +{ -op => [ $k =~ /^-(.*)$/, $self->_expand_expr($v) ] };
695 and exists $v->{-value}
696 and not defined $v->{-value}
699 return $self->_expand_expr({ $k => { $self->{cmp} => undef } });
701 if (!ref($v) or Scalar::Util::blessed($v)) {
702 my $d = our $Default_Scalar_To;
706 $self->_expand_ident(-ident => $k),
708 ? $self->_expand_expr($d => $v)
709 : { -bind => [ $k, $v ] }
714 if (ref($v) eq 'HASH') {
718 map $self->_expand_expr({ $k => { $_ => $v->{$_} } }),
722 return undef unless keys %$v;
724 my $op = join ' ', split '_', (map lc, $vk =~ /^-?(.*)$/)[0];
725 $self->_assert_pass_injection_guard($op);
726 if ($op =~ s/ [_\s]? \d+ $//x ) {
727 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
728 . "You probably wanted ...-and => [ -$op => COND1, -$op => COND2 ... ]";
730 if ($op =~ /^(?:not )?between$/) {
731 local our $Cur_Col_Meta = $k;
732 my @rhs = map $self->_expand_expr($_),
733 ref($vv) eq 'ARRAY' ? @$vv : $vv;
735 (@rhs == 1 and ref($rhs[0]) eq 'HASH' and $rhs[0]->{-literal})
737 (@rhs == 2 and defined($rhs[0]) and defined($rhs[1]))
739 puke "Operator '${\uc($op)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref";
743 $self->_expand_ident(-ident => $k),
747 if ($op =~ /^(?:not )?in$/) {
748 if (my $literal = is_literal_value($vv)) {
749 my ($sql, @bind) = @$literal;
750 my $opened_sql = $self->_open_outer_paren($sql);
752 $op, $self->_expand_ident(-ident => $k),
753 [ { -literal => [ $opened_sql, @bind ] } ]
757 'SQL::Abstract before v1.75 used to generate incorrect SQL when the '
758 . "-${\uc($op)} operator was given an undef-containing list: !!!AUDIT YOUR CODE "
759 . 'AND DATA!!! (the upcoming Data::Query-based version of SQL::Abstract '
760 . 'will emit the logically correct SQL instead of raising this exception)'
762 puke("Argument passed to the '${\uc($op)}' operator can not be undefined")
764 my @rhs = map $self->_expand_expr($_),
765 map { ref($_) ? $_ : { -bind => [ $k, $_ ] } }
766 map { defined($_) ? $_: puke($undef_err) }
767 (ref($vv) eq 'ARRAY' ? @$vv : $vv);
768 return $self->${\($op =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs;
772 $self->_expand_ident(-ident => $k),
776 if ($op eq 'ident') {
777 if (! defined $vv or (ref($vv) and ref($vv) eq 'ARRAY')) {
778 puke "-$op requires a single plain scalar argument (a quotable identifier) or an arrayref of identifier parts";
782 $self->_expand_ident(-ident => $k),
783 $self->_expand_ident(-ident => $vv),
786 if ($op eq 'value') {
787 return $self->_expand_expr({ $k, undef }) unless defined($vv);
790 $self->_expand_ident(-ident => $k),
791 { -bind => [ $k, $vv ] }
794 if ($op =~ /^is(?: not)?$/) {
795 puke "$op can only take undef as argument"
799 and exists($vv->{-value})
800 and !defined($vv->{-value})
802 return +{ -op => [ $op.' null', $self->_expand_ident(-ident => $k) ] };
804 if ($op =~ /^(and|or)$/) {
805 if (ref($vv) eq 'HASH') {
808 map $self->_expand_expr({ $k, { $_ => $vv->{$_} } }),
813 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}) {
814 return { -op => [ $op, $self->_expand_ident(-ident => $k), $vv ] };
816 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
819 $self->_expand_ident(-ident => $k),
820 { -op => [ $op, $vv ] }
823 if (ref($vv) eq 'ARRAY') {
824 my ($logic, @values) = (
825 (defined($vv->[0]) and $vv->[0] =~ /^-(and|or)$/i)
830 $op =~ $self->{inequality_op}
831 or $op =~ $self->{not_like_op}
833 if (lc($logic) eq '-or' and @values > 1) {
834 belch "A multi-element arrayref as an argument to the inequality op '${\uc($op)}' "
835 . 'is technically equivalent to an always-true 1=1 (you probably wanted '
836 . "to say ...{ \$inequality_op => [ -and => \@values ] }... instead)"
841 # try to DWIM on equality operators
843 $op =~ $self->{equality_op} ? $self->sqlfalse
844 : $op =~ $self->{like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqlfalse
845 : $op =~ $self->{inequality_op} ? $self->sqltrue
846 : $op =~ $self->{not_like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqltrue
847 : puke "operator '$op' applied on an empty array (field '$k')";
851 map $self->_expand_expr({ $k => { $vk => $_ } }),
859 and exists $vv->{-value}
860 and not defined $vv->{-value}
864 $op =~ /^not$/i ? 'is not' # legacy
865 : $op =~ $self->{equality_op} ? 'is'
866 : $op =~ $self->{like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is'
867 : $op =~ $self->{inequality_op} ? 'is not'
868 : $op =~ $self->{not_like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is not'
869 : puke "unexpected operator '$op' with undef operand";
870 return +{ -op => [ $is.' null', $self->_expand_ident(-ident => $k) ] };
872 local our $Cur_Col_Meta = $k;
875 $self->_expand_ident(-ident => $k),
876 $self->_expand_expr($vv)
879 if (ref($v) eq 'ARRAY') {
880 return $self->sqlfalse unless @$v;
881 $self->_debug("ARRAY($k) means distribute over elements");
883 $v->[0] =~ /^-((?:and|or))$/i
884 ? ($v = [ @{$v}[1..$#$v] ], $1)
885 : ($self->{logic} || 'or')
889 map $self->_expand_expr({ $k => $_ }, $this_logic), @$v
892 if (my $literal = is_literal_value($v)) {
894 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
897 my ($sql, @bind) = @$literal;
898 if ($self->{bindtype} eq 'columns') {
900 $self->_assert_bindval_matches_bindtype($_);
903 return +{ -literal => [ $self->_quote($k).' '.$sql, @bind ] };
909 my ($self, undef, $body) = @_;
910 my @parts = map split(/\Q${\($self->{name_sep}||'.')}\E/, $_),
911 ref($body) ? @$body : $body;
912 return { -ident => $parts[-1] } if $self->{_dequalify_idents};
913 unless ($self->{quote_char}) {
914 $self->_assert_pass_injection_guard($_) for @parts;
916 return +{ -ident => \@parts };
920 my ($self, $where, $logic) = @_;
922 # Special case: top level simple string treated as literal
924 my $where_exp = (ref($where)
925 ? $self->_expand_expr($where, $logic)
926 : { -literal => [ $where ] });
928 # dispatch expanded expression
930 my ($sql, @bind) = defined($where_exp) ? $self->render_aqt($where_exp) : (undef);
931 # DBIx::Class used to call _recurse_where in scalar context
932 # something else might too...
934 return ($sql, @bind);
937 belch "Calling _recurse_where in scalar context is deprecated and will go away before 2.0";
943 my ($self, $ident) = @_;
945 return $self->_convert($self->_quote($ident));
948 my %unop_postfix = map +($_ => 1),
949 'is null', 'is not null',
957 my ($self, $args) = @_;
958 my ($left, $low, $high) = @$args;
959 my ($rhsql, @rhbind) = do {
961 puke "Single arg to between must be a literal"
962 unless $low->{-literal};
965 my ($l, $h) = map [ $self->render_aqt($_) ], $low, $high;
966 (join(' ', $l->[0], $self->_sqlcase('and'), $h->[0]),
967 @{$l}[1..$#$l], @{$h}[1..$#$h])
970 my ($lhsql, @lhbind) = $self->render_aqt($left);
972 join(' ', '(', $lhsql, $self->_sqlcase($op), $rhsql, ')'),
976 }), 'between', 'not between'),
980 my ($self, $args) = @_;
981 my ($lhs, $rhs) = @$args;
984 my ($sql, @bind) = $self->render_aqt($_);
985 push @in_bind, @bind;
988 my ($lhsql, @lbind) = $self->render_aqt($lhs);
990 $lhsql.' '.$self->_sqlcase($op).' ( '
1000 my ($self, $v) = @_;
1001 my ($op, @args) = @$v;
1002 $op =~ s/^-// if length($op) > 1;
1004 if (my $h = $special{$op}) {
1005 return $self->$h(\@args);
1007 my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
1008 if ($us and @args > 1) {
1009 puke "Special op '${op}' requires first value to be identifier"
1010 unless my ($ident) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0];
1011 my $k = join(($self->{name_sep}||'.'), @$ident);
1012 local our $Expand_Depth = 1;
1013 return $self->${\($us->{handler})}($k, $op, $args[1]);
1015 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
1016 return $self->${\($us->{handler})}($op, $args[0]);
1018 my $final_op = $op =~ /^(?:is|not)_/ ? join(' ', split '_', $op) : $op;
1019 if (@args == 1 and $op !~ /^(and|or)$/) {
1020 my ($expr_sql, @bind) = $self->render_aqt($args[0]);
1021 my $op_sql = $self->_sqlcase($final_op);
1023 $unop_postfix{lc($final_op)}
1024 ? "${expr_sql} ${op_sql}"
1025 : "${op_sql} ${expr_sql}"
1027 return (($op eq 'not' || $us ? '('.$final_sql.')' : $final_sql), @bind);
1029 my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @args;
1030 return '' unless @parts;
1031 my $is_andor = !!($op =~ /^(and|or)$/);
1032 return @{$parts[0]} if $is_andor and @parts == 1;
1033 my ($final_sql) = map +($is_andor ? "( ${_} )" : $_), join(
1034 ' '.$self->_sqlcase($final_op).' ',
1039 map @{$_}[1..$#$_], @parts
1046 my ($self, $list) = @_;
1047 my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @$list;
1048 return join(', ', map $_->[0], @parts), map @{$_}[1..$#$_], @parts;
1052 my ($self, $rest) = @_;
1053 my ($func, @args) = @$rest;
1057 push @arg_sql, shift @x;
1059 } map [ $self->render_aqt($_) ], @args;
1060 return ($self->_sqlcase($func).'('.join(', ', @arg_sql).')', @bind);
1064 my ($self, $bind) = @_;
1065 return ($self->_convert('?'), $self->_bindtype(@$bind));
1068 sub _render_literal {
1069 my ($self, $literal) = @_;
1070 $self->_assert_bindval_matches_bindtype(@{$literal}[1..$#$literal]);
1074 # Some databases (SQLite) treat col IN (1, 2) different from
1075 # col IN ( (1, 2) ). Use this to strip all outer parens while
1076 # adding them back in the corresponding method
1077 sub _open_outer_paren {
1078 my ($self, $sql) = @_;
1080 while (my ($inner) = $sql =~ /^ \s* \( (.*) \) \s* $/xs) {
1082 # there are closing parens inside, need the heavy duty machinery
1083 # to reevaluate the extraction starting from $sql (full reevaluation)
1084 if ($inner =~ /\)/) {
1085 require Text::Balanced;
1087 my (undef, $remainder) = do {
1088 # idiotic design - writes to $@ but *DOES NOT* throw exceptions
1090 Text::Balanced::extract_bracketed($sql, '()', qr/\s*/);
1093 # the entire expression needs to be a balanced bracketed thing
1094 # (after an extract no remainder sans trailing space)
1095 last if defined $remainder and $remainder =~ /\S/;
1105 #======================================================================
1107 #======================================================================
1109 sub _expand_order_by {
1110 my ($self, $arg) = @_;
1112 return unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg);
1114 my $expander = sub {
1115 my ($self, $dir, $expr) = @_;
1116 my @to_expand = ref($expr) eq 'ARRAY' ? @$expr : $expr;
1117 foreach my $arg (@to_expand) {
1121 and grep /^-(asc|desc)$/, keys %$arg
1123 puke "ordering direction hash passed to order by must have exactly one key (-asc or -desc)";
1126 my @exp = map +(defined($dir) ? { -op => [ $dir => $_ ] } : $_),
1127 map $self->expand_expr($_, -ident),
1128 map ref($_) eq 'ARRAY' ? @$_ : $_, @to_expand;
1129 return (@exp > 1 ? { -list => \@exp } : $exp[0]);
1132 local @{$self->{expand_unary}}{qw(-asc -desc)} = (
1133 sub { shift->$expander(asc => @_) },
1134 sub { shift->$expander(desc => @_) },
1137 return $self->$expander(undef, $arg);
1141 my ($self, $arg) = @_;
1143 return '' unless defined(my $expanded = $self->_expand_order_by($arg));
1145 my ($sql, @bind) = $self->render_aqt($expanded);
1147 return '' unless length($sql);
1149 my $final_sql = $self->_sqlcase(' order by ').$sql;
1151 return wantarray ? ($final_sql, @bind) : $final_sql;
1154 # _order_by no longer needs to call this so doesn't but DBIC uses it.
1156 sub _order_by_chunks {
1157 my ($self, $arg) = @_;
1159 return () unless defined(my $expanded = $self->_expand_order_by($arg));
1161 return $self->_chunkify_order_by($expanded);
1164 sub _chunkify_order_by {
1165 my ($self, $expanded) = @_;
1167 return grep length, $self->render_aqt($expanded)
1168 if $expanded->{-ident} or @{$expanded->{-literal}||[]} == 1;
1171 if (ref() eq 'HASH' and my $l = $_->{-list}) {
1172 return map $self->_chunkify_order_by($_), @$l;
1174 return [ $self->render_aqt($_) ];
1178 #======================================================================
1179 # DATASOURCE (FOR NOW, JUST PLAIN TABLE OR LIST OF TABLES)
1180 #======================================================================
1186 $self->_expand_maybe_list_expr($from, undef, -ident)
1191 #======================================================================
1193 #======================================================================
1195 sub _expand_maybe_list_expr {
1196 my ($self, $expr, $logic, $default) = @_;
1198 if (ref($expr) eq 'ARRAY') {
1200 map $self->expand_expr($_, $default), @$expr
1207 return $self->expand_expr($e, $default);
1210 # highly optimized, as it's called way too often
1212 # my ($self, $label) = @_;
1214 return '' unless defined $_[1];
1215 return ${$_[1]} if ref($_[1]) eq 'SCALAR';
1216 puke 'Identifier cannot be hashref' if ref($_[1]) eq 'HASH';
1218 unless ($_[0]->{quote_char}) {
1219 if (ref($_[1]) eq 'ARRAY') {
1220 return join($_[0]->{name_sep}||'.', @{$_[1]});
1222 $_[0]->_assert_pass_injection_guard($_[1]);
1227 my $qref = ref $_[0]->{quote_char};
1229 !$qref ? ($_[0]->{quote_char}, $_[0]->{quote_char})
1230 : ($qref eq 'ARRAY') ? @{$_[0]->{quote_char}}
1231 : puke "Unsupported quote_char format: $_[0]->{quote_char}";
1233 my $esc = $_[0]->{escape_char} || $r;
1235 # parts containing * are naturally unquoted
1237 $_[0]->{name_sep}||'',
1241 : do { (my $n = $_) =~ s/(\Q$esc\E|\Q$r\E)/$esc$1/g; $l . $n . $r }
1243 (ref($_[1]) eq 'ARRAY'
1247 ? split (/\Q$_[0]->{name_sep}\E/, $_[1] )
1255 # Conversion, if applicable
1257 #my ($self, $arg) = @_;
1258 if ($_[0]->{convert_where}) {
1259 return $_[0]->_sqlcase($_[0]->{convert_where}) .'(' . $_[1] . ')';
1266 #my ($self, $col, @vals) = @_;
1267 # called often - tighten code
1268 return $_[0]->{bindtype} eq 'columns'
1269 ? map {[$_[1], $_]} @_[2 .. $#_]
1274 # Dies if any element of @bind is not in [colname => value] format
1275 # if bindtype is 'columns'.
1276 sub _assert_bindval_matches_bindtype {
1277 # my ($self, @bind) = @_;
1279 if ($self->{bindtype} eq 'columns') {
1281 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
1282 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
1288 sub _join_sql_clauses {
1289 my ($self, $logic, $clauses_aref, $bind_aref) = @_;
1291 if (@$clauses_aref > 1) {
1292 my $join = " " . $self->_sqlcase($logic) . " ";
1293 my $sql = '( ' . join($join, @$clauses_aref) . ' )';
1294 return ($sql, @$bind_aref);
1296 elsif (@$clauses_aref) {
1297 return ($clauses_aref->[0], @$bind_aref); # no parentheses
1300 return (); # if no SQL, ignore @$bind_aref
1305 # Fix SQL case, if so requested
1307 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
1308 # don't touch the argument ... crooked logic, but let's not change it!
1309 return $_[0]->{case} ? $_[1] : uc($_[1]);
1313 #======================================================================
1314 # DISPATCHING FROM REFKIND
1315 #======================================================================
1318 my ($self, $data) = @_;
1320 return 'UNDEF' unless defined $data;
1322 # blessed objects are treated like scalars
1323 my $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
1325 return 'SCALAR' unless $ref;
1328 while ($ref eq 'REF') {
1330 $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
1334 return ($ref||'SCALAR') . ('REF' x $n_steps);
1338 my ($self, $data) = @_;
1339 my @try = ($self->_refkind($data));
1340 push @try, 'SCALAR_or_UNDEF' if $try[0] eq 'SCALAR' || $try[0] eq 'UNDEF';
1341 push @try, 'FALLBACK';
1345 sub _METHOD_FOR_refkind {
1346 my ($self, $meth_prefix, $data) = @_;
1349 for (@{$self->_try_refkind($data)}) {
1350 $method = $self->can($meth_prefix."_".$_)
1354 return $method || puke "cannot dispatch on '$meth_prefix' for ".$self->_refkind($data);
1358 sub _SWITCH_refkind {
1359 my ($self, $data, $dispatch_table) = @_;
1362 for (@{$self->_try_refkind($data)}) {
1363 $coderef = $dispatch_table->{$_}
1367 puke "no dispatch entry for ".$self->_refkind($data)
1376 #======================================================================
1377 # VALUES, GENERATE, AUTOLOAD
1378 #======================================================================
1380 # LDNOTE: original code from nwiger, didn't touch code in that section
1381 # I feel the AUTOLOAD stuff should not be the default, it should
1382 # only be activated on explicit demand by user.
1386 my $data = shift || return;
1387 puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
1388 unless ref $data eq 'HASH';
1391 foreach my $k (sort keys %$data) {
1392 my $v = $data->{$k};
1393 $self->_SWITCH_refkind($v, {
1395 if ($self->{array_datatypes}) { # array datatype
1396 push @all_bind, $self->_bindtype($k, $v);
1398 else { # literal SQL with bind
1399 my ($sql, @bind) = @$v;
1400 $self->_assert_bindval_matches_bindtype(@bind);
1401 push @all_bind, @bind;
1404 ARRAYREFREF => sub { # literal SQL with bind
1405 my ($sql, @bind) = @${$v};
1406 $self->_assert_bindval_matches_bindtype(@bind);
1407 push @all_bind, @bind;
1409 SCALARREF => sub { # literal SQL without bind
1411 SCALAR_or_UNDEF => sub {
1412 push @all_bind, $self->_bindtype($k, $v);
1423 my(@sql, @sqlq, @sqlv);
1427 if ($ref eq 'HASH') {
1428 for my $k (sort keys %$_) {
1431 my $label = $self->_quote($k);
1432 if ($r eq 'ARRAY') {
1433 # literal SQL with bind
1434 my ($sql, @bind) = @$v;
1435 $self->_assert_bindval_matches_bindtype(@bind);
1436 push @sqlq, "$label = $sql";
1438 } elsif ($r eq 'SCALAR') {
1439 # literal SQL without bind
1440 push @sqlq, "$label = $$v";
1442 push @sqlq, "$label = ?";
1443 push @sqlv, $self->_bindtype($k, $v);
1446 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
1447 } elsif ($ref eq 'ARRAY') {
1448 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
1451 if ($r eq 'ARRAY') { # literal SQL with bind
1452 my ($sql, @bind) = @$v;
1453 $self->_assert_bindval_matches_bindtype(@bind);
1456 } elsif ($r eq 'SCALAR') { # literal SQL without bind
1457 # embedded literal SQL
1464 push @sql, '(' . join(', ', @sqlq) . ')';
1465 } elsif ($ref eq 'SCALAR') {
1469 # strings get case twiddled
1470 push @sql, $self->_sqlcase($_);
1474 my $sql = join ' ', @sql;
1476 # this is pretty tricky
1477 # if ask for an array, return ($stmt, @bind)
1478 # otherwise, s/?/shift @sqlv/ to put it inline
1480 return ($sql, @sqlv);
1482 1 while $sql =~ s/\?/my $d = shift(@sqlv);
1483 ref $d ? $d->[1] : $d/e;
1492 # This allows us to check for a local, then _form, attr
1494 my($name) = $AUTOLOAD =~ /.*::(.+)/;
1495 return $self->generate($name, @_);
1506 SQL::Abstract - Generate SQL from Perl data structures
1512 my $sql = SQL::Abstract->new;
1514 my($stmt, @bind) = $sql->select($source, \@fields, \%where, $order);
1516 my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
1518 my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
1520 my($stmt, @bind) = $sql->delete($table, \%where);
1522 # Then, use these in your DBI statements
1523 my $sth = $dbh->prepare($stmt);
1524 $sth->execute(@bind);
1526 # Just generate the WHERE clause
1527 my($stmt, @bind) = $sql->where(\%where, $order);
1529 # Return values in the same order, for hashed queries
1530 # See PERFORMANCE section for more details
1531 my @bind = $sql->values(\%fieldvals);
1535 This module was inspired by the excellent L<DBIx::Abstract>.
1536 However, in using that module I found that what I really wanted
1537 to do was generate SQL, but still retain complete control over my
1538 statement handles and use the DBI interface. So, I set out to
1539 create an abstract SQL generation module.
1541 While based on the concepts used by L<DBIx::Abstract>, there are
1542 several important differences, especially when it comes to WHERE
1543 clauses. I have modified the concepts used to make the SQL easier
1544 to generate from Perl data structures and, IMO, more intuitive.
1545 The underlying idea is for this module to do what you mean, based
1546 on the data structures you provide it. The big advantage is that
1547 you don't have to modify your code every time your data changes,
1548 as this module figures it out.
1550 To begin with, an SQL INSERT is as easy as just specifying a hash
1551 of C<key=value> pairs:
1554 name => 'Jimbo Bobson',
1555 phone => '123-456-7890',
1556 address => '42 Sister Lane',
1557 city => 'St. Louis',
1558 state => 'Louisiana',
1561 The SQL can then be generated with this:
1563 my($stmt, @bind) = $sql->insert('people', \%data);
1565 Which would give you something like this:
1567 $stmt = "INSERT INTO people
1568 (address, city, name, phone, state)
1569 VALUES (?, ?, ?, ?, ?)";
1570 @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
1571 '123-456-7890', 'Louisiana');
1573 These are then used directly in your DBI code:
1575 my $sth = $dbh->prepare($stmt);
1576 $sth->execute(@bind);
1578 =head2 Inserting and Updating Arrays
1580 If your database has array types (like for example Postgres),
1581 activate the special option C<< array_datatypes => 1 >>
1582 when creating the C<SQL::Abstract> object.
1583 Then you may use an arrayref to insert and update database array types:
1585 my $sql = SQL::Abstract->new(array_datatypes => 1);
1587 planets => [qw/Mercury Venus Earth Mars/]
1590 my($stmt, @bind) = $sql->insert('solar_system', \%data);
1594 $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
1596 @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
1599 =head2 Inserting and Updating SQL
1601 In order to apply SQL functions to elements of your C<%data> you may
1602 specify a reference to an arrayref for the given hash value. For example,
1603 if you need to execute the Oracle C<to_date> function on a value, you can
1604 say something like this:
1608 date_entered => \[ "to_date(?,'MM/DD/YYYY')", "03/02/2003" ],
1611 The first value in the array is the actual SQL. Any other values are
1612 optional and would be included in the bind values array. This gives
1615 my($stmt, @bind) = $sql->insert('people', \%data);
1617 $stmt = "INSERT INTO people (name, date_entered)
1618 VALUES (?, to_date(?,'MM/DD/YYYY'))";
1619 @bind = ('Bill', '03/02/2003');
1621 An UPDATE is just as easy, all you change is the name of the function:
1623 my($stmt, @bind) = $sql->update('people', \%data);
1625 Notice that your C<%data> isn't touched; the module will generate
1626 the appropriately quirky SQL for you automatically. Usually you'll
1627 want to specify a WHERE clause for your UPDATE, though, which is
1628 where handling C<%where> hashes comes in handy...
1630 =head2 Complex where statements
1632 This module can generate pretty complicated WHERE statements
1633 easily. For example, simple C<key=value> pairs are taken to mean
1634 equality, and if you want to see if a field is within a set
1635 of values, you can use an arrayref. Let's say we wanted to
1636 SELECT some data based on this criteria:
1639 requestor => 'inna',
1640 worker => ['nwiger', 'rcwe', 'sfz'],
1641 status => { '!=', 'completed' }
1644 my($stmt, @bind) = $sql->select('tickets', '*', \%where);
1646 The above would give you something like this:
1648 $stmt = "SELECT * FROM tickets WHERE
1649 ( requestor = ? ) AND ( status != ? )
1650 AND ( worker = ? OR worker = ? OR worker = ? )";
1651 @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
1653 Which you could then use in DBI code like so:
1655 my $sth = $dbh->prepare($stmt);
1656 $sth->execute(@bind);
1662 The methods are simple. There's one for every major SQL operation,
1663 and a constructor you use first. The arguments are specified in a
1664 similar order for each method (table, then fields, then a where
1665 clause) to try and simplify things.
1667 =head2 new(option => 'value')
1669 The C<new()> function takes a list of options and values, and returns
1670 a new B<SQL::Abstract> object which can then be used to generate SQL
1671 through the methods below. The options accepted are:
1677 If set to 'lower', then SQL will be generated in all lowercase. By
1678 default SQL is generated in "textbook" case meaning something like:
1680 SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
1682 Any setting other than 'lower' is ignored.
1686 This determines what the default comparison operator is. By default
1687 it is C<=>, meaning that a hash like this:
1689 %where = (name => 'nwiger', email => 'nate@wiger.org');
1691 Will generate SQL like this:
1693 WHERE name = 'nwiger' AND email = 'nate@wiger.org'
1695 However, you may want loose comparisons by default, so if you set
1696 C<cmp> to C<like> you would get SQL such as:
1698 WHERE name like 'nwiger' AND email like 'nate@wiger.org'
1700 You can also override the comparison on an individual basis - see
1701 the huge section on L</"WHERE CLAUSES"> at the bottom.
1703 =item sqltrue, sqlfalse
1705 Expressions for inserting boolean values within SQL statements.
1706 By default these are C<1=1> and C<1=0>. They are used
1707 by the special operators C<-in> and C<-not_in> for generating
1708 correct SQL even when the argument is an empty array (see below).
1712 This determines the default logical operator for multiple WHERE
1713 statements in arrays or hashes. If absent, the default logic is "or"
1714 for arrays, and "and" for hashes. This means that a WHERE
1718 event_date => {'>=', '2/13/99'},
1719 event_date => {'<=', '4/24/03'},
1722 will generate SQL like this:
1724 WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
1726 This is probably not what you want given this query, though (look
1727 at the dates). To change the "OR" to an "AND", simply specify:
1729 my $sql = SQL::Abstract->new(logic => 'and');
1731 Which will change the above C<WHERE> to:
1733 WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
1735 The logic can also be changed locally by inserting
1736 a modifier in front of an arrayref:
1738 @where = (-and => [event_date => {'>=', '2/13/99'},
1739 event_date => {'<=', '4/24/03'} ]);
1741 See the L</"WHERE CLAUSES"> section for explanations.
1745 This will automatically convert comparisons using the specified SQL
1746 function for both column and value. This is mostly used with an argument
1747 of C<upper> or C<lower>, so that the SQL will have the effect of
1748 case-insensitive "searches". For example, this:
1750 $sql = SQL::Abstract->new(convert => 'upper');
1751 %where = (keywords => 'MaKe iT CAse inSeNSItive');
1753 Will turn out the following SQL:
1755 WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
1757 The conversion can be C<upper()>, C<lower()>, or any other SQL function
1758 that can be applied symmetrically to fields (actually B<SQL::Abstract> does
1759 not validate this option; it will just pass through what you specify verbatim).
1763 This is a kludge because many databases suck. For example, you can't
1764 just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
1765 Instead, you have to use C<bind_param()>:
1767 $sth->bind_param(1, 'reg data');
1768 $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
1770 The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
1771 which loses track of which field each slot refers to. Fear not.
1773 If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
1774 Currently, you can specify either C<normal> (default) or C<columns>. If you
1775 specify C<columns>, you will get an array that looks like this:
1777 my $sql = SQL::Abstract->new(bindtype => 'columns');
1778 my($stmt, @bind) = $sql->insert(...);
1781 [ 'column1', 'value1' ],
1782 [ 'column2', 'value2' ],
1783 [ 'column3', 'value3' ],
1786 You can then iterate through this manually, using DBI's C<bind_param()>.
1788 $sth->prepare($stmt);
1791 my($col, $data) = @$_;
1792 if ($col eq 'details' || $col eq 'comments') {
1793 $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
1794 } elsif ($col eq 'image') {
1795 $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
1797 $sth->bind_param($i, $data);
1801 $sth->execute; # execute without @bind now
1803 Now, why would you still use B<SQL::Abstract> if you have to do this crap?
1804 Basically, the advantage is still that you don't have to care which fields
1805 are or are not included. You could wrap that above C<for> loop in a simple
1806 sub called C<bind_fields()> or something and reuse it repeatedly. You still
1807 get a layer of abstraction over manual SQL specification.
1809 Note that if you set L</bindtype> to C<columns>, the C<\[ $sql, @bind ]>
1810 construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
1811 will expect the bind values in this format.
1815 This is the character that a table or column name will be quoted
1816 with. By default this is an empty string, but you could set it to
1817 the character C<`>, to generate SQL like this:
1819 SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
1821 Alternatively, you can supply an array ref of two items, the first being the left
1822 hand quote character, and the second the right hand quote character. For
1823 example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
1824 that generates SQL like this:
1826 SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
1828 Quoting is useful if you have tables or columns names that are reserved
1829 words in your database's SQL dialect.
1833 This is the character that will be used to escape L</quote_char>s appearing
1834 in an identifier before it has been quoted.
1836 The parameter default in case of a single L</quote_char> character is the quote
1839 When opening-closing-style quoting is used (L</quote_char> is an arrayref)
1840 this parameter defaults to the B<closing (right)> L</quote_char>. Occurrences
1841 of the B<opening (left)> L</quote_char> within the identifier are currently left
1842 untouched. The default for opening-closing-style quotes may change in future
1843 versions, thus you are B<strongly encouraged> to specify the escape character
1848 This is the character that separates a table and column name. It is
1849 necessary to specify this when the C<quote_char> option is selected,
1850 so that tables and column names can be individually quoted like this:
1852 SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
1854 =item injection_guard
1856 A regular expression C<qr/.../> that is applied to any C<-function> and unquoted
1857 column name specified in a query structure. This is a safety mechanism to avoid
1858 injection attacks when mishandling user input e.g.:
1860 my %condition_as_column_value_pairs = get_values_from_user();
1861 $sqla->select( ... , \%condition_as_column_value_pairs );
1863 If the expression matches an exception is thrown. Note that literal SQL
1864 supplied via C<\'...'> or C<\['...']> is B<not> checked in any way.
1866 Defaults to checking for C<;> and the C<GO> keyword (TransactSQL)
1868 =item array_datatypes
1870 When this option is true, arrayrefs in INSERT or UPDATE are
1871 interpreted as array datatypes and are passed directly
1873 When this option is false, arrayrefs are interpreted
1874 as literal SQL, just like refs to arrayrefs
1875 (but this behavior is for backwards compatibility; when writing
1876 new queries, use the "reference to arrayref" syntax
1882 Takes a reference to a list of "special operators"
1883 to extend the syntax understood by L<SQL::Abstract>.
1884 See section L</"SPECIAL OPERATORS"> for details.
1888 Takes a reference to a list of "unary operators"
1889 to extend the syntax understood by L<SQL::Abstract>.
1890 See section L</"UNARY OPERATORS"> for details.
1896 =head2 insert($table, \@values || \%fieldvals, \%options)
1898 This is the simplest function. You simply give it a table name
1899 and either an arrayref of values or hashref of field/value pairs.
1900 It returns an SQL INSERT statement and a list of bind values.
1901 See the sections on L</"Inserting and Updating Arrays"> and
1902 L</"Inserting and Updating SQL"> for information on how to insert
1903 with those data types.
1905 The optional C<\%options> hash reference may contain additional
1906 options to generate the insert SQL. Currently supported options
1913 Takes either a scalar of raw SQL fields, or an array reference of
1914 field names, and adds on an SQL C<RETURNING> statement at the end.
1915 This allows you to return data generated by the insert statement
1916 (such as row IDs) without performing another C<SELECT> statement.
1917 Note, however, this is not part of the SQL standard and may not
1918 be supported by all database engines.
1922 =head2 update($table, \%fieldvals, \%where, \%options)
1924 This takes a table, hashref of field/value pairs, and an optional
1925 hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
1927 See the sections on L</"Inserting and Updating Arrays"> and
1928 L</"Inserting and Updating SQL"> for information on how to insert
1929 with those data types.
1931 The optional C<\%options> hash reference may contain additional
1932 options to generate the update SQL. Currently supported options
1939 See the C<returning> option to
1940 L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
1944 =head2 select($source, $fields, $where, $order)
1946 This returns a SQL SELECT statement and associated list of bind values, as
1947 specified by the arguments:
1953 Specification of the 'FROM' part of the statement.
1954 The argument can be either a plain scalar (interpreted as a table
1955 name, will be quoted), or an arrayref (interpreted as a list
1956 of table names, joined by commas, quoted), or a scalarref
1957 (literal SQL, not quoted).
1961 Specification of the list of fields to retrieve from
1963 The argument can be either an arrayref (interpreted as a list
1964 of field names, will be joined by commas and quoted), or a
1965 plain scalar (literal SQL, not quoted).
1966 Please observe that this API is not as flexible as that of
1967 the first argument C<$source>, for backwards compatibility reasons.
1971 Optional argument to specify the WHERE part of the query.
1972 The argument is most often a hashref, but can also be
1973 an arrayref or plain scalar --
1974 see section L<WHERE clause|/"WHERE CLAUSES"> for details.
1978 Optional argument to specify the ORDER BY part of the query.
1979 The argument can be a scalar, a hashref or an arrayref
1980 -- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
1986 =head2 delete($table, \%where, \%options)
1988 This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
1989 It returns an SQL DELETE statement and list of bind values.
1991 The optional C<\%options> hash reference may contain additional
1992 options to generate the delete SQL. Currently supported options
1999 See the C<returning> option to
2000 L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
2004 =head2 where(\%where, $order)
2006 This is used to generate just the WHERE clause. For example,
2007 if you have an arbitrary data structure and know what the
2008 rest of your SQL is going to look like, but want an easy way
2009 to produce a WHERE clause, use this. It returns an SQL WHERE
2010 clause and list of bind values.
2013 =head2 values(\%data)
2015 This just returns the values from the hash C<%data>, in the same
2016 order that would be returned from any of the other above queries.
2017 Using this allows you to markedly speed up your queries if you
2018 are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
2020 =head2 generate($any, 'number', $of, \@data, $struct, \%types)
2022 Warning: This is an experimental method and subject to change.
2024 This returns arbitrarily generated SQL. It's a really basic shortcut.
2025 It will return two different things, depending on return context:
2027 my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
2028 my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
2030 These would return the following:
2032 # First calling form
2033 $stmt = "CREATE TABLE test (?, ?)";
2034 @bind = (field1, field2);
2036 # Second calling form
2037 $stmt_and_val = "CREATE TABLE test (field1, field2)";
2039 Depending on what you're trying to do, it's up to you to choose the correct
2040 format. In this example, the second form is what you would want.
2044 $sql->generate('alter session', { nls_date_format => 'MM/YY' });
2048 ALTER SESSION SET nls_date_format = 'MM/YY'
2050 You get the idea. Strings get their case twiddled, but everything
2051 else remains verbatim.
2053 =head1 EXPORTABLE FUNCTIONS
2055 =head2 is_plain_value
2057 Determines if the supplied argument is a plain value as understood by this
2062 =item * The value is C<undef>
2064 =item * The value is a non-reference
2066 =item * The value is an object with stringification overloading
2068 =item * The value is of the form C<< { -value => $anything } >>
2072 On failure returns C<undef>, on success returns a B<scalar> reference
2073 to the original supplied argument.
2079 The stringification overloading detection is rather advanced: it takes
2080 into consideration not only the presence of a C<""> overload, but if that
2081 fails also checks for enabled
2082 L<autogenerated versions of C<"">|overload/Magic Autogeneration>, based
2083 on either C<0+> or C<bool>.
2085 Unfortunately testing in the field indicates that this
2086 detection B<< may tickle a latent bug in perl versions before 5.018 >>,
2087 but only when very large numbers of stringifying objects are involved.
2088 At the time of writing ( Sep 2014 ) there is no clear explanation of
2089 the direct cause, nor is there a manageably small test case that reliably
2090 reproduces the problem.
2092 If you encounter any of the following exceptions in B<random places within
2093 your application stack> - this module may be to blame:
2095 Operation "ne": no method found,
2096 left argument in overloaded package <something>,
2097 right argument in overloaded package <something>
2101 Stub found while resolving method "???" overloading """" in package <something>
2103 If you fall victim to the above - please attempt to reduce the problem
2104 to something that could be sent to the L<SQL::Abstract developers
2105 |DBIx::Class/GETTING HELP/SUPPORT>
2106 (either publicly or privately). As a workaround in the meantime you can
2107 set C<$ENV{SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION}> to a true
2108 value, which will most likely eliminate your problem (at the expense of
2109 not being able to properly detect exotic forms of stringification).
2111 This notice and environment variable will be removed in a future version,
2112 as soon as the underlying problem is found and a reliable workaround is
2117 =head2 is_literal_value
2119 Determines if the supplied argument is a literal value as understood by this
2124 =item * C<\$sql_string>
2126 =item * C<\[ $sql_string, @bind_values ]>
2130 On failure returns C<undef>, on success returns an B<array> reference
2131 containing the unpacked version of the supplied literal SQL and bind values.
2133 =head1 WHERE CLAUSES
2137 This module uses a variation on the idea from L<DBIx::Abstract>. It
2138 is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
2139 module is that things in arrays are OR'ed, and things in hashes
2142 The easiest way to explain is to show lots of examples. After
2143 each C<%where> hash shown, it is assumed you used:
2145 my($stmt, @bind) = $sql->where(\%where);
2147 However, note that the C<%where> hash can be used directly in any
2148 of the other functions as well, as described above.
2150 =head2 Key-value pairs
2152 So, let's get started. To begin, a simple hash:
2156 status => 'completed'
2159 Is converted to SQL C<key = val> statements:
2161 $stmt = "WHERE user = ? AND status = ?";
2162 @bind = ('nwiger', 'completed');
2164 One common thing I end up doing is having a list of values that
2165 a field can be in. To do this, simply specify a list inside of
2170 status => ['assigned', 'in-progress', 'pending'];
2173 This simple code will create the following:
2175 $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
2176 @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
2178 A field associated to an empty arrayref will be considered a
2179 logical false and will generate 0=1.
2181 =head2 Tests for NULL values
2183 If the value part is C<undef> then this is converted to SQL <IS NULL>
2192 $stmt = "WHERE user = ? AND status IS NULL";
2195 To test if a column IS NOT NULL:
2199 status => { '!=', undef },
2202 =head2 Specific comparison operators
2204 If you want to specify a different type of operator for your comparison,
2205 you can use a hashref for a given column:
2209 status => { '!=', 'completed' }
2212 Which would generate:
2214 $stmt = "WHERE user = ? AND status != ?";
2215 @bind = ('nwiger', 'completed');
2217 To test against multiple values, just enclose the values in an arrayref:
2219 status => { '=', ['assigned', 'in-progress', 'pending'] };
2221 Which would give you:
2223 "WHERE status = ? OR status = ? OR status = ?"
2226 The hashref can also contain multiple pairs, in which case it is expanded
2227 into an C<AND> of its elements:
2231 status => { '!=', 'completed', -not_like => 'pending%' }
2234 # Or more dynamically, like from a form
2235 $where{user} = 'nwiger';
2236 $where{status}{'!='} = 'completed';
2237 $where{status}{'-not_like'} = 'pending%';
2239 # Both generate this
2240 $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
2241 @bind = ('nwiger', 'completed', 'pending%');
2244 To get an OR instead, you can combine it with the arrayref idea:
2248 priority => [ { '=', 2 }, { '>', 5 } ]
2251 Which would generate:
2253 $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?";
2254 @bind = ('2', '5', 'nwiger');
2256 If you want to include literal SQL (with or without bind values), just use a
2257 scalar reference or reference to an arrayref as the value:
2260 date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
2261 date_expires => { '<' => \"now()" }
2264 Which would generate:
2266 $stmt = "WHERE date_entered > to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
2267 @bind = ('11/26/2008');
2270 =head2 Logic and nesting operators
2272 In the example above,
2273 there is a subtle trap if you want to say something like
2274 this (notice the C<AND>):
2276 WHERE priority != ? AND priority != ?
2278 Because, in Perl you I<can't> do this:
2280 priority => { '!=' => 2, '!=' => 1 }
2282 As the second C<!=> key will obliterate the first. The solution
2283 is to use the special C<-modifier> form inside an arrayref:
2285 priority => [ -and => {'!=', 2},
2289 Normally, these would be joined by C<OR>, but the modifier tells it
2290 to use C<AND> instead. (Hint: You can use this in conjunction with the
2291 C<logic> option to C<new()> in order to change the way your queries
2292 work by default.) B<Important:> Note that the C<-modifier> goes
2293 B<INSIDE> the arrayref, as an extra first element. This will
2294 B<NOT> do what you think it might:
2296 priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG!
2298 Here is a quick list of equivalencies, since there is some overlap:
2301 status => {'!=', 'completed', 'not like', 'pending%' }
2302 status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
2305 status => {'=', ['assigned', 'in-progress']}
2306 status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
2307 status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
2311 =head2 Special operators: IN, BETWEEN, etc.
2313 You can also use the hashref format to compare a list of fields using the
2314 C<IN> comparison operator, by specifying the list as an arrayref:
2317 status => 'completed',
2318 reportid => { -in => [567, 2335, 2] }
2321 Which would generate:
2323 $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
2324 @bind = ('completed', '567', '2335', '2');
2326 The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
2329 If the argument to C<-in> is an empty array, 'sqlfalse' is generated
2330 (by default: C<1=0>). Similarly, C<< -not_in => [] >> generates
2331 'sqltrue' (by default: C<1=1>).
2333 In addition to the array you can supply a chunk of literal sql or
2334 literal sql with bind:
2337 customer => { -in => \[
2338 'SELECT cust_id FROM cust WHERE balance > ?',
2341 status => { -in => \'SELECT status_codes FROM states' },
2347 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
2348 AND status IN ( SELECT status_codes FROM states )
2352 Finally, if the argument to C<-in> is not a reference, it will be
2353 treated as a single-element array.
2355 Another pair of operators is C<-between> and C<-not_between>,
2356 used with an arrayref of two values:
2360 completion_date => {
2361 -not_between => ['2002-10-01', '2003-02-06']
2367 WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
2369 Just like with C<-in> all plausible combinations of literal SQL
2373 start0 => { -between => [ 1, 2 ] },
2374 start1 => { -between => \["? AND ?", 1, 2] },
2375 start2 => { -between => \"lower(x) AND upper(y)" },
2376 start3 => { -between => [
2378 \["upper(?)", 'stuff' ],
2385 ( start0 BETWEEN ? AND ? )
2386 AND ( start1 BETWEEN ? AND ? )
2387 AND ( start2 BETWEEN lower(x) AND upper(y) )
2388 AND ( start3 BETWEEN lower(x) AND upper(?) )
2390 @bind = (1, 2, 1, 2, 'stuff');
2393 These are the two builtin "special operators"; but the
2394 list can be expanded: see section L</"SPECIAL OPERATORS"> below.
2396 =head2 Unary operators: bool
2398 If you wish to test against boolean columns or functions within your
2399 database you can use the C<-bool> and C<-not_bool> operators. For
2400 example to test the column C<is_user> being true and the column
2401 C<is_enabled> being false you would use:-
2405 -not_bool => 'is_enabled',
2410 WHERE is_user AND NOT is_enabled
2412 If a more complex combination is required, testing more conditions,
2413 then you should use the and/or operators:-
2418 -not_bool => { two=> { -rlike => 'bar' } },
2419 -not_bool => { three => [ { '=', 2 }, { '>', 5 } ] },
2430 (NOT ( three = ? OR three > ? ))
2433 =head2 Nested conditions, -and/-or prefixes
2435 So far, we've seen how multiple conditions are joined with a top-level
2436 C<AND>. We can change this by putting the different conditions we want in
2437 hashes and then putting those hashes in an array. For example:
2442 status => { -like => ['pending%', 'dispatched'] },
2446 status => 'unassigned',
2450 This data structure would create the following:
2452 $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
2453 OR ( user = ? AND status = ? ) )";
2454 @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
2457 Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or>
2458 to change the logic inside:
2464 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
2465 -or => { workhrs => {'<', 50}, geo => 'EURO' },
2472 $stmt = "WHERE ( user = ?
2473 AND ( ( workhrs > ? AND geo = ? )
2474 OR ( workhrs < ? OR geo = ? ) ) )";
2475 @bind = ('nwiger', '20', 'ASIA', '50', 'EURO');
2477 =head3 Algebraic inconsistency, for historical reasons
2479 C<Important note>: when connecting several conditions, the C<-and->|C<-or>
2480 operator goes C<outside> of the nested structure; whereas when connecting
2481 several constraints on one column, the C<-and> operator goes
2482 C<inside> the arrayref. Here is an example combining both features:
2485 -and => [a => 1, b => 2],
2486 -or => [c => 3, d => 4],
2487 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
2492 WHERE ( ( ( a = ? AND b = ? )
2493 OR ( c = ? OR d = ? )
2494 OR ( e LIKE ? AND e LIKE ? ) ) )
2496 This difference in syntax is unfortunate but must be preserved for
2497 historical reasons. So be careful: the two examples below would
2498 seem algebraically equivalent, but they are not
2501 { -like => 'foo%' },
2502 { -like => '%bar' },
2504 # yields: WHERE ( ( col LIKE ? AND col LIKE ? ) )
2507 { col => { -like => 'foo%' } },
2508 { col => { -like => '%bar' } },
2510 # yields: WHERE ( ( col LIKE ? OR col LIKE ? ) )
2513 =head2 Literal SQL and value type operators
2515 The basic premise of SQL::Abstract is that in WHERE specifications the "left
2516 side" is a column name and the "right side" is a value (normally rendered as
2517 a placeholder). This holds true for both hashrefs and arrayref pairs as you
2518 see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to
2519 alter this behavior. There are several ways of doing so.
2523 This is a virtual operator that signals the string to its right side is an
2524 identifier (a column name) and not a value. For example to compare two
2525 columns you would write:
2528 priority => { '<', 2 },
2529 requestor => { -ident => 'submitter' },
2534 $stmt = "WHERE priority < ? AND requestor = submitter";
2537 If you are maintaining legacy code you may see a different construct as
2538 described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new
2543 This is a virtual operator that signals that the construct to its right side
2544 is a value to be passed to DBI. This is for example necessary when you want
2545 to write a where clause against an array (for RDBMS that support such
2546 datatypes). For example:
2549 array => { -value => [1, 2, 3] }
2554 $stmt = 'WHERE array = ?';
2555 @bind = ([1, 2, 3]);
2557 Note that if you were to simply say:
2563 the result would probably not be what you wanted:
2565 $stmt = 'WHERE array = ? OR array = ? OR array = ?';
2570 Finally, sometimes only literal SQL will do. To include a random snippet
2571 of SQL verbatim, you specify it as a scalar reference. Consider this only
2572 as a last resort. Usually there is a better way. For example:
2575 priority => { '<', 2 },
2576 requestor => { -in => \'(SELECT name FROM hitmen)' },
2581 $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)"
2584 Note that in this example, you only get one bind parameter back, since
2585 the verbatim SQL is passed as part of the statement.
2589 Never use untrusted input as a literal SQL argument - this is a massive
2590 security risk (there is no way to check literal snippets for SQL
2591 injections and other nastyness). If you need to deal with untrusted input
2592 use literal SQL with placeholders as described next.
2594 =head3 Literal SQL with placeholders and bind values (subqueries)
2596 If the literal SQL to be inserted has placeholders and bind values,
2597 use a reference to an arrayref (yes this is a double reference --
2598 not so common, but perfectly legal Perl). For example, to find a date
2599 in Postgres you can use something like this:
2602 date_column => \[ "= date '2008-09-30' - ?::integer", 10 ]
2607 $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
2610 Note that you must pass the bind values in the same format as they are returned
2611 by L<where|/where(\%where, $order)>. This means that if you set L</bindtype>
2612 to C<columns>, you must provide the bind values in the
2613 C<< [ column_meta => value ] >> format, where C<column_meta> is an opaque
2614 scalar value; most commonly the column name, but you can use any scalar value
2615 (including references and blessed references), L<SQL::Abstract> will simply
2616 pass it through intact. So if C<bindtype> is set to C<columns> the above
2617 example will look like:
2620 date_column => \[ "= date '2008-09-30' - ?::integer", [ {} => 10 ] ]
2623 Literal SQL is especially useful for nesting parenthesized clauses in the
2624 main SQL query. Here is a first example:
2626 my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
2630 bar => \["IN ($sub_stmt)" => @sub_bind],
2635 $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
2636 WHERE c2 < ? AND c3 LIKE ?))";
2637 @bind = (1234, 100, "foo%");
2639 Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
2640 are expressed in the same way. Of course the C<$sub_stmt> and
2641 its associated bind values can be generated through a former call
2644 my ($sub_stmt, @sub_bind)
2645 = $sql->select("t1", "c1", {c2 => {"<" => 100},
2646 c3 => {-like => "foo%"}});
2649 bar => \["> ALL ($sub_stmt)" => @sub_bind],
2652 In the examples above, the subquery was used as an operator on a column;
2653 but the same principle also applies for a clause within the main C<%where>
2654 hash, like an EXISTS subquery:
2656 my ($sub_stmt, @sub_bind)
2657 = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
2658 my %where = ( -and => [
2660 \["EXISTS ($sub_stmt)" => @sub_bind],
2665 $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
2666 WHERE c1 = ? AND c2 > t0.c0))";
2670 Observe that the condition on C<c2> in the subquery refers to
2671 column C<t0.c0> of the main query: this is I<not> a bind
2672 value, so we have to express it through a scalar ref.
2673 Writing C<< c2 => {">" => "t0.c0"} >> would have generated
2674 C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
2675 what we wanted here.
2677 Finally, here is an example where a subquery is used
2678 for expressing unary negation:
2680 my ($sub_stmt, @sub_bind)
2681 = $sql->where({age => [{"<" => 10}, {">" => 20}]});
2682 $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
2684 lname => {like => '%son%'},
2685 \["NOT ($sub_stmt)" => @sub_bind],
2690 $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
2691 @bind = ('%son%', 10, 20)
2693 =head3 Deprecated usage of Literal SQL
2695 Below are some examples of archaic use of literal SQL. It is shown only as
2696 reference for those who deal with legacy code. Each example has a much
2697 better, cleaner and safer alternative that users should opt for in new code.
2703 my %where = ( requestor => \'IS NOT NULL' )
2705 $stmt = "WHERE requestor IS NOT NULL"
2707 This used to be the way of generating NULL comparisons, before the handling
2708 of C<undef> got formalized. For new code please use the superior syntax as
2709 described in L</Tests for NULL values>.
2713 my %where = ( requestor => \'= submitter' )
2715 $stmt = "WHERE requestor = submitter"
2717 This used to be the only way to compare columns. Use the superior L</-ident>
2718 method for all new code. For example an identifier declared in such a way
2719 will be properly quoted if L</quote_char> is properly set, while the legacy
2720 form will remain as supplied.
2724 my %where = ( is_ready => \"", completed => { '>', '2012-12-21' } )
2726 $stmt = "WHERE completed > ? AND is_ready"
2727 @bind = ('2012-12-21')
2729 Using an empty string literal used to be the only way to express a boolean.
2730 For all new code please use the much more readable
2731 L<-bool|/Unary operators: bool> operator.
2737 These pages could go on for a while, since the nesting of the data
2738 structures this module can handle are pretty much unlimited (the
2739 module implements the C<WHERE> expansion as a recursive function
2740 internally). Your best bet is to "play around" with the module a
2741 little to see how the data structures behave, and choose the best
2742 format for your data based on that.
2744 And of course, all the values above will probably be replaced with
2745 variables gotten from forms or the command line. After all, if you
2746 knew everything ahead of time, you wouldn't have to worry about
2747 dynamically-generating SQL and could just hardwire it into your
2750 =head1 ORDER BY CLAUSES
2752 Some functions take an order by clause. This can either be a scalar (just a
2753 column name), a hashref of C<< { -desc => 'col' } >> or C<< { -asc => 'col' }
2754 >>, a scalarref, an arrayref-ref, or an arrayref of any of the previous
2757 Given | Will Generate
2758 ---------------------------------------------------------------
2760 'colA' | ORDER BY colA
2762 [qw/colA colB/] | ORDER BY colA, colB
2764 {-asc => 'colA'} | ORDER BY colA ASC
2766 {-desc => 'colB'} | ORDER BY colB DESC
2768 ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC
2770 { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC
2772 \'colA DESC' | ORDER BY colA DESC
2774 \[ 'FUNC(colA, ?)', $x ] | ORDER BY FUNC(colA, ?)
2775 | /* ...with $x bound to ? */
2778 { -asc => 'colA' }, | colA ASC,
2779 { -desc => [qw/colB/] }, | colB DESC,
2780 { -asc => [qw/colC colD/] },| colC ASC, colD ASC,
2781 \'colE DESC', | colE DESC,
2782 \[ 'FUNC(colF, ?)', $x ], | FUNC(colF, ?)
2783 ] | /* ...with $x bound to ? */
2784 ===============================================================
2788 =head1 SPECIAL OPERATORS
2790 my $sqlmaker = SQL::Abstract->new(special_ops => [
2794 my ($self, $field, $op, $arg) = @_;
2800 handler => 'method_name',
2804 A "special operator" is a SQL syntactic clause that can be
2805 applied to a field, instead of a usual binary operator.
2808 WHERE field IN (?, ?, ?)
2809 WHERE field BETWEEN ? AND ?
2810 WHERE MATCH(field) AGAINST (?, ?)
2812 Special operators IN and BETWEEN are fairly standard and therefore
2813 are builtin within C<SQL::Abstract> (as the overridable methods
2814 C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
2815 like the MATCH .. AGAINST example above which is specific to MySQL,
2816 you can write your own operator handlers - supply a C<special_ops>
2817 argument to the C<new> method. That argument takes an arrayref of
2818 operator definitions; each operator definition is a hashref with two
2825 the regular expression to match the operator
2829 Either a coderef or a plain scalar method name. In both cases
2830 the expected return is C<< ($sql, @bind) >>.
2832 When supplied with a method name, it is simply called on the
2833 L<SQL::Abstract> object as:
2835 $self->$method_name($field, $op, $arg)
2839 $field is the LHS of the operator
2840 $op is the part that matched the handler regex
2843 When supplied with a coderef, it is called as:
2845 $coderef->($self, $field, $op, $arg)
2850 For example, here is an implementation
2851 of the MATCH .. AGAINST syntax for MySQL
2853 my $sqlmaker = SQL::Abstract->new(special_ops => [
2855 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
2856 {regex => qr/^match$/i,
2858 my ($self, $field, $op, $arg) = @_;
2859 $arg = [$arg] if not ref $arg;
2860 my $label = $self->_quote($field);
2861 my ($placeholder) = $self->_convert('?');
2862 my $placeholders = join ", ", (($placeholder) x @$arg);
2863 my $sql = $self->_sqlcase('match') . " ($label) "
2864 . $self->_sqlcase('against') . " ($placeholders) ";
2865 my @bind = $self->_bindtype($field, @$arg);
2866 return ($sql, @bind);
2873 =head1 UNARY OPERATORS
2875 my $sqlmaker = SQL::Abstract->new(unary_ops => [
2879 my ($self, $op, $arg) = @_;
2885 handler => 'method_name',
2889 A "unary operator" is a SQL syntactic clause that can be
2890 applied to a field - the operator goes before the field
2892 You can write your own operator handlers - supply a C<unary_ops>
2893 argument to the C<new> method. That argument takes an arrayref of
2894 operator definitions; each operator definition is a hashref with two
2901 the regular expression to match the operator
2905 Either a coderef or a plain scalar method name. In both cases
2906 the expected return is C<< $sql >>.
2908 When supplied with a method name, it is simply called on the
2909 L<SQL::Abstract> object as:
2911 $self->$method_name($op, $arg)
2915 $op is the part that matched the handler regex
2916 $arg is the RHS or argument of the operator
2918 When supplied with a coderef, it is called as:
2920 $coderef->($self, $op, $arg)
2928 Thanks to some benchmarking by Mark Stosberg, it turns out that
2929 this module is many orders of magnitude faster than using C<DBIx::Abstract>.
2930 I must admit this wasn't an intentional design issue, but it's a
2931 byproduct of the fact that you get to control your C<DBI> handles
2934 To maximize performance, use a code snippet like the following:
2936 # prepare a statement handle using the first row
2937 # and then reuse it for the rest of the rows
2939 for my $href (@array_of_hashrefs) {
2940 $stmt ||= $sql->insert('table', $href);
2941 $sth ||= $dbh->prepare($stmt);
2942 $sth->execute($sql->values($href));
2945 The reason this works is because the keys in your C<$href> are sorted
2946 internally by B<SQL::Abstract>. Thus, as long as your data retains
2947 the same structure, you only have to generate the SQL the first time
2948 around. On subsequent queries, simply use the C<values> function provided
2949 by this module to return your values in the correct order.
2951 However this depends on the values having the same type - if, for
2952 example, the values of a where clause may either have values
2953 (resulting in sql of the form C<column = ?> with a single bind
2954 value), or alternatively the values might be C<undef> (resulting in
2955 sql of the form C<column IS NULL> with no bind value) then the
2956 caching technique suggested will not work.
2960 If you use my C<CGI::FormBuilder> module at all, you'll hopefully
2961 really like this part (I do, at least). Building up a complex query
2962 can be as simple as the following:
2969 use CGI::FormBuilder;
2972 my $form = CGI::FormBuilder->new(...);
2973 my $sql = SQL::Abstract->new;
2975 if ($form->submitted) {
2976 my $field = $form->field;
2977 my $id = delete $field->{id};
2978 my($stmt, @bind) = $sql->update('table', $field, {id => $id});
2981 Of course, you would still have to connect using C<DBI> to run the
2982 query, but the point is that if you make your form look like your
2983 table, the actual query script can be extremely simplistic.
2985 If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
2986 a fast interface to returning and formatting data. I frequently
2987 use these three modules together to write complex database query
2988 apps in under 50 lines.
2990 =head1 HOW TO CONTRIBUTE
2992 Contributions are always welcome, in all usable forms (we especially
2993 welcome documentation improvements). The delivery methods include git-
2994 or unified-diff formatted patches, GitHub pull requests, or plain bug
2995 reports either via RT or the Mailing list. Contributors are generally
2996 granted full access to the official repository after their first several
2997 patches pass successful review.
2999 This project is maintained in a git repository. The code and related tools are
3000 accessible at the following locations:
3004 =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git>
3006 =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git>
3008 =item * GitHub mirror: L<https://github.com/dbsrgits/sql-abstract>
3010 =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/SQL-Abstract.git>
3016 Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
3017 Great care has been taken to preserve the I<published> behavior
3018 documented in previous versions in the 1.* family; however,
3019 some features that were previously undocumented, or behaved
3020 differently from the documentation, had to be changed in order
3021 to clarify the semantics. Hence, client code that was relying
3022 on some dark areas of C<SQL::Abstract> v1.*
3023 B<might behave differently> in v1.50.
3025 The main changes are:
3031 support for literal SQL through the C<< \ [ $sql, @bind ] >> syntax.
3035 support for the { operator => \"..." } construct (to embed literal SQL)
3039 support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
3043 optional support for L<array datatypes|/"Inserting and Updating Arrays">
3047 defensive programming: check arguments
3051 fixed bug with global logic, which was previously implemented
3052 through global variables yielding side-effects. Prior versions would
3053 interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
3054 as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
3055 Now this is interpreted
3056 as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
3061 fixed semantics of _bindtype on array args
3065 dropped the C<_anoncopy> of the %where tree. No longer necessary,
3066 we just avoid shifting arrays within that tree.
3070 dropped the C<_modlogic> function
3074 =head1 ACKNOWLEDGEMENTS
3076 There are a number of individuals that have really helped out with
3077 this module. Unfortunately, most of them submitted bugs via CPAN
3078 so I have no idea who they are! But the people I do know are:
3080 Ash Berlin (order_by hash term support)
3081 Matt Trout (DBIx::Class support)
3082 Mark Stosberg (benchmarking)
3083 Chas Owens (initial "IN" operator support)
3084 Philip Collins (per-field SQL functions)
3085 Eric Kolve (hashref "AND" support)
3086 Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
3087 Dan Kubb (support for "quote_char" and "name_sep")
3088 Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
3089 Laurent Dami (internal refactoring, extensible list of special operators, literal SQL)
3090 Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
3091 Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
3092 Oliver Charles (support for "RETURNING" after "INSERT")
3098 L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
3102 Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
3104 This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
3106 For support, your best bet is to try the C<DBIx::Class> users mailing list.
3107 While not an official support venue, C<DBIx::Class> makes heavy use of
3108 C<SQL::Abstract>, and as such list members there are very familiar with
3109 how to create queries.
3113 This module is free software; you may copy this under the same
3114 terms as perl itself (either the GNU General Public License or
3115 the Artistic License)