X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FAbstract.pm;h=01bf68b8a8e495ab308d0b6d330f201d0897bd0e;hb=801b679d8122f0f30184a7236f08034db30cfe97;hp=de447fafa3ca389956e40348732932ddfe952de8;hpb=495fb7ce707f33784bf1f3057ae420359527e1a5;p=dbsrgits%2FSQL-Abstract.git diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index de447fa..01bf68b 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -37,11 +37,11 @@ our $AUTOLOAD; # special operators (-in, -between). May be extended/overridden by user. # See section WHERE: BUILTIN SPECIAL OPERATORS below for implementation my @BUILTIN_SPECIAL_OPS = ( - {regex => qr/^ (?: not \s )? between $/ix, handler => '_where_field_BETWEEN'}, - {regex => qr/^ (?: not \s )? in $/ix, handler => '_where_field_IN'}, - {regex => qr/^ ident $/ix, handler => '_where_op_IDENT'}, - {regex => qr/^ value $/ix, handler => '_where_op_VALUE'}, - {regex => qr/^ is (?: \s+ not )? $/ix, handler => '_where_field_IS'}, + {regex => qr/^ (?: not \s )? between $/ix, handler => sub { die "NOPE" }}, + {regex => qr/^ (?: not \s )? in $/ix, handler => sub { die "NOPE" }}, + {regex => qr/^ ident $/ix, handler => sub { die "NOPE" }}, + {regex => qr/^ value $/ix, handler => sub { die "NOPE" }}, + {regex => qr/^ is (?: \s+ not )? $/ix, handler => sub { die "NOPE" }}, ); # unaryish operators - key maps to handler @@ -55,6 +55,7 @@ my @BUILTIN_UNARY_OPS = ( { regex => qr/^ value $/xi, handler => '_where_op_VALUE' }, { regex => qr/^ op $/xi, handler => '_where_op_OP' }, { regex => qr/^ bind $/xi, handler => '_where_op_BIND' }, + { regex => qr/^ literal $/xi, handler => '_where_op_LITERAL' }, ); #====================================================================== @@ -539,6 +540,7 @@ sub where { sub _expand_expr { my ($self, $expr, $logic) = @_; + return undef unless defined($expr); if (ref($expr) eq 'HASH') { if (keys %$expr > 1) { $logic ||= 'and'; @@ -575,15 +577,22 @@ sub _expand_expr { } return { '-'.$logic => \@res }; } - return $expr; + if (my $literal = is_literal_value($expr)) { + return +{ -literal => $literal }; + } + if (!ref($expr)) { + return +{ -value => $expr }; + } + #::Ddie([ HUH => $expr ]); + die "notreached"; } sub _expand_expr_hashpair { my ($self, $k, $v, $logic) = @_; unless (defined($k) and length($k)) { - if (defined($k) and is_literal_value($v)) { + if (defined($k) and my $literal = is_literal_value($v)) { belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead'; - return $v; + return { -literal => $literal }; } puke "Supplying an empty left hand side argument is not supported"; } @@ -601,6 +610,11 @@ sub _expand_expr_hashpair { if (my ($rest) = $k =~/^-not[_ ](.*)$/) { return $self->_expand_expr({ -not => { "-${rest}", $v } }, $logic); } + if (my ($logic) = $k =~ /^-(and|or)$/) { + if (ref($v) eq 'HASH') { + return $self->_expand_expr($v, $logic); + } + } } else { unless (defined($v)) { my $orig_op = my $op = $self->{cmp}; @@ -622,6 +636,96 @@ sub _expand_expr_hashpair { ] }; } + if (ref($v) eq 'HASH') { + if (keys %$v > 1) { + return { -and => [ + map $self->_expand_expr_hashpair($k => { $_ => $v->{$_} }), + sort keys %$v + ] }; + } + my ($vk, $vv) = %$v; + $vk =~ s/^-//; + $vk = lc($vk); + if ($vk =~ /^(?:not[ _])?between$/) { + my @rhs = map $self->_expand_expr($_), + ref($vv) eq 'ARRAY' ? @$vv : $vv; + unless ( + (@rhs == 1 and ref($rhs[0]) eq 'HASH' and $rhs[0]->{-literal}) + or + (@rhs == 2 and defined($rhs[0]) and defined($rhs[1])) + ) { + puke "Operator '${\uc($vk)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref"; + } + return +{ -op => [ + join(' ', split '_', $vk), + { -ident => $k }, + map { + my $v = ref($_) ? $_->{-value} :$_; + ($v ? { -bind => [ $k, $v ] } : $_) + } @rhs + ] } + } + if ($vk =~ /^(?:not[ _])?in$/) { + if (my $literal = is_literal_value($vv)) { + my ($sql, @bind) = @$literal; + my $opened_sql = $self->_open_outer_paren($sql); + return +{ -op => [ + $vk, { -ident => $k }, + [ { -literal => [ $opened_sql, @bind ] } ] + ] }; + } + my $undef_err = + 'SQL::Abstract before v1.75 used to generate incorrect SQL when the ' + . "-${\uc($vk)} operator was given an undef-containing list: !!!AUDIT YOUR CODE " + . 'AND DATA!!! (the upcoming Data::Query-based version of SQL::Abstract ' + . 'will emit the logically correct SQL instead of raising this exception)' + ; + puke("Argument passed to the '${\uc($vk)}' operator can not be undefined") + if !defined($vv); + my @rhs = map $self->_expand_expr($_), + map { ref($_) ? $_ : { -bind => [ $k, $_ ] } } + map { defined($_) ? $_: puke($undef_err) } + (ref($vv) eq 'ARRAY' ? @$vv : $vv); + return +{ + -literal => [ $self->{$vk =~ /^not/ ? 'sqltrue' : 'sqlfalse'} ] + } unless @rhs; + + return +{ -op => [ + join(' ', split '_', $vk), + { -ident => $k }, + \@rhs + ] }; + } + if ($vk eq 'ident') { + if (! defined $vv or ref $vv) { + puke "-$vk requires a single plain scalar argument (a quotable identifier)"; + } + return +{ -op => [ + $self->{cmp}, + { -ident => $k }, + { -ident => $vv } + ] }; + } + if ($vk eq 'value') { + return $self->_expand_expr_hashpair($k, undef) unless defined($vv); + return +{ -op => [ + $self->{cmp}, + { -ident => $k }, + { -bind => [ $k, $vv ] } + ] }; + } + if ($vk =~ /^is(?:[ _]not)?$/) { + puke "$vk can only take undef as argument" + if defined($vv) + and not ( + ref($vv) eq 'HASH' + and exists($vv->{-value}) + and !defined($vv->{-value}) + ); + $vk =~ s/_/ /g; + return +{ -op => [ $vk.' null', { -ident => $k } ] }; + } + } if (ref($v) eq 'ARRAY') { return $self->{sqlfalse} unless @$v; $self->_debug("ARRAY($k) means distribute over elements"); @@ -638,7 +742,14 @@ sub _expand_expr_hashpair { return \$literal; } my ($sql, @bind) = @$literal; - return \[ $self->_quote($k).' '.$sql, @bind ]; + if ($self->{bindtype} eq 'columns') { + for (@bind) { + if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) { + puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]" + } + } + } + return +{ -literal => [ $self->_quote($k).' '.$sql, @bind ] }; } } return { $k => $v }; @@ -791,6 +902,8 @@ sub _where_HASHREF { sub _where_unary_op { my ($self, $op, $rhs) = @_; + $op =~ s/^-// if length($op) > 1; + # top level special ops are illegal in general puke "Illegal use of top-level '-$op'" if !(defined $self->{_nested_func_lhs}) @@ -982,11 +1095,64 @@ sub _where_op_VALUE { my %unop_postfix = map +($_ => 1), 'is null', 'is not null'; +my %special = ( + (map +($_ => do { + my $op = $_; + sub { + my ($self, $args) = @_; + my ($left, $low, $high) = @$args; + my ($rhsql, @rhbind) = do { + if (@$args == 2) { + puke "Single arg to between must be a literal" + unless $low->{-literal}; + @{$low->{-literal}} + } else { + local $self->{_nested_func_lhs} = $left->{-ident} + if ref($left) eq 'HASH' and $left->{-ident}; + my ($l, $h) = map [ $self->_where_unary_op(%$_) ], $low, $high; + (join(' ', $l->[0], $self->_sqlcase('and'), $h->[0]), + @{$l}[1..$#$l], @{$h}[1..$#$h]) + } + }; + my ($lhsql, @lhbind) = $self->_recurse_where($left); + return ( + join(' ', '(', $lhsql, $self->_sqlcase($op), $rhsql, ')'), + @lhbind, @rhbind + ); + } + }), 'between', 'not between'), + (map +($_ => do { + my $op = $_; + sub { + my ($self, $args) = @_; + my ($lhs, $rhs) = @$args; + my @in_bind; + my @in_sql = map { + local $self->{_nested_func_lhs} = $lhs->{-ident} + if ref($lhs) eq 'HASH' and $lhs->{-ident}; + my ($sql, @bind) = $self->_where_unary_op(%$_); + push @in_bind, @bind; + $sql; + } @$rhs; + my ($lhsql, @lbind) = $self->_recurse_where($lhs); + return ( + $lhsql.' '.$self->_sqlcase($op).' ( ' + .join(', ', @in_sql) + .' )', + @lbind, @in_bind + ); + } + }), 'in', 'not in'), +); + sub _where_op_OP { my ($self, undef, $v) = @_; my ($op, @args) = @$v; $op =~ s/^-// if length($op) > 1; local $self->{_nested_func_lhs}; + if (my $h = $special{$op}) { + return $self->$h(\@args); + } if (@args == 1) { my ($expr_sql, @bind) = $self->_recurse_where($args[0]); my $final_op = join ' ', split '_', $op; @@ -1009,6 +1175,12 @@ sub _where_op_BIND { return ($self->_convert('?'), $self->_bindtype(@$bind)); } +sub _where_op_LITERAL { + my ($self, undef, $literal) = @_; + $self->_assert_bindval_matches_bindtype(@{$literal}[1..$#$literal]); + return @$literal; +} + sub _where_hashpair_ARRAYREF { my ($self, $k, $v) = @_;