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{user_special_ops} = [ @{$opt{special_ops} ||= []} ];
159 # regexes are applied in order, thus push after user-defines
160 push @{$opt{special_ops}}, @BUILTIN_SPECIAL_OPS;
163 $opt{unary_ops} ||= [];
165 # rudimentary sanity-check for user supplied bits treated as functions/operators
166 # If a purported function matches this regular expression, an exception is thrown.
167 # Literal SQL is *NOT* subject to this check, only functions (and column names
168 # when quoting is not in effect)
171 # need to guard against ()'s in column names too, but this will break tons of
172 # hacks... ideas anyone?
173 $opt{injection_guard} ||= qr/
179 $opt{node_types} = +{
180 map +("-$_" => '_render_'.$_),
181 qw(op func value bind ident literal)
184 return bless \%opt, $class;
187 sub sqltrue { +{ -literal => [ $_[0]->{sqltrue} ] } }
188 sub sqlfalse { +{ -literal => [ $_[0]->{sqlfalse} ] } }
190 sub _assert_pass_injection_guard {
191 if ($_[1] =~ $_[0]->{injection_guard}) {
192 my $class = ref $_[0];
193 puke "Possible SQL injection attempt '$_[1]'. If this is indeed a part of the "
194 . "desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply your own "
195 . "{injection_guard} attribute to ${class}->new()"
200 #======================================================================
202 #======================================================================
206 my $table = $self->_table(shift);
207 my $data = shift || return;
210 my $method = $self->_METHOD_FOR_refkind("_insert", $data);
211 my ($sql, @bind) = $self->$method($data);
212 $sql = join " ", $self->_sqlcase('insert into'), $table, $sql;
214 if ($options->{returning}) {
215 my ($s, @b) = $self->_insert_returning($options);
220 return wantarray ? ($sql, @bind) : $sql;
223 # So that subclasses can override INSERT ... RETURNING separately from
224 # UPDATE and DELETE (e.g. DBIx::Class::SQLMaker::Oracle does this)
225 sub _insert_returning { shift->_returning(@_) }
228 my ($self, $options) = @_;
230 my $f = $options->{returning};
232 my ($sql, @bind) = $self->_render_expr(
233 $self->_expand_maybe_list_expr($f, undef, -ident)
236 ? $self->_sqlcase(' returning ') . $sql
237 : ($self->_sqlcase(' returning ').$sql, @bind);
240 sub _insert_HASHREF { # explicit list of fields and then values
241 my ($self, $data) = @_;
243 my @fields = sort keys %$data;
245 my ($sql, @bind) = $self->_insert_values($data);
248 $_ = $self->_quote($_) foreach @fields;
249 $sql = "( ".join(", ", @fields).") ".$sql;
251 return ($sql, @bind);
254 sub _insert_ARRAYREF { # just generate values(?,?) part (no list of fields)
255 my ($self, $data) = @_;
257 # no names (arrayref) so can't generate bindtype
258 $self->{bindtype} ne 'columns'
259 or belch "can't do 'columns' bindtype when called with arrayref";
261 my (@values, @all_bind);
262 foreach my $value (@$data) {
263 my ($values, @bind) = $self->_insert_value(undef, $value);
264 push @values, $values;
265 push @all_bind, @bind;
267 my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
268 return ($sql, @all_bind);
271 sub _insert_ARRAYREFREF { # literal SQL with bind
272 my ($self, $data) = @_;
274 my ($sql, @bind) = @${$data};
275 $self->_assert_bindval_matches_bindtype(@bind);
277 return ($sql, @bind);
281 sub _insert_SCALARREF { # literal SQL without bind
282 my ($self, $data) = @_;
288 my ($self, $data) = @_;
290 my (@values, @all_bind);
291 foreach my $column (sort keys %$data) {
292 my ($values, @bind) = $self->_insert_value($column, $data->{$column});
293 push @values, $values;
294 push @all_bind, @bind;
296 my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
297 return ($sql, @all_bind);
301 my ($self, $column, $v) = @_;
303 return $self->_render_expr(
304 $self->_expand_insert_value($column, $v)
308 sub _expand_insert_value {
309 my ($self, $column, $v) = @_;
311 if (ref($v) eq 'ARRAY') {
312 if ($self->{array_datatypes}) {
313 return +{ -bind => [ $column, $v ] };
315 my ($sql, @bind) = @$v;
316 $self->_assert_bindval_matches_bindtype(@bind);
317 return +{ -literal => $v };
319 if (ref($v) eq 'HASH') {
320 if (grep !/^-/, keys %$v) {
321 belch "HASH ref as bind value in insert is not supported";
322 return +{ -bind => [ $column, $v ] };
326 return +{ -bind => [ $column, undef ] };
328 local our $Cur_Col_Meta = $column;
329 return $self->_expand_expr($v);
334 #======================================================================
336 #======================================================================
341 my $table = $self->_table(shift);
342 my $data = shift || return;
346 # first build the 'SET' part of the sql statement
347 puke "Unsupported data type specified to \$sql->update"
348 unless ref $data eq 'HASH';
350 my ($sql, @all_bind) = $self->_update_set_values($data);
351 $sql = $self->_sqlcase('update ') . $table . $self->_sqlcase(' set ')
355 my($where_sql, @where_bind) = $self->where($where);
357 push @all_bind, @where_bind;
360 if ($options->{returning}) {
361 my ($returning_sql, @returning_bind) = $self->_update_returning($options);
362 $sql .= $returning_sql;
363 push @all_bind, @returning_bind;
366 return wantarray ? ($sql, @all_bind) : $sql;
369 sub _update_set_values {
370 my ($self, $data) = @_;
372 return $self->_render_expr(
373 $self->_expand_update_set_values($data),
377 sub _expand_update_set_values {
378 my ($self, $data) = @_;
379 $self->_expand_maybe_list_expr( [
382 +{ -op => [ '=', { -ident => $k }, $set ] };
388 ? ($self->{array_datatypes}
389 ? [ $k, +{ -bind => [ $k, $v ] } ]
390 : [ $k, +{ -literal => $v } ])
392 local our $Cur_Col_Meta = $k;
393 [ $k, $self->_expand_expr($v) ]
400 # So that subclasses can override UPDATE ... RETURNING separately from
402 sub _update_returning { shift->_returning(@_) }
406 #======================================================================
408 #======================================================================
413 my $table = $self->_table(shift);
414 my $fields = shift || '*';
418 my ($fields_sql, @bind) = $self->_select_fields($fields);
420 my ($where_sql, @where_bind) = $self->where($where, $order);
421 push @bind, @where_bind;
423 my $sql = join(' ', $self->_sqlcase('select'), $fields_sql,
424 $self->_sqlcase('from'), $table)
427 return wantarray ? ($sql, @bind) : $sql;
431 my ($self, $fields) = @_;
432 return $self->_render_expr(
433 $self->_expand_maybe_list_expr($fields, undef, '-ident')
437 #======================================================================
439 #======================================================================
444 my $table = $self->_table(shift);
448 my($where_sql, @bind) = $self->where($where);
449 my $sql = $self->_sqlcase('delete from ') . $table . $where_sql;
451 if ($options->{returning}) {
452 my ($returning_sql, @returning_bind) = $self->_delete_returning($options);
453 $sql .= $returning_sql;
454 push @bind, @returning_bind;
457 return wantarray ? ($sql, @bind) : $sql;
460 # So that subclasses can override DELETE ... RETURNING separately from
462 sub _delete_returning { shift->_returning(@_) }
466 #======================================================================
468 #======================================================================
472 # Finally, a separate routine just to handle WHERE clauses
474 my ($self, $where, $order) = @_;
476 local $self->{convert_where} = $self->{convert};
479 my ($sql, @bind) = defined($where)
480 ? $self->_recurse_where($where)
482 $sql = (defined $sql and length $sql) ? $self->_sqlcase(' where ') . "( $sql )" : '';
486 my ($order_sql, @order_bind) = $self->_order_by($order);
488 push @bind, @order_bind;
491 return wantarray ? ($sql, @bind) : $sql;
495 my ($self, $expr, $logic, $default_scalar_to) = @_;
496 local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to;
497 return undef unless defined($expr);
498 if (ref($expr) eq 'HASH') {
499 if (keys %$expr > 1) {
503 map $self->_expand_expr_hashpair($_ => $expr->{$_}, $logic),
507 return unless %$expr;
508 return $self->_expand_expr_hashpair(%$expr, $logic);
510 if (ref($expr) eq 'ARRAY') {
511 my $logic = lc($logic || $self->{logic});
512 $logic eq 'and' or $logic eq 'or' or puke "unknown logic: $logic";
518 while (my ($el) = splice @expr, 0, 1) {
519 puke "Supplying an empty left hand side argument is not supported in array-pairs"
520 unless defined($el) and length($el);
521 my $elref = ref($el);
523 push(@res, $self->_expand_expr({ $el, shift(@expr) }));
524 } elsif ($elref eq 'ARRAY') {
525 push(@res, $self->_expand_expr($el)) if @$el;
526 } elsif (my $l = is_literal_value($el)) {
527 push @res, { -literal => $l };
528 } elsif ($elref eq 'HASH') {
529 push @res, $self->_expand_expr($el);
534 return { -op => [ $logic, @res ] };
536 if (my $literal = is_literal_value($expr)) {
537 return +{ -literal => $literal };
539 if (!ref($expr) or Scalar::Util::blessed($expr)) {
540 if (my $d = $Default_Scalar_To) {
541 return +{ $d => $expr };
543 if (my $m = our $Cur_Col_Meta) {
544 return +{ -bind => [ $m, $expr ] };
546 return +{ -value => $expr };
551 sub _expand_expr_hashpair {
552 my ($self, $k, $v, $logic) = @_;
553 unless (defined($k) and length($k)) {
554 if (defined($k) and my $literal = is_literal_value($v)) {
555 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
556 return { -literal => $literal };
558 puke "Supplying an empty left hand side argument is not supported";
561 $self->_assert_pass_injection_guard($k =~ /^-(.*)$/s);
562 if ($k =~ s/ [_\s]? \d+ $//x ) {
563 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
564 . "You probably wanted ...-and => [ $k => COND1, $k => COND2 ... ]";
567 return $self->_expand_expr($v);
571 return $self->_expand_expr($v);
573 puke "-bool => undef not supported" unless defined($v);
574 return { -ident => $v };
577 return { -op => [ 'not', $self->_expand_expr($v) ] };
579 if (my ($rest) = $k =~/^-not[_ ](.*)$/) {
582 $self->_expand_expr_hashpair("-${rest}", $v, $logic)
585 if (my ($logic) = $k =~ /^-(and|or)$/i) {
586 if (ref($v) eq 'HASH') {
587 return $self->_expand_expr($v, $logic);
589 if (ref($v) eq 'ARRAY') {
590 return $self->_expand_expr($v, $logic);
595 $op =~ s/^-// if length($op) > 1;
597 # top level special ops are illegal in general
598 puke "Illegal use of top-level '-$op'"
599 if List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
601 if ($k eq '-value' and my $m = our $Cur_Col_Meta) {
602 return +{ -bind => [ $m, $v ] };
604 if ($self->{node_types}{$k}) {
607 if (my $custom = $self->{custom_expansions}{($k =~ /^-(.*)$/)[0]}) {
608 return $self->$custom($v);
613 and (keys %$v)[0] =~ /^-/
615 my ($func) = $k =~ /^-(.*)$/;
616 return +{ -func => [ $func, $self->_expand_expr($v) ] };
618 if (!ref($v) or is_literal_value($v)) {
619 return +{ -op => [ $k =~ /^-(.*)$/, $self->_expand_expr($v) ] };
626 and exists $v->{-value}
627 and not defined $v->{-value}
630 return $self->_expand_expr_hashpair($k => { $self->{cmp} => undef });
632 if (!ref($v) or Scalar::Util::blessed($v)) {
637 { -bind => [ $k, $v ] }
641 if (ref($v) eq 'HASH') {
645 map $self->_expand_expr_hashpair($k => { $_ => $v->{$_} }),
652 $self->_assert_pass_injection_guard($vk);
653 if ($vk =~ s/ [_\s]? \d+ $//x ) {
654 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
655 . "You probably wanted ...-and => [ -$vk => COND1, -$vk => COND2 ... ]";
657 if ($vk =~ /^(?:not[ _])?between$/) {
658 local our $Cur_Col_Meta = $k;
659 my @rhs = map $self->_expand_expr($_),
660 ref($vv) eq 'ARRAY' ? @$vv : $vv;
662 (@rhs == 1 and ref($rhs[0]) eq 'HASH' and $rhs[0]->{-literal})
664 (@rhs == 2 and defined($rhs[0]) and defined($rhs[1]))
666 puke "Operator '${\uc($vk)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref";
669 join(' ', split '_', $vk),
674 if ($vk =~ /^(?:not[ _])?in$/) {
675 if (my $literal = is_literal_value($vv)) {
676 my ($sql, @bind) = @$literal;
677 my $opened_sql = $self->_open_outer_paren($sql);
679 $vk, { -ident => $k },
680 [ { -literal => [ $opened_sql, @bind ] } ]
684 'SQL::Abstract before v1.75 used to generate incorrect SQL when the '
685 . "-${\uc($vk)} operator was given an undef-containing list: !!!AUDIT YOUR CODE "
686 . 'AND DATA!!! (the upcoming Data::Query-based version of SQL::Abstract '
687 . 'will emit the logically correct SQL instead of raising this exception)'
689 puke("Argument passed to the '${\uc($vk)}' operator can not be undefined")
691 my @rhs = map $self->_expand_expr($_),
692 map { ref($_) ? $_ : { -bind => [ $k, $_ ] } }
693 map { defined($_) ? $_: puke($undef_err) }
694 (ref($vv) eq 'ARRAY' ? @$vv : $vv);
695 return $self->${\($vk =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs;
698 join(' ', split '_', $vk),
703 if ($vk eq 'ident') {
704 if (! defined $vv or ref $vv) {
705 puke "-$vk requires a single plain scalar argument (a quotable identifier)";
713 if ($vk eq 'value') {
714 return $self->_expand_expr_hashpair($k, undef) unless defined($vv);
718 { -bind => [ $k, $vv ] }
721 if ($vk =~ /^is(?:[ _]not)?$/) {
722 puke "$vk can only take undef as argument"
726 and exists($vv->{-value})
727 and !defined($vv->{-value})
730 return +{ -op => [ $vk.' null', { -ident => $k } ] };
732 if ($vk =~ /^(and|or)$/) {
733 if (ref($vv) eq 'HASH') {
736 map $self->_expand_expr_hashpair($k, { $_ => $vv->{$_} }),
741 if (my $us = List::Util::first { $vk =~ $_->{regex} } @{$self->{user_special_ops}}) {
742 return { -op => [ $vk, { -ident => $k }, $vv ] };
744 if (ref($vv) eq 'ARRAY') {
745 my ($logic, @values) = (
746 (defined($vv->[0]) and $vv->[0] =~ /^-(and|or)$/i)
751 $vk =~ $self->{inequality_op}
752 or join(' ', split '_', $vk) =~ $self->{not_like_op}
754 if (lc($logic) eq '-or' and @values > 1) {
755 my $op = uc join ' ', split '_', $vk;
756 belch "A multi-element arrayref as an argument to the inequality op '$op' "
757 . 'is technically equivalent to an always-true 1=1 (you probably wanted '
758 . "to say ...{ \$inequality_op => [ -and => \@values ] }... instead)"
763 # try to DWIM on equality operators
764 my $op = join ' ', split '_', $vk;
766 $op =~ $self->{equality_op} ? $self->sqlfalse
767 : $op =~ $self->{like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqlfalse
768 : $op =~ $self->{inequality_op} ? $self->sqltrue
769 : $op =~ $self->{not_like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqltrue
770 : puke "operator '$op' applied on an empty array (field '$k')";
774 map $self->_expand_expr_hashpair($k => { $vk => $_ }),
782 and exists $vv->{-value}
783 and not defined $vv->{-value}
786 my $op = join ' ', split '_', $vk;
788 $op =~ /^not$/i ? 'is not' # legacy
789 : $op =~ $self->{equality_op} ? 'is'
790 : $op =~ $self->{like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is'
791 : $op =~ $self->{inequality_op} ? 'is not'
792 : $op =~ $self->{not_like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is not'
793 : puke "unexpected operator '$op' with undef operand";
794 return +{ -op => [ $is.' null', { -ident => $k } ] };
796 local our $Cur_Col_Meta = $k;
800 $self->_expand_expr($vv)
803 if (ref($v) eq 'ARRAY') {
804 return $self->sqlfalse unless @$v;
805 $self->_debug("ARRAY($k) means distribute over elements");
807 $v->[0] =~ /^-((?:and|or))$/i
808 ? ($v = [ @{$v}[1..$#$v] ], $1)
809 : ($self->{logic} || 'or')
813 map $self->_expand_expr({ $k => $_ }, $this_logic), @$v
816 if (my $literal = is_literal_value($v)) {
818 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
821 my ($sql, @bind) = @$literal;
822 if ($self->{bindtype} eq 'columns') {
824 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
825 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
829 return +{ -literal => [ $self->_quote($k).' '.$sql, @bind ] };
835 my ($self, $expr) = @_;
836 my ($k, $v, @rest) = %$expr;
838 if (my $meth = $self->{node_types}{$k}) {
839 return $self->$meth($v);
841 die "notreached: $k";
845 my ($self, $where, $logic) = @_;
847 #print STDERR Data::Dumper::Concise::Dumper([ $where, $logic ]);
849 my $where_exp = $self->_expand_expr($where, $logic);
851 #print STDERR Data::Dumper::Concise::Dumper([ EXP => $where_exp ]);
853 # dispatch on appropriate method according to refkind of $where
854 # my $method = $self->_METHOD_FOR_refkind("_where", $where_exp);
856 # my ($sql, @bind) = $self->$method($where_exp, $logic);
858 my ($sql, @bind) = defined($where_exp) ? $self->_render_expr($where_exp) : (undef);
860 # DBIx::Class used to call _recurse_where in scalar context
861 # something else might too...
863 return ($sql, @bind);
866 belch "Calling _recurse_where in scalar context is deprecated and will go away before 2.0";
872 my ($self, $ident) = @_;
874 return $self->_convert($self->_quote($ident));
878 my ($self, $value) = @_;
880 return ($self->_convert('?'), $self->_bindtype(undef, $value));
883 my %unop_postfix = map +($_ => 1),
884 'is null', 'is not null',
892 my ($self, $args) = @_;
893 my ($left, $low, $high) = @$args;
894 my ($rhsql, @rhbind) = do {
896 puke "Single arg to between must be a literal"
897 unless $low->{-literal};
900 my ($l, $h) = map [ $self->_render_expr($_) ], $low, $high;
901 (join(' ', $l->[0], $self->_sqlcase('and'), $h->[0]),
902 @{$l}[1..$#$l], @{$h}[1..$#$h])
905 my ($lhsql, @lhbind) = $self->_render_expr($left);
907 join(' ', '(', $lhsql, $self->_sqlcase($op), $rhsql, ')'),
911 }), 'between', 'not between'),
915 my ($self, $args) = @_;
916 my ($lhs, $rhs) = @$args;
919 my ($sql, @bind) = $self->_render_expr($_);
920 push @in_bind, @bind;
923 my ($lhsql, @lbind) = $self->_render_expr($lhs);
925 $lhsql.' '.$self->_sqlcase($op).' ( '
936 my ($op, @args) = @$v;
937 $op =~ s/^-// if length($op) > 1;
939 if (my $h = $special{$op}) {
940 return $self->$h(\@args);
942 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{user_special_ops}}) {
943 puke "Special op '${op}' requires first value to be identifier"
944 unless my ($k) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0];
945 return $self->${\($us->{handler})}($k, $op, $args[1]);
947 my $final_op = $op =~ /^(?:is|not)_/ ? join(' ', split '_', $op) : $op;
948 if (@args == 1 and $op !~ /^(and|or)$/) {
949 my ($expr_sql, @bind) = $self->_render_expr($args[0]);
950 my $op_sql = $self->_sqlcase($final_op);
952 $unop_postfix{lc($final_op)}
953 ? "${expr_sql} ${op_sql}"
954 : "${op_sql} ${expr_sql}"
956 return (($op eq 'not' ? '('.$final_sql.')' : $final_sql), @bind);
958 my @parts = map [ $self->_render_expr($_) ], @args;
959 my ($final_sql) = map +($op =~ /^(and|or)$/ ? "(${_})" : $_), join(
960 ($final_op eq ',' ? '' : ' ').$self->_sqlcase($final_op).' ',
965 map @{$_}[1..$#$_], @parts
972 my ($self, $rest) = @_;
973 my ($func, @args) = @$rest;
977 push @arg_sql, shift @x;
979 } map [ $self->_render_expr($_) ], @args;
980 return ($self->_sqlcase($func).'('.join(', ', @arg_sql).')', @bind);
984 my ($self, $bind) = @_;
985 return ($self->_convert('?'), $self->_bindtype(@$bind));
988 sub _render_literal {
989 my ($self, $literal) = @_;
990 $self->_assert_bindval_matches_bindtype(@{$literal}[1..$#$literal]);
994 # Some databases (SQLite) treat col IN (1, 2) different from
995 # col IN ( (1, 2) ). Use this to strip all outer parens while
996 # adding them back in the corresponding method
997 sub _open_outer_paren {
998 my ($self, $sql) = @_;
1000 while (my ($inner) = $sql =~ /^ \s* \( (.*) \) \s* $/xs) {
1002 # there are closing parens inside, need the heavy duty machinery
1003 # to reevaluate the extraction starting from $sql (full reevaluation)
1004 if ($inner =~ /\)/) {
1005 require Text::Balanced;
1007 my (undef, $remainder) = do {
1008 # idiotic design - writes to $@ but *DOES NOT* throw exceptions
1010 Text::Balanced::extract_bracketed($sql, '()', qr/\s*/);
1013 # the entire expression needs to be a balanced bracketed thing
1014 # (after an extract no remainder sans trailing space)
1015 last if defined $remainder and $remainder =~ /\S/;
1025 #======================================================================
1027 #======================================================================
1030 my ($self, $arg) = @_;
1032 return '' unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg);
1034 my $expander = sub {
1035 my ($self, $dir, $expr) = @_;
1036 my @exp = map +(defined($dir) ? { -op => [ $dir => $_ ] } : $_),
1037 map $self->_expand_expr($_, undef, -ident),
1038 ref($expr) eq 'ARRAY' ? @$expr : $expr;
1039 return (@exp > 1 ? { -op => [ ',', @exp ] } : $exp[0]);
1042 local $self->{custom_expansions} = {
1043 asc => sub { shift->$expander(asc => @_) },
1044 desc => sub { shift->$expander(desc => @_) },
1047 my $expanded = $self->$expander(undef, $arg);
1049 my ($sql, @bind) = $self->_render_expr($expanded);
1051 my $final_sql = $self->_sqlcase(' order by ').$sql;
1053 return wantarray ? ($final_sql, @bind) : $final_sql;
1056 #======================================================================
1057 # DATASOURCE (FOR NOW, JUST PLAIN TABLE OR LIST OF TABLES)
1058 #======================================================================
1063 ($self->_render_expr(
1064 $self->_expand_maybe_list_expr($from, undef, -ident)
1069 #======================================================================
1071 #======================================================================
1073 sub _expand_maybe_list_expr {
1074 my ($self, $expr, $logic, $default) = @_;
1076 if (ref($expr) eq 'ARRAY') {
1078 ',', map $self->_expand_expr($_, $logic, $default), @$expr
1085 return $self->_expand_expr($e, $logic, $default);
1088 # highly optimized, as it's called way too often
1090 # my ($self, $label) = @_;
1092 return '' unless defined $_[1];
1093 return ${$_[1]} if ref($_[1]) eq 'SCALAR';
1094 puke 'Identifier cannot be hashref' if ref($_[1]) eq 'HASH';
1096 unless ($_[0]->{quote_char}) {
1097 if (ref($_[1]) eq 'ARRAY') {
1098 return join($_[0]->{name_sep}||'.', @{$_[1]});
1100 $_[0]->_assert_pass_injection_guard($_[1]);
1105 my $qref = ref $_[0]->{quote_char};
1107 !$qref ? ($_[0]->{quote_char}, $_[0]->{quote_char})
1108 : ($qref eq 'ARRAY') ? @{$_[0]->{quote_char}}
1109 : puke "Unsupported quote_char format: $_[0]->{quote_char}";
1111 my $esc = $_[0]->{escape_char} || $r;
1113 # parts containing * are naturally unquoted
1115 $_[0]->{name_sep}||'',
1119 : do { (my $n = $_) =~ s/(\Q$esc\E|\Q$r\E)/$esc$1/g; $l . $n . $r }
1121 (ref($_[1]) eq 'ARRAY'
1125 ? split (/\Q$_[0]->{name_sep}\E/, $_[1] )
1133 # Conversion, if applicable
1135 #my ($self, $arg) = @_;
1136 if ($_[0]->{convert_where}) {
1137 return $_[0]->_sqlcase($_[0]->{convert_where}) .'(' . $_[1] . ')';
1144 #my ($self, $col, @vals) = @_;
1145 # called often - tighten code
1146 return $_[0]->{bindtype} eq 'columns'
1147 ? map {[$_[1], $_]} @_[2 .. $#_]
1152 # Dies if any element of @bind is not in [colname => value] format
1153 # if bindtype is 'columns'.
1154 sub _assert_bindval_matches_bindtype {
1155 # my ($self, @bind) = @_;
1157 if ($self->{bindtype} eq 'columns') {
1159 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
1160 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
1166 sub _join_sql_clauses {
1167 my ($self, $logic, $clauses_aref, $bind_aref) = @_;
1169 if (@$clauses_aref > 1) {
1170 my $join = " " . $self->_sqlcase($logic) . " ";
1171 my $sql = '( ' . join($join, @$clauses_aref) . ' )';
1172 return ($sql, @$bind_aref);
1174 elsif (@$clauses_aref) {
1175 return ($clauses_aref->[0], @$bind_aref); # no parentheses
1178 return (); # if no SQL, ignore @$bind_aref
1183 # Fix SQL case, if so requested
1185 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
1186 # don't touch the argument ... crooked logic, but let's not change it!
1187 return $_[0]->{case} ? $_[1] : uc($_[1]);
1191 #======================================================================
1192 # DISPATCHING FROM REFKIND
1193 #======================================================================
1196 my ($self, $data) = @_;
1198 return 'UNDEF' unless defined $data;
1200 # blessed objects are treated like scalars
1201 my $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
1203 return 'SCALAR' unless $ref;
1206 while ($ref eq 'REF') {
1208 $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
1212 return ($ref||'SCALAR') . ('REF' x $n_steps);
1216 my ($self, $data) = @_;
1217 my @try = ($self->_refkind($data));
1218 push @try, 'SCALAR_or_UNDEF' if $try[0] eq 'SCALAR' || $try[0] eq 'UNDEF';
1219 push @try, 'FALLBACK';
1223 sub _METHOD_FOR_refkind {
1224 my ($self, $meth_prefix, $data) = @_;
1227 for (@{$self->_try_refkind($data)}) {
1228 $method = $self->can($meth_prefix."_".$_)
1232 return $method || puke "cannot dispatch on '$meth_prefix' for ".$self->_refkind($data);
1236 sub _SWITCH_refkind {
1237 my ($self, $data, $dispatch_table) = @_;
1240 for (@{$self->_try_refkind($data)}) {
1241 $coderef = $dispatch_table->{$_}
1245 puke "no dispatch entry for ".$self->_refkind($data)
1254 #======================================================================
1255 # VALUES, GENERATE, AUTOLOAD
1256 #======================================================================
1258 # LDNOTE: original code from nwiger, didn't touch code in that section
1259 # I feel the AUTOLOAD stuff should not be the default, it should
1260 # only be activated on explicit demand by user.
1264 my $data = shift || return;
1265 puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
1266 unless ref $data eq 'HASH';
1269 foreach my $k (sort keys %$data) {
1270 my $v = $data->{$k};
1271 $self->_SWITCH_refkind($v, {
1273 if ($self->{array_datatypes}) { # array datatype
1274 push @all_bind, $self->_bindtype($k, $v);
1276 else { # literal SQL with bind
1277 my ($sql, @bind) = @$v;
1278 $self->_assert_bindval_matches_bindtype(@bind);
1279 push @all_bind, @bind;
1282 ARRAYREFREF => sub { # literal SQL with bind
1283 my ($sql, @bind) = @${$v};
1284 $self->_assert_bindval_matches_bindtype(@bind);
1285 push @all_bind, @bind;
1287 SCALARREF => sub { # literal SQL without bind
1289 SCALAR_or_UNDEF => sub {
1290 push @all_bind, $self->_bindtype($k, $v);
1301 my(@sql, @sqlq, @sqlv);
1305 if ($ref eq 'HASH') {
1306 for my $k (sort keys %$_) {
1309 my $label = $self->_quote($k);
1310 if ($r eq 'ARRAY') {
1311 # literal SQL with bind
1312 my ($sql, @bind) = @$v;
1313 $self->_assert_bindval_matches_bindtype(@bind);
1314 push @sqlq, "$label = $sql";
1316 } elsif ($r eq 'SCALAR') {
1317 # literal SQL without bind
1318 push @sqlq, "$label = $$v";
1320 push @sqlq, "$label = ?";
1321 push @sqlv, $self->_bindtype($k, $v);
1324 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
1325 } elsif ($ref eq 'ARRAY') {
1326 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
1329 if ($r eq 'ARRAY') { # literal SQL with bind
1330 my ($sql, @bind) = @$v;
1331 $self->_assert_bindval_matches_bindtype(@bind);
1334 } elsif ($r eq 'SCALAR') { # literal SQL without bind
1335 # embedded literal SQL
1342 push @sql, '(' . join(', ', @sqlq) . ')';
1343 } elsif ($ref eq 'SCALAR') {
1347 # strings get case twiddled
1348 push @sql, $self->_sqlcase($_);
1352 my $sql = join ' ', @sql;
1354 # this is pretty tricky
1355 # if ask for an array, return ($stmt, @bind)
1356 # otherwise, s/?/shift @sqlv/ to put it inline
1358 return ($sql, @sqlv);
1360 1 while $sql =~ s/\?/my $d = shift(@sqlv);
1361 ref $d ? $d->[1] : $d/e;
1370 # This allows us to check for a local, then _form, attr
1372 my($name) = $AUTOLOAD =~ /.*::(.+)/;
1373 return $self->generate($name, @_);
1384 SQL::Abstract - Generate SQL from Perl data structures
1390 my $sql = SQL::Abstract->new;
1392 my($stmt, @bind) = $sql->select($source, \@fields, \%where, $order);
1394 my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
1396 my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
1398 my($stmt, @bind) = $sql->delete($table, \%where);
1400 # Then, use these in your DBI statements
1401 my $sth = $dbh->prepare($stmt);
1402 $sth->execute(@bind);
1404 # Just generate the WHERE clause
1405 my($stmt, @bind) = $sql->where(\%where, $order);
1407 # Return values in the same order, for hashed queries
1408 # See PERFORMANCE section for more details
1409 my @bind = $sql->values(\%fieldvals);
1413 This module was inspired by the excellent L<DBIx::Abstract>.
1414 However, in using that module I found that what I really wanted
1415 to do was generate SQL, but still retain complete control over my
1416 statement handles and use the DBI interface. So, I set out to
1417 create an abstract SQL generation module.
1419 While based on the concepts used by L<DBIx::Abstract>, there are
1420 several important differences, especially when it comes to WHERE
1421 clauses. I have modified the concepts used to make the SQL easier
1422 to generate from Perl data structures and, IMO, more intuitive.
1423 The underlying idea is for this module to do what you mean, based
1424 on the data structures you provide it. The big advantage is that
1425 you don't have to modify your code every time your data changes,
1426 as this module figures it out.
1428 To begin with, an SQL INSERT is as easy as just specifying a hash
1429 of C<key=value> pairs:
1432 name => 'Jimbo Bobson',
1433 phone => '123-456-7890',
1434 address => '42 Sister Lane',
1435 city => 'St. Louis',
1436 state => 'Louisiana',
1439 The SQL can then be generated with this:
1441 my($stmt, @bind) = $sql->insert('people', \%data);
1443 Which would give you something like this:
1445 $stmt = "INSERT INTO people
1446 (address, city, name, phone, state)
1447 VALUES (?, ?, ?, ?, ?)";
1448 @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
1449 '123-456-7890', 'Louisiana');
1451 These are then used directly in your DBI code:
1453 my $sth = $dbh->prepare($stmt);
1454 $sth->execute(@bind);
1456 =head2 Inserting and Updating Arrays
1458 If your database has array types (like for example Postgres),
1459 activate the special option C<< array_datatypes => 1 >>
1460 when creating the C<SQL::Abstract> object.
1461 Then you may use an arrayref to insert and update database array types:
1463 my $sql = SQL::Abstract->new(array_datatypes => 1);
1465 planets => [qw/Mercury Venus Earth Mars/]
1468 my($stmt, @bind) = $sql->insert('solar_system', \%data);
1472 $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
1474 @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
1477 =head2 Inserting and Updating SQL
1479 In order to apply SQL functions to elements of your C<%data> you may
1480 specify a reference to an arrayref for the given hash value. For example,
1481 if you need to execute the Oracle C<to_date> function on a value, you can
1482 say something like this:
1486 date_entered => \[ "to_date(?,'MM/DD/YYYY')", "03/02/2003" ],
1489 The first value in the array is the actual SQL. Any other values are
1490 optional and would be included in the bind values array. This gives
1493 my($stmt, @bind) = $sql->insert('people', \%data);
1495 $stmt = "INSERT INTO people (name, date_entered)
1496 VALUES (?, to_date(?,'MM/DD/YYYY'))";
1497 @bind = ('Bill', '03/02/2003');
1499 An UPDATE is just as easy, all you change is the name of the function:
1501 my($stmt, @bind) = $sql->update('people', \%data);
1503 Notice that your C<%data> isn't touched; the module will generate
1504 the appropriately quirky SQL for you automatically. Usually you'll
1505 want to specify a WHERE clause for your UPDATE, though, which is
1506 where handling C<%where> hashes comes in handy...
1508 =head2 Complex where statements
1510 This module can generate pretty complicated WHERE statements
1511 easily. For example, simple C<key=value> pairs are taken to mean
1512 equality, and if you want to see if a field is within a set
1513 of values, you can use an arrayref. Let's say we wanted to
1514 SELECT some data based on this criteria:
1517 requestor => 'inna',
1518 worker => ['nwiger', 'rcwe', 'sfz'],
1519 status => { '!=', 'completed' }
1522 my($stmt, @bind) = $sql->select('tickets', '*', \%where);
1524 The above would give you something like this:
1526 $stmt = "SELECT * FROM tickets WHERE
1527 ( requestor = ? ) AND ( status != ? )
1528 AND ( worker = ? OR worker = ? OR worker = ? )";
1529 @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
1531 Which you could then use in DBI code like so:
1533 my $sth = $dbh->prepare($stmt);
1534 $sth->execute(@bind);
1540 The methods are simple. There's one for every major SQL operation,
1541 and a constructor you use first. The arguments are specified in a
1542 similar order for each method (table, then fields, then a where
1543 clause) to try and simplify things.
1545 =head2 new(option => 'value')
1547 The C<new()> function takes a list of options and values, and returns
1548 a new B<SQL::Abstract> object which can then be used to generate SQL
1549 through the methods below. The options accepted are:
1555 If set to 'lower', then SQL will be generated in all lowercase. By
1556 default SQL is generated in "textbook" case meaning something like:
1558 SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
1560 Any setting other than 'lower' is ignored.
1564 This determines what the default comparison operator is. By default
1565 it is C<=>, meaning that a hash like this:
1567 %where = (name => 'nwiger', email => 'nate@wiger.org');
1569 Will generate SQL like this:
1571 WHERE name = 'nwiger' AND email = 'nate@wiger.org'
1573 However, you may want loose comparisons by default, so if you set
1574 C<cmp> to C<like> you would get SQL such as:
1576 WHERE name like 'nwiger' AND email like 'nate@wiger.org'
1578 You can also override the comparison on an individual basis - see
1579 the huge section on L</"WHERE CLAUSES"> at the bottom.
1581 =item sqltrue, sqlfalse
1583 Expressions for inserting boolean values within SQL statements.
1584 By default these are C<1=1> and C<1=0>. They are used
1585 by the special operators C<-in> and C<-not_in> for generating
1586 correct SQL even when the argument is an empty array (see below).
1590 This determines the default logical operator for multiple WHERE
1591 statements in arrays or hashes. If absent, the default logic is "or"
1592 for arrays, and "and" for hashes. This means that a WHERE
1596 event_date => {'>=', '2/13/99'},
1597 event_date => {'<=', '4/24/03'},
1600 will generate SQL like this:
1602 WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
1604 This is probably not what you want given this query, though (look
1605 at the dates). To change the "OR" to an "AND", simply specify:
1607 my $sql = SQL::Abstract->new(logic => 'and');
1609 Which will change the above C<WHERE> to:
1611 WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
1613 The logic can also be changed locally by inserting
1614 a modifier in front of an arrayref:
1616 @where = (-and => [event_date => {'>=', '2/13/99'},
1617 event_date => {'<=', '4/24/03'} ]);
1619 See the L</"WHERE CLAUSES"> section for explanations.
1623 This will automatically convert comparisons using the specified SQL
1624 function for both column and value. This is mostly used with an argument
1625 of C<upper> or C<lower>, so that the SQL will have the effect of
1626 case-insensitive "searches". For example, this:
1628 $sql = SQL::Abstract->new(convert => 'upper');
1629 %where = (keywords => 'MaKe iT CAse inSeNSItive');
1631 Will turn out the following SQL:
1633 WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
1635 The conversion can be C<upper()>, C<lower()>, or any other SQL function
1636 that can be applied symmetrically to fields (actually B<SQL::Abstract> does
1637 not validate this option; it will just pass through what you specify verbatim).
1641 This is a kludge because many databases suck. For example, you can't
1642 just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
1643 Instead, you have to use C<bind_param()>:
1645 $sth->bind_param(1, 'reg data');
1646 $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
1648 The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
1649 which loses track of which field each slot refers to. Fear not.
1651 If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
1652 Currently, you can specify either C<normal> (default) or C<columns>. If you
1653 specify C<columns>, you will get an array that looks like this:
1655 my $sql = SQL::Abstract->new(bindtype => 'columns');
1656 my($stmt, @bind) = $sql->insert(...);
1659 [ 'column1', 'value1' ],
1660 [ 'column2', 'value2' ],
1661 [ 'column3', 'value3' ],
1664 You can then iterate through this manually, using DBI's C<bind_param()>.
1666 $sth->prepare($stmt);
1669 my($col, $data) = @$_;
1670 if ($col eq 'details' || $col eq 'comments') {
1671 $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
1672 } elsif ($col eq 'image') {
1673 $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
1675 $sth->bind_param($i, $data);
1679 $sth->execute; # execute without @bind now
1681 Now, why would you still use B<SQL::Abstract> if you have to do this crap?
1682 Basically, the advantage is still that you don't have to care which fields
1683 are or are not included. You could wrap that above C<for> loop in a simple
1684 sub called C<bind_fields()> or something and reuse it repeatedly. You still
1685 get a layer of abstraction over manual SQL specification.
1687 Note that if you set L</bindtype> to C<columns>, the C<\[ $sql, @bind ]>
1688 construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
1689 will expect the bind values in this format.
1693 This is the character that a table or column name will be quoted
1694 with. By default this is an empty string, but you could set it to
1695 the character C<`>, to generate SQL like this:
1697 SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
1699 Alternatively, you can supply an array ref of two items, the first being the left
1700 hand quote character, and the second the right hand quote character. For
1701 example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
1702 that generates SQL like this:
1704 SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
1706 Quoting is useful if you have tables or columns names that are reserved
1707 words in your database's SQL dialect.
1711 This is the character that will be used to escape L</quote_char>s appearing
1712 in an identifier before it has been quoted.
1714 The parameter default in case of a single L</quote_char> character is the quote
1717 When opening-closing-style quoting is used (L</quote_char> is an arrayref)
1718 this parameter defaults to the B<closing (right)> L</quote_char>. Occurrences
1719 of the B<opening (left)> L</quote_char> within the identifier are currently left
1720 untouched. The default for opening-closing-style quotes may change in future
1721 versions, thus you are B<strongly encouraged> to specify the escape character
1726 This is the character that separates a table and column name. It is
1727 necessary to specify this when the C<quote_char> option is selected,
1728 so that tables and column names can be individually quoted like this:
1730 SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
1732 =item injection_guard
1734 A regular expression C<qr/.../> that is applied to any C<-function> and unquoted
1735 column name specified in a query structure. This is a safety mechanism to avoid
1736 injection attacks when mishandling user input e.g.:
1738 my %condition_as_column_value_pairs = get_values_from_user();
1739 $sqla->select( ... , \%condition_as_column_value_pairs );
1741 If the expression matches an exception is thrown. Note that literal SQL
1742 supplied via C<\'...'> or C<\['...']> is B<not> checked in any way.
1744 Defaults to checking for C<;> and the C<GO> keyword (TransactSQL)
1746 =item array_datatypes
1748 When this option is true, arrayrefs in INSERT or UPDATE are
1749 interpreted as array datatypes and are passed directly
1751 When this option is false, arrayrefs are interpreted
1752 as literal SQL, just like refs to arrayrefs
1753 (but this behavior is for backwards compatibility; when writing
1754 new queries, use the "reference to arrayref" syntax
1760 Takes a reference to a list of "special operators"
1761 to extend the syntax understood by L<SQL::Abstract>.
1762 See section L</"SPECIAL OPERATORS"> for details.
1766 Takes a reference to a list of "unary operators"
1767 to extend the syntax understood by L<SQL::Abstract>.
1768 See section L</"UNARY OPERATORS"> for details.
1774 =head2 insert($table, \@values || \%fieldvals, \%options)
1776 This is the simplest function. You simply give it a table name
1777 and either an arrayref of values or hashref of field/value pairs.
1778 It returns an SQL INSERT statement and a list of bind values.
1779 See the sections on L</"Inserting and Updating Arrays"> and
1780 L</"Inserting and Updating SQL"> for information on how to insert
1781 with those data types.
1783 The optional C<\%options> hash reference may contain additional
1784 options to generate the insert SQL. Currently supported options
1791 Takes either a scalar of raw SQL fields, or an array reference of
1792 field names, and adds on an SQL C<RETURNING> statement at the end.
1793 This allows you to return data generated by the insert statement
1794 (such as row IDs) without performing another C<SELECT> statement.
1795 Note, however, this is not part of the SQL standard and may not
1796 be supported by all database engines.
1800 =head2 update($table, \%fieldvals, \%where, \%options)
1802 This takes a table, hashref of field/value pairs, and an optional
1803 hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
1805 See the sections on L</"Inserting and Updating Arrays"> and
1806 L</"Inserting and Updating SQL"> for information on how to insert
1807 with those data types.
1809 The optional C<\%options> hash reference may contain additional
1810 options to generate the update SQL. Currently supported options
1817 See the C<returning> option to
1818 L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
1822 =head2 select($source, $fields, $where, $order)
1824 This returns a SQL SELECT statement and associated list of bind values, as
1825 specified by the arguments:
1831 Specification of the 'FROM' part of the statement.
1832 The argument can be either a plain scalar (interpreted as a table
1833 name, will be quoted), or an arrayref (interpreted as a list
1834 of table names, joined by commas, quoted), or a scalarref
1835 (literal SQL, not quoted).
1839 Specification of the list of fields to retrieve from
1841 The argument can be either an arrayref (interpreted as a list
1842 of field names, will be joined by commas and quoted), or a
1843 plain scalar (literal SQL, not quoted).
1844 Please observe that this API is not as flexible as that of
1845 the first argument C<$source>, for backwards compatibility reasons.
1849 Optional argument to specify the WHERE part of the query.
1850 The argument is most often a hashref, but can also be
1851 an arrayref or plain scalar --
1852 see section L<WHERE clause|/"WHERE CLAUSES"> for details.
1856 Optional argument to specify the ORDER BY part of the query.
1857 The argument can be a scalar, a hashref or an arrayref
1858 -- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
1864 =head2 delete($table, \%where, \%options)
1866 This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
1867 It returns an SQL DELETE statement and list of bind values.
1869 The optional C<\%options> hash reference may contain additional
1870 options to generate the delete SQL. Currently supported options
1877 See the C<returning> option to
1878 L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
1882 =head2 where(\%where, $order)
1884 This is used to generate just the WHERE clause. For example,
1885 if you have an arbitrary data structure and know what the
1886 rest of your SQL is going to look like, but want an easy way
1887 to produce a WHERE clause, use this. It returns an SQL WHERE
1888 clause and list of bind values.
1891 =head2 values(\%data)
1893 This just returns the values from the hash C<%data>, in the same
1894 order that would be returned from any of the other above queries.
1895 Using this allows you to markedly speed up your queries if you
1896 are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
1898 =head2 generate($any, 'number', $of, \@data, $struct, \%types)
1900 Warning: This is an experimental method and subject to change.
1902 This returns arbitrarily generated SQL. It's a really basic shortcut.
1903 It will return two different things, depending on return context:
1905 my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
1906 my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
1908 These would return the following:
1910 # First calling form
1911 $stmt = "CREATE TABLE test (?, ?)";
1912 @bind = (field1, field2);
1914 # Second calling form
1915 $stmt_and_val = "CREATE TABLE test (field1, field2)";
1917 Depending on what you're trying to do, it's up to you to choose the correct
1918 format. In this example, the second form is what you would want.
1922 $sql->generate('alter session', { nls_date_format => 'MM/YY' });
1926 ALTER SESSION SET nls_date_format = 'MM/YY'
1928 You get the idea. Strings get their case twiddled, but everything
1929 else remains verbatim.
1931 =head1 EXPORTABLE FUNCTIONS
1933 =head2 is_plain_value
1935 Determines if the supplied argument is a plain value as understood by this
1940 =item * The value is C<undef>
1942 =item * The value is a non-reference
1944 =item * The value is an object with stringification overloading
1946 =item * The value is of the form C<< { -value => $anything } >>
1950 On failure returns C<undef>, on success returns a B<scalar> reference
1951 to the original supplied argument.
1957 The stringification overloading detection is rather advanced: it takes
1958 into consideration not only the presence of a C<""> overload, but if that
1959 fails also checks for enabled
1960 L<autogenerated versions of C<"">|overload/Magic Autogeneration>, based
1961 on either C<0+> or C<bool>.
1963 Unfortunately testing in the field indicates that this
1964 detection B<< may tickle a latent bug in perl versions before 5.018 >>,
1965 but only when very large numbers of stringifying objects are involved.
1966 At the time of writing ( Sep 2014 ) there is no clear explanation of
1967 the direct cause, nor is there a manageably small test case that reliably
1968 reproduces the problem.
1970 If you encounter any of the following exceptions in B<random places within
1971 your application stack> - this module may be to blame:
1973 Operation "ne": no method found,
1974 left argument in overloaded package <something>,
1975 right argument in overloaded package <something>
1979 Stub found while resolving method "???" overloading """" in package <something>
1981 If you fall victim to the above - please attempt to reduce the problem
1982 to something that could be sent to the L<SQL::Abstract developers
1983 |DBIx::Class/GETTING HELP/SUPPORT>
1984 (either publicly or privately). As a workaround in the meantime you can
1985 set C<$ENV{SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION}> to a true
1986 value, which will most likely eliminate your problem (at the expense of
1987 not being able to properly detect exotic forms of stringification).
1989 This notice and environment variable will be removed in a future version,
1990 as soon as the underlying problem is found and a reliable workaround is
1995 =head2 is_literal_value
1997 Determines if the supplied argument is a literal value as understood by this
2002 =item * C<\$sql_string>
2004 =item * C<\[ $sql_string, @bind_values ]>
2008 On failure returns C<undef>, on success returns an B<array> reference
2009 containing the unpacked version of the supplied literal SQL and bind values.
2011 =head1 WHERE CLAUSES
2015 This module uses a variation on the idea from L<DBIx::Abstract>. It
2016 is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
2017 module is that things in arrays are OR'ed, and things in hashes
2020 The easiest way to explain is to show lots of examples. After
2021 each C<%where> hash shown, it is assumed you used:
2023 my($stmt, @bind) = $sql->where(\%where);
2025 However, note that the C<%where> hash can be used directly in any
2026 of the other functions as well, as described above.
2028 =head2 Key-value pairs
2030 So, let's get started. To begin, a simple hash:
2034 status => 'completed'
2037 Is converted to SQL C<key = val> statements:
2039 $stmt = "WHERE user = ? AND status = ?";
2040 @bind = ('nwiger', 'completed');
2042 One common thing I end up doing is having a list of values that
2043 a field can be in. To do this, simply specify a list inside of
2048 status => ['assigned', 'in-progress', 'pending'];
2051 This simple code will create the following:
2053 $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
2054 @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
2056 A field associated to an empty arrayref will be considered a
2057 logical false and will generate 0=1.
2059 =head2 Tests for NULL values
2061 If the value part is C<undef> then this is converted to SQL <IS NULL>
2070 $stmt = "WHERE user = ? AND status IS NULL";
2073 To test if a column IS NOT NULL:
2077 status => { '!=', undef },
2080 =head2 Specific comparison operators
2082 If you want to specify a different type of operator for your comparison,
2083 you can use a hashref for a given column:
2087 status => { '!=', 'completed' }
2090 Which would generate:
2092 $stmt = "WHERE user = ? AND status != ?";
2093 @bind = ('nwiger', 'completed');
2095 To test against multiple values, just enclose the values in an arrayref:
2097 status => { '=', ['assigned', 'in-progress', 'pending'] };
2099 Which would give you:
2101 "WHERE status = ? OR status = ? OR status = ?"
2104 The hashref can also contain multiple pairs, in which case it is expanded
2105 into an C<AND> of its elements:
2109 status => { '!=', 'completed', -not_like => 'pending%' }
2112 # Or more dynamically, like from a form
2113 $where{user} = 'nwiger';
2114 $where{status}{'!='} = 'completed';
2115 $where{status}{'-not_like'} = 'pending%';
2117 # Both generate this
2118 $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
2119 @bind = ('nwiger', 'completed', 'pending%');
2122 To get an OR instead, you can combine it with the arrayref idea:
2126 priority => [ { '=', 2 }, { '>', 5 } ]
2129 Which would generate:
2131 $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?";
2132 @bind = ('2', '5', 'nwiger');
2134 If you want to include literal SQL (with or without bind values), just use a
2135 scalar reference or reference to an arrayref as the value:
2138 date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
2139 date_expires => { '<' => \"now()" }
2142 Which would generate:
2144 $stmt = "WHERE date_entered > to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
2145 @bind = ('11/26/2008');
2148 =head2 Logic and nesting operators
2150 In the example above,
2151 there is a subtle trap if you want to say something like
2152 this (notice the C<AND>):
2154 WHERE priority != ? AND priority != ?
2156 Because, in Perl you I<can't> do this:
2158 priority => { '!=' => 2, '!=' => 1 }
2160 As the second C<!=> key will obliterate the first. The solution
2161 is to use the special C<-modifier> form inside an arrayref:
2163 priority => [ -and => {'!=', 2},
2167 Normally, these would be joined by C<OR>, but the modifier tells it
2168 to use C<AND> instead. (Hint: You can use this in conjunction with the
2169 C<logic> option to C<new()> in order to change the way your queries
2170 work by default.) B<Important:> Note that the C<-modifier> goes
2171 B<INSIDE> the arrayref, as an extra first element. This will
2172 B<NOT> do what you think it might:
2174 priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG!
2176 Here is a quick list of equivalencies, since there is some overlap:
2179 status => {'!=', 'completed', 'not like', 'pending%' }
2180 status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
2183 status => {'=', ['assigned', 'in-progress']}
2184 status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
2185 status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
2189 =head2 Special operators: IN, BETWEEN, etc.
2191 You can also use the hashref format to compare a list of fields using the
2192 C<IN> comparison operator, by specifying the list as an arrayref:
2195 status => 'completed',
2196 reportid => { -in => [567, 2335, 2] }
2199 Which would generate:
2201 $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
2202 @bind = ('completed', '567', '2335', '2');
2204 The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
2207 If the argument to C<-in> is an empty array, 'sqlfalse' is generated
2208 (by default: C<1=0>). Similarly, C<< -not_in => [] >> generates
2209 'sqltrue' (by default: C<1=1>).
2211 In addition to the array you can supply a chunk of literal sql or
2212 literal sql with bind:
2215 customer => { -in => \[
2216 'SELECT cust_id FROM cust WHERE balance > ?',
2219 status => { -in => \'SELECT status_codes FROM states' },
2225 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
2226 AND status IN ( SELECT status_codes FROM states )
2230 Finally, if the argument to C<-in> is not a reference, it will be
2231 treated as a single-element array.
2233 Another pair of operators is C<-between> and C<-not_between>,
2234 used with an arrayref of two values:
2238 completion_date => {
2239 -not_between => ['2002-10-01', '2003-02-06']
2245 WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
2247 Just like with C<-in> all plausible combinations of literal SQL
2251 start0 => { -between => [ 1, 2 ] },
2252 start1 => { -between => \["? AND ?", 1, 2] },
2253 start2 => { -between => \"lower(x) AND upper(y)" },
2254 start3 => { -between => [
2256 \["upper(?)", 'stuff' ],
2263 ( start0 BETWEEN ? AND ? )
2264 AND ( start1 BETWEEN ? AND ? )
2265 AND ( start2 BETWEEN lower(x) AND upper(y) )
2266 AND ( start3 BETWEEN lower(x) AND upper(?) )
2268 @bind = (1, 2, 1, 2, 'stuff');
2271 These are the two builtin "special operators"; but the
2272 list can be expanded: see section L</"SPECIAL OPERATORS"> below.
2274 =head2 Unary operators: bool
2276 If you wish to test against boolean columns or functions within your
2277 database you can use the C<-bool> and C<-not_bool> operators. For
2278 example to test the column C<is_user> being true and the column
2279 C<is_enabled> being false you would use:-
2283 -not_bool => 'is_enabled',
2288 WHERE is_user AND NOT is_enabled
2290 If a more complex combination is required, testing more conditions,
2291 then you should use the and/or operators:-
2296 -not_bool => { two=> { -rlike => 'bar' } },
2297 -not_bool => { three => [ { '=', 2 }, { '>', 5 } ] },
2308 (NOT ( three = ? OR three > ? ))
2311 =head2 Nested conditions, -and/-or prefixes
2313 So far, we've seen how multiple conditions are joined with a top-level
2314 C<AND>. We can change this by putting the different conditions we want in
2315 hashes and then putting those hashes in an array. For example:
2320 status => { -like => ['pending%', 'dispatched'] },
2324 status => 'unassigned',
2328 This data structure would create the following:
2330 $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
2331 OR ( user = ? AND status = ? ) )";
2332 @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
2335 Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or>
2336 to change the logic inside:
2342 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
2343 -or => { workhrs => {'<', 50}, geo => 'EURO' },
2350 $stmt = "WHERE ( user = ?
2351 AND ( ( workhrs > ? AND geo = ? )
2352 OR ( workhrs < ? OR geo = ? ) ) )";
2353 @bind = ('nwiger', '20', 'ASIA', '50', 'EURO');
2355 =head3 Algebraic inconsistency, for historical reasons
2357 C<Important note>: when connecting several conditions, the C<-and->|C<-or>
2358 operator goes C<outside> of the nested structure; whereas when connecting
2359 several constraints on one column, the C<-and> operator goes
2360 C<inside> the arrayref. Here is an example combining both features:
2363 -and => [a => 1, b => 2],
2364 -or => [c => 3, d => 4],
2365 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
2370 WHERE ( ( ( a = ? AND b = ? )
2371 OR ( c = ? OR d = ? )
2372 OR ( e LIKE ? AND e LIKE ? ) ) )
2374 This difference in syntax is unfortunate but must be preserved for
2375 historical reasons. So be careful: the two examples below would
2376 seem algebraically equivalent, but they are not
2379 { -like => 'foo%' },
2380 { -like => '%bar' },
2382 # yields: WHERE ( ( col LIKE ? AND col LIKE ? ) )
2385 { col => { -like => 'foo%' } },
2386 { col => { -like => '%bar' } },
2388 # yields: WHERE ( ( col LIKE ? OR col LIKE ? ) )
2391 =head2 Literal SQL and value type operators
2393 The basic premise of SQL::Abstract is that in WHERE specifications the "left
2394 side" is a column name and the "right side" is a value (normally rendered as
2395 a placeholder). This holds true for both hashrefs and arrayref pairs as you
2396 see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to
2397 alter this behavior. There are several ways of doing so.
2401 This is a virtual operator that signals the string to its right side is an
2402 identifier (a column name) and not a value. For example to compare two
2403 columns you would write:
2406 priority => { '<', 2 },
2407 requestor => { -ident => 'submitter' },
2412 $stmt = "WHERE priority < ? AND requestor = submitter";
2415 If you are maintaining legacy code you may see a different construct as
2416 described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new
2421 This is a virtual operator that signals that the construct to its right side
2422 is a value to be passed to DBI. This is for example necessary when you want
2423 to write a where clause against an array (for RDBMS that support such
2424 datatypes). For example:
2427 array => { -value => [1, 2, 3] }
2432 $stmt = 'WHERE array = ?';
2433 @bind = ([1, 2, 3]);
2435 Note that if you were to simply say:
2441 the result would probably not be what you wanted:
2443 $stmt = 'WHERE array = ? OR array = ? OR array = ?';
2448 Finally, sometimes only literal SQL will do. To include a random snippet
2449 of SQL verbatim, you specify it as a scalar reference. Consider this only
2450 as a last resort. Usually there is a better way. For example:
2453 priority => { '<', 2 },
2454 requestor => { -in => \'(SELECT name FROM hitmen)' },
2459 $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)"
2462 Note that in this example, you only get one bind parameter back, since
2463 the verbatim SQL is passed as part of the statement.
2467 Never use untrusted input as a literal SQL argument - this is a massive
2468 security risk (there is no way to check literal snippets for SQL
2469 injections and other nastyness). If you need to deal with untrusted input
2470 use literal SQL with placeholders as described next.
2472 =head3 Literal SQL with placeholders and bind values (subqueries)
2474 If the literal SQL to be inserted has placeholders and bind values,
2475 use a reference to an arrayref (yes this is a double reference --
2476 not so common, but perfectly legal Perl). For example, to find a date
2477 in Postgres you can use something like this:
2480 date_column => \[ "= date '2008-09-30' - ?::integer", 10 ]
2485 $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
2488 Note that you must pass the bind values in the same format as they are returned
2489 by L<where|/where(\%where, $order)>. This means that if you set L</bindtype>
2490 to C<columns>, you must provide the bind values in the
2491 C<< [ column_meta => value ] >> format, where C<column_meta> is an opaque
2492 scalar value; most commonly the column name, but you can use any scalar value
2493 (including references and blessed references), L<SQL::Abstract> will simply
2494 pass it through intact. So if C<bindtype> is set to C<columns> the above
2495 example will look like:
2498 date_column => \[ "= date '2008-09-30' - ?::integer", [ {} => 10 ] ]
2501 Literal SQL is especially useful for nesting parenthesized clauses in the
2502 main SQL query. Here is a first example:
2504 my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
2508 bar => \["IN ($sub_stmt)" => @sub_bind],
2513 $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
2514 WHERE c2 < ? AND c3 LIKE ?))";
2515 @bind = (1234, 100, "foo%");
2517 Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
2518 are expressed in the same way. Of course the C<$sub_stmt> and
2519 its associated bind values can be generated through a former call
2522 my ($sub_stmt, @sub_bind)
2523 = $sql->select("t1", "c1", {c2 => {"<" => 100},
2524 c3 => {-like => "foo%"}});
2527 bar => \["> ALL ($sub_stmt)" => @sub_bind],
2530 In the examples above, the subquery was used as an operator on a column;
2531 but the same principle also applies for a clause within the main C<%where>
2532 hash, like an EXISTS subquery:
2534 my ($sub_stmt, @sub_bind)
2535 = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
2536 my %where = ( -and => [
2538 \["EXISTS ($sub_stmt)" => @sub_bind],
2543 $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
2544 WHERE c1 = ? AND c2 > t0.c0))";
2548 Observe that the condition on C<c2> in the subquery refers to
2549 column C<t0.c0> of the main query: this is I<not> a bind
2550 value, so we have to express it through a scalar ref.
2551 Writing C<< c2 => {">" => "t0.c0"} >> would have generated
2552 C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
2553 what we wanted here.
2555 Finally, here is an example where a subquery is used
2556 for expressing unary negation:
2558 my ($sub_stmt, @sub_bind)
2559 = $sql->where({age => [{"<" => 10}, {">" => 20}]});
2560 $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
2562 lname => {like => '%son%'},
2563 \["NOT ($sub_stmt)" => @sub_bind],
2568 $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
2569 @bind = ('%son%', 10, 20)
2571 =head3 Deprecated usage of Literal SQL
2573 Below are some examples of archaic use of literal SQL. It is shown only as
2574 reference for those who deal with legacy code. Each example has a much
2575 better, cleaner and safer alternative that users should opt for in new code.
2581 my %where = ( requestor => \'IS NOT NULL' )
2583 $stmt = "WHERE requestor IS NOT NULL"
2585 This used to be the way of generating NULL comparisons, before the handling
2586 of C<undef> got formalized. For new code please use the superior syntax as
2587 described in L</Tests for NULL values>.
2591 my %where = ( requestor => \'= submitter' )
2593 $stmt = "WHERE requestor = submitter"
2595 This used to be the only way to compare columns. Use the superior L</-ident>
2596 method for all new code. For example an identifier declared in such a way
2597 will be properly quoted if L</quote_char> is properly set, while the legacy
2598 form will remain as supplied.
2602 my %where = ( is_ready => \"", completed => { '>', '2012-12-21' } )
2604 $stmt = "WHERE completed > ? AND is_ready"
2605 @bind = ('2012-12-21')
2607 Using an empty string literal used to be the only way to express a boolean.
2608 For all new code please use the much more readable
2609 L<-bool|/Unary operators: bool> operator.
2615 These pages could go on for a while, since the nesting of the data
2616 structures this module can handle are pretty much unlimited (the
2617 module implements the C<WHERE> expansion as a recursive function
2618 internally). Your best bet is to "play around" with the module a
2619 little to see how the data structures behave, and choose the best
2620 format for your data based on that.
2622 And of course, all the values above will probably be replaced with
2623 variables gotten from forms or the command line. After all, if you
2624 knew everything ahead of time, you wouldn't have to worry about
2625 dynamically-generating SQL and could just hardwire it into your
2628 =head1 ORDER BY CLAUSES
2630 Some functions take an order by clause. This can either be a scalar (just a
2631 column name), a hashref of C<< { -desc => 'col' } >> or C<< { -asc => 'col' }
2632 >>, a scalarref, an arrayref-ref, or an arrayref of any of the previous
2635 Given | Will Generate
2636 ---------------------------------------------------------------
2638 'colA' | ORDER BY colA
2640 [qw/colA colB/] | ORDER BY colA, colB
2642 {-asc => 'colA'} | ORDER BY colA ASC
2644 {-desc => 'colB'} | ORDER BY colB DESC
2646 ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC
2648 { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC
2650 \'colA DESC' | ORDER BY colA DESC
2652 \[ 'FUNC(colA, ?)', $x ] | ORDER BY FUNC(colA, ?)
2653 | /* ...with $x bound to ? */
2656 { -asc => 'colA' }, | colA ASC,
2657 { -desc => [qw/colB/] }, | colB DESC,
2658 { -asc => [qw/colC colD/] },| colC ASC, colD ASC,
2659 \'colE DESC', | colE DESC,
2660 \[ 'FUNC(colF, ?)', $x ], | FUNC(colF, ?)
2661 ] | /* ...with $x bound to ? */
2662 ===============================================================
2666 =head1 SPECIAL OPERATORS
2668 my $sqlmaker = SQL::Abstract->new(special_ops => [
2672 my ($self, $field, $op, $arg) = @_;
2678 handler => 'method_name',
2682 A "special operator" is a SQL syntactic clause that can be
2683 applied to a field, instead of a usual binary operator.
2686 WHERE field IN (?, ?, ?)
2687 WHERE field BETWEEN ? AND ?
2688 WHERE MATCH(field) AGAINST (?, ?)
2690 Special operators IN and BETWEEN are fairly standard and therefore
2691 are builtin within C<SQL::Abstract> (as the overridable methods
2692 C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
2693 like the MATCH .. AGAINST example above which is specific to MySQL,
2694 you can write your own operator handlers - supply a C<special_ops>
2695 argument to the C<new> method. That argument takes an arrayref of
2696 operator definitions; each operator definition is a hashref with two
2703 the regular expression to match the operator
2707 Either a coderef or a plain scalar method name. In both cases
2708 the expected return is C<< ($sql, @bind) >>.
2710 When supplied with a method name, it is simply called on the
2711 L<SQL::Abstract> object as:
2713 $self->$method_name($field, $op, $arg)
2717 $field is the LHS of the operator
2718 $op is the part that matched the handler regex
2721 When supplied with a coderef, it is called as:
2723 $coderef->($self, $field, $op, $arg)
2728 For example, here is an implementation
2729 of the MATCH .. AGAINST syntax for MySQL
2731 my $sqlmaker = SQL::Abstract->new(special_ops => [
2733 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
2734 {regex => qr/^match$/i,
2736 my ($self, $field, $op, $arg) = @_;
2737 $arg = [$arg] if not ref $arg;
2738 my $label = $self->_quote($field);
2739 my ($placeholder) = $self->_convert('?');
2740 my $placeholders = join ", ", (($placeholder) x @$arg);
2741 my $sql = $self->_sqlcase('match') . " ($label) "
2742 . $self->_sqlcase('against') . " ($placeholders) ";
2743 my @bind = $self->_bindtype($field, @$arg);
2744 return ($sql, @bind);
2751 =head1 UNARY OPERATORS
2753 my $sqlmaker = SQL::Abstract->new(unary_ops => [
2757 my ($self, $op, $arg) = @_;
2763 handler => 'method_name',
2767 A "unary operator" is a SQL syntactic clause that can be
2768 applied to a field - the operator goes before the field
2770 You can write your own operator handlers - supply a C<unary_ops>
2771 argument to the C<new> method. That argument takes an arrayref of
2772 operator definitions; each operator definition is a hashref with two
2779 the regular expression to match the operator
2783 Either a coderef or a plain scalar method name. In both cases
2784 the expected return is C<< $sql >>.
2786 When supplied with a method name, it is simply called on the
2787 L<SQL::Abstract> object as:
2789 $self->$method_name($op, $arg)
2793 $op is the part that matched the handler regex
2794 $arg is the RHS or argument of the operator
2796 When supplied with a coderef, it is called as:
2798 $coderef->($self, $op, $arg)
2806 Thanks to some benchmarking by Mark Stosberg, it turns out that
2807 this module is many orders of magnitude faster than using C<DBIx::Abstract>.
2808 I must admit this wasn't an intentional design issue, but it's a
2809 byproduct of the fact that you get to control your C<DBI> handles
2812 To maximize performance, use a code snippet like the following:
2814 # prepare a statement handle using the first row
2815 # and then reuse it for the rest of the rows
2817 for my $href (@array_of_hashrefs) {
2818 $stmt ||= $sql->insert('table', $href);
2819 $sth ||= $dbh->prepare($stmt);
2820 $sth->execute($sql->values($href));
2823 The reason this works is because the keys in your C<$href> are sorted
2824 internally by B<SQL::Abstract>. Thus, as long as your data retains
2825 the same structure, you only have to generate the SQL the first time
2826 around. On subsequent queries, simply use the C<values> function provided
2827 by this module to return your values in the correct order.
2829 However this depends on the values having the same type - if, for
2830 example, the values of a where clause may either have values
2831 (resulting in sql of the form C<column = ?> with a single bind
2832 value), or alternatively the values might be C<undef> (resulting in
2833 sql of the form C<column IS NULL> with no bind value) then the
2834 caching technique suggested will not work.
2838 If you use my C<CGI::FormBuilder> module at all, you'll hopefully
2839 really like this part (I do, at least). Building up a complex query
2840 can be as simple as the following:
2847 use CGI::FormBuilder;
2850 my $form = CGI::FormBuilder->new(...);
2851 my $sql = SQL::Abstract->new;
2853 if ($form->submitted) {
2854 my $field = $form->field;
2855 my $id = delete $field->{id};
2856 my($stmt, @bind) = $sql->update('table', $field, {id => $id});
2859 Of course, you would still have to connect using C<DBI> to run the
2860 query, but the point is that if you make your form look like your
2861 table, the actual query script can be extremely simplistic.
2863 If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
2864 a fast interface to returning and formatting data. I frequently
2865 use these three modules together to write complex database query
2866 apps in under 50 lines.
2868 =head1 HOW TO CONTRIBUTE
2870 Contributions are always welcome, in all usable forms (we especially
2871 welcome documentation improvements). The delivery methods include git-
2872 or unified-diff formatted patches, GitHub pull requests, or plain bug
2873 reports either via RT or the Mailing list. Contributors are generally
2874 granted full access to the official repository after their first several
2875 patches pass successful review.
2877 This project is maintained in a git repository. The code and related tools are
2878 accessible at the following locations:
2882 =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git>
2884 =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git>
2886 =item * GitHub mirror: L<https://github.com/dbsrgits/sql-abstract>
2888 =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/SQL-Abstract.git>
2894 Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
2895 Great care has been taken to preserve the I<published> behavior
2896 documented in previous versions in the 1.* family; however,
2897 some features that were previously undocumented, or behaved
2898 differently from the documentation, had to be changed in order
2899 to clarify the semantics. Hence, client code that was relying
2900 on some dark areas of C<SQL::Abstract> v1.*
2901 B<might behave differently> in v1.50.
2903 The main changes are:
2909 support for literal SQL through the C<< \ [ $sql, @bind ] >> syntax.
2913 support for the { operator => \"..." } construct (to embed literal SQL)
2917 support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
2921 optional support for L<array datatypes|/"Inserting and Updating Arrays">
2925 defensive programming: check arguments
2929 fixed bug with global logic, which was previously implemented
2930 through global variables yielding side-effects. Prior versions would
2931 interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
2932 as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
2933 Now this is interpreted
2934 as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
2939 fixed semantics of _bindtype on array args
2943 dropped the C<_anoncopy> of the %where tree. No longer necessary,
2944 we just avoid shifting arrays within that tree.
2948 dropped the C<_modlogic> function
2952 =head1 ACKNOWLEDGEMENTS
2954 There are a number of individuals that have really helped out with
2955 this module. Unfortunately, most of them submitted bugs via CPAN
2956 so I have no idea who they are! But the people I do know are:
2958 Ash Berlin (order_by hash term support)
2959 Matt Trout (DBIx::Class support)
2960 Mark Stosberg (benchmarking)
2961 Chas Owens (initial "IN" operator support)
2962 Philip Collins (per-field SQL functions)
2963 Eric Kolve (hashref "AND" support)
2964 Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
2965 Dan Kubb (support for "quote_char" and "name_sep")
2966 Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
2967 Laurent Dami (internal refactoring, extensible list of special operators, literal SQL)
2968 Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
2969 Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
2970 Oliver Charles (support for "RETURNING" after "INSERT")
2976 L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
2980 Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
2982 This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
2984 For support, your best bet is to try the C<DBIx::Class> users mailing list.
2985 While not an official support venue, C<DBIx::Class> makes heavy use of
2986 C<SQL::Abstract>, and as such list members there are very familiar with
2987 how to create queries.
2991 This module is free software; you may copy this under the same
2992 terms as perl itself (either the GNU General Public License or
2993 the Artistic License)