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 return bless \%opt, $class;
182 sub sqltrue { +{ -literal => [ $_[0]->{sqltrue} ] } }
183 sub sqlfalse { +{ -literal => [ $_[0]->{sqlfalse} ] } }
185 sub _assert_pass_injection_guard {
186 if ($_[1] =~ $_[0]->{injection_guard}) {
187 my $class = ref $_[0];
188 puke "Possible SQL injection attempt '$_[1]'. If this is indeed a part of the "
189 . "desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply your own "
190 . "{injection_guard} attribute to ${class}->new()"
195 #======================================================================
197 #======================================================================
201 my $table = $self->_table(shift);
202 my $data = shift || return;
205 my $method = $self->_METHOD_FOR_refkind("_insert", $data);
206 my ($sql, @bind) = $self->$method($data);
207 $sql = join " ", $self->_sqlcase('insert into'), $table, $sql;
209 if ($options->{returning}) {
210 my ($s, @b) = $self->_insert_returning($options);
215 return wantarray ? ($sql, @bind) : $sql;
218 # So that subclasses can override INSERT ... RETURNING separately from
219 # UPDATE and DELETE (e.g. DBIx::Class::SQLMaker::Oracle does this)
220 sub _insert_returning { shift->_returning(@_) }
223 my ($self, $options) = @_;
225 my $f = $options->{returning};
227 my ($sql, @bind) = $self->_render_expr(
228 $self->_expand_maybe_list_expr($f, undef, -ident)
231 ? $self->_sqlcase(' returning ') . $sql
232 : ($self->_sqlcase(' returning ').$sql, @bind);
235 sub _insert_HASHREF { # explicit list of fields and then values
236 my ($self, $data) = @_;
238 my @fields = sort keys %$data;
240 my ($sql, @bind) = $self->_insert_values($data);
243 $_ = $self->_quote($_) foreach @fields;
244 $sql = "( ".join(", ", @fields).") ".$sql;
246 return ($sql, @bind);
249 sub _insert_ARRAYREF { # just generate values(?,?) part (no list of fields)
250 my ($self, $data) = @_;
252 # no names (arrayref) so can't generate bindtype
253 $self->{bindtype} ne 'columns'
254 or belch "can't do 'columns' bindtype when called with arrayref";
256 my (@values, @all_bind);
257 foreach my $value (@$data) {
258 my ($values, @bind) = $self->_insert_value(undef, $value);
259 push @values, $values;
260 push @all_bind, @bind;
262 my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
263 return ($sql, @all_bind);
266 sub _insert_ARRAYREFREF { # literal SQL with bind
267 my ($self, $data) = @_;
269 my ($sql, @bind) = @${$data};
270 $self->_assert_bindval_matches_bindtype(@bind);
272 return ($sql, @bind);
276 sub _insert_SCALARREF { # literal SQL without bind
277 my ($self, $data) = @_;
283 my ($self, $data) = @_;
285 my (@values, @all_bind);
286 foreach my $column (sort keys %$data) {
287 my ($values, @bind) = $self->_insert_value($column, $data->{$column});
288 push @values, $values;
289 push @all_bind, @bind;
291 my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
292 return ($sql, @all_bind);
296 my ($self, $column, $v) = @_;
298 return $self->_render_expr(
299 $self->_expand_insert_value($column, $v)
303 sub _expand_insert_value {
304 my ($self, $column, $v) = @_;
306 if (ref($v) eq 'ARRAY') {
307 if ($self->{array_datatypes}) {
308 return +{ -bind => [ $column, $v ] };
310 my ($sql, @bind) = @$v;
311 $self->_assert_bindval_matches_bindtype(@bind);
312 return +{ -literal => $v };
314 if (ref($v) eq 'HASH') {
315 if (grep !/^-/, keys %$v) {
316 belch "HASH ref as bind value in insert is not supported";
317 return +{ -bind => [ $column, $v ] };
321 return +{ -bind => [ $column, undef ] };
323 local our $Cur_Col_Meta = $column;
324 return $self->_expand_expr($v);
329 #======================================================================
331 #======================================================================
336 my $table = $self->_table(shift);
337 my $data = shift || return;
341 # first build the 'SET' part of the sql statement
342 puke "Unsupported data type specified to \$sql->update"
343 unless ref $data eq 'HASH';
345 my ($sql, @all_bind) = $self->_update_set_values($data);
346 $sql = $self->_sqlcase('update ') . $table . $self->_sqlcase(' set ')
350 my($where_sql, @where_bind) = $self->where($where);
352 push @all_bind, @where_bind;
355 if ($options->{returning}) {
356 my ($returning_sql, @returning_bind) = $self->_update_returning($options);
357 $sql .= $returning_sql;
358 push @all_bind, @returning_bind;
361 return wantarray ? ($sql, @all_bind) : $sql;
364 sub _update_set_values {
365 my ($self, $data) = @_;
367 my (@set, @all_bind);
368 for my $k (sort keys %$data) {
371 my $label = $self->_quote($k);
373 $self->_SWITCH_refkind($v, {
375 if ($self->{array_datatypes}) { # array datatype
376 push @set, "$label = ?";
377 push @all_bind, $self->_bindtype($k, $v);
379 else { # literal SQL with bind
380 my ($sql, @bind) = @$v;
381 $self->_assert_bindval_matches_bindtype(@bind);
382 push @set, "$label = $sql";
383 push @all_bind, @bind;
386 ARRAYREFREF => sub { # literal SQL with bind
387 my ($sql, @bind) = @${$v};
388 $self->_assert_bindval_matches_bindtype(@bind);
389 push @set, "$label = $sql";
390 push @all_bind, @bind;
392 SCALARREF => sub { # literal SQL without bind
393 push @set, "$label = $$v";
396 my ($op, $arg, @rest) = %$v;
398 puke 'Operator calls in update must be in the form { -op => $arg }'
399 if (@rest or not $op =~ /^\-(.+)/);
401 local our $Cur_Col_Meta = $k;
402 my ($sql, @bind) = $self->_render_expr(
403 $self->_expand_expr_hashpair($op, $arg)
406 push @set, "$label = $sql";
407 push @all_bind, @bind;
409 SCALAR_or_UNDEF => sub {
410 push @set, "$label = ?";
411 push @all_bind, $self->_bindtype($k, $v);
417 my $sql = join ', ', @set;
419 return ($sql, @all_bind);
422 # So that subclasses can override UPDATE ... RETURNING separately from
424 sub _update_returning { shift->_returning(@_) }
428 #======================================================================
430 #======================================================================
435 my $table = $self->_table(shift);
436 my $fields = shift || '*';
440 my ($fields_sql, @bind) = $self->_select_fields($fields);
442 my ($where_sql, @where_bind) = $self->where($where, $order);
443 push @bind, @where_bind;
445 my $sql = join(' ', $self->_sqlcase('select'), $fields_sql,
446 $self->_sqlcase('from'), $table)
449 return wantarray ? ($sql, @bind) : $sql;
453 my ($self, $fields) = @_;
454 return $self->_render_expr(
455 $self->_expand_maybe_list_expr($fields, undef, '-ident')
459 #======================================================================
461 #======================================================================
466 my $table = $self->_table(shift);
470 my($where_sql, @bind) = $self->where($where);
471 my $sql = $self->_sqlcase('delete from ') . $table . $where_sql;
473 if ($options->{returning}) {
474 my ($returning_sql, @returning_bind) = $self->_delete_returning($options);
475 $sql .= $returning_sql;
476 push @bind, @returning_bind;
479 return wantarray ? ($sql, @bind) : $sql;
482 # So that subclasses can override DELETE ... RETURNING separately from
484 sub _delete_returning { shift->_returning(@_) }
488 #======================================================================
490 #======================================================================
494 # Finally, a separate routine just to handle WHERE clauses
496 my ($self, $where, $order) = @_;
498 local $self->{convert_where} = $self->{convert};
501 my ($sql, @bind) = defined($where)
502 ? $self->_recurse_where($where)
504 $sql = (defined $sql and length $sql) ? $self->_sqlcase(' where ') . "( $sql )" : '';
508 my ($order_sql, @order_bind) = $self->_order_by($order);
510 push @bind, @order_bind;
513 return wantarray ? ($sql, @bind) : $sql;
517 my ($self, $expr, $logic, $default_scalar_to) = @_;
518 local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to;
519 return undef unless defined($expr);
520 if (ref($expr) eq 'HASH') {
521 if (keys %$expr > 1) {
525 map $self->_expand_expr_hashpair($_ => $expr->{$_}, $logic),
529 return unless %$expr;
530 return $self->_expand_expr_hashpair(%$expr, $logic);
532 if (ref($expr) eq 'ARRAY') {
533 my $logic = lc($logic || $self->{logic});
534 $logic eq 'and' or $logic eq 'or' or puke "unknown logic: $logic";
540 while (my ($el) = splice @expr, 0, 1) {
541 puke "Supplying an empty left hand side argument is not supported in array-pairs"
542 unless defined($el) and length($el);
543 my $elref = ref($el);
545 push(@res, $self->_expand_expr({ $el, shift(@expr) }));
546 } elsif ($elref eq 'ARRAY') {
547 push(@res, $self->_expand_expr($el)) if @$el;
548 } elsif (my $l = is_literal_value($el)) {
549 push @res, { -literal => $l };
550 } elsif ($elref eq 'HASH') {
551 push @res, $self->_expand_expr($el);
556 return { -op => [ $logic, @res ] };
558 if (my $literal = is_literal_value($expr)) {
559 return +{ -literal => $literal };
561 if (!ref($expr) or Scalar::Util::blessed($expr)) {
562 if (my $d = $Default_Scalar_To) {
563 return +{ $d => $expr };
565 if (my $m = our $Cur_Col_Meta) {
566 return +{ -bind => [ $m, $expr ] };
568 return +{ -value => $expr };
573 sub _expand_expr_hashpair {
574 my ($self, $k, $v, $logic) = @_;
575 unless (defined($k) and length($k)) {
576 if (defined($k) and my $literal = is_literal_value($v)) {
577 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
578 return { -literal => $literal };
580 puke "Supplying an empty left hand side argument is not supported";
583 $self->_assert_pass_injection_guard($k =~ /^-(.*)$/s);
584 if ($k =~ s/ [_\s]? \d+ $//x ) {
585 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
586 . "You probably wanted ...-and => [ $k => COND1, $k => COND2 ... ]";
589 return $self->_expand_expr($v);
593 return $self->_expand_expr($v);
595 puke "-bool => undef not supported" unless defined($v);
596 return { -ident => $v };
599 return { -op => [ 'not', $self->_expand_expr($v) ] };
601 if (my ($rest) = $k =~/^-not[_ ](.*)$/) {
604 $self->_expand_expr_hashpair("-${rest}", $v, $logic)
607 if (my ($logic) = $k =~ /^-(and|or)$/i) {
608 if (ref($v) eq 'HASH') {
609 return $self->_expand_expr($v, $logic);
611 if (ref($v) eq 'ARRAY') {
612 return $self->_expand_expr($v, $logic);
617 $op =~ s/^-// if length($op) > 1;
619 # top level special ops are illegal in general
620 puke "Illegal use of top-level '-$op'"
621 if List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
623 if ($k eq '-value' and my $m = our $Cur_Col_Meta) {
624 return +{ -bind => [ $m, $v ] };
626 if ($k eq '-op' or $k eq '-ident' or $k eq '-value' or $k eq '-bind' or $k eq '-literal' or $k eq '-func') {
629 if (my $custom = $self->{custom_expansions}{($k =~ /^-(.*)$/)[0]}) {
630 return $self->$custom($v);
635 and (keys %$v)[0] =~ /^-/
637 my ($func) = $k =~ /^-(.*)$/;
638 return +{ -func => [ $func, $self->_expand_expr($v) ] };
640 if (!ref($v) or is_literal_value($v)) {
641 return +{ -op => [ $k =~ /^-(.*)$/, $self->_expand_expr($v) ] };
648 and exists $v->{-value}
649 and not defined $v->{-value}
652 return $self->_expand_expr_hashpair($k => { $self->{cmp} => undef });
654 if (!ref($v) or Scalar::Util::blessed($v)) {
659 { -bind => [ $k, $v ] }
663 if (ref($v) eq 'HASH') {
667 map $self->_expand_expr_hashpair($k => { $_ => $v->{$_} }),
674 $self->_assert_pass_injection_guard($vk);
675 if ($vk =~ s/ [_\s]? \d+ $//x ) {
676 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
677 . "You probably wanted ...-and => [ -$vk => COND1, -$vk => COND2 ... ]";
679 if ($vk =~ /^(?:not[ _])?between$/) {
680 local our $Cur_Col_Meta = $k;
681 my @rhs = map $self->_expand_expr($_),
682 ref($vv) eq 'ARRAY' ? @$vv : $vv;
684 (@rhs == 1 and ref($rhs[0]) eq 'HASH' and $rhs[0]->{-literal})
686 (@rhs == 2 and defined($rhs[0]) and defined($rhs[1]))
688 puke "Operator '${\uc($vk)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref";
691 join(' ', split '_', $vk),
696 if ($vk =~ /^(?:not[ _])?in$/) {
697 if (my $literal = is_literal_value($vv)) {
698 my ($sql, @bind) = @$literal;
699 my $opened_sql = $self->_open_outer_paren($sql);
701 $vk, { -ident => $k },
702 [ { -literal => [ $opened_sql, @bind ] } ]
706 'SQL::Abstract before v1.75 used to generate incorrect SQL when the '
707 . "-${\uc($vk)} operator was given an undef-containing list: !!!AUDIT YOUR CODE "
708 . 'AND DATA!!! (the upcoming Data::Query-based version of SQL::Abstract '
709 . 'will emit the logically correct SQL instead of raising this exception)'
711 puke("Argument passed to the '${\uc($vk)}' operator can not be undefined")
713 my @rhs = map $self->_expand_expr($_),
714 map { ref($_) ? $_ : { -bind => [ $k, $_ ] } }
715 map { defined($_) ? $_: puke($undef_err) }
716 (ref($vv) eq 'ARRAY' ? @$vv : $vv);
717 return $self->${\($vk =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs;
720 join(' ', split '_', $vk),
725 if ($vk eq 'ident') {
726 if (! defined $vv or ref $vv) {
727 puke "-$vk requires a single plain scalar argument (a quotable identifier)";
735 if ($vk eq 'value') {
736 return $self->_expand_expr_hashpair($k, undef) unless defined($vv);
740 { -bind => [ $k, $vv ] }
743 if ($vk =~ /^is(?:[ _]not)?$/) {
744 puke "$vk can only take undef as argument"
748 and exists($vv->{-value})
749 and !defined($vv->{-value})
752 return +{ -op => [ $vk.' null', { -ident => $k } ] };
754 if ($vk =~ /^(and|or)$/) {
755 if (ref($vv) eq 'HASH') {
758 map $self->_expand_expr_hashpair($k, { $_ => $vv->{$_} }),
763 if (my $us = List::Util::first { $vk =~ $_->{regex} } @{$self->{user_special_ops}}) {
764 return { -op => [ $vk, { -ident => $k }, $vv ] };
766 if (ref($vv) eq 'ARRAY') {
767 my ($logic, @values) = (
768 (defined($vv->[0]) and $vv->[0] =~ /^-(and|or)$/i)
773 $vk =~ $self->{inequality_op}
774 or join(' ', split '_', $vk) =~ $self->{not_like_op}
776 if (lc($logic) eq '-or' and @values > 1) {
777 my $op = uc join ' ', split '_', $vk;
778 belch "A multi-element arrayref as an argument to the inequality op '$op' "
779 . 'is technically equivalent to an always-true 1=1 (you probably wanted '
780 . "to say ...{ \$inequality_op => [ -and => \@values ] }... instead)"
785 # try to DWIM on equality operators
786 my $op = join ' ', split '_', $vk;
788 $op =~ $self->{equality_op} ? $self->sqlfalse
789 : $op =~ $self->{like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqlfalse
790 : $op =~ $self->{inequality_op} ? $self->sqltrue
791 : $op =~ $self->{not_like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqltrue
792 : puke "operator '$op' applied on an empty array (field '$k')";
796 map $self->_expand_expr_hashpair($k => { $vk => $_ }),
804 and exists $vv->{-value}
805 and not defined $vv->{-value}
808 my $op = join ' ', split '_', $vk;
810 $op =~ /^not$/i ? 'is not' # legacy
811 : $op =~ $self->{equality_op} ? 'is'
812 : $op =~ $self->{like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is'
813 : $op =~ $self->{inequality_op} ? 'is not'
814 : $op =~ $self->{not_like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is not'
815 : puke "unexpected operator '$op' with undef operand";
816 return +{ -op => [ $is.' null', { -ident => $k } ] };
818 local our $Cur_Col_Meta = $k;
822 $self->_expand_expr($vv)
825 if (ref($v) eq 'ARRAY') {
826 return $self->sqlfalse unless @$v;
827 $self->_debug("ARRAY($k) means distribute over elements");
829 $v->[0] =~ /^-((?:and|or))$/i
830 ? ($v = [ @{$v}[1..$#$v] ], $1)
831 : ($self->{logic} || 'or')
835 map $self->_expand_expr({ $k => $_ }, $this_logic), @$v
838 if (my $literal = is_literal_value($v)) {
840 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
843 my ($sql, @bind) = @$literal;
844 if ($self->{bindtype} eq 'columns') {
846 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
847 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
851 return +{ -literal => [ $self->_quote($k).' '.$sql, @bind ] };
857 my ($self, $expr) = @_;
858 my ($k, $v, @rest) = %$expr;
860 my %op = map +("-$_" => '_render_'.$_),
861 qw(op func value bind ident literal);
862 if (my $meth = $op{$k}) {
863 return $self->$meth($v);
865 die "notreached: $k";
869 my ($self, $where, $logic) = @_;
871 #print STDERR Data::Dumper::Concise::Dumper([ $where, $logic ]);
873 my $where_exp = $self->_expand_expr($where, $logic);
875 #print STDERR Data::Dumper::Concise::Dumper([ EXP => $where_exp ]);
877 # dispatch on appropriate method according to refkind of $where
878 # my $method = $self->_METHOD_FOR_refkind("_where", $where_exp);
880 # my ($sql, @bind) = $self->$method($where_exp, $logic);
882 my ($sql, @bind) = defined($where_exp) ? $self->_render_expr($where_exp) : (undef);
884 # DBIx::Class used to call _recurse_where in scalar context
885 # something else might too...
887 return ($sql, @bind);
890 belch "Calling _recurse_where in scalar context is deprecated and will go away before 2.0";
896 my ($self, $ident) = @_;
898 return $self->_convert($self->_quote($ident));
902 my ($self, $value) = @_;
904 return ($self->_convert('?'), $self->_bindtype(undef, $value));
907 my %unop_postfix = map +($_ => 1),
908 'is null', 'is not null',
916 my ($self, $args) = @_;
917 my ($left, $low, $high) = @$args;
918 my ($rhsql, @rhbind) = do {
920 puke "Single arg to between must be a literal"
921 unless $low->{-literal};
924 my ($l, $h) = map [ $self->_render_expr($_) ], $low, $high;
925 (join(' ', $l->[0], $self->_sqlcase('and'), $h->[0]),
926 @{$l}[1..$#$l], @{$h}[1..$#$h])
929 my ($lhsql, @lhbind) = $self->_render_expr($left);
931 join(' ', '(', $lhsql, $self->_sqlcase($op), $rhsql, ')'),
935 }), 'between', 'not between'),
939 my ($self, $args) = @_;
940 my ($lhs, $rhs) = @$args;
943 my ($sql, @bind) = $self->_render_expr($_);
944 push @in_bind, @bind;
947 my ($lhsql, @lbind) = $self->_render_expr($lhs);
949 $lhsql.' '.$self->_sqlcase($op).' ( '
960 my ($op, @args) = @$v;
961 $op =~ s/^-// if length($op) > 1;
963 if (my $h = $special{$op}) {
964 return $self->$h(\@args);
966 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{user_special_ops}}) {
967 puke "Special op '${op}' requires first value to be identifier"
968 unless my ($k) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0];
969 return $self->${\($us->{handler})}($k, $op, $args[1]);
971 my $final_op = $op =~ /^(?:is|not)_/ ? join(' ', split '_', $op) : $op;
972 if (@args == 1 and $op !~ /^(and|or)$/) {
973 my ($expr_sql, @bind) = $self->_render_expr($args[0]);
974 my $op_sql = $self->_sqlcase($final_op);
976 $unop_postfix{lc($final_op)}
977 ? "${expr_sql} ${op_sql}"
978 : "${op_sql} ${expr_sql}"
980 return (($op eq 'not' ? '('.$final_sql.')' : $final_sql), @bind);
982 my @parts = map [ $self->_render_expr($_) ], @args;
983 my ($final_sql) = map +($op =~ /^(and|or)$/ ? "(${_})" : $_), join(
984 ($final_op eq ',' ? '' : ' ').$self->_sqlcase($final_op).' ',
989 map @{$_}[1..$#$_], @parts
996 my ($self, $rest) = @_;
997 my ($func, @args) = @$rest;
1001 push @arg_sql, shift @x;
1003 } map [ $self->_render_expr($_) ], @args;
1004 return ($self->_sqlcase($func).'('.join(', ', @arg_sql).')', @bind);
1008 my ($self, $bind) = @_;
1009 return ($self->_convert('?'), $self->_bindtype(@$bind));
1012 sub _render_literal {
1013 my ($self, $literal) = @_;
1014 $self->_assert_bindval_matches_bindtype(@{$literal}[1..$#$literal]);
1018 # Some databases (SQLite) treat col IN (1, 2) different from
1019 # col IN ( (1, 2) ). Use this to strip all outer parens while
1020 # adding them back in the corresponding method
1021 sub _open_outer_paren {
1022 my ($self, $sql) = @_;
1024 while (my ($inner) = $sql =~ /^ \s* \( (.*) \) \s* $/xs) {
1026 # there are closing parens inside, need the heavy duty machinery
1027 # to reevaluate the extraction starting from $sql (full reevaluation)
1028 if ($inner =~ /\)/) {
1029 require Text::Balanced;
1031 my (undef, $remainder) = do {
1032 # idiotic design - writes to $@ but *DOES NOT* throw exceptions
1034 Text::Balanced::extract_bracketed($sql, '()', qr/\s*/);
1037 # the entire expression needs to be a balanced bracketed thing
1038 # (after an extract no remainder sans trailing space)
1039 last if defined $remainder and $remainder =~ /\S/;
1049 #======================================================================
1051 #======================================================================
1054 my ($self, $arg) = @_;
1056 return '' unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg);
1058 my $expander = sub {
1059 my ($self, $dir, $expr) = @_;
1060 my @exp = map +(defined($dir) ? { -op => [ $dir => $_ ] } : $_),
1061 map $self->_expand_expr($_, undef, -ident),
1062 ref($expr) eq 'ARRAY' ? @$expr : $expr;
1063 return (@exp > 1 ? { -op => [ ',', @exp ] } : $exp[0]);
1066 local $self->{custom_expansions} = {
1067 asc => sub { shift->$expander(asc => @_) },
1068 desc => sub { shift->$expander(desc => @_) },
1071 my $expanded = $self->$expander(undef, $arg);
1073 my ($sql, @bind) = $self->_render_expr($expanded);
1075 my $final_sql = $self->_sqlcase(' order by ').$sql;
1077 return wantarray ? ($final_sql, @bind) : $final_sql;
1080 #======================================================================
1081 # DATASOURCE (FOR NOW, JUST PLAIN TABLE OR LIST OF TABLES)
1082 #======================================================================
1087 ($self->_render_expr(
1088 $self->_expand_maybe_list_expr($from, undef, -ident)
1093 #======================================================================
1095 #======================================================================
1097 sub _expand_maybe_list_expr {
1098 my ($self, $expr, $logic, $default) = @_;
1100 if (ref($expr) eq 'ARRAY') {
1102 ',', map $self->_expand_expr($_, $logic, $default), @$expr
1109 return $self->_expand_expr($e, $logic, $default);
1112 # highly optimized, as it's called way too often
1114 # my ($self, $label) = @_;
1116 return '' unless defined $_[1];
1117 return ${$_[1]} if ref($_[1]) eq 'SCALAR';
1119 $_[0]->{quote_char} or
1120 ($_[0]->_assert_pass_injection_guard($_[1]), return $_[1]);
1122 my $qref = ref $_[0]->{quote_char};
1124 !$qref ? ($_[0]->{quote_char}, $_[0]->{quote_char})
1125 : ($qref eq 'ARRAY') ? @{$_[0]->{quote_char}}
1126 : puke "Unsupported quote_char format: $_[0]->{quote_char}";
1128 my $esc = $_[0]->{escape_char} || $r;
1130 # parts containing * are naturally unquoted
1131 return join($_[0]->{name_sep}||'', map
1132 +( $_ eq '*' ? $_ : do { (my $n = $_) =~ s/(\Q$esc\E|\Q$r\E)/$esc$1/g; $l . $n . $r } ),
1133 ( $_[0]->{name_sep} ? split (/\Q$_[0]->{name_sep}\E/, $_[1] ) : $_[1] )
1138 # Conversion, if applicable
1140 #my ($self, $arg) = @_;
1141 if ($_[0]->{convert_where}) {
1142 return $_[0]->_sqlcase($_[0]->{convert_where}) .'(' . $_[1] . ')';
1149 #my ($self, $col, @vals) = @_;
1150 # called often - tighten code
1151 return $_[0]->{bindtype} eq 'columns'
1152 ? map {[$_[1], $_]} @_[2 .. $#_]
1157 # Dies if any element of @bind is not in [colname => value] format
1158 # if bindtype is 'columns'.
1159 sub _assert_bindval_matches_bindtype {
1160 # my ($self, @bind) = @_;
1162 if ($self->{bindtype} eq 'columns') {
1164 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
1165 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
1171 sub _join_sql_clauses {
1172 my ($self, $logic, $clauses_aref, $bind_aref) = @_;
1174 if (@$clauses_aref > 1) {
1175 my $join = " " . $self->_sqlcase($logic) . " ";
1176 my $sql = '( ' . join($join, @$clauses_aref) . ' )';
1177 return ($sql, @$bind_aref);
1179 elsif (@$clauses_aref) {
1180 return ($clauses_aref->[0], @$bind_aref); # no parentheses
1183 return (); # if no SQL, ignore @$bind_aref
1188 # Fix SQL case, if so requested
1190 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
1191 # don't touch the argument ... crooked logic, but let's not change it!
1192 return $_[0]->{case} ? $_[1] : uc($_[1]);
1196 #======================================================================
1197 # DISPATCHING FROM REFKIND
1198 #======================================================================
1201 my ($self, $data) = @_;
1203 return 'UNDEF' unless defined $data;
1205 # blessed objects are treated like scalars
1206 my $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
1208 return 'SCALAR' unless $ref;
1211 while ($ref eq 'REF') {
1213 $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
1217 return ($ref||'SCALAR') . ('REF' x $n_steps);
1221 my ($self, $data) = @_;
1222 my @try = ($self->_refkind($data));
1223 push @try, 'SCALAR_or_UNDEF' if $try[0] eq 'SCALAR' || $try[0] eq 'UNDEF';
1224 push @try, 'FALLBACK';
1228 sub _METHOD_FOR_refkind {
1229 my ($self, $meth_prefix, $data) = @_;
1232 for (@{$self->_try_refkind($data)}) {
1233 $method = $self->can($meth_prefix."_".$_)
1237 return $method || puke "cannot dispatch on '$meth_prefix' for ".$self->_refkind($data);
1241 sub _SWITCH_refkind {
1242 my ($self, $data, $dispatch_table) = @_;
1245 for (@{$self->_try_refkind($data)}) {
1246 $coderef = $dispatch_table->{$_}
1250 puke "no dispatch entry for ".$self->_refkind($data)
1259 #======================================================================
1260 # VALUES, GENERATE, AUTOLOAD
1261 #======================================================================
1263 # LDNOTE: original code from nwiger, didn't touch code in that section
1264 # I feel the AUTOLOAD stuff should not be the default, it should
1265 # only be activated on explicit demand by user.
1269 my $data = shift || return;
1270 puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
1271 unless ref $data eq 'HASH';
1274 foreach my $k (sort keys %$data) {
1275 my $v = $data->{$k};
1276 $self->_SWITCH_refkind($v, {
1278 if ($self->{array_datatypes}) { # array datatype
1279 push @all_bind, $self->_bindtype($k, $v);
1281 else { # literal SQL with bind
1282 my ($sql, @bind) = @$v;
1283 $self->_assert_bindval_matches_bindtype(@bind);
1284 push @all_bind, @bind;
1287 ARRAYREFREF => sub { # literal SQL with bind
1288 my ($sql, @bind) = @${$v};
1289 $self->_assert_bindval_matches_bindtype(@bind);
1290 push @all_bind, @bind;
1292 SCALARREF => sub { # literal SQL without bind
1294 SCALAR_or_UNDEF => sub {
1295 push @all_bind, $self->_bindtype($k, $v);
1306 my(@sql, @sqlq, @sqlv);
1310 if ($ref eq 'HASH') {
1311 for my $k (sort keys %$_) {
1314 my $label = $self->_quote($k);
1315 if ($r eq 'ARRAY') {
1316 # literal SQL with bind
1317 my ($sql, @bind) = @$v;
1318 $self->_assert_bindval_matches_bindtype(@bind);
1319 push @sqlq, "$label = $sql";
1321 } elsif ($r eq 'SCALAR') {
1322 # literal SQL without bind
1323 push @sqlq, "$label = $$v";
1325 push @sqlq, "$label = ?";
1326 push @sqlv, $self->_bindtype($k, $v);
1329 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
1330 } elsif ($ref eq 'ARRAY') {
1331 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
1334 if ($r eq 'ARRAY') { # literal SQL with bind
1335 my ($sql, @bind) = @$v;
1336 $self->_assert_bindval_matches_bindtype(@bind);
1339 } elsif ($r eq 'SCALAR') { # literal SQL without bind
1340 # embedded literal SQL
1347 push @sql, '(' . join(', ', @sqlq) . ')';
1348 } elsif ($ref eq 'SCALAR') {
1352 # strings get case twiddled
1353 push @sql, $self->_sqlcase($_);
1357 my $sql = join ' ', @sql;
1359 # this is pretty tricky
1360 # if ask for an array, return ($stmt, @bind)
1361 # otherwise, s/?/shift @sqlv/ to put it inline
1363 return ($sql, @sqlv);
1365 1 while $sql =~ s/\?/my $d = shift(@sqlv);
1366 ref $d ? $d->[1] : $d/e;
1375 # This allows us to check for a local, then _form, attr
1377 my($name) = $AUTOLOAD =~ /.*::(.+)/;
1378 return $self->generate($name, @_);
1389 SQL::Abstract - Generate SQL from Perl data structures
1395 my $sql = SQL::Abstract->new;
1397 my($stmt, @bind) = $sql->select($source, \@fields, \%where, $order);
1399 my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
1401 my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
1403 my($stmt, @bind) = $sql->delete($table, \%where);
1405 # Then, use these in your DBI statements
1406 my $sth = $dbh->prepare($stmt);
1407 $sth->execute(@bind);
1409 # Just generate the WHERE clause
1410 my($stmt, @bind) = $sql->where(\%where, $order);
1412 # Return values in the same order, for hashed queries
1413 # See PERFORMANCE section for more details
1414 my @bind = $sql->values(\%fieldvals);
1418 This module was inspired by the excellent L<DBIx::Abstract>.
1419 However, in using that module I found that what I really wanted
1420 to do was generate SQL, but still retain complete control over my
1421 statement handles and use the DBI interface. So, I set out to
1422 create an abstract SQL generation module.
1424 While based on the concepts used by L<DBIx::Abstract>, there are
1425 several important differences, especially when it comes to WHERE
1426 clauses. I have modified the concepts used to make the SQL easier
1427 to generate from Perl data structures and, IMO, more intuitive.
1428 The underlying idea is for this module to do what you mean, based
1429 on the data structures you provide it. The big advantage is that
1430 you don't have to modify your code every time your data changes,
1431 as this module figures it out.
1433 To begin with, an SQL INSERT is as easy as just specifying a hash
1434 of C<key=value> pairs:
1437 name => 'Jimbo Bobson',
1438 phone => '123-456-7890',
1439 address => '42 Sister Lane',
1440 city => 'St. Louis',
1441 state => 'Louisiana',
1444 The SQL can then be generated with this:
1446 my($stmt, @bind) = $sql->insert('people', \%data);
1448 Which would give you something like this:
1450 $stmt = "INSERT INTO people
1451 (address, city, name, phone, state)
1452 VALUES (?, ?, ?, ?, ?)";
1453 @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
1454 '123-456-7890', 'Louisiana');
1456 These are then used directly in your DBI code:
1458 my $sth = $dbh->prepare($stmt);
1459 $sth->execute(@bind);
1461 =head2 Inserting and Updating Arrays
1463 If your database has array types (like for example Postgres),
1464 activate the special option C<< array_datatypes => 1 >>
1465 when creating the C<SQL::Abstract> object.
1466 Then you may use an arrayref to insert and update database array types:
1468 my $sql = SQL::Abstract->new(array_datatypes => 1);
1470 planets => [qw/Mercury Venus Earth Mars/]
1473 my($stmt, @bind) = $sql->insert('solar_system', \%data);
1477 $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
1479 @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
1482 =head2 Inserting and Updating SQL
1484 In order to apply SQL functions to elements of your C<%data> you may
1485 specify a reference to an arrayref for the given hash value. For example,
1486 if you need to execute the Oracle C<to_date> function on a value, you can
1487 say something like this:
1491 date_entered => \[ "to_date(?,'MM/DD/YYYY')", "03/02/2003" ],
1494 The first value in the array is the actual SQL. Any other values are
1495 optional and would be included in the bind values array. This gives
1498 my($stmt, @bind) = $sql->insert('people', \%data);
1500 $stmt = "INSERT INTO people (name, date_entered)
1501 VALUES (?, to_date(?,'MM/DD/YYYY'))";
1502 @bind = ('Bill', '03/02/2003');
1504 An UPDATE is just as easy, all you change is the name of the function:
1506 my($stmt, @bind) = $sql->update('people', \%data);
1508 Notice that your C<%data> isn't touched; the module will generate
1509 the appropriately quirky SQL for you automatically. Usually you'll
1510 want to specify a WHERE clause for your UPDATE, though, which is
1511 where handling C<%where> hashes comes in handy...
1513 =head2 Complex where statements
1515 This module can generate pretty complicated WHERE statements
1516 easily. For example, simple C<key=value> pairs are taken to mean
1517 equality, and if you want to see if a field is within a set
1518 of values, you can use an arrayref. Let's say we wanted to
1519 SELECT some data based on this criteria:
1522 requestor => 'inna',
1523 worker => ['nwiger', 'rcwe', 'sfz'],
1524 status => { '!=', 'completed' }
1527 my($stmt, @bind) = $sql->select('tickets', '*', \%where);
1529 The above would give you something like this:
1531 $stmt = "SELECT * FROM tickets WHERE
1532 ( requestor = ? ) AND ( status != ? )
1533 AND ( worker = ? OR worker = ? OR worker = ? )";
1534 @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
1536 Which you could then use in DBI code like so:
1538 my $sth = $dbh->prepare($stmt);
1539 $sth->execute(@bind);
1545 The methods are simple. There's one for every major SQL operation,
1546 and a constructor you use first. The arguments are specified in a
1547 similar order for each method (table, then fields, then a where
1548 clause) to try and simplify things.
1550 =head2 new(option => 'value')
1552 The C<new()> function takes a list of options and values, and returns
1553 a new B<SQL::Abstract> object which can then be used to generate SQL
1554 through the methods below. The options accepted are:
1560 If set to 'lower', then SQL will be generated in all lowercase. By
1561 default SQL is generated in "textbook" case meaning something like:
1563 SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
1565 Any setting other than 'lower' is ignored.
1569 This determines what the default comparison operator is. By default
1570 it is C<=>, meaning that a hash like this:
1572 %where = (name => 'nwiger', email => 'nate@wiger.org');
1574 Will generate SQL like this:
1576 WHERE name = 'nwiger' AND email = 'nate@wiger.org'
1578 However, you may want loose comparisons by default, so if you set
1579 C<cmp> to C<like> you would get SQL such as:
1581 WHERE name like 'nwiger' AND email like 'nate@wiger.org'
1583 You can also override the comparison on an individual basis - see
1584 the huge section on L</"WHERE CLAUSES"> at the bottom.
1586 =item sqltrue, sqlfalse
1588 Expressions for inserting boolean values within SQL statements.
1589 By default these are C<1=1> and C<1=0>. They are used
1590 by the special operators C<-in> and C<-not_in> for generating
1591 correct SQL even when the argument is an empty array (see below).
1595 This determines the default logical operator for multiple WHERE
1596 statements in arrays or hashes. If absent, the default logic is "or"
1597 for arrays, and "and" for hashes. This means that a WHERE
1601 event_date => {'>=', '2/13/99'},
1602 event_date => {'<=', '4/24/03'},
1605 will generate SQL like this:
1607 WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
1609 This is probably not what you want given this query, though (look
1610 at the dates). To change the "OR" to an "AND", simply specify:
1612 my $sql = SQL::Abstract->new(logic => 'and');
1614 Which will change the above C<WHERE> to:
1616 WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
1618 The logic can also be changed locally by inserting
1619 a modifier in front of an arrayref:
1621 @where = (-and => [event_date => {'>=', '2/13/99'},
1622 event_date => {'<=', '4/24/03'} ]);
1624 See the L</"WHERE CLAUSES"> section for explanations.
1628 This will automatically convert comparisons using the specified SQL
1629 function for both column and value. This is mostly used with an argument
1630 of C<upper> or C<lower>, so that the SQL will have the effect of
1631 case-insensitive "searches". For example, this:
1633 $sql = SQL::Abstract->new(convert => 'upper');
1634 %where = (keywords => 'MaKe iT CAse inSeNSItive');
1636 Will turn out the following SQL:
1638 WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
1640 The conversion can be C<upper()>, C<lower()>, or any other SQL function
1641 that can be applied symmetrically to fields (actually B<SQL::Abstract> does
1642 not validate this option; it will just pass through what you specify verbatim).
1646 This is a kludge because many databases suck. For example, you can't
1647 just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
1648 Instead, you have to use C<bind_param()>:
1650 $sth->bind_param(1, 'reg data');
1651 $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
1653 The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
1654 which loses track of which field each slot refers to. Fear not.
1656 If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
1657 Currently, you can specify either C<normal> (default) or C<columns>. If you
1658 specify C<columns>, you will get an array that looks like this:
1660 my $sql = SQL::Abstract->new(bindtype => 'columns');
1661 my($stmt, @bind) = $sql->insert(...);
1664 [ 'column1', 'value1' ],
1665 [ 'column2', 'value2' ],
1666 [ 'column3', 'value3' ],
1669 You can then iterate through this manually, using DBI's C<bind_param()>.
1671 $sth->prepare($stmt);
1674 my($col, $data) = @$_;
1675 if ($col eq 'details' || $col eq 'comments') {
1676 $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
1677 } elsif ($col eq 'image') {
1678 $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
1680 $sth->bind_param($i, $data);
1684 $sth->execute; # execute without @bind now
1686 Now, why would you still use B<SQL::Abstract> if you have to do this crap?
1687 Basically, the advantage is still that you don't have to care which fields
1688 are or are not included. You could wrap that above C<for> loop in a simple
1689 sub called C<bind_fields()> or something and reuse it repeatedly. You still
1690 get a layer of abstraction over manual SQL specification.
1692 Note that if you set L</bindtype> to C<columns>, the C<\[ $sql, @bind ]>
1693 construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
1694 will expect the bind values in this format.
1698 This is the character that a table or column name will be quoted
1699 with. By default this is an empty string, but you could set it to
1700 the character C<`>, to generate SQL like this:
1702 SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
1704 Alternatively, you can supply an array ref of two items, the first being the left
1705 hand quote character, and the second the right hand quote character. For
1706 example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
1707 that generates SQL like this:
1709 SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
1711 Quoting is useful if you have tables or columns names that are reserved
1712 words in your database's SQL dialect.
1716 This is the character that will be used to escape L</quote_char>s appearing
1717 in an identifier before it has been quoted.
1719 The parameter default in case of a single L</quote_char> character is the quote
1722 When opening-closing-style quoting is used (L</quote_char> is an arrayref)
1723 this parameter defaults to the B<closing (right)> L</quote_char>. Occurrences
1724 of the B<opening (left)> L</quote_char> within the identifier are currently left
1725 untouched. The default for opening-closing-style quotes may change in future
1726 versions, thus you are B<strongly encouraged> to specify the escape character
1731 This is the character that separates a table and column name. It is
1732 necessary to specify this when the C<quote_char> option is selected,
1733 so that tables and column names can be individually quoted like this:
1735 SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
1737 =item injection_guard
1739 A regular expression C<qr/.../> that is applied to any C<-function> and unquoted
1740 column name specified in a query structure. This is a safety mechanism to avoid
1741 injection attacks when mishandling user input e.g.:
1743 my %condition_as_column_value_pairs = get_values_from_user();
1744 $sqla->select( ... , \%condition_as_column_value_pairs );
1746 If the expression matches an exception is thrown. Note that literal SQL
1747 supplied via C<\'...'> or C<\['...']> is B<not> checked in any way.
1749 Defaults to checking for C<;> and the C<GO> keyword (TransactSQL)
1751 =item array_datatypes
1753 When this option is true, arrayrefs in INSERT or UPDATE are
1754 interpreted as array datatypes and are passed directly
1756 When this option is false, arrayrefs are interpreted
1757 as literal SQL, just like refs to arrayrefs
1758 (but this behavior is for backwards compatibility; when writing
1759 new queries, use the "reference to arrayref" syntax
1765 Takes a reference to a list of "special operators"
1766 to extend the syntax understood by L<SQL::Abstract>.
1767 See section L</"SPECIAL OPERATORS"> for details.
1771 Takes a reference to a list of "unary operators"
1772 to extend the syntax understood by L<SQL::Abstract>.
1773 See section L</"UNARY OPERATORS"> for details.
1779 =head2 insert($table, \@values || \%fieldvals, \%options)
1781 This is the simplest function. You simply give it a table name
1782 and either an arrayref of values or hashref of field/value pairs.
1783 It returns an SQL INSERT statement and a list of bind values.
1784 See the sections on L</"Inserting and Updating Arrays"> and
1785 L</"Inserting and Updating SQL"> for information on how to insert
1786 with those data types.
1788 The optional C<\%options> hash reference may contain additional
1789 options to generate the insert SQL. Currently supported options
1796 Takes either a scalar of raw SQL fields, or an array reference of
1797 field names, and adds on an SQL C<RETURNING> statement at the end.
1798 This allows you to return data generated by the insert statement
1799 (such as row IDs) without performing another C<SELECT> statement.
1800 Note, however, this is not part of the SQL standard and may not
1801 be supported by all database engines.
1805 =head2 update($table, \%fieldvals, \%where, \%options)
1807 This takes a table, hashref of field/value pairs, and an optional
1808 hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
1810 See the sections on L</"Inserting and Updating Arrays"> and
1811 L</"Inserting and Updating SQL"> for information on how to insert
1812 with those data types.
1814 The optional C<\%options> hash reference may contain additional
1815 options to generate the update SQL. Currently supported options
1822 See the C<returning> option to
1823 L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
1827 =head2 select($source, $fields, $where, $order)
1829 This returns a SQL SELECT statement and associated list of bind values, as
1830 specified by the arguments:
1836 Specification of the 'FROM' part of the statement.
1837 The argument can be either a plain scalar (interpreted as a table
1838 name, will be quoted), or an arrayref (interpreted as a list
1839 of table names, joined by commas, quoted), or a scalarref
1840 (literal SQL, not quoted).
1844 Specification of the list of fields to retrieve from
1846 The argument can be either an arrayref (interpreted as a list
1847 of field names, will be joined by commas and quoted), or a
1848 plain scalar (literal SQL, not quoted).
1849 Please observe that this API is not as flexible as that of
1850 the first argument C<$source>, for backwards compatibility reasons.
1854 Optional argument to specify the WHERE part of the query.
1855 The argument is most often a hashref, but can also be
1856 an arrayref or plain scalar --
1857 see section L<WHERE clause|/"WHERE CLAUSES"> for details.
1861 Optional argument to specify the ORDER BY part of the query.
1862 The argument can be a scalar, a hashref or an arrayref
1863 -- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
1869 =head2 delete($table, \%where, \%options)
1871 This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
1872 It returns an SQL DELETE statement and list of bind values.
1874 The optional C<\%options> hash reference may contain additional
1875 options to generate the delete SQL. Currently supported options
1882 See the C<returning> option to
1883 L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
1887 =head2 where(\%where, $order)
1889 This is used to generate just the WHERE clause. For example,
1890 if you have an arbitrary data structure and know what the
1891 rest of your SQL is going to look like, but want an easy way
1892 to produce a WHERE clause, use this. It returns an SQL WHERE
1893 clause and list of bind values.
1896 =head2 values(\%data)
1898 This just returns the values from the hash C<%data>, in the same
1899 order that would be returned from any of the other above queries.
1900 Using this allows you to markedly speed up your queries if you
1901 are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
1903 =head2 generate($any, 'number', $of, \@data, $struct, \%types)
1905 Warning: This is an experimental method and subject to change.
1907 This returns arbitrarily generated SQL. It's a really basic shortcut.
1908 It will return two different things, depending on return context:
1910 my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
1911 my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
1913 These would return the following:
1915 # First calling form
1916 $stmt = "CREATE TABLE test (?, ?)";
1917 @bind = (field1, field2);
1919 # Second calling form
1920 $stmt_and_val = "CREATE TABLE test (field1, field2)";
1922 Depending on what you're trying to do, it's up to you to choose the correct
1923 format. In this example, the second form is what you would want.
1927 $sql->generate('alter session', { nls_date_format => 'MM/YY' });
1931 ALTER SESSION SET nls_date_format = 'MM/YY'
1933 You get the idea. Strings get their case twiddled, but everything
1934 else remains verbatim.
1936 =head1 EXPORTABLE FUNCTIONS
1938 =head2 is_plain_value
1940 Determines if the supplied argument is a plain value as understood by this
1945 =item * The value is C<undef>
1947 =item * The value is a non-reference
1949 =item * The value is an object with stringification overloading
1951 =item * The value is of the form C<< { -value => $anything } >>
1955 On failure returns C<undef>, on success returns a B<scalar> reference
1956 to the original supplied argument.
1962 The stringification overloading detection is rather advanced: it takes
1963 into consideration not only the presence of a C<""> overload, but if that
1964 fails also checks for enabled
1965 L<autogenerated versions of C<"">|overload/Magic Autogeneration>, based
1966 on either C<0+> or C<bool>.
1968 Unfortunately testing in the field indicates that this
1969 detection B<< may tickle a latent bug in perl versions before 5.018 >>,
1970 but only when very large numbers of stringifying objects are involved.
1971 At the time of writing ( Sep 2014 ) there is no clear explanation of
1972 the direct cause, nor is there a manageably small test case that reliably
1973 reproduces the problem.
1975 If you encounter any of the following exceptions in B<random places within
1976 your application stack> - this module may be to blame:
1978 Operation "ne": no method found,
1979 left argument in overloaded package <something>,
1980 right argument in overloaded package <something>
1984 Stub found while resolving method "???" overloading """" in package <something>
1986 If you fall victim to the above - please attempt to reduce the problem
1987 to something that could be sent to the L<SQL::Abstract developers
1988 |DBIx::Class/GETTING HELP/SUPPORT>
1989 (either publicly or privately). As a workaround in the meantime you can
1990 set C<$ENV{SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION}> to a true
1991 value, which will most likely eliminate your problem (at the expense of
1992 not being able to properly detect exotic forms of stringification).
1994 This notice and environment variable will be removed in a future version,
1995 as soon as the underlying problem is found and a reliable workaround is
2000 =head2 is_literal_value
2002 Determines if the supplied argument is a literal value as understood by this
2007 =item * C<\$sql_string>
2009 =item * C<\[ $sql_string, @bind_values ]>
2013 On failure returns C<undef>, on success returns an B<array> reference
2014 containing the unpacked version of the supplied literal SQL and bind values.
2016 =head1 WHERE CLAUSES
2020 This module uses a variation on the idea from L<DBIx::Abstract>. It
2021 is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
2022 module is that things in arrays are OR'ed, and things in hashes
2025 The easiest way to explain is to show lots of examples. After
2026 each C<%where> hash shown, it is assumed you used:
2028 my($stmt, @bind) = $sql->where(\%where);
2030 However, note that the C<%where> hash can be used directly in any
2031 of the other functions as well, as described above.
2033 =head2 Key-value pairs
2035 So, let's get started. To begin, a simple hash:
2039 status => 'completed'
2042 Is converted to SQL C<key = val> statements:
2044 $stmt = "WHERE user = ? AND status = ?";
2045 @bind = ('nwiger', 'completed');
2047 One common thing I end up doing is having a list of values that
2048 a field can be in. To do this, simply specify a list inside of
2053 status => ['assigned', 'in-progress', 'pending'];
2056 This simple code will create the following:
2058 $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
2059 @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
2061 A field associated to an empty arrayref will be considered a
2062 logical false and will generate 0=1.
2064 =head2 Tests for NULL values
2066 If the value part is C<undef> then this is converted to SQL <IS NULL>
2075 $stmt = "WHERE user = ? AND status IS NULL";
2078 To test if a column IS NOT NULL:
2082 status => { '!=', undef },
2085 =head2 Specific comparison operators
2087 If you want to specify a different type of operator for your comparison,
2088 you can use a hashref for a given column:
2092 status => { '!=', 'completed' }
2095 Which would generate:
2097 $stmt = "WHERE user = ? AND status != ?";
2098 @bind = ('nwiger', 'completed');
2100 To test against multiple values, just enclose the values in an arrayref:
2102 status => { '=', ['assigned', 'in-progress', 'pending'] };
2104 Which would give you:
2106 "WHERE status = ? OR status = ? OR status = ?"
2109 The hashref can also contain multiple pairs, in which case it is expanded
2110 into an C<AND> of its elements:
2114 status => { '!=', 'completed', -not_like => 'pending%' }
2117 # Or more dynamically, like from a form
2118 $where{user} = 'nwiger';
2119 $where{status}{'!='} = 'completed';
2120 $where{status}{'-not_like'} = 'pending%';
2122 # Both generate this
2123 $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
2124 @bind = ('nwiger', 'completed', 'pending%');
2127 To get an OR instead, you can combine it with the arrayref idea:
2131 priority => [ { '=', 2 }, { '>', 5 } ]
2134 Which would generate:
2136 $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?";
2137 @bind = ('2', '5', 'nwiger');
2139 If you want to include literal SQL (with or without bind values), just use a
2140 scalar reference or reference to an arrayref as the value:
2143 date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
2144 date_expires => { '<' => \"now()" }
2147 Which would generate:
2149 $stmt = "WHERE date_entered > to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
2150 @bind = ('11/26/2008');
2153 =head2 Logic and nesting operators
2155 In the example above,
2156 there is a subtle trap if you want to say something like
2157 this (notice the C<AND>):
2159 WHERE priority != ? AND priority != ?
2161 Because, in Perl you I<can't> do this:
2163 priority => { '!=' => 2, '!=' => 1 }
2165 As the second C<!=> key will obliterate the first. The solution
2166 is to use the special C<-modifier> form inside an arrayref:
2168 priority => [ -and => {'!=', 2},
2172 Normally, these would be joined by C<OR>, but the modifier tells it
2173 to use C<AND> instead. (Hint: You can use this in conjunction with the
2174 C<logic> option to C<new()> in order to change the way your queries
2175 work by default.) B<Important:> Note that the C<-modifier> goes
2176 B<INSIDE> the arrayref, as an extra first element. This will
2177 B<NOT> do what you think it might:
2179 priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG!
2181 Here is a quick list of equivalencies, since there is some overlap:
2184 status => {'!=', 'completed', 'not like', 'pending%' }
2185 status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
2188 status => {'=', ['assigned', 'in-progress']}
2189 status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
2190 status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
2194 =head2 Special operators: IN, BETWEEN, etc.
2196 You can also use the hashref format to compare a list of fields using the
2197 C<IN> comparison operator, by specifying the list as an arrayref:
2200 status => 'completed',
2201 reportid => { -in => [567, 2335, 2] }
2204 Which would generate:
2206 $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
2207 @bind = ('completed', '567', '2335', '2');
2209 The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
2212 If the argument to C<-in> is an empty array, 'sqlfalse' is generated
2213 (by default: C<1=0>). Similarly, C<< -not_in => [] >> generates
2214 'sqltrue' (by default: C<1=1>).
2216 In addition to the array you can supply a chunk of literal sql or
2217 literal sql with bind:
2220 customer => { -in => \[
2221 'SELECT cust_id FROM cust WHERE balance > ?',
2224 status => { -in => \'SELECT status_codes FROM states' },
2230 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
2231 AND status IN ( SELECT status_codes FROM states )
2235 Finally, if the argument to C<-in> is not a reference, it will be
2236 treated as a single-element array.
2238 Another pair of operators is C<-between> and C<-not_between>,
2239 used with an arrayref of two values:
2243 completion_date => {
2244 -not_between => ['2002-10-01', '2003-02-06']
2250 WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
2252 Just like with C<-in> all plausible combinations of literal SQL
2256 start0 => { -between => [ 1, 2 ] },
2257 start1 => { -between => \["? AND ?", 1, 2] },
2258 start2 => { -between => \"lower(x) AND upper(y)" },
2259 start3 => { -between => [
2261 \["upper(?)", 'stuff' ],
2268 ( start0 BETWEEN ? AND ? )
2269 AND ( start1 BETWEEN ? AND ? )
2270 AND ( start2 BETWEEN lower(x) AND upper(y) )
2271 AND ( start3 BETWEEN lower(x) AND upper(?) )
2273 @bind = (1, 2, 1, 2, 'stuff');
2276 These are the two builtin "special operators"; but the
2277 list can be expanded: see section L</"SPECIAL OPERATORS"> below.
2279 =head2 Unary operators: bool
2281 If you wish to test against boolean columns or functions within your
2282 database you can use the C<-bool> and C<-not_bool> operators. For
2283 example to test the column C<is_user> being true and the column
2284 C<is_enabled> being false you would use:-
2288 -not_bool => 'is_enabled',
2293 WHERE is_user AND NOT is_enabled
2295 If a more complex combination is required, testing more conditions,
2296 then you should use the and/or operators:-
2301 -not_bool => { two=> { -rlike => 'bar' } },
2302 -not_bool => { three => [ { '=', 2 }, { '>', 5 } ] },
2313 (NOT ( three = ? OR three > ? ))
2316 =head2 Nested conditions, -and/-or prefixes
2318 So far, we've seen how multiple conditions are joined with a top-level
2319 C<AND>. We can change this by putting the different conditions we want in
2320 hashes and then putting those hashes in an array. For example:
2325 status => { -like => ['pending%', 'dispatched'] },
2329 status => 'unassigned',
2333 This data structure would create the following:
2335 $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
2336 OR ( user = ? AND status = ? ) )";
2337 @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
2340 Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or>
2341 to change the logic inside:
2347 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
2348 -or => { workhrs => {'<', 50}, geo => 'EURO' },
2355 $stmt = "WHERE ( user = ?
2356 AND ( ( workhrs > ? AND geo = ? )
2357 OR ( workhrs < ? OR geo = ? ) ) )";
2358 @bind = ('nwiger', '20', 'ASIA', '50', 'EURO');
2360 =head3 Algebraic inconsistency, for historical reasons
2362 C<Important note>: when connecting several conditions, the C<-and->|C<-or>
2363 operator goes C<outside> of the nested structure; whereas when connecting
2364 several constraints on one column, the C<-and> operator goes
2365 C<inside> the arrayref. Here is an example combining both features:
2368 -and => [a => 1, b => 2],
2369 -or => [c => 3, d => 4],
2370 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
2375 WHERE ( ( ( a = ? AND b = ? )
2376 OR ( c = ? OR d = ? )
2377 OR ( e LIKE ? AND e LIKE ? ) ) )
2379 This difference in syntax is unfortunate but must be preserved for
2380 historical reasons. So be careful: the two examples below would
2381 seem algebraically equivalent, but they are not
2384 { -like => 'foo%' },
2385 { -like => '%bar' },
2387 # yields: WHERE ( ( col LIKE ? AND col LIKE ? ) )
2390 { col => { -like => 'foo%' } },
2391 { col => { -like => '%bar' } },
2393 # yields: WHERE ( ( col LIKE ? OR col LIKE ? ) )
2396 =head2 Literal SQL and value type operators
2398 The basic premise of SQL::Abstract is that in WHERE specifications the "left
2399 side" is a column name and the "right side" is a value (normally rendered as
2400 a placeholder). This holds true for both hashrefs and arrayref pairs as you
2401 see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to
2402 alter this behavior. There are several ways of doing so.
2406 This is a virtual operator that signals the string to its right side is an
2407 identifier (a column name) and not a value. For example to compare two
2408 columns you would write:
2411 priority => { '<', 2 },
2412 requestor => { -ident => 'submitter' },
2417 $stmt = "WHERE priority < ? AND requestor = submitter";
2420 If you are maintaining legacy code you may see a different construct as
2421 described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new
2426 This is a virtual operator that signals that the construct to its right side
2427 is a value to be passed to DBI. This is for example necessary when you want
2428 to write a where clause against an array (for RDBMS that support such
2429 datatypes). For example:
2432 array => { -value => [1, 2, 3] }
2437 $stmt = 'WHERE array = ?';
2438 @bind = ([1, 2, 3]);
2440 Note that if you were to simply say:
2446 the result would probably not be what you wanted:
2448 $stmt = 'WHERE array = ? OR array = ? OR array = ?';
2453 Finally, sometimes only literal SQL will do. To include a random snippet
2454 of SQL verbatim, you specify it as a scalar reference. Consider this only
2455 as a last resort. Usually there is a better way. For example:
2458 priority => { '<', 2 },
2459 requestor => { -in => \'(SELECT name FROM hitmen)' },
2464 $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)"
2467 Note that in this example, you only get one bind parameter back, since
2468 the verbatim SQL is passed as part of the statement.
2472 Never use untrusted input as a literal SQL argument - this is a massive
2473 security risk (there is no way to check literal snippets for SQL
2474 injections and other nastyness). If you need to deal with untrusted input
2475 use literal SQL with placeholders as described next.
2477 =head3 Literal SQL with placeholders and bind values (subqueries)
2479 If the literal SQL to be inserted has placeholders and bind values,
2480 use a reference to an arrayref (yes this is a double reference --
2481 not so common, but perfectly legal Perl). For example, to find a date
2482 in Postgres you can use something like this:
2485 date_column => \[ "= date '2008-09-30' - ?::integer", 10 ]
2490 $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
2493 Note that you must pass the bind values in the same format as they are returned
2494 by L<where|/where(\%where, $order)>. This means that if you set L</bindtype>
2495 to C<columns>, you must provide the bind values in the
2496 C<< [ column_meta => value ] >> format, where C<column_meta> is an opaque
2497 scalar value; most commonly the column name, but you can use any scalar value
2498 (including references and blessed references), L<SQL::Abstract> will simply
2499 pass it through intact. So if C<bindtype> is set to C<columns> the above
2500 example will look like:
2503 date_column => \[ "= date '2008-09-30' - ?::integer", [ {} => 10 ] ]
2506 Literal SQL is especially useful for nesting parenthesized clauses in the
2507 main SQL query. Here is a first example:
2509 my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
2513 bar => \["IN ($sub_stmt)" => @sub_bind],
2518 $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
2519 WHERE c2 < ? AND c3 LIKE ?))";
2520 @bind = (1234, 100, "foo%");
2522 Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
2523 are expressed in the same way. Of course the C<$sub_stmt> and
2524 its associated bind values can be generated through a former call
2527 my ($sub_stmt, @sub_bind)
2528 = $sql->select("t1", "c1", {c2 => {"<" => 100},
2529 c3 => {-like => "foo%"}});
2532 bar => \["> ALL ($sub_stmt)" => @sub_bind],
2535 In the examples above, the subquery was used as an operator on a column;
2536 but the same principle also applies for a clause within the main C<%where>
2537 hash, like an EXISTS subquery:
2539 my ($sub_stmt, @sub_bind)
2540 = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
2541 my %where = ( -and => [
2543 \["EXISTS ($sub_stmt)" => @sub_bind],
2548 $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
2549 WHERE c1 = ? AND c2 > t0.c0))";
2553 Observe that the condition on C<c2> in the subquery refers to
2554 column C<t0.c0> of the main query: this is I<not> a bind
2555 value, so we have to express it through a scalar ref.
2556 Writing C<< c2 => {">" => "t0.c0"} >> would have generated
2557 C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
2558 what we wanted here.
2560 Finally, here is an example where a subquery is used
2561 for expressing unary negation:
2563 my ($sub_stmt, @sub_bind)
2564 = $sql->where({age => [{"<" => 10}, {">" => 20}]});
2565 $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
2567 lname => {like => '%son%'},
2568 \["NOT ($sub_stmt)" => @sub_bind],
2573 $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
2574 @bind = ('%son%', 10, 20)
2576 =head3 Deprecated usage of Literal SQL
2578 Below are some examples of archaic use of literal SQL. It is shown only as
2579 reference for those who deal with legacy code. Each example has a much
2580 better, cleaner and safer alternative that users should opt for in new code.
2586 my %where = ( requestor => \'IS NOT NULL' )
2588 $stmt = "WHERE requestor IS NOT NULL"
2590 This used to be the way of generating NULL comparisons, before the handling
2591 of C<undef> got formalized. For new code please use the superior syntax as
2592 described in L</Tests for NULL values>.
2596 my %where = ( requestor => \'= submitter' )
2598 $stmt = "WHERE requestor = submitter"
2600 This used to be the only way to compare columns. Use the superior L</-ident>
2601 method for all new code. For example an identifier declared in such a way
2602 will be properly quoted if L</quote_char> is properly set, while the legacy
2603 form will remain as supplied.
2607 my %where = ( is_ready => \"", completed => { '>', '2012-12-21' } )
2609 $stmt = "WHERE completed > ? AND is_ready"
2610 @bind = ('2012-12-21')
2612 Using an empty string literal used to be the only way to express a boolean.
2613 For all new code please use the much more readable
2614 L<-bool|/Unary operators: bool> operator.
2620 These pages could go on for a while, since the nesting of the data
2621 structures this module can handle are pretty much unlimited (the
2622 module implements the C<WHERE> expansion as a recursive function
2623 internally). Your best bet is to "play around" with the module a
2624 little to see how the data structures behave, and choose the best
2625 format for your data based on that.
2627 And of course, all the values above will probably be replaced with
2628 variables gotten from forms or the command line. After all, if you
2629 knew everything ahead of time, you wouldn't have to worry about
2630 dynamically-generating SQL and could just hardwire it into your
2633 =head1 ORDER BY CLAUSES
2635 Some functions take an order by clause. This can either be a scalar (just a
2636 column name), a hashref of C<< { -desc => 'col' } >> or C<< { -asc => 'col' }
2637 >>, a scalarref, an arrayref-ref, or an arrayref of any of the previous
2640 Given | Will Generate
2641 ---------------------------------------------------------------
2643 'colA' | ORDER BY colA
2645 [qw/colA colB/] | ORDER BY colA, colB
2647 {-asc => 'colA'} | ORDER BY colA ASC
2649 {-desc => 'colB'} | ORDER BY colB DESC
2651 ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC
2653 { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC
2655 \'colA DESC' | ORDER BY colA DESC
2657 \[ 'FUNC(colA, ?)', $x ] | ORDER BY FUNC(colA, ?)
2658 | /* ...with $x bound to ? */
2661 { -asc => 'colA' }, | colA ASC,
2662 { -desc => [qw/colB/] }, | colB DESC,
2663 { -asc => [qw/colC colD/] },| colC ASC, colD ASC,
2664 \'colE DESC', | colE DESC,
2665 \[ 'FUNC(colF, ?)', $x ], | FUNC(colF, ?)
2666 ] | /* ...with $x bound to ? */
2667 ===============================================================
2671 =head1 SPECIAL OPERATORS
2673 my $sqlmaker = SQL::Abstract->new(special_ops => [
2677 my ($self, $field, $op, $arg) = @_;
2683 handler => 'method_name',
2687 A "special operator" is a SQL syntactic clause that can be
2688 applied to a field, instead of a usual binary operator.
2691 WHERE field IN (?, ?, ?)
2692 WHERE field BETWEEN ? AND ?
2693 WHERE MATCH(field) AGAINST (?, ?)
2695 Special operators IN and BETWEEN are fairly standard and therefore
2696 are builtin within C<SQL::Abstract> (as the overridable methods
2697 C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
2698 like the MATCH .. AGAINST example above which is specific to MySQL,
2699 you can write your own operator handlers - supply a C<special_ops>
2700 argument to the C<new> method. That argument takes an arrayref of
2701 operator definitions; each operator definition is a hashref with two
2708 the regular expression to match the operator
2712 Either a coderef or a plain scalar method name. In both cases
2713 the expected return is C<< ($sql, @bind) >>.
2715 When supplied with a method name, it is simply called on the
2716 L<SQL::Abstract> object as:
2718 $self->$method_name($field, $op, $arg)
2722 $field is the LHS of the operator
2723 $op is the part that matched the handler regex
2726 When supplied with a coderef, it is called as:
2728 $coderef->($self, $field, $op, $arg)
2733 For example, here is an implementation
2734 of the MATCH .. AGAINST syntax for MySQL
2736 my $sqlmaker = SQL::Abstract->new(special_ops => [
2738 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
2739 {regex => qr/^match$/i,
2741 my ($self, $field, $op, $arg) = @_;
2742 $arg = [$arg] if not ref $arg;
2743 my $label = $self->_quote($field);
2744 my ($placeholder) = $self->_convert('?');
2745 my $placeholders = join ", ", (($placeholder) x @$arg);
2746 my $sql = $self->_sqlcase('match') . " ($label) "
2747 . $self->_sqlcase('against') . " ($placeholders) ";
2748 my @bind = $self->_bindtype($field, @$arg);
2749 return ($sql, @bind);
2756 =head1 UNARY OPERATORS
2758 my $sqlmaker = SQL::Abstract->new(unary_ops => [
2762 my ($self, $op, $arg) = @_;
2768 handler => 'method_name',
2772 A "unary operator" is a SQL syntactic clause that can be
2773 applied to a field - the operator goes before the field
2775 You can write your own operator handlers - supply a C<unary_ops>
2776 argument to the C<new> method. That argument takes an arrayref of
2777 operator definitions; each operator definition is a hashref with two
2784 the regular expression to match the operator
2788 Either a coderef or a plain scalar method name. In both cases
2789 the expected return is C<< $sql >>.
2791 When supplied with a method name, it is simply called on the
2792 L<SQL::Abstract> object as:
2794 $self->$method_name($op, $arg)
2798 $op is the part that matched the handler regex
2799 $arg is the RHS or argument of the operator
2801 When supplied with a coderef, it is called as:
2803 $coderef->($self, $op, $arg)
2811 Thanks to some benchmarking by Mark Stosberg, it turns out that
2812 this module is many orders of magnitude faster than using C<DBIx::Abstract>.
2813 I must admit this wasn't an intentional design issue, but it's a
2814 byproduct of the fact that you get to control your C<DBI> handles
2817 To maximize performance, use a code snippet like the following:
2819 # prepare a statement handle using the first row
2820 # and then reuse it for the rest of the rows
2822 for my $href (@array_of_hashrefs) {
2823 $stmt ||= $sql->insert('table', $href);
2824 $sth ||= $dbh->prepare($stmt);
2825 $sth->execute($sql->values($href));
2828 The reason this works is because the keys in your C<$href> are sorted
2829 internally by B<SQL::Abstract>. Thus, as long as your data retains
2830 the same structure, you only have to generate the SQL the first time
2831 around. On subsequent queries, simply use the C<values> function provided
2832 by this module to return your values in the correct order.
2834 However this depends on the values having the same type - if, for
2835 example, the values of a where clause may either have values
2836 (resulting in sql of the form C<column = ?> with a single bind
2837 value), or alternatively the values might be C<undef> (resulting in
2838 sql of the form C<column IS NULL> with no bind value) then the
2839 caching technique suggested will not work.
2843 If you use my C<CGI::FormBuilder> module at all, you'll hopefully
2844 really like this part (I do, at least). Building up a complex query
2845 can be as simple as the following:
2852 use CGI::FormBuilder;
2855 my $form = CGI::FormBuilder->new(...);
2856 my $sql = SQL::Abstract->new;
2858 if ($form->submitted) {
2859 my $field = $form->field;
2860 my $id = delete $field->{id};
2861 my($stmt, @bind) = $sql->update('table', $field, {id => $id});
2864 Of course, you would still have to connect using C<DBI> to run the
2865 query, but the point is that if you make your form look like your
2866 table, the actual query script can be extremely simplistic.
2868 If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
2869 a fast interface to returning and formatting data. I frequently
2870 use these three modules together to write complex database query
2871 apps in under 50 lines.
2873 =head1 HOW TO CONTRIBUTE
2875 Contributions are always welcome, in all usable forms (we especially
2876 welcome documentation improvements). The delivery methods include git-
2877 or unified-diff formatted patches, GitHub pull requests, or plain bug
2878 reports either via RT or the Mailing list. Contributors are generally
2879 granted full access to the official repository after their first several
2880 patches pass successful review.
2882 This project is maintained in a git repository. The code and related tools are
2883 accessible at the following locations:
2887 =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git>
2889 =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git>
2891 =item * GitHub mirror: L<https://github.com/dbsrgits/sql-abstract>
2893 =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/SQL-Abstract.git>
2899 Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
2900 Great care has been taken to preserve the I<published> behavior
2901 documented in previous versions in the 1.* family; however,
2902 some features that were previously undocumented, or behaved
2903 differently from the documentation, had to be changed in order
2904 to clarify the semantics. Hence, client code that was relying
2905 on some dark areas of C<SQL::Abstract> v1.*
2906 B<might behave differently> in v1.50.
2908 The main changes are:
2914 support for literal SQL through the C<< \ [ $sql, @bind ] >> syntax.
2918 support for the { operator => \"..." } construct (to embed literal SQL)
2922 support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
2926 optional support for L<array datatypes|/"Inserting and Updating Arrays">
2930 defensive programming: check arguments
2934 fixed bug with global logic, which was previously implemented
2935 through global variables yielding side-effects. Prior versions would
2936 interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
2937 as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
2938 Now this is interpreted
2939 as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
2944 fixed semantics of _bindtype on array args
2948 dropped the C<_anoncopy> of the %where tree. No longer necessary,
2949 we just avoid shifting arrays within that tree.
2953 dropped the C<_modlogic> function
2957 =head1 ACKNOWLEDGEMENTS
2959 There are a number of individuals that have really helped out with
2960 this module. Unfortunately, most of them submitted bugs via CPAN
2961 so I have no idea who they are! But the people I do know are:
2963 Ash Berlin (order_by hash term support)
2964 Matt Trout (DBIx::Class support)
2965 Mark Stosberg (benchmarking)
2966 Chas Owens (initial "IN" operator support)
2967 Philip Collins (per-field SQL functions)
2968 Eric Kolve (hashref "AND" support)
2969 Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
2970 Dan Kubb (support for "quote_char" and "name_sep")
2971 Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
2972 Laurent Dami (internal refactoring, extensible list of special operators, literal SQL)
2973 Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
2974 Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
2975 Oliver Charles (support for "RETURNING" after "INSERT")
2981 L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
2985 Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
2987 This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
2989 For support, your best bet is to try the C<DBIx::Class> users mailing list.
2990 While not an official support venue, C<DBIx::Class> makes heavy use of
2991 C<SQL::Abstract>, and as such list members there are very familiar with
2992 how to create queries.
2996 This module is free software; you may copy this under the same
2997 terms as perl itself (either the GNU General Public License or
2998 the Artistic License)