-or => '_expand_andor',
};
- $opt{render_op} = our $RENDER_OP;
+ $opt{expand_op} = {
+ 'between' => '_expand_between',
+ 'not between' => '_expand_between',
+ 'in' => '_expand_in',
+ 'not in' => '_expand_in',
+ };
$opt{render} = {
(map +("-$_", "_render_$_"), qw(op func bind ident literal list)),
%{$opt{render}||{}}
};
+ $opt{render_op} = our $RENDER_OP;
+
return bless \%opt, $class;
}
}
if (ref($v) eq 'HASH') {
if (keys %$v > 1) {
- return { -op => [
- 'and',
- map $self->_expand_expr({ $k => { $_ => $v->{$_} } }),
+ return $self->_expand_andor(-and => [
+ map +{ $k => { $_ => $v->{$_} } },
sort keys %$v
- ] };
+ ]);
}
return undef unless keys %$v;
my ($vk, $vv) = %$v;
belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
. "You probably wanted ...-and => [ -$op => COND1, -$op => COND2 ... ]";
}
- if ($op =~ /^(?:not )?between$/) {
- local our $Cur_Col_Meta = $k;
- 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($op)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref";
- }
- return +{ -op => [
- $op,
- $self->_expand_ident(-ident => $k),
- @rhs
- ] }
- }
- if ($op =~ /^(?:not )?in$/) {
- if (my $literal = is_literal_value($vv)) {
- my ($sql, @bind) = @$literal;
- my $opened_sql = $self->_open_outer_paren($sql);
- return +{ -op => [
- $op, $self->_expand_ident(-ident => $k),
- [ { -literal => [ $opened_sql, @bind ] } ]
- ] };
- }
- my $undef_err =
- 'SQL::Abstract before v1.75 used to generate incorrect SQL when the '
- . "-${\uc($op)} 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($op)}' 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 $self->${\($op =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs;
-
- return +{ -op => [
- $op,
- $self->_expand_ident(-ident => $k),
- \@rhs
- ] };
+ if (my $x = $self->{expand_op}{$op}) {
+ return $self->$x($op, $vv, $k);
}
if ($op eq 'ident') {
- if (! defined $vv or (ref($vv) and ref($vv) eq 'ARRAY')) {
- puke "-$op requires a single plain scalar argument (a quotable identifier) or an arrayref of identifier parts";
- }
return +{ -op => [
$self->{cmp},
$self->_expand_ident(-ident => $k),
- $self->_expand_ident(-ident => $vv),
+ $self->_expand_expr({ -ident => $vv }),
] };
}
if ($op eq 'value') {
}
sub _expand_ident {
- my ($self, undef, $body) = @_;
+ my ($self, $op, $body) = @_;
+ unless (defined($body) or (ref($body) and ref($body) eq 'ARRAY')) {
+ puke "$op requires a single plain scalar argument (a quotable identifier) or an arrayref of identifier parts";
+ }
my @parts = map split(/\Q${\($self->{name_sep}||'.')}\E/, $_),
ref($body) ? @$body : $body;
return { -ident => $parts[-1] } if $self->{_dequalify_idents};
die "notreached";
}
+sub _expand_between {
+ my ($self, $op, $vv, $k) = @_;
+ local our $Cur_Col_Meta = $k;
+ 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($op)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref";
+ }
+ return +{ -op => [
+ $op,
+ $self->_expand_ident(-ident => $k),
+ @rhs
+ ] }
+}
+
+sub _expand_in {
+ my ($self, $op, $vv, $k) = @_;
+ if (my $literal = is_literal_value($vv)) {
+ my ($sql, @bind) = @$literal;
+ my $opened_sql = $self->_open_outer_paren($sql);
+ return +{ -op => [
+ $op, $self->_expand_ident(-ident => $k),
+ [ { -literal => [ $opened_sql, @bind ] } ]
+ ] };
+ }
+ my $undef_err =
+ 'SQL::Abstract before v1.75 used to generate incorrect SQL when the '
+ . "-${\uc($op)} 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($op)}' 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 $self->${\($op =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs;
+
+ return +{ -op => [
+ $op,
+ $self->_expand_ident(-ident => $k),
+ \@rhs
+ ] };
+}
+
sub _recurse_where {
my ($self, $where, $logic) = @_;