$opt{sqlfalse} ||= '0=1';
# special operators
- $opt{user_special_ops} = [ @{$opt{special_ops} ||= []} ];
+ $opt{special_ops} ||= [];
+
# regexes are applied in order, thus push after user-defines
push @{$opt{special_ops}}, @BUILTIN_SPECIAL_OPS;
qw(op func value bind ident literal)
};
+ $opt{expand_unary} = {};
+
return bless \%opt, $class;
}
$self->_expand_maybe_list_expr( [
map {
my ($k, $set) = @$_;
+ $set = { -bind => $_ } unless defined $set;
+{ -op => [ '=', { -ident => $k }, $set ] };
}
map {
sub _select_fields {
my ($self, $fields) = @_;
+ return $fields unless ref($fields);
return $self->_render_expr(
$self->_expand_maybe_list_expr($fields, undef, '-ident')
);
# top level special ops are illegal in general
puke "Illegal use of top-level '-$op'"
if List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
+ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
+ return { -op => [ $op, $v ] };
+ }
}
if ($k eq '-value' and my $m = our $Cur_Col_Meta) {
return +{ -bind => [ $m, $v ] };
}
+ if (my $custom = $self->{expand_unary}{$k}) {
+ return $self->$custom($v);
+ }
if ($self->{node_types}{$k}) {
return { $k => $v };
}
- if (my $custom = $self->{custom_expansions}{($k =~ /^-(.*)$/)[0]}) {
- return $self->$custom($v);
- }
if (
ref($v) eq 'HASH'
and keys %$v == 1
return $self->_expand_expr_hashpair($k => { $self->{cmp} => undef });
}
if (!ref($v) or Scalar::Util::blessed($v)) {
+ my $d = our $Default_Scalar_To;
return +{
-op => [
$self->{cmp},
{ -ident => $k },
- { -bind => [ $k, $v ] }
+ ($d ? { $d => $v } : { -bind => [ $k, $v ] })
]
};
}
] };
}
}
- if (my $us = List::Util::first { $vk =~ $_->{regex} } @{$self->{user_special_ops}}) {
+ if (my $us = List::Util::first { $vk =~ $_->{regex} } @{$self->{special_ops}}) {
return { -op => [ $vk, { -ident => $k }, $vv ] };
}
+ if (my $us = List::Util::first { $vk =~ $_->{regex} } @{$self->{unary_ops}}) {
+ return { -op => [
+ $self->{cmp},
+ { -ident => $k },
+ { -op => [ $vk, $vv ] }
+ ] };
+ }
if (ref($vv) eq 'ARRAY') {
my ($logic, @values) = (
(defined($vv->[0]) and $vv->[0] =~ /^-(and|or)$/i)
if (my $h = $special{$op}) {
return $self->$h(\@args);
}
- if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{user_special_ops}}) {
+ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}) {
puke "Special op '${op}' requires first value to be identifier"
unless my ($k) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0];
return $self->${\($us->{handler})}($k, $op, $args[1]);
}
+ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
+ return $self->${\($us->{handler})}($op, $args[0]);
+ }
my $final_op = $op =~ /^(?:is|not)_/ ? join(' ', split '_', $op) : $op;
if (@args == 1 and $op !~ /^(and|or)$/) {
my ($expr_sql, @bind) = $self->_render_expr($args[0]);
my $expander = sub {
my ($self, $dir, $expr) = @_;
+ my @to_expand = ref($expr) eq 'ARRAY' ? @$expr : $expr;
+ foreach my $arg (@to_expand) {
+ if (
+ ref($arg) eq 'HASH'
+ and keys %$arg > 1
+ and grep /^-(asc|desc)$/, keys %$arg
+ ) {
+ puke "ordering direction hash passed to order by must have exactly one key (-asc or -desc)";
+ }
+ }
my @exp = map +(defined($dir) ? { -op => [ $dir => $_ ] } : $_),
- map $self->_expand_expr($_, undef, -ident),
- ref($expr) eq 'ARRAY' ? @$expr : $expr;
+ map $self->_expand_expr($_, undef, -ident), @to_expand;
return (@exp > 1 ? { -op => [ ',', @exp ] } : $exp[0]);
};
- local $self->{custom_expansions} = {
- asc => sub { shift->$expander(asc => @_) },
- desc => sub { shift->$expander(desc => @_) },
- };
+ local @{$self->{expand_unary}}{qw(-asc -desc)} = (
+ sub { shift->$expander(asc => @_) },
+ sub { shift->$expander(desc => @_) },
+ );
my $expanded = $self->$expander(undef, $arg);