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.86';
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 $fieldlist = $self->_SWITCH_refkind($f, {
228 ARRAYREF => sub {join ', ', map { $self->_quote($_) } @$f;},
229 SCALAR => sub {$self->_quote($f)},
230 SCALARREF => sub {$$f},
232 return $self->_sqlcase(' returning ') . $fieldlist;
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 my (@values, @all_bind);
299 $self->_SWITCH_refkind($v, {
302 if ($self->{array_datatypes}) { # if array datatype are activated
304 push @all_bind, $self->_bindtype($column, $v);
306 else { # else literal SQL with bind
307 my ($sql, @bind) = @$v;
308 $self->_assert_bindval_matches_bindtype(@bind);
310 push @all_bind, @bind;
314 ARRAYREFREF => sub { # literal SQL with bind
315 my ($sql, @bind) = @${$v};
316 $self->_assert_bindval_matches_bindtype(@bind);
318 push @all_bind, @bind;
321 # THINK: anything useful to do with a HASHREF ?
322 HASHREF => sub { # (nothing, but old SQLA passed it through)
323 #TODO in SQLA >= 2.0 it will die instead
324 belch "HASH ref as bind value in insert is not supported";
326 push @all_bind, $self->_bindtype($column, $v);
329 SCALARREF => sub { # literal SQL without bind
333 SCALAR_or_UNDEF => sub {
335 push @all_bind, $self->_bindtype($column, $v);
340 my $sql = join(", ", @values);
341 return ($sql, @all_bind);
346 #======================================================================
348 #======================================================================
353 my $table = $self->_table(shift);
354 my $data = shift || return;
358 # first build the 'SET' part of the sql statement
359 puke "Unsupported data type specified to \$sql->update"
360 unless ref $data eq 'HASH';
362 my ($sql, @all_bind) = $self->_update_set_values($data);
363 $sql = $self->_sqlcase('update ') . $table . $self->_sqlcase(' set ')
367 my($where_sql, @where_bind) = $self->where($where);
369 push @all_bind, @where_bind;
372 if ($options->{returning}) {
373 my ($returning_sql, @returning_bind) = $self->_update_returning($options);
374 $sql .= $returning_sql;
375 push @all_bind, @returning_bind;
378 return wantarray ? ($sql, @all_bind) : $sql;
381 sub _update_set_values {
382 my ($self, $data) = @_;
384 my (@set, @all_bind);
385 for my $k (sort keys %$data) {
388 my $label = $self->_quote($k);
390 $self->_SWITCH_refkind($v, {
392 if ($self->{array_datatypes}) { # array datatype
393 push @set, "$label = ?";
394 push @all_bind, $self->_bindtype($k, $v);
396 else { # literal SQL with bind
397 my ($sql, @bind) = @$v;
398 $self->_assert_bindval_matches_bindtype(@bind);
399 push @set, "$label = $sql";
400 push @all_bind, @bind;
403 ARRAYREFREF => sub { # literal SQL with bind
404 my ($sql, @bind) = @${$v};
405 $self->_assert_bindval_matches_bindtype(@bind);
406 push @set, "$label = $sql";
407 push @all_bind, @bind;
409 SCALARREF => sub { # literal SQL without bind
410 push @set, "$label = $$v";
413 my ($op, $arg, @rest) = %$v;
415 puke 'Operator calls in update must be in the form { -op => $arg }'
416 if (@rest or not $op =~ /^\-(.+)/);
418 local our $Cur_Col_Meta = $k;
419 my ($sql, @bind) = $self->_render_expr(
420 $self->_expand_expr_hashpair($op, $arg)
423 push @set, "$label = $sql";
424 push @all_bind, @bind;
426 SCALAR_or_UNDEF => sub {
427 push @set, "$label = ?";
428 push @all_bind, $self->_bindtype($k, $v);
434 my $sql = join ', ', @set;
436 return ($sql, @all_bind);
439 # So that subclasses can override UPDATE ... RETURNING separately from
441 sub _update_returning { shift->_returning(@_) }
445 #======================================================================
447 #======================================================================
452 my $table = $self->_table(shift);
453 my $fields = shift || '*';
457 my ($fields_sql, @bind) = $self->_select_fields($fields);
459 my ($where_sql, @where_bind) = $self->where($where, $order);
460 push @bind, @where_bind;
462 my $sql = join(' ', $self->_sqlcase('select'), $fields_sql,
463 $self->_sqlcase('from'), $table)
466 return wantarray ? ($sql, @bind) : $sql;
470 my ($self, $fields) = @_;
471 return ref $fields eq 'ARRAY' ? join ', ', map { $self->_quote($_) } @$fields
475 #======================================================================
477 #======================================================================
482 my $table = $self->_table(shift);
486 my($where_sql, @bind) = $self->where($where);
487 my $sql = $self->_sqlcase('delete from ') . $table . $where_sql;
489 if ($options->{returning}) {
490 my ($returning_sql, @returning_bind) = $self->_delete_returning($options);
491 $sql .= $returning_sql;
492 push @bind, @returning_bind;
495 return wantarray ? ($sql, @bind) : $sql;
498 # So that subclasses can override DELETE ... RETURNING separately from
500 sub _delete_returning { shift->_returning(@_) }
504 #======================================================================
506 #======================================================================
510 # Finally, a separate routine just to handle WHERE clauses
512 my ($self, $where, $order) = @_;
515 my ($sql, @bind) = defined($where)
516 ? $self->_recurse_where($where)
518 $sql = (defined $sql and length $sql) ? $self->_sqlcase(' where ') . "( $sql )" : '';
522 my ($order_sql, @order_bind) = $self->_order_by($order);
524 push @bind, @order_bind;
527 return wantarray ? ($sql, @bind) : $sql;
531 my ($self, $expr, $logic, $default_scalar_to) = @_;
532 local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to;
533 return undef unless defined($expr);
534 if (ref($expr) eq 'HASH') {
535 if (keys %$expr > 1) {
539 map $self->_expand_expr_hashpair($_ => $expr->{$_}, $logic),
543 return unless %$expr;
544 return $self->_expand_expr_hashpair(%$expr, $logic);
546 if (ref($expr) eq 'ARRAY') {
547 my $logic = lc($logic || $self->{logic});
548 $logic eq 'and' or $logic eq 'or' or puke "unknown logic: $logic";
554 while (my ($el) = splice @expr, 0, 1) {
555 puke "Supplying an empty left hand side argument is not supported in array-pairs"
556 unless defined($el) and length($el);
557 my $elref = ref($el);
559 push(@res, $self->_expand_expr({ $el, shift(@expr) }));
560 } elsif ($elref eq 'ARRAY') {
561 push(@res, $self->_expand_expr($el)) if @$el;
562 } elsif (my $l = is_literal_value($el)) {
563 push @res, { -literal => $l };
564 } elsif ($elref eq 'HASH') {
565 push @res, $self->_expand_expr($el);
570 return { -op => [ $logic, @res ] };
572 if (my $literal = is_literal_value($expr)) {
573 return +{ -literal => $literal };
575 if (!ref($expr) or Scalar::Util::blessed($expr)) {
576 if (my $d = $Default_Scalar_To) {
577 return +{ $d => $expr };
579 if (my $m = our $Cur_Col_Meta) {
580 return +{ -bind => [ $m, $expr ] };
582 return +{ -value => $expr };
587 sub _expand_expr_hashpair {
588 my ($self, $k, $v, $logic) = @_;
589 unless (defined($k) and length($k)) {
590 if (defined($k) and my $literal = is_literal_value($v)) {
591 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
592 return { -literal => $literal };
594 puke "Supplying an empty left hand side argument is not supported";
597 $self->_assert_pass_injection_guard($k =~ /^-(.*)$/s);
598 if ($k =~ s/ [_\s]? \d+ $//x ) {
599 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
600 . "You probably wanted ...-and => [ $k => COND1, $k => COND2 ... ]";
603 return $self->_expand_expr($v);
607 return $self->_expand_expr($v);
609 puke "-bool => undef not supported" unless defined($v);
610 return { -ident => $v };
613 return { -op => [ 'not', $self->_expand_expr($v) ] };
615 if (my ($rest) = $k =~/^-not[_ ](.*)$/) {
618 $self->_expand_expr_hashpair("-${rest}", $v, $logic)
621 if (my ($logic) = $k =~ /^-(and|or)$/i) {
622 if (ref($v) eq 'HASH') {
623 return $self->_expand_expr($v, $logic);
625 if (ref($v) eq 'ARRAY') {
626 return $self->_expand_expr($v, $logic);
631 $op =~ s/^-// if length($op) > 1;
633 # top level special ops are illegal in general
634 puke "Illegal use of top-level '-$op'"
635 if List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
637 if ($k eq '-value' and my $m = our $Cur_Col_Meta) {
638 return +{ -bind => [ $m, $v ] };
640 if ($k eq '-op' or $k eq '-ident' or $k eq '-value' or $k eq '-bind' or $k eq '-literal' or $k eq '-func') {
643 if (my $custom = $self->{custom_expansions}{($k =~ /^-(.*)$/)[0]}) {
644 return $self->$custom($v);
649 and (keys %$v)[0] =~ /^-/
651 my ($func) = $k =~ /^-(.*)$/;
652 return +{ -func => [ $func, $self->_expand_expr($v) ] };
654 if (!ref($v) or is_literal_value($v)) {
655 return +{ -op => [ $k =~ /^-(.*)$/, $self->_expand_expr($v) ] };
662 and exists $v->{-value}
663 and not defined $v->{-value}
666 return $self->_expand_expr_hashpair($k => { $self->{cmp} => undef });
668 if (!ref($v) or Scalar::Util::blessed($v)) {
673 { -bind => [ $k, $v ] }
677 if (ref($v) eq 'HASH') {
681 map $self->_expand_expr_hashpair($k => { $_ => $v->{$_} }),
688 $self->_assert_pass_injection_guard($vk);
689 if ($vk =~ s/ [_\s]? \d+ $//x ) {
690 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
691 . "You probably wanted ...-and => [ -$vk => COND1, -$vk => COND2 ... ]";
693 if ($vk =~ /^(?:not[ _])?between$/) {
694 local our $Cur_Col_Meta = $k;
695 my @rhs = map $self->_expand_expr($_),
696 ref($vv) eq 'ARRAY' ? @$vv : $vv;
698 (@rhs == 1 and ref($rhs[0]) eq 'HASH' and $rhs[0]->{-literal})
700 (@rhs == 2 and defined($rhs[0]) and defined($rhs[1]))
702 puke "Operator '${\uc($vk)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref";
705 join(' ', split '_', $vk),
710 if ($vk =~ /^(?:not[ _])?in$/) {
711 if (my $literal = is_literal_value($vv)) {
712 my ($sql, @bind) = @$literal;
713 my $opened_sql = $self->_open_outer_paren($sql);
715 $vk, { -ident => $k },
716 [ { -literal => [ $opened_sql, @bind ] } ]
720 'SQL::Abstract before v1.75 used to generate incorrect SQL when the '
721 . "-${\uc($vk)} operator was given an undef-containing list: !!!AUDIT YOUR CODE "
722 . 'AND DATA!!! (the upcoming Data::Query-based version of SQL::Abstract '
723 . 'will emit the logically correct SQL instead of raising this exception)'
725 puke("Argument passed to the '${\uc($vk)}' operator can not be undefined")
727 my @rhs = map $self->_expand_expr($_),
728 map { ref($_) ? $_ : { -bind => [ $k, $_ ] } }
729 map { defined($_) ? $_: puke($undef_err) }
730 (ref($vv) eq 'ARRAY' ? @$vv : $vv);
731 return $self->${\($vk =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs;
734 join(' ', split '_', $vk),
739 if ($vk eq 'ident') {
740 if (! defined $vv or ref $vv) {
741 puke "-$vk requires a single plain scalar argument (a quotable identifier)";
749 if ($vk eq 'value') {
750 return $self->_expand_expr_hashpair($k, undef) unless defined($vv);
754 { -bind => [ $k, $vv ] }
757 if ($vk =~ /^is(?:[ _]not)?$/) {
758 puke "$vk can only take undef as argument"
762 and exists($vv->{-value})
763 and !defined($vv->{-value})
766 return +{ -op => [ $vk.' null', { -ident => $k } ] };
768 if ($vk =~ /^(and|or)$/) {
769 if (ref($vv) eq 'HASH') {
772 map $self->_expand_expr_hashpair($k, { $_ => $vv->{$_} }),
777 if (my $us = List::Util::first { $vk =~ $_->{regex} } @{$self->{user_special_ops}}) {
778 return { -op => [ $vk, { -ident => $k }, $vv ] };
780 if (ref($vv) eq 'ARRAY') {
781 my ($logic, @values) = (
782 (defined($vv->[0]) and $vv->[0] =~ /^-(and|or)$/i)
787 $vk =~ $self->{inequality_op}
788 or join(' ', split '_', $vk) =~ $self->{not_like_op}
790 if (lc($logic) eq '-or' and @values > 1) {
791 my $op = uc join ' ', split '_', $vk;
792 belch "A multi-element arrayref as an argument to the inequality op '$op' "
793 . 'is technically equivalent to an always-true 1=1 (you probably wanted '
794 . "to say ...{ \$inequality_op => [ -and => \@values ] }... instead)"
799 # try to DWIM on equality operators
800 my $op = join ' ', split '_', $vk;
802 $op =~ $self->{equality_op} ? $self->sqlfalse
803 : $op =~ $self->{like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqlfalse
804 : $op =~ $self->{inequality_op} ? $self->sqltrue
805 : $op =~ $self->{not_like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqltrue
806 : puke "operator '$op' applied on an empty array (field '$k')";
810 map $self->_expand_expr_hashpair($k => { $vk => $_ }),
818 and exists $vv->{-value}
819 and not defined $vv->{-value}
822 my $op = join ' ', split '_', $vk;
824 $op =~ /^not$/i ? 'is not' # legacy
825 : $op =~ $self->{equality_op} ? 'is'
826 : $op =~ $self->{like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is'
827 : $op =~ $self->{inequality_op} ? 'is not'
828 : $op =~ $self->{not_like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is not'
829 : puke "unexpected operator '$op' with undef operand";
830 return +{ -op => [ $is.' null', { -ident => $k } ] };
832 local our $Cur_Col_Meta = $k;
836 $self->_expand_expr($vv)
839 if (ref($v) eq 'ARRAY') {
840 return $self->sqlfalse unless @$v;
841 $self->_debug("ARRAY($k) means distribute over elements");
843 $v->[0] =~ /^-((?:and|or))$/i
844 ? ($v = [ @{$v}[1..$#$v] ], $1)
845 : ($self->{logic} || 'or')
849 map $self->_expand_expr({ $k => $_ }, $this_logic), @$v
852 if (my $literal = is_literal_value($v)) {
854 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
857 my ($sql, @bind) = @$literal;
858 if ($self->{bindtype} eq 'columns') {
860 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
861 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
865 return +{ -literal => [ $self->_quote($k).' '.$sql, @bind ] };
871 my ($self, $expr) = @_;
872 my ($k, $v, @rest) = %$expr;
874 my %op = map +("-$_" => '_render_'.$_),
875 qw(op func value bind ident literal);
876 if (my $meth = $op{$k}) {
877 return $self->$meth($v);
879 die "notreached: $k";
883 my ($self, $where, $logic) = @_;
885 #print STDERR Data::Dumper::Concise::Dumper([ $where, $logic ]);
887 my $where_exp = $self->_expand_expr($where, $logic);
889 #print STDERR Data::Dumper::Concise::Dumper([ EXP => $where_exp ]);
891 # dispatch on appropriate method according to refkind of $where
892 # my $method = $self->_METHOD_FOR_refkind("_where", $where_exp);
894 # my ($sql, @bind) = $self->$method($where_exp, $logic);
896 my ($sql, @bind) = defined($where_exp) ? $self->_render_expr($where_exp) : (undef);
898 # DBIx::Class used to call _recurse_where in scalar context
899 # something else might too...
901 return ($sql, @bind);
904 belch "Calling _recurse_where in scalar context is deprecated and will go away before 2.0";
910 my ($self, $ident) = @_;
912 return $self->_convert($self->_quote($ident));
916 my ($self, $value) = @_;
918 return ($self->_convert('?'), $self->_bindtype(undef, $value));
921 my %unop_postfix = map +($_ => 1),
922 'is null', 'is not null',
930 my ($self, $args) = @_;
931 my ($left, $low, $high) = @$args;
932 my ($rhsql, @rhbind) = do {
934 puke "Single arg to between must be a literal"
935 unless $low->{-literal};
938 my ($l, $h) = map [ $self->_render_expr($_) ], $low, $high;
939 (join(' ', $l->[0], $self->_sqlcase('and'), $h->[0]),
940 @{$l}[1..$#$l], @{$h}[1..$#$h])
943 my ($lhsql, @lhbind) = $self->_render_expr($left);
945 join(' ', '(', $lhsql, $self->_sqlcase($op), $rhsql, ')'),
949 }), 'between', 'not between'),
953 my ($self, $args) = @_;
954 my ($lhs, $rhs) = @$args;
957 my ($sql, @bind) = $self->_render_expr($_);
958 push @in_bind, @bind;
961 my ($lhsql, @lbind) = $self->_render_expr($lhs);
963 $lhsql.' '.$self->_sqlcase($op).' ( '
974 my ($op, @args) = @$v;
975 $op =~ s/^-// if length($op) > 1;
977 if (my $h = $special{$op}) {
978 return $self->$h(\@args);
980 if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{user_special_ops}}) {
981 puke "Special op '${op}' requires first value to be identifier"
982 unless my ($k) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0];
983 return $self->${\($us->{handler})}($k, $op, $args[1]);
985 my $final_op = $op =~ /^(?:is|not)_/ ? join(' ', split '_', $op) : $op;
986 if (@args == 1 and $op !~ /^(and|or)$/) {
987 my ($expr_sql, @bind) = $self->_render_expr($args[0]);
988 my $op_sql = $self->_sqlcase($final_op);
990 $unop_postfix{lc($final_op)}
991 ? "${expr_sql} ${op_sql}"
992 : "${op_sql} ${expr_sql}"
994 return (($op eq 'not' ? '('.$final_sql.')' : $final_sql), @bind);
996 my @parts = map [ $self->_render_expr($_) ], @args;
997 my ($final_sql) = map +($op =~ /^(and|or)$/ ? "(${_})" : $_), join(
998 ' '.$self->_sqlcase($final_op).' ',
1003 map @{$_}[1..$#$_], @parts
1010 my ($self, $rest) = @_;
1011 my ($func, @args) = @$rest;
1015 push @arg_sql, shift @x;
1017 } map [ $self->_render_expr($_) ], @args;
1018 return ($self->_sqlcase($func).'('.join(', ', @arg_sql).')', @bind);
1022 my ($self, $bind) = @_;
1023 return ($self->_convert('?'), $self->_bindtype(@$bind));
1026 sub _render_literal {
1027 my ($self, $literal) = @_;
1028 $self->_assert_bindval_matches_bindtype(@{$literal}[1..$#$literal]);
1032 # Some databases (SQLite) treat col IN (1, 2) different from
1033 # col IN ( (1, 2) ). Use this to strip all outer parens while
1034 # adding them back in the corresponding method
1035 sub _open_outer_paren {
1036 my ($self, $sql) = @_;
1038 while (my ($inner) = $sql =~ /^ \s* \( (.*) \) \s* $/xs) {
1040 # there are closing parens inside, need the heavy duty machinery
1041 # to reevaluate the extraction starting from $sql (full reevaluation)
1042 if ($inner =~ /\)/) {
1043 require Text::Balanced;
1045 my (undef, $remainder) = do {
1046 # idiotic design - writes to $@ but *DOES NOT* throw exceptions
1048 Text::Balanced::extract_bracketed($sql, '()', qr/\s*/);
1051 # the entire expression needs to be a balanced bracketed thing
1052 # (after an extract no remainder sans trailing space)
1053 last if defined $remainder and $remainder =~ /\S/;
1063 #======================================================================
1065 #======================================================================
1068 my ($self, $arg) = @_;
1070 return '' unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg);
1072 my $expander = sub {
1073 my ($self, $dir, $expr) = @_;
1074 my @exp = map +(defined($dir) ? { -op => [ $dir => $_ ] } : $_),
1075 map $self->_expand_expr($_, undef, -ident),
1076 ref($expr) eq 'ARRAY' ? @$expr : $expr;
1077 return (@exp > 1 ? { -op => [ ',', @exp ] } : $exp[0]);
1080 local $self->{custom_expansions} = {
1081 asc => sub { shift->$expander(asc => @_) },
1082 desc => sub { shift->$expander(desc => @_) },
1085 my $expanded = $self->$expander(undef, $arg);
1087 my ($sql, @bind) = $self->_render_expr($expanded);
1089 my $final_sql = $self->_sqlcase(' order by ').$sql;
1091 return wantarray ? ($final_sql, @bind) : $final_sql;
1094 #======================================================================
1095 # DATASOURCE (FOR NOW, JUST PLAIN TABLE OR LIST OF TABLES)
1096 #======================================================================
1101 $self->_SWITCH_refkind($from, {
1102 ARRAYREF => sub {join ', ', map { $self->_quote($_) } @$from;},
1103 SCALAR => sub {$self->_quote($from)},
1104 SCALARREF => sub {$$from},
1109 #======================================================================
1111 #======================================================================
1113 # highly optimized, as it's called way too often
1115 # my ($self, $label) = @_;
1117 return '' unless defined $_[1];
1118 return ${$_[1]} if ref($_[1]) eq 'SCALAR';
1120 $_[0]->{quote_char} or
1121 ($_[0]->_assert_pass_injection_guard($_[1]), return $_[1]);
1123 my $qref = ref $_[0]->{quote_char};
1125 !$qref ? ($_[0]->{quote_char}, $_[0]->{quote_char})
1126 : ($qref eq 'ARRAY') ? @{$_[0]->{quote_char}}
1127 : puke "Unsupported quote_char format: $_[0]->{quote_char}";
1129 my $esc = $_[0]->{escape_char} || $r;
1131 # parts containing * are naturally unquoted
1132 return join($_[0]->{name_sep}||'', map
1133 +( $_ eq '*' ? $_ : do { (my $n = $_) =~ s/(\Q$esc\E|\Q$r\E)/$esc$1/g; $l . $n . $r } ),
1134 ( $_[0]->{name_sep} ? split (/\Q$_[0]->{name_sep}\E/, $_[1] ) : $_[1] )
1139 # Conversion, if applicable
1141 #my ($self, $arg) = @_;
1142 if ($_[0]->{convert}) {
1143 return $_[0]->_sqlcase($_[0]->{convert}) .'(' . $_[1] . ')';
1150 #my ($self, $col, @vals) = @_;
1151 # called often - tighten code
1152 return $_[0]->{bindtype} eq 'columns'
1153 ? map {[$_[1], $_]} @_[2 .. $#_]
1158 # Dies if any element of @bind is not in [colname => value] format
1159 # if bindtype is 'columns'.
1160 sub _assert_bindval_matches_bindtype {
1161 # my ($self, @bind) = @_;
1163 if ($self->{bindtype} eq 'columns') {
1165 if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
1166 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
1172 sub _join_sql_clauses {
1173 my ($self, $logic, $clauses_aref, $bind_aref) = @_;
1175 if (@$clauses_aref > 1) {
1176 my $join = " " . $self->_sqlcase($logic) . " ";
1177 my $sql = '( ' . join($join, @$clauses_aref) . ' )';
1178 return ($sql, @$bind_aref);
1180 elsif (@$clauses_aref) {
1181 return ($clauses_aref->[0], @$bind_aref); # no parentheses
1184 return (); # if no SQL, ignore @$bind_aref
1189 # Fix SQL case, if so requested
1191 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
1192 # don't touch the argument ... crooked logic, but let's not change it!
1193 return $_[0]->{case} ? $_[1] : uc($_[1]);
1197 #======================================================================
1198 # DISPATCHING FROM REFKIND
1199 #======================================================================
1202 my ($self, $data) = @_;
1204 return 'UNDEF' unless defined $data;
1206 # blessed objects are treated like scalars
1207 my $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
1209 return 'SCALAR' unless $ref;
1212 while ($ref eq 'REF') {
1214 $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
1218 return ($ref||'SCALAR') . ('REF' x $n_steps);
1222 my ($self, $data) = @_;
1223 my @try = ($self->_refkind($data));
1224 push @try, 'SCALAR_or_UNDEF' if $try[0] eq 'SCALAR' || $try[0] eq 'UNDEF';
1225 push @try, 'FALLBACK';
1229 sub _METHOD_FOR_refkind {
1230 my ($self, $meth_prefix, $data) = @_;
1233 for (@{$self->_try_refkind($data)}) {
1234 $method = $self->can($meth_prefix."_".$_)
1238 return $method || puke "cannot dispatch on '$meth_prefix' for ".$self->_refkind($data);
1242 sub _SWITCH_refkind {
1243 my ($self, $data, $dispatch_table) = @_;
1246 for (@{$self->_try_refkind($data)}) {
1247 $coderef = $dispatch_table->{$_}
1251 puke "no dispatch entry for ".$self->_refkind($data)
1260 #======================================================================
1261 # VALUES, GENERATE, AUTOLOAD
1262 #======================================================================
1264 # LDNOTE: original code from nwiger, didn't touch code in that section
1265 # I feel the AUTOLOAD stuff should not be the default, it should
1266 # only be activated on explicit demand by user.
1270 my $data = shift || return;
1271 puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
1272 unless ref $data eq 'HASH';
1275 foreach my $k (sort keys %$data) {
1276 my $v = $data->{$k};
1277 $self->_SWITCH_refkind($v, {
1279 if ($self->{array_datatypes}) { # array datatype
1280 push @all_bind, $self->_bindtype($k, $v);
1282 else { # literal SQL with bind
1283 my ($sql, @bind) = @$v;
1284 $self->_assert_bindval_matches_bindtype(@bind);
1285 push @all_bind, @bind;
1288 ARRAYREFREF => sub { # literal SQL with bind
1289 my ($sql, @bind) = @${$v};
1290 $self->_assert_bindval_matches_bindtype(@bind);
1291 push @all_bind, @bind;
1293 SCALARREF => sub { # literal SQL without bind
1295 SCALAR_or_UNDEF => sub {
1296 push @all_bind, $self->_bindtype($k, $v);
1307 my(@sql, @sqlq, @sqlv);
1311 if ($ref eq 'HASH') {
1312 for my $k (sort keys %$_) {
1315 my $label = $self->_quote($k);
1316 if ($r eq 'ARRAY') {
1317 # literal SQL with bind
1318 my ($sql, @bind) = @$v;
1319 $self->_assert_bindval_matches_bindtype(@bind);
1320 push @sqlq, "$label = $sql";
1322 } elsif ($r eq 'SCALAR') {
1323 # literal SQL without bind
1324 push @sqlq, "$label = $$v";
1326 push @sqlq, "$label = ?";
1327 push @sqlv, $self->_bindtype($k, $v);
1330 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
1331 } elsif ($ref eq 'ARRAY') {
1332 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
1335 if ($r eq 'ARRAY') { # literal SQL with bind
1336 my ($sql, @bind) = @$v;
1337 $self->_assert_bindval_matches_bindtype(@bind);
1340 } elsif ($r eq 'SCALAR') { # literal SQL without bind
1341 # embedded literal SQL
1348 push @sql, '(' . join(', ', @sqlq) . ')';
1349 } elsif ($ref eq 'SCALAR') {
1353 # strings get case twiddled
1354 push @sql, $self->_sqlcase($_);
1358 my $sql = join ' ', @sql;
1360 # this is pretty tricky
1361 # if ask for an array, return ($stmt, @bind)
1362 # otherwise, s/?/shift @sqlv/ to put it inline
1364 return ($sql, @sqlv);
1366 1 while $sql =~ s/\?/my $d = shift(@sqlv);
1367 ref $d ? $d->[1] : $d/e;
1376 # This allows us to check for a local, then _form, attr
1378 my($name) = $AUTOLOAD =~ /.*::(.+)/;
1379 return $self->generate($name, @_);
1390 SQL::Abstract - Generate SQL from Perl data structures
1396 my $sql = SQL::Abstract->new;
1398 my($stmt, @bind) = $sql->select($source, \@fields, \%where, $order);
1400 my($stmt, @bind) = $sql->insert($table, \%fieldvals || \@values);
1402 my($stmt, @bind) = $sql->update($table, \%fieldvals, \%where);
1404 my($stmt, @bind) = $sql->delete($table, \%where);
1406 # Then, use these in your DBI statements
1407 my $sth = $dbh->prepare($stmt);
1408 $sth->execute(@bind);
1410 # Just generate the WHERE clause
1411 my($stmt, @bind) = $sql->where(\%where, $order);
1413 # Return values in the same order, for hashed queries
1414 # See PERFORMANCE section for more details
1415 my @bind = $sql->values(\%fieldvals);
1419 This module was inspired by the excellent L<DBIx::Abstract>.
1420 However, in using that module I found that what I really wanted
1421 to do was generate SQL, but still retain complete control over my
1422 statement handles and use the DBI interface. So, I set out to
1423 create an abstract SQL generation module.
1425 While based on the concepts used by L<DBIx::Abstract>, there are
1426 several important differences, especially when it comes to WHERE
1427 clauses. I have modified the concepts used to make the SQL easier
1428 to generate from Perl data structures and, IMO, more intuitive.
1429 The underlying idea is for this module to do what you mean, based
1430 on the data structures you provide it. The big advantage is that
1431 you don't have to modify your code every time your data changes,
1432 as this module figures it out.
1434 To begin with, an SQL INSERT is as easy as just specifying a hash
1435 of C<key=value> pairs:
1438 name => 'Jimbo Bobson',
1439 phone => '123-456-7890',
1440 address => '42 Sister Lane',
1441 city => 'St. Louis',
1442 state => 'Louisiana',
1445 The SQL can then be generated with this:
1447 my($stmt, @bind) = $sql->insert('people', \%data);
1449 Which would give you something like this:
1451 $stmt = "INSERT INTO people
1452 (address, city, name, phone, state)
1453 VALUES (?, ?, ?, ?, ?)";
1454 @bind = ('42 Sister Lane', 'St. Louis', 'Jimbo Bobson',
1455 '123-456-7890', 'Louisiana');
1457 These are then used directly in your DBI code:
1459 my $sth = $dbh->prepare($stmt);
1460 $sth->execute(@bind);
1462 =head2 Inserting and Updating Arrays
1464 If your database has array types (like for example Postgres),
1465 activate the special option C<< array_datatypes => 1 >>
1466 when creating the C<SQL::Abstract> object.
1467 Then you may use an arrayref to insert and update database array types:
1469 my $sql = SQL::Abstract->new(array_datatypes => 1);
1471 planets => [qw/Mercury Venus Earth Mars/]
1474 my($stmt, @bind) = $sql->insert('solar_system', \%data);
1478 $stmt = "INSERT INTO solar_system (planets) VALUES (?)"
1480 @bind = (['Mercury', 'Venus', 'Earth', 'Mars']);
1483 =head2 Inserting and Updating SQL
1485 In order to apply SQL functions to elements of your C<%data> you may
1486 specify a reference to an arrayref for the given hash value. For example,
1487 if you need to execute the Oracle C<to_date> function on a value, you can
1488 say something like this:
1492 date_entered => \[ "to_date(?,'MM/DD/YYYY')", "03/02/2003" ],
1495 The first value in the array is the actual SQL. Any other values are
1496 optional and would be included in the bind values array. This gives
1499 my($stmt, @bind) = $sql->insert('people', \%data);
1501 $stmt = "INSERT INTO people (name, date_entered)
1502 VALUES (?, to_date(?,'MM/DD/YYYY'))";
1503 @bind = ('Bill', '03/02/2003');
1505 An UPDATE is just as easy, all you change is the name of the function:
1507 my($stmt, @bind) = $sql->update('people', \%data);
1509 Notice that your C<%data> isn't touched; the module will generate
1510 the appropriately quirky SQL for you automatically. Usually you'll
1511 want to specify a WHERE clause for your UPDATE, though, which is
1512 where handling C<%where> hashes comes in handy...
1514 =head2 Complex where statements
1516 This module can generate pretty complicated WHERE statements
1517 easily. For example, simple C<key=value> pairs are taken to mean
1518 equality, and if you want to see if a field is within a set
1519 of values, you can use an arrayref. Let's say we wanted to
1520 SELECT some data based on this criteria:
1523 requestor => 'inna',
1524 worker => ['nwiger', 'rcwe', 'sfz'],
1525 status => { '!=', 'completed' }
1528 my($stmt, @bind) = $sql->select('tickets', '*', \%where);
1530 The above would give you something like this:
1532 $stmt = "SELECT * FROM tickets WHERE
1533 ( requestor = ? ) AND ( status != ? )
1534 AND ( worker = ? OR worker = ? OR worker = ? )";
1535 @bind = ('inna', 'completed', 'nwiger', 'rcwe', 'sfz');
1537 Which you could then use in DBI code like so:
1539 my $sth = $dbh->prepare($stmt);
1540 $sth->execute(@bind);
1546 The methods are simple. There's one for every major SQL operation,
1547 and a constructor you use first. The arguments are specified in a
1548 similar order for each method (table, then fields, then a where
1549 clause) to try and simplify things.
1551 =head2 new(option => 'value')
1553 The C<new()> function takes a list of options and values, and returns
1554 a new B<SQL::Abstract> object which can then be used to generate SQL
1555 through the methods below. The options accepted are:
1561 If set to 'lower', then SQL will be generated in all lowercase. By
1562 default SQL is generated in "textbook" case meaning something like:
1564 SELECT a_field FROM a_table WHERE some_field LIKE '%someval%'
1566 Any setting other than 'lower' is ignored.
1570 This determines what the default comparison operator is. By default
1571 it is C<=>, meaning that a hash like this:
1573 %where = (name => 'nwiger', email => 'nate@wiger.org');
1575 Will generate SQL like this:
1577 WHERE name = 'nwiger' AND email = 'nate@wiger.org'
1579 However, you may want loose comparisons by default, so if you set
1580 C<cmp> to C<like> you would get SQL such as:
1582 WHERE name like 'nwiger' AND email like 'nate@wiger.org'
1584 You can also override the comparison on an individual basis - see
1585 the huge section on L</"WHERE CLAUSES"> at the bottom.
1587 =item sqltrue, sqlfalse
1589 Expressions for inserting boolean values within SQL statements.
1590 By default these are C<1=1> and C<1=0>. They are used
1591 by the special operators C<-in> and C<-not_in> for generating
1592 correct SQL even when the argument is an empty array (see below).
1596 This determines the default logical operator for multiple WHERE
1597 statements in arrays or hashes. If absent, the default logic is "or"
1598 for arrays, and "and" for hashes. This means that a WHERE
1602 event_date => {'>=', '2/13/99'},
1603 event_date => {'<=', '4/24/03'},
1606 will generate SQL like this:
1608 WHERE event_date >= '2/13/99' OR event_date <= '4/24/03'
1610 This is probably not what you want given this query, though (look
1611 at the dates). To change the "OR" to an "AND", simply specify:
1613 my $sql = SQL::Abstract->new(logic => 'and');
1615 Which will change the above C<WHERE> to:
1617 WHERE event_date >= '2/13/99' AND event_date <= '4/24/03'
1619 The logic can also be changed locally by inserting
1620 a modifier in front of an arrayref:
1622 @where = (-and => [event_date => {'>=', '2/13/99'},
1623 event_date => {'<=', '4/24/03'} ]);
1625 See the L</"WHERE CLAUSES"> section for explanations.
1629 This will automatically convert comparisons using the specified SQL
1630 function for both column and value. This is mostly used with an argument
1631 of C<upper> or C<lower>, so that the SQL will have the effect of
1632 case-insensitive "searches". For example, this:
1634 $sql = SQL::Abstract->new(convert => 'upper');
1635 %where = (keywords => 'MaKe iT CAse inSeNSItive');
1637 Will turn out the following SQL:
1639 WHERE upper(keywords) like upper('MaKe iT CAse inSeNSItive')
1641 The conversion can be C<upper()>, C<lower()>, or any other SQL function
1642 that can be applied symmetrically to fields (actually B<SQL::Abstract> does
1643 not validate this option; it will just pass through what you specify verbatim).
1647 This is a kludge because many databases suck. For example, you can't
1648 just bind values using DBI's C<execute()> for Oracle C<CLOB> or C<BLOB> fields.
1649 Instead, you have to use C<bind_param()>:
1651 $sth->bind_param(1, 'reg data');
1652 $sth->bind_param(2, $lots, {ora_type => ORA_CLOB});
1654 The problem is, B<SQL::Abstract> will normally just return a C<@bind> array,
1655 which loses track of which field each slot refers to. Fear not.
1657 If you specify C<bindtype> in new, you can determine how C<@bind> is returned.
1658 Currently, you can specify either C<normal> (default) or C<columns>. If you
1659 specify C<columns>, you will get an array that looks like this:
1661 my $sql = SQL::Abstract->new(bindtype => 'columns');
1662 my($stmt, @bind) = $sql->insert(...);
1665 [ 'column1', 'value1' ],
1666 [ 'column2', 'value2' ],
1667 [ 'column3', 'value3' ],
1670 You can then iterate through this manually, using DBI's C<bind_param()>.
1672 $sth->prepare($stmt);
1675 my($col, $data) = @$_;
1676 if ($col eq 'details' || $col eq 'comments') {
1677 $sth->bind_param($i, $data, {ora_type => ORA_CLOB});
1678 } elsif ($col eq 'image') {
1679 $sth->bind_param($i, $data, {ora_type => ORA_BLOB});
1681 $sth->bind_param($i, $data);
1685 $sth->execute; # execute without @bind now
1687 Now, why would you still use B<SQL::Abstract> if you have to do this crap?
1688 Basically, the advantage is still that you don't have to care which fields
1689 are or are not included. You could wrap that above C<for> loop in a simple
1690 sub called C<bind_fields()> or something and reuse it repeatedly. You still
1691 get a layer of abstraction over manual SQL specification.
1693 Note that if you set L</bindtype> to C<columns>, the C<\[ $sql, @bind ]>
1694 construct (see L</Literal SQL with placeholders and bind values (subqueries)>)
1695 will expect the bind values in this format.
1699 This is the character that a table or column name will be quoted
1700 with. By default this is an empty string, but you could set it to
1701 the character C<`>, to generate SQL like this:
1703 SELECT `a_field` FROM `a_table` WHERE `some_field` LIKE '%someval%'
1705 Alternatively, you can supply an array ref of two items, the first being the left
1706 hand quote character, and the second the right hand quote character. For
1707 example, you could supply C<['[',']']> for SQL Server 2000 compliant quotes
1708 that generates SQL like this:
1710 SELECT [a_field] FROM [a_table] WHERE [some_field] LIKE '%someval%'
1712 Quoting is useful if you have tables or columns names that are reserved
1713 words in your database's SQL dialect.
1717 This is the character that will be used to escape L</quote_char>s appearing
1718 in an identifier before it has been quoted.
1720 The parameter default in case of a single L</quote_char> character is the quote
1723 When opening-closing-style quoting is used (L</quote_char> is an arrayref)
1724 this parameter defaults to the B<closing (right)> L</quote_char>. Occurrences
1725 of the B<opening (left)> L</quote_char> within the identifier are currently left
1726 untouched. The default for opening-closing-style quotes may change in future
1727 versions, thus you are B<strongly encouraged> to specify the escape character
1732 This is the character that separates a table and column name. It is
1733 necessary to specify this when the C<quote_char> option is selected,
1734 so that tables and column names can be individually quoted like this:
1736 SELECT `table`.`one_field` FROM `table` WHERE `table`.`other_field` = 1
1738 =item injection_guard
1740 A regular expression C<qr/.../> that is applied to any C<-function> and unquoted
1741 column name specified in a query structure. This is a safety mechanism to avoid
1742 injection attacks when mishandling user input e.g.:
1744 my %condition_as_column_value_pairs = get_values_from_user();
1745 $sqla->select( ... , \%condition_as_column_value_pairs );
1747 If the expression matches an exception is thrown. Note that literal SQL
1748 supplied via C<\'...'> or C<\['...']> is B<not> checked in any way.
1750 Defaults to checking for C<;> and the C<GO> keyword (TransactSQL)
1752 =item array_datatypes
1754 When this option is true, arrayrefs in INSERT or UPDATE are
1755 interpreted as array datatypes and are passed directly
1757 When this option is false, arrayrefs are interpreted
1758 as literal SQL, just like refs to arrayrefs
1759 (but this behavior is for backwards compatibility; when writing
1760 new queries, use the "reference to arrayref" syntax
1766 Takes a reference to a list of "special operators"
1767 to extend the syntax understood by L<SQL::Abstract>.
1768 See section L</"SPECIAL OPERATORS"> for details.
1772 Takes a reference to a list of "unary operators"
1773 to extend the syntax understood by L<SQL::Abstract>.
1774 See section L</"UNARY OPERATORS"> for details.
1780 =head2 insert($table, \@values || \%fieldvals, \%options)
1782 This is the simplest function. You simply give it a table name
1783 and either an arrayref of values or hashref of field/value pairs.
1784 It returns an SQL INSERT statement and a list of bind values.
1785 See the sections on L</"Inserting and Updating Arrays"> and
1786 L</"Inserting and Updating SQL"> for information on how to insert
1787 with those data types.
1789 The optional C<\%options> hash reference may contain additional
1790 options to generate the insert SQL. Currently supported options
1797 Takes either a scalar of raw SQL fields, or an array reference of
1798 field names, and adds on an SQL C<RETURNING> statement at the end.
1799 This allows you to return data generated by the insert statement
1800 (such as row IDs) without performing another C<SELECT> statement.
1801 Note, however, this is not part of the SQL standard and may not
1802 be supported by all database engines.
1806 =head2 update($table, \%fieldvals, \%where, \%options)
1808 This takes a table, hashref of field/value pairs, and an optional
1809 hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
1811 See the sections on L</"Inserting and Updating Arrays"> and
1812 L</"Inserting and Updating SQL"> for information on how to insert
1813 with those data types.
1815 The optional C<\%options> hash reference may contain additional
1816 options to generate the update SQL. Currently supported options
1823 See the C<returning> option to
1824 L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
1828 =head2 select($source, $fields, $where, $order)
1830 This returns a SQL SELECT statement and associated list of bind values, as
1831 specified by the arguments:
1837 Specification of the 'FROM' part of the statement.
1838 The argument can be either a plain scalar (interpreted as a table
1839 name, will be quoted), or an arrayref (interpreted as a list
1840 of table names, joined by commas, quoted), or a scalarref
1841 (literal SQL, not quoted).
1845 Specification of the list of fields to retrieve from
1847 The argument can be either an arrayref (interpreted as a list
1848 of field names, will be joined by commas and quoted), or a
1849 plain scalar (literal SQL, not quoted).
1850 Please observe that this API is not as flexible as that of
1851 the first argument C<$source>, for backwards compatibility reasons.
1855 Optional argument to specify the WHERE part of the query.
1856 The argument is most often a hashref, but can also be
1857 an arrayref or plain scalar --
1858 see section L<WHERE clause|/"WHERE CLAUSES"> for details.
1862 Optional argument to specify the ORDER BY part of the query.
1863 The argument can be a scalar, a hashref or an arrayref
1864 -- see section L<ORDER BY clause|/"ORDER BY CLAUSES">
1870 =head2 delete($table, \%where, \%options)
1872 This takes a table name and optional hashref L<WHERE clause|/WHERE CLAUSES>.
1873 It returns an SQL DELETE statement and list of bind values.
1875 The optional C<\%options> hash reference may contain additional
1876 options to generate the delete SQL. Currently supported options
1883 See the C<returning> option to
1884 L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
1888 =head2 where(\%where, $order)
1890 This is used to generate just the WHERE clause. For example,
1891 if you have an arbitrary data structure and know what the
1892 rest of your SQL is going to look like, but want an easy way
1893 to produce a WHERE clause, use this. It returns an SQL WHERE
1894 clause and list of bind values.
1897 =head2 values(\%data)
1899 This just returns the values from the hash C<%data>, in the same
1900 order that would be returned from any of the other above queries.
1901 Using this allows you to markedly speed up your queries if you
1902 are affecting lots of rows. See below under the L</"PERFORMANCE"> section.
1904 =head2 generate($any, 'number', $of, \@data, $struct, \%types)
1906 Warning: This is an experimental method and subject to change.
1908 This returns arbitrarily generated SQL. It's a really basic shortcut.
1909 It will return two different things, depending on return context:
1911 my($stmt, @bind) = $sql->generate('create table', \$table, \@fields);
1912 my $stmt_and_val = $sql->generate('create table', \$table, \@fields);
1914 These would return the following:
1916 # First calling form
1917 $stmt = "CREATE TABLE test (?, ?)";
1918 @bind = (field1, field2);
1920 # Second calling form
1921 $stmt_and_val = "CREATE TABLE test (field1, field2)";
1923 Depending on what you're trying to do, it's up to you to choose the correct
1924 format. In this example, the second form is what you would want.
1928 $sql->generate('alter session', { nls_date_format => 'MM/YY' });
1932 ALTER SESSION SET nls_date_format = 'MM/YY'
1934 You get the idea. Strings get their case twiddled, but everything
1935 else remains verbatim.
1937 =head1 EXPORTABLE FUNCTIONS
1939 =head2 is_plain_value
1941 Determines if the supplied argument is a plain value as understood by this
1946 =item * The value is C<undef>
1948 =item * The value is a non-reference
1950 =item * The value is an object with stringification overloading
1952 =item * The value is of the form C<< { -value => $anything } >>
1956 On failure returns C<undef>, on success returns a B<scalar> reference
1957 to the original supplied argument.
1963 The stringification overloading detection is rather advanced: it takes
1964 into consideration not only the presence of a C<""> overload, but if that
1965 fails also checks for enabled
1966 L<autogenerated versions of C<"">|overload/Magic Autogeneration>, based
1967 on either C<0+> or C<bool>.
1969 Unfortunately testing in the field indicates that this
1970 detection B<< may tickle a latent bug in perl versions before 5.018 >>,
1971 but only when very large numbers of stringifying objects are involved.
1972 At the time of writing ( Sep 2014 ) there is no clear explanation of
1973 the direct cause, nor is there a manageably small test case that reliably
1974 reproduces the problem.
1976 If you encounter any of the following exceptions in B<random places within
1977 your application stack> - this module may be to blame:
1979 Operation "ne": no method found,
1980 left argument in overloaded package <something>,
1981 right argument in overloaded package <something>
1985 Stub found while resolving method "???" overloading """" in package <something>
1987 If you fall victim to the above - please attempt to reduce the problem
1988 to something that could be sent to the L<SQL::Abstract developers
1989 |DBIx::Class/GETTING HELP/SUPPORT>
1990 (either publicly or privately). As a workaround in the meantime you can
1991 set C<$ENV{SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION}> to a true
1992 value, which will most likely eliminate your problem (at the expense of
1993 not being able to properly detect exotic forms of stringification).
1995 This notice and environment variable will be removed in a future version,
1996 as soon as the underlying problem is found and a reliable workaround is
2001 =head2 is_literal_value
2003 Determines if the supplied argument is a literal value as understood by this
2008 =item * C<\$sql_string>
2010 =item * C<\[ $sql_string, @bind_values ]>
2014 On failure returns C<undef>, on success returns an B<array> reference
2015 containing the unpacked version of the supplied literal SQL and bind values.
2017 =head1 WHERE CLAUSES
2021 This module uses a variation on the idea from L<DBIx::Abstract>. It
2022 is B<NOT>, repeat I<not> 100% compatible. B<The main logic of this
2023 module is that things in arrays are OR'ed, and things in hashes
2026 The easiest way to explain is to show lots of examples. After
2027 each C<%where> hash shown, it is assumed you used:
2029 my($stmt, @bind) = $sql->where(\%where);
2031 However, note that the C<%where> hash can be used directly in any
2032 of the other functions as well, as described above.
2034 =head2 Key-value pairs
2036 So, let's get started. To begin, a simple hash:
2040 status => 'completed'
2043 Is converted to SQL C<key = val> statements:
2045 $stmt = "WHERE user = ? AND status = ?";
2046 @bind = ('nwiger', 'completed');
2048 One common thing I end up doing is having a list of values that
2049 a field can be in. To do this, simply specify a list inside of
2054 status => ['assigned', 'in-progress', 'pending'];
2057 This simple code will create the following:
2059 $stmt = "WHERE user = ? AND ( status = ? OR status = ? OR status = ? )";
2060 @bind = ('nwiger', 'assigned', 'in-progress', 'pending');
2062 A field associated to an empty arrayref will be considered a
2063 logical false and will generate 0=1.
2065 =head2 Tests for NULL values
2067 If the value part is C<undef> then this is converted to SQL <IS NULL>
2076 $stmt = "WHERE user = ? AND status IS NULL";
2079 To test if a column IS NOT NULL:
2083 status => { '!=', undef },
2086 =head2 Specific comparison operators
2088 If you want to specify a different type of operator for your comparison,
2089 you can use a hashref for a given column:
2093 status => { '!=', 'completed' }
2096 Which would generate:
2098 $stmt = "WHERE user = ? AND status != ?";
2099 @bind = ('nwiger', 'completed');
2101 To test against multiple values, just enclose the values in an arrayref:
2103 status => { '=', ['assigned', 'in-progress', 'pending'] };
2105 Which would give you:
2107 "WHERE status = ? OR status = ? OR status = ?"
2110 The hashref can also contain multiple pairs, in which case it is expanded
2111 into an C<AND> of its elements:
2115 status => { '!=', 'completed', -not_like => 'pending%' }
2118 # Or more dynamically, like from a form
2119 $where{user} = 'nwiger';
2120 $where{status}{'!='} = 'completed';
2121 $where{status}{'-not_like'} = 'pending%';
2123 # Both generate this
2124 $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?";
2125 @bind = ('nwiger', 'completed', 'pending%');
2128 To get an OR instead, you can combine it with the arrayref idea:
2132 priority => [ { '=', 2 }, { '>', 5 } ]
2135 Which would generate:
2137 $stmt = "WHERE ( priority = ? OR priority > ? ) AND user = ?";
2138 @bind = ('2', '5', 'nwiger');
2140 If you want to include literal SQL (with or without bind values), just use a
2141 scalar reference or reference to an arrayref as the value:
2144 date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] },
2145 date_expires => { '<' => \"now()" }
2148 Which would generate:
2150 $stmt = "WHERE date_entered > to_date(?, 'MM/DD/YYYY') AND date_expires < now()";
2151 @bind = ('11/26/2008');
2154 =head2 Logic and nesting operators
2156 In the example above,
2157 there is a subtle trap if you want to say something like
2158 this (notice the C<AND>):
2160 WHERE priority != ? AND priority != ?
2162 Because, in Perl you I<can't> do this:
2164 priority => { '!=' => 2, '!=' => 1 }
2166 As the second C<!=> key will obliterate the first. The solution
2167 is to use the special C<-modifier> form inside an arrayref:
2169 priority => [ -and => {'!=', 2},
2173 Normally, these would be joined by C<OR>, but the modifier tells it
2174 to use C<AND> instead. (Hint: You can use this in conjunction with the
2175 C<logic> option to C<new()> in order to change the way your queries
2176 work by default.) B<Important:> Note that the C<-modifier> goes
2177 B<INSIDE> the arrayref, as an extra first element. This will
2178 B<NOT> do what you think it might:
2180 priority => -and => [{'!=', 2}, {'!=', 1}] # WRONG!
2182 Here is a quick list of equivalencies, since there is some overlap:
2185 status => {'!=', 'completed', 'not like', 'pending%' }
2186 status => [ -and => {'!=', 'completed'}, {'not like', 'pending%'}]
2189 status => {'=', ['assigned', 'in-progress']}
2190 status => [ -or => {'=', 'assigned'}, {'=', 'in-progress'}]
2191 status => [ {'=', 'assigned'}, {'=', 'in-progress'} ]
2195 =head2 Special operators: IN, BETWEEN, etc.
2197 You can also use the hashref format to compare a list of fields using the
2198 C<IN> comparison operator, by specifying the list as an arrayref:
2201 status => 'completed',
2202 reportid => { -in => [567, 2335, 2] }
2205 Which would generate:
2207 $stmt = "WHERE status = ? AND reportid IN (?,?,?)";
2208 @bind = ('completed', '567', '2335', '2');
2210 The reverse operator C<-not_in> generates SQL C<NOT IN> and is used in
2213 If the argument to C<-in> is an empty array, 'sqlfalse' is generated
2214 (by default: C<1=0>). Similarly, C<< -not_in => [] >> generates
2215 'sqltrue' (by default: C<1=1>).
2217 In addition to the array you can supply a chunk of literal sql or
2218 literal sql with bind:
2221 customer => { -in => \[
2222 'SELECT cust_id FROM cust WHERE balance > ?',
2225 status => { -in => \'SELECT status_codes FROM states' },
2231 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
2232 AND status IN ( SELECT status_codes FROM states )
2236 Finally, if the argument to C<-in> is not a reference, it will be
2237 treated as a single-element array.
2239 Another pair of operators is C<-between> and C<-not_between>,
2240 used with an arrayref of two values:
2244 completion_date => {
2245 -not_between => ['2002-10-01', '2003-02-06']
2251 WHERE user = ? AND completion_date NOT BETWEEN ( ? AND ? )
2253 Just like with C<-in> all plausible combinations of literal SQL
2257 start0 => { -between => [ 1, 2 ] },
2258 start1 => { -between => \["? AND ?", 1, 2] },
2259 start2 => { -between => \"lower(x) AND upper(y)" },
2260 start3 => { -between => [
2262 \["upper(?)", 'stuff' ],
2269 ( start0 BETWEEN ? AND ? )
2270 AND ( start1 BETWEEN ? AND ? )
2271 AND ( start2 BETWEEN lower(x) AND upper(y) )
2272 AND ( start3 BETWEEN lower(x) AND upper(?) )
2274 @bind = (1, 2, 1, 2, 'stuff');
2277 These are the two builtin "special operators"; but the
2278 list can be expanded: see section L</"SPECIAL OPERATORS"> below.
2280 =head2 Unary operators: bool
2282 If you wish to test against boolean columns or functions within your
2283 database you can use the C<-bool> and C<-not_bool> operators. For
2284 example to test the column C<is_user> being true and the column
2285 C<is_enabled> being false you would use:-
2289 -not_bool => 'is_enabled',
2294 WHERE is_user AND NOT is_enabled
2296 If a more complex combination is required, testing more conditions,
2297 then you should use the and/or operators:-
2302 -not_bool => { two=> { -rlike => 'bar' } },
2303 -not_bool => { three => [ { '=', 2 }, { '>', 5 } ] },
2314 (NOT ( three = ? OR three > ? ))
2317 =head2 Nested conditions, -and/-or prefixes
2319 So far, we've seen how multiple conditions are joined with a top-level
2320 C<AND>. We can change this by putting the different conditions we want in
2321 hashes and then putting those hashes in an array. For example:
2326 status => { -like => ['pending%', 'dispatched'] },
2330 status => 'unassigned',
2334 This data structure would create the following:
2336 $stmt = "WHERE ( user = ? AND ( status LIKE ? OR status LIKE ? ) )
2337 OR ( user = ? AND status = ? ) )";
2338 @bind = ('nwiger', 'pending', 'dispatched', 'robot', 'unassigned');
2341 Clauses in hashrefs or arrayrefs can be prefixed with an C<-and> or C<-or>
2342 to change the logic inside:
2348 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
2349 -or => { workhrs => {'<', 50}, geo => 'EURO' },
2356 $stmt = "WHERE ( user = ?
2357 AND ( ( workhrs > ? AND geo = ? )
2358 OR ( workhrs < ? OR geo = ? ) ) )";
2359 @bind = ('nwiger', '20', 'ASIA', '50', 'EURO');
2361 =head3 Algebraic inconsistency, for historical reasons
2363 C<Important note>: when connecting several conditions, the C<-and->|C<-or>
2364 operator goes C<outside> of the nested structure; whereas when connecting
2365 several constraints on one column, the C<-and> operator goes
2366 C<inside> the arrayref. Here is an example combining both features:
2369 -and => [a => 1, b => 2],
2370 -or => [c => 3, d => 4],
2371 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ]
2376 WHERE ( ( ( a = ? AND b = ? )
2377 OR ( c = ? OR d = ? )
2378 OR ( e LIKE ? AND e LIKE ? ) ) )
2380 This difference in syntax is unfortunate but must be preserved for
2381 historical reasons. So be careful: the two examples below would
2382 seem algebraically equivalent, but they are not
2385 { -like => 'foo%' },
2386 { -like => '%bar' },
2388 # yields: WHERE ( ( col LIKE ? AND col LIKE ? ) )
2391 { col => { -like => 'foo%' } },
2392 { col => { -like => '%bar' } },
2394 # yields: WHERE ( ( col LIKE ? OR col LIKE ? ) )
2397 =head2 Literal SQL and value type operators
2399 The basic premise of SQL::Abstract is that in WHERE specifications the "left
2400 side" is a column name and the "right side" is a value (normally rendered as
2401 a placeholder). This holds true for both hashrefs and arrayref pairs as you
2402 see in the L</WHERE CLAUSES> examples above. Sometimes it is necessary to
2403 alter this behavior. There are several ways of doing so.
2407 This is a virtual operator that signals the string to its right side is an
2408 identifier (a column name) and not a value. For example to compare two
2409 columns you would write:
2412 priority => { '<', 2 },
2413 requestor => { -ident => 'submitter' },
2418 $stmt = "WHERE priority < ? AND requestor = submitter";
2421 If you are maintaining legacy code you may see a different construct as
2422 described in L</Deprecated usage of Literal SQL>, please use C<-ident> in new
2427 This is a virtual operator that signals that the construct to its right side
2428 is a value to be passed to DBI. This is for example necessary when you want
2429 to write a where clause against an array (for RDBMS that support such
2430 datatypes). For example:
2433 array => { -value => [1, 2, 3] }
2438 $stmt = 'WHERE array = ?';
2439 @bind = ([1, 2, 3]);
2441 Note that if you were to simply say:
2447 the result would probably not be what you wanted:
2449 $stmt = 'WHERE array = ? OR array = ? OR array = ?';
2454 Finally, sometimes only literal SQL will do. To include a random snippet
2455 of SQL verbatim, you specify it as a scalar reference. Consider this only
2456 as a last resort. Usually there is a better way. For example:
2459 priority => { '<', 2 },
2460 requestor => { -in => \'(SELECT name FROM hitmen)' },
2465 $stmt = "WHERE priority < ? AND requestor IN (SELECT name FROM hitmen)"
2468 Note that in this example, you only get one bind parameter back, since
2469 the verbatim SQL is passed as part of the statement.
2473 Never use untrusted input as a literal SQL argument - this is a massive
2474 security risk (there is no way to check literal snippets for SQL
2475 injections and other nastyness). If you need to deal with untrusted input
2476 use literal SQL with placeholders as described next.
2478 =head3 Literal SQL with placeholders and bind values (subqueries)
2480 If the literal SQL to be inserted has placeholders and bind values,
2481 use a reference to an arrayref (yes this is a double reference --
2482 not so common, but perfectly legal Perl). For example, to find a date
2483 in Postgres you can use something like this:
2486 date_column => \[ "= date '2008-09-30' - ?::integer", 10 ]
2491 $stmt = "WHERE ( date_column = date '2008-09-30' - ?::integer )"
2494 Note that you must pass the bind values in the same format as they are returned
2495 by L<where|/where(\%where, $order)>. This means that if you set L</bindtype>
2496 to C<columns>, you must provide the bind values in the
2497 C<< [ column_meta => value ] >> format, where C<column_meta> is an opaque
2498 scalar value; most commonly the column name, but you can use any scalar value
2499 (including references and blessed references), L<SQL::Abstract> will simply
2500 pass it through intact. So if C<bindtype> is set to C<columns> the above
2501 example will look like:
2504 date_column => \[ "= date '2008-09-30' - ?::integer", [ {} => 10 ] ]
2507 Literal SQL is especially useful for nesting parenthesized clauses in the
2508 main SQL query. Here is a first example:
2510 my ($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?",
2514 bar => \["IN ($sub_stmt)" => @sub_bind],
2519 $stmt = "WHERE (foo = ? AND bar IN (SELECT c1 FROM t1
2520 WHERE c2 < ? AND c3 LIKE ?))";
2521 @bind = (1234, 100, "foo%");
2523 Other subquery operators, like for example C<"E<gt> ALL"> or C<"NOT IN">,
2524 are expressed in the same way. Of course the C<$sub_stmt> and
2525 its associated bind values can be generated through a former call
2528 my ($sub_stmt, @sub_bind)
2529 = $sql->select("t1", "c1", {c2 => {"<" => 100},
2530 c3 => {-like => "foo%"}});
2533 bar => \["> ALL ($sub_stmt)" => @sub_bind],
2536 In the examples above, the subquery was used as an operator on a column;
2537 but the same principle also applies for a clause within the main C<%where>
2538 hash, like an EXISTS subquery:
2540 my ($sub_stmt, @sub_bind)
2541 = $sql->select("t1", "*", {c1 => 1, c2 => \"> t0.c0"});
2542 my %where = ( -and => [
2544 \["EXISTS ($sub_stmt)" => @sub_bind],
2549 $stmt = "WHERE (foo = ? AND EXISTS (SELECT * FROM t1
2550 WHERE c1 = ? AND c2 > t0.c0))";
2554 Observe that the condition on C<c2> in the subquery refers to
2555 column C<t0.c0> of the main query: this is I<not> a bind
2556 value, so we have to express it through a scalar ref.
2557 Writing C<< c2 => {">" => "t0.c0"} >> would have generated
2558 C<< c2 > ? >> with bind value C<"t0.c0"> ... not exactly
2559 what we wanted here.
2561 Finally, here is an example where a subquery is used
2562 for expressing unary negation:
2564 my ($sub_stmt, @sub_bind)
2565 = $sql->where({age => [{"<" => 10}, {">" => 20}]});
2566 $sub_stmt =~ s/^ where //i; # don't want "WHERE" in the subclause
2568 lname => {like => '%son%'},
2569 \["NOT ($sub_stmt)" => @sub_bind],
2574 $stmt = "lname LIKE ? AND NOT ( age < ? OR age > ? )"
2575 @bind = ('%son%', 10, 20)
2577 =head3 Deprecated usage of Literal SQL
2579 Below are some examples of archaic use of literal SQL. It is shown only as
2580 reference for those who deal with legacy code. Each example has a much
2581 better, cleaner and safer alternative that users should opt for in new code.
2587 my %where = ( requestor => \'IS NOT NULL' )
2589 $stmt = "WHERE requestor IS NOT NULL"
2591 This used to be the way of generating NULL comparisons, before the handling
2592 of C<undef> got formalized. For new code please use the superior syntax as
2593 described in L</Tests for NULL values>.
2597 my %where = ( requestor => \'= submitter' )
2599 $stmt = "WHERE requestor = submitter"
2601 This used to be the only way to compare columns. Use the superior L</-ident>
2602 method for all new code. For example an identifier declared in such a way
2603 will be properly quoted if L</quote_char> is properly set, while the legacy
2604 form will remain as supplied.
2608 my %where = ( is_ready => \"", completed => { '>', '2012-12-21' } )
2610 $stmt = "WHERE completed > ? AND is_ready"
2611 @bind = ('2012-12-21')
2613 Using an empty string literal used to be the only way to express a boolean.
2614 For all new code please use the much more readable
2615 L<-bool|/Unary operators: bool> operator.
2621 These pages could go on for a while, since the nesting of the data
2622 structures this module can handle are pretty much unlimited (the
2623 module implements the C<WHERE> expansion as a recursive function
2624 internally). Your best bet is to "play around" with the module a
2625 little to see how the data structures behave, and choose the best
2626 format for your data based on that.
2628 And of course, all the values above will probably be replaced with
2629 variables gotten from forms or the command line. After all, if you
2630 knew everything ahead of time, you wouldn't have to worry about
2631 dynamically-generating SQL and could just hardwire it into your
2634 =head1 ORDER BY CLAUSES
2636 Some functions take an order by clause. This can either be a scalar (just a
2637 column name), a hashref of C<< { -desc => 'col' } >> or C<< { -asc => 'col' }
2638 >>, a scalarref, an arrayref-ref, or an arrayref of any of the previous
2641 Given | Will Generate
2642 ---------------------------------------------------------------
2644 'colA' | ORDER BY colA
2646 [qw/colA colB/] | ORDER BY colA, colB
2648 {-asc => 'colA'} | ORDER BY colA ASC
2650 {-desc => 'colB'} | ORDER BY colB DESC
2652 ['colA', {-asc => 'colB'}] | ORDER BY colA, colB ASC
2654 { -asc => [qw/colA colB/] } | ORDER BY colA ASC, colB ASC
2656 \'colA DESC' | ORDER BY colA DESC
2658 \[ 'FUNC(colA, ?)', $x ] | ORDER BY FUNC(colA, ?)
2659 | /* ...with $x bound to ? */
2662 { -asc => 'colA' }, | colA ASC,
2663 { -desc => [qw/colB/] }, | colB DESC,
2664 { -asc => [qw/colC colD/] },| colC ASC, colD ASC,
2665 \'colE DESC', | colE DESC,
2666 \[ 'FUNC(colF, ?)', $x ], | FUNC(colF, ?)
2667 ] | /* ...with $x bound to ? */
2668 ===============================================================
2672 =head1 SPECIAL OPERATORS
2674 my $sqlmaker = SQL::Abstract->new(special_ops => [
2678 my ($self, $field, $op, $arg) = @_;
2684 handler => 'method_name',
2688 A "special operator" is a SQL syntactic clause that can be
2689 applied to a field, instead of a usual binary operator.
2692 WHERE field IN (?, ?, ?)
2693 WHERE field BETWEEN ? AND ?
2694 WHERE MATCH(field) AGAINST (?, ?)
2696 Special operators IN and BETWEEN are fairly standard and therefore
2697 are builtin within C<SQL::Abstract> (as the overridable methods
2698 C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
2699 like the MATCH .. AGAINST example above which is specific to MySQL,
2700 you can write your own operator handlers - supply a C<special_ops>
2701 argument to the C<new> method. That argument takes an arrayref of
2702 operator definitions; each operator definition is a hashref with two
2709 the regular expression to match the operator
2713 Either a coderef or a plain scalar method name. In both cases
2714 the expected return is C<< ($sql, @bind) >>.
2716 When supplied with a method name, it is simply called on the
2717 L<SQL::Abstract> object as:
2719 $self->$method_name($field, $op, $arg)
2723 $field is the LHS of the operator
2724 $op is the part that matched the handler regex
2727 When supplied with a coderef, it is called as:
2729 $coderef->($self, $field, $op, $arg)
2734 For example, here is an implementation
2735 of the MATCH .. AGAINST syntax for MySQL
2737 my $sqlmaker = SQL::Abstract->new(special_ops => [
2739 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
2740 {regex => qr/^match$/i,
2742 my ($self, $field, $op, $arg) = @_;
2743 $arg = [$arg] if not ref $arg;
2744 my $label = $self->_quote($field);
2745 my ($placeholder) = $self->_convert('?');
2746 my $placeholders = join ", ", (($placeholder) x @$arg);
2747 my $sql = $self->_sqlcase('match') . " ($label) "
2748 . $self->_sqlcase('against') . " ($placeholders) ";
2749 my @bind = $self->_bindtype($field, @$arg);
2750 return ($sql, @bind);
2757 =head1 UNARY OPERATORS
2759 my $sqlmaker = SQL::Abstract->new(unary_ops => [
2763 my ($self, $op, $arg) = @_;
2769 handler => 'method_name',
2773 A "unary operator" is a SQL syntactic clause that can be
2774 applied to a field - the operator goes before the field
2776 You can write your own operator handlers - supply a C<unary_ops>
2777 argument to the C<new> method. That argument takes an arrayref of
2778 operator definitions; each operator definition is a hashref with two
2785 the regular expression to match the operator
2789 Either a coderef or a plain scalar method name. In both cases
2790 the expected return is C<< $sql >>.
2792 When supplied with a method name, it is simply called on the
2793 L<SQL::Abstract> object as:
2795 $self->$method_name($op, $arg)
2799 $op is the part that matched the handler regex
2800 $arg is the RHS or argument of the operator
2802 When supplied with a coderef, it is called as:
2804 $coderef->($self, $op, $arg)
2812 Thanks to some benchmarking by Mark Stosberg, it turns out that
2813 this module is many orders of magnitude faster than using C<DBIx::Abstract>.
2814 I must admit this wasn't an intentional design issue, but it's a
2815 byproduct of the fact that you get to control your C<DBI> handles
2818 To maximize performance, use a code snippet like the following:
2820 # prepare a statement handle using the first row
2821 # and then reuse it for the rest of the rows
2823 for my $href (@array_of_hashrefs) {
2824 $stmt ||= $sql->insert('table', $href);
2825 $sth ||= $dbh->prepare($stmt);
2826 $sth->execute($sql->values($href));
2829 The reason this works is because the keys in your C<$href> are sorted
2830 internally by B<SQL::Abstract>. Thus, as long as your data retains
2831 the same structure, you only have to generate the SQL the first time
2832 around. On subsequent queries, simply use the C<values> function provided
2833 by this module to return your values in the correct order.
2835 However this depends on the values having the same type - if, for
2836 example, the values of a where clause may either have values
2837 (resulting in sql of the form C<column = ?> with a single bind
2838 value), or alternatively the values might be C<undef> (resulting in
2839 sql of the form C<column IS NULL> with no bind value) then the
2840 caching technique suggested will not work.
2844 If you use my C<CGI::FormBuilder> module at all, you'll hopefully
2845 really like this part (I do, at least). Building up a complex query
2846 can be as simple as the following:
2853 use CGI::FormBuilder;
2856 my $form = CGI::FormBuilder->new(...);
2857 my $sql = SQL::Abstract->new;
2859 if ($form->submitted) {
2860 my $field = $form->field;
2861 my $id = delete $field->{id};
2862 my($stmt, @bind) = $sql->update('table', $field, {id => $id});
2865 Of course, you would still have to connect using C<DBI> to run the
2866 query, but the point is that if you make your form look like your
2867 table, the actual query script can be extremely simplistic.
2869 If you're B<REALLY> lazy (I am), check out C<HTML::QuickTable> for
2870 a fast interface to returning and formatting data. I frequently
2871 use these three modules together to write complex database query
2872 apps in under 50 lines.
2874 =head1 HOW TO CONTRIBUTE
2876 Contributions are always welcome, in all usable forms (we especially
2877 welcome documentation improvements). The delivery methods include git-
2878 or unified-diff formatted patches, GitHub pull requests, or plain bug
2879 reports either via RT or the Mailing list. Contributors are generally
2880 granted full access to the official repository after their first several
2881 patches pass successful review.
2883 This project is maintained in a git repository. The code and related tools are
2884 accessible at the following locations:
2888 =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Abstract.git>
2890 =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Abstract.git>
2892 =item * GitHub mirror: L<https://github.com/dbsrgits/sql-abstract>
2894 =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/SQL-Abstract.git>
2900 Version 1.50 was a major internal refactoring of C<SQL::Abstract>.
2901 Great care has been taken to preserve the I<published> behavior
2902 documented in previous versions in the 1.* family; however,
2903 some features that were previously undocumented, or behaved
2904 differently from the documentation, had to be changed in order
2905 to clarify the semantics. Hence, client code that was relying
2906 on some dark areas of C<SQL::Abstract> v1.*
2907 B<might behave differently> in v1.50.
2909 The main changes are:
2915 support for literal SQL through the C<< \ [ $sql, @bind ] >> syntax.
2919 support for the { operator => \"..." } construct (to embed literal SQL)
2923 support for the { operator => \["...", @bind] } construct (to embed literal SQL with bind values)
2927 optional support for L<array datatypes|/"Inserting and Updating Arrays">
2931 defensive programming: check arguments
2935 fixed bug with global logic, which was previously implemented
2936 through global variables yielding side-effects. Prior versions would
2937 interpret C<< [ {cond1, cond2}, [cond3, cond4] ] >>
2938 as C<< "(cond1 AND cond2) OR (cond3 AND cond4)" >>.
2939 Now this is interpreted
2940 as C<< "(cond1 AND cond2) OR (cond3 OR cond4)" >>.
2945 fixed semantics of _bindtype on array args
2949 dropped the C<_anoncopy> of the %where tree. No longer necessary,
2950 we just avoid shifting arrays within that tree.
2954 dropped the C<_modlogic> function
2958 =head1 ACKNOWLEDGEMENTS
2960 There are a number of individuals that have really helped out with
2961 this module. Unfortunately, most of them submitted bugs via CPAN
2962 so I have no idea who they are! But the people I do know are:
2964 Ash Berlin (order_by hash term support)
2965 Matt Trout (DBIx::Class support)
2966 Mark Stosberg (benchmarking)
2967 Chas Owens (initial "IN" operator support)
2968 Philip Collins (per-field SQL functions)
2969 Eric Kolve (hashref "AND" support)
2970 Mike Fragassi (enhancements to "BETWEEN" and "LIKE")
2971 Dan Kubb (support for "quote_char" and "name_sep")
2972 Guillermo Roditi (patch to cleanup "IN" and "BETWEEN", fix and tests for _order_by)
2973 Laurent Dami (internal refactoring, extensible list of special operators, literal SQL)
2974 Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
2975 Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
2976 Oliver Charles (support for "RETURNING" after "INSERT")
2982 L<DBIx::Class>, L<DBIx::Abstract>, L<CGI::FormBuilder>, L<HTML::QuickTable>.
2986 Copyright (c) 2001-2007 Nathan Wiger <nwiger@cpan.org>. All Rights Reserved.
2988 This module is actively maintained by Matt Trout <mst@shadowcatsystems.co.uk>
2990 For support, your best bet is to try the C<DBIx::Class> users mailing list.
2991 While not an official support venue, C<DBIx::Class> makes heavy use of
2992 C<SQL::Abstract>, and as such list members there are very familiar with
2993 how to create queries.
2997 This module is free software; you may copy this under the same
2998 terms as perl itself (either the GNU General Public License or
2999 the Artistic License)