# See section WHERE: BUILTIN SPECIAL OPERATORS below for implementation
my @BUILTIN_SPECIAL_OPS = (
{regex => qr/^ (?: not \s )? between $/ix, handler => sub { die "NOPE" }},
- {regex => qr/^ (?: not \s )? in $/ix, handler => sub { die "NOPE" }},
{regex => qr/^ is (?: \s+ not )? $/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" }},
);
#======================================================================
: undef;
}
+sub is_undef_value ($) {
+ !defined($_[0])
+ or (
+ ref($_[0]) eq 'HASH'
+ and exists $_[0]->{-value}
+ and not defined $_[0]->{-value}
+ );
+}
+
# FIXME XSify - this can be done so much more efficiently
sub is_plain_value ($) {
no strict 'refs';
$opt{equality_op} = qr/^( \Q$opt{cmp}\E | \= )$/ix;
$opt{inequality_op} = qr/^( != | <> )$/ix;
- $opt{like_op} = qr/^ (is\s+)? r?like $/xi;
- $opt{not_like_op} = qr/^ (is\s+)? not \s+ r?like $/xi;
+ $opt{like_op} = qr/^ (is_)?r?like $/xi;
+ $opt{not_like_op} = qr/^ (is_)?not_r?like $/xi;
# SQL booleans
$opt{sqltrue} ||= '1=1';
# special operators
$opt{special_ops} ||= [];
- # regexes are applied in order, thus push after user-defines
- push @{$opt{special_ops}}, @BUILTIN_SPECIAL_OPS;
-
if ($class->isa('DBIx::Class::SQLMaker')) {
- push @{$opt{special_ops}}, our $DBIC_Compat_Op ||= {
- regex => qr/^(?:ident|value)$/i, handler => sub { die "NOPE" }
- };
- $opt{is_dbic_sqlmaker} = 1;
+ $opt{warn_once_on_nest} = 1;
+ $opt{disable_old_special_ops} = 1;
}
# unary operators
^ \s* go \s
/xmi;
+ $opt{expand_unary} = {};
+
+ $opt{expand} = {
+ not => '_expand_not',
+ bool => '_expand_bool',
+ and => '_expand_op_andor',
+ or => '_expand_op_andor',
+ nest => '_expand_nest',
+ bind => '_expand_bind',
+ in => '_expand_in',
+ not_in => '_expand_in',
+ row => '_expand_row',
+ between => '_expand_between',
+ not_between => '_expand_between',
+ op => '_expand_op',
+ (map +($_ => '_expand_op_is'), ('is', 'is_not')),
+ ident => '_expand_ident',
+ value => '_expand_value',
+ func => '_expand_func',
+ };
+
+ $opt{expand_op} = {
+ 'between' => '_expand_between',
+ 'not_between' => '_expand_between',
+ 'in' => '_expand_in',
+ 'not_in' => '_expand_in',
+ 'nest' => '_expand_nest',
+ (map +($_ => '_expand_op_andor'), ('and', 'or')),
+ (map +($_ => '_expand_op_is'), ('is', 'is_not')),
+ 'ident' => '_expand_ident',
+ 'value' => '_expand_value',
+ };
+
$opt{render} = {
- (map +("-$_", "_render_$_"), qw(op func bind ident literal list)),
+ (map +($_, "_render_$_"), qw(op func bind ident literal row)),
%{$opt{render}||{}}
};
- $opt{expand_unary} = {};
+ $opt{render_op} = {
+ (map +($_ => '_render_op_between'), 'between', 'not_between'),
+ (map +($_ => '_render_op_in'), 'in', 'not_in'),
+ (map +($_ => '_render_unop_postfix'),
+ 'is_null', 'is_not_null', 'asc', 'desc',
+ ),
+ (not => '_render_unop_paren'),
+ (map +($_ => '_render_op_andor'), qw(and or)),
+ ',' => '_render_op_multop',
+ };
return bless \%opt, $class;
}
my $data = shift || return;
my $options = shift;
- my $method = $self->_METHOD_FOR_refkind("_insert", $data);
- my ($sql, @bind) = $self->$method($data);
- $sql = join " ", $self->_sqlcase('insert into'), $table, $sql;
+ my $fields;
+
+ my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data);
+
+ my @parts = ([ $self->_sqlcase('insert into').' '.$table ]);
+ push @parts, [ $self->render_aqt($f_aqt) ] if $f_aqt;
+ push @parts, [ $self->render_aqt($v_aqt) ];
+ $parts[-1][0] =~ s/^/VALUES /;
if ($options->{returning}) {
- my ($s, @b) = $self->_insert_returning($options);
- $sql .= $s;
- push @bind, @b;
+ push @parts, [ $self->_insert_returning($options) ];
}
- return wantarray ? ($sql, @bind) : $sql;
+ return $self->_join_parts(' ', @parts);
+}
+
+sub _expand_insert_values {
+ my ($self, $data) = @_;
+ if (is_literal_value($data)) {
+ (undef, $self->expand_expr($data));
+ } else {
+ my ($fields, $values) = (
+ ref($data) eq 'HASH' ?
+ ([ sort keys %$data ], [ @{$data}{sort keys %$data} ])
+ : ([], $data)
+ );
+
+ # no names (arrayref) means can't generate bindtype
+ !($fields) && $self->{bindtype} eq 'columns'
+ && belch "can't do 'columns' bindtype when called with arrayref";
+
+ +(
+ (@$fields
+ ? $self->expand_expr({ -row => $fields }, -ident)
+ : undef
+ ),
+ +{ -row => [
+ map {
+ local our $Cur_Col_Meta = $fields->[$_];
+ $self->_expand_insert_value($values->[$_])
+ } 0..$#$values
+ ] },
+ );
+ }
}
# So that subclasses can override INSERT ... RETURNING separately from
my $f = $options->{returning};
my ($sql, @bind) = $self->render_aqt(
- $self->_expand_maybe_list_expr($f, undef, -ident)
+ $self->_expand_maybe_list_expr($f, -ident)
);
return wantarray
? $self->_sqlcase(' returning ') . $sql
: ($self->_sqlcase(' returning ').$sql, @bind);
}
-sub _insert_HASHREF { # explicit list of fields and then values
- my ($self, $data) = @_;
-
- my @fields = sort keys %$data;
-
- my ($sql, @bind) = $self->_insert_values($data);
-
- # assemble SQL
- $_ = $self->_quote($_) foreach @fields;
- $sql = "( ".join(", ", @fields).") ".$sql;
-
- return ($sql, @bind);
-}
-
-sub _insert_ARRAYREF { # just generate values(?,?) part (no list of fields)
- my ($self, $data) = @_;
-
- # no names (arrayref) so can't generate bindtype
- $self->{bindtype} ne 'columns'
- or belch "can't do 'columns' bindtype when called with arrayref";
-
- my (@values, @all_bind);
- foreach my $value (@$data) {
- my ($values, @bind) = $self->_insert_value(undef, $value);
- push @values, $values;
- push @all_bind, @bind;
- }
- my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
- return ($sql, @all_bind);
-}
-
-sub _insert_ARRAYREFREF { # literal SQL with bind
- my ($self, $data) = @_;
-
- my ($sql, @bind) = @${$data};
- $self->_assert_bindval_matches_bindtype(@bind);
-
- return ($sql, @bind);
-}
-
-
-sub _insert_SCALARREF { # literal SQL without bind
- my ($self, $data) = @_;
-
- return ($$data);
-}
-
-sub _insert_values {
- my ($self, $data) = @_;
-
- my (@values, @all_bind);
- foreach my $column (sort keys %$data) {
- my ($values, @bind) = $self->_insert_value($column, $data->{$column});
- push @values, $values;
- push @all_bind, @bind;
- }
- my $sql = $self->_sqlcase('values')." ( ".join(", ", @values)." )";
- return ($sql, @all_bind);
-}
-
-sub _insert_value {
- my ($self, $column, $v) = @_;
-
- return $self->render_aqt(
- $self->_expand_insert_value($column, $v)
- );
-}
-
sub _expand_insert_value {
- my ($self, $column, $v) = @_;
+ my ($self, $v) = @_;
+
+ my $k = our $Cur_Col_Meta;
if (ref($v) eq 'ARRAY') {
if ($self->{array_datatypes}) {
- return +{ -bind => [ $column, $v ] };
+ return +{ -bind => [ $k, $v ] };
}
my ($sql, @bind) = @$v;
$self->_assert_bindval_matches_bindtype(@bind);
if (ref($v) eq 'HASH') {
if (grep !/^-/, keys %$v) {
belch "HASH ref as bind value in insert is not supported";
- return +{ -bind => [ $column, $v ] };
+ return +{ -bind => [ $k, $v ] };
}
}
if (!defined($v)) {
- return +{ -bind => [ $column, undef ] };
+ return +{ -bind => [ $k, undef ] };
}
- local our $Cur_Col_Meta = $column;
return $self->expand_expr($v);
}
my ($self, $fields) = @_;
return $fields unless ref($fields);
return $self->render_aqt(
- $self->_expand_maybe_list_expr($fields, undef, '-ident')
+ $self->_expand_maybe_list_expr($fields, '-ident')
);
}
return wantarray ? ($sql, @bind) : $sql;
}
+{ our $Default_Scalar_To = -value }
+
sub expand_expr {
my ($self, $expr, $default_scalar_to) = @_;
local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to;
- $self->_expand_expr($expr, undef);
+ $self->_expand_expr($expr);
}
sub render_aqt {
my ($self, $aqt) = @_;
my ($k, $v, @rest) = %$aqt;
die "No" if @rest;
+ die "Not a node type: $k" unless $k =~ s/^-//;
if (my $meth = $self->{render}{$k}) {
return $self->$meth($v);
}
}
sub render_expr {
- my ($self, $expr) = @_;
- $self->render_aqt($self->expand_expr($expr));
+ my ($self, $expr, $default_scalar_to) = @_;
+ my ($sql, @bind) = $self->render_aqt(
+ $self->expand_expr($expr, $default_scalar_to)
+ );
+ return (wantarray ? ($sql, @bind) : $sql);
+}
+
+sub _normalize_op {
+ my ($self, $raw) = @_;
+ s/^-(?=.)//, s/\s+/_/g for my $op = lc $raw;
+ $op;
}
sub _expand_expr {
- my ($self, $expr, $logic, $default_scalar_to) = @_;
- local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to;
+ my ($self, $expr) = @_;
our $Expand_Depth ||= 0; local $Expand_Depth = $Expand_Depth + 1;
return undef unless defined($expr);
if (ref($expr) eq 'HASH') {
- if (keys %$expr > 1) {
- $logic ||= 'and';
- return +{ -op => [
- $logic,
- map $self->_expand_expr_hashpair($_ => $expr->{$_}, $logic),
- sort keys %$expr
- ] };
+ return undef unless my $kc = keys %$expr;
+ if ($kc > 1) {
+ return $self->_expand_op_andor(and => $expr);
+ }
+ my ($key, $value) = %$expr;
+ if ($key =~ /^-/ and $key =~ s/ [_\s]? \d+ $//x ) {
+ belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
+ . "You probably wanted ...-and => [ $key => COND1, $key => COND2 ... ]";
}
- return undef unless keys %$expr;
- return $self->_expand_expr_hashpair(%$expr, $logic);
+ return $self->_expand_hashpair($key, $value);
}
if (ref($expr) eq 'ARRAY') {
- my $logic = lc($logic || $self->{logic});
- $logic eq 'and' or $logic eq 'or' or puke "unknown logic: $logic";
-
- my @expr = grep {
- (ref($_) eq 'ARRAY' and @$_)
- or (ref($_) eq 'HASH' and %$_)
- or 1
- } @$expr;
-
- my @res;
-
- while (my ($el) = splice @expr, 0, 1) {
- puke "Supplying an empty left hand side argument is not supported in array-pairs"
- unless defined($el) and length($el);
- my $elref = ref($el);
- if (!$elref) {
- local $Expand_Depth = 0;
- push(@res, grep defined, $self->_expand_expr({ $el, shift(@expr) }));
- } elsif ($elref eq 'ARRAY') {
- push(@res, grep defined, $self->_expand_expr($el)) if @$el;
- } elsif (my $l = is_literal_value($el)) {
- push @res, { -literal => $l };
- } elsif ($elref eq 'HASH') {
- local $Expand_Depth = 0;
- push @res, grep defined, $self->_expand_expr($el) if %$el;
- } else {
- die "notreached";
- }
- }
- return { -op => [ $logic, @res ] };
+ return $self->_expand_op_andor(lc($self->{logic}), $expr);
}
if (my $literal = is_literal_value($expr)) {
return +{ -literal => $literal };
}
if (!ref($expr) or Scalar::Util::blessed($expr)) {
- if (my $d = $Default_Scalar_To) {
- return $self->_expand_expr({ $d => $expr });
- }
- if (my $m = our $Cur_Col_Meta) {
- return +{ -bind => [ $m, $expr ] };
- }
- return +{ -bind => [ undef, $expr ] };
+ return $self->_expand_scalar($expr);
}
die "notreached";
}
-sub _expand_expr_hashpair {
- my ($self, $k, $v, $logic) = @_;
+sub _expand_hashpair {
+ my ($self, $k, $v) = @_;
unless (defined($k) and length($k)) {
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';
puke "Supplying an empty left hand side argument is not supported";
}
if ($k =~ /^-/) {
- $self->_assert_pass_injection_guard($k =~ /^-(.*)$/s);
- if ($k =~ s/ [_\s]? \d+ $//x ) {
- belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
- . "You probably wanted ...-and => [ $k => COND1, $k => COND2 ... ]";
- }
- if ($k eq '-nest') {
- # DBIx::Class requires a nest warning to be emitted once but the private
- # method it overrode to do so no longer exists
- if ($self->{is_dbic_sqlmaker}) {
- unless (our $Nest_Warned) {
- belch(
- "-nest in search conditions is deprecated, you most probably wanted:\n"
- .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }|
- );
- $Nest_Warned = 1;
- }
- }
- return $self->_expand_expr($v);
- }
- if ($k eq '-bool') {
- if (ref($v)) {
- return $self->_expand_expr($v);
- }
- puke "-bool => undef not supported" unless defined($v);
- return $self->_expand_ident(-ident => $v);
- }
- if ($k eq '-not') {
- return { -op => [ 'not', $self->_expand_expr($v) ] };
- }
- if (my ($rest) = $k =~/^-not[_ ](.*)$/) {
- return +{ -op => [
- 'not',
- $self->_expand_expr_hashpair("-${rest}", $v, $logic)
- ] };
- }
- if (my ($logic) = $k =~ /^-(and|or)$/i) {
- if (ref($v) eq 'HASH') {
- return $self->_expand_expr($v, $logic);
- }
- if (ref($v) eq 'ARRAY') {
- return $self->_expand_expr($v, $logic);
- }
- }
- {
- my $op = $k;
- $op =~ s/^-// if length($op) > 1;
-
- # top level special ops are illegal in general
- # note that, arguably, if it makes no sense at top level, it also
- # makes no sense on the other side of an = sign or similar but DBIC
- # gets disappointingly upset if I disallow it
- if (
- (our $Expand_Depth) == 1
- and List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}
- ) {
- puke "Illegal use of top-level '-$op'"
- }
- if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
- return { -op => [ $op, $v ] };
- }
- }
- if ($k eq '-value') {
- return +{ -bind => [ our $Cur_Col_Meta, $v ] };
- }
- if (my $custom = $self->{expand_unary}{$k}) {
- return $self->$custom($v);
- }
- if ($self->{render}{$k}) {
- return { $k => $v };
- }
- if (
- ref($v) eq 'HASH'
- and keys %$v == 1
- and (keys %$v)[0] =~ /^-/
- ) {
- my ($func) = $k =~ /^-(.*)$/;
- if (List::Util::first { $func =~ $_->{regex} } @{$self->{special_ops}}) {
- return +{ -op => [ $func, $self->_expand_expr($v) ] };
- }
- return +{ -func => [ $func, $self->_expand_expr($v) ] };
- }
- if (!ref($v) or is_literal_value($v)) {
- return +{ -op => [ $k =~ /^-(.*)$/, $self->_expand_expr($v) ] };
- }
+ return $self->_expand_hashpair_op($k, $v);
+ } elsif ($k =~ /^[^\w]/i) {
+ my ($lhs, @rhs) = @$v;
+ return $self->_expand_op(
+ -op, [ $k, $self->expand_expr($lhs, -ident), @rhs ]
+ );
}
- if (
- !defined($v)
- or (
- ref($v) eq 'HASH'
- and exists $v->{-value}
- and not defined $v->{-value}
- )
- ) {
- return $self->_expand_expr_hashpair($k => { $self->{cmp} => undef });
+ return $self->_expand_hashpair_ident($k, $v);
+}
+
+sub _expand_hashpair_ident {
+ my ($self, $k, $v) = @_;
+
+ local our $Cur_Col_Meta = $k;
+
+ # hash with multiple or no elements is andor
+
+ if (ref($v) eq 'HASH' and keys %$v != 1) {
+ return $self->_expand_op_andor(and => $v, $k);
+ }
+
+ # undef needs to be re-sent with cmp to achieve IS/IS NOT NULL
+
+ if (is_undef_value($v)) {
+ return $self->_expand_hashpair_cmp($k => undef);
}
+
+ # scalars and objects get expanded as whatever requested or values
+
if (!ref($v) or Scalar::Util::blessed($v)) {
- my $d = our $Default_Scalar_To;
- return +{
- -op => [
- $self->{cmp},
- $self->_expand_ident(-ident => $k),
- ($d ? { $d => $v } : { -bind => [ $k, $v ] })
- ]
- };
+ return $self->_expand_hashpair_scalar($k, $v);
}
+
+ # single key hashref is a hashtriple
+
if (ref($v) eq 'HASH') {
- if (keys %$v > 1) {
- return { -op => [
- 'and',
- map $self->_expand_expr_hashpair($k => { $_ => $v->{$_} }),
- sort keys %$v
- ] };
- }
- return undef unless keys %$v;
- my ($vk, $vv) = %$v;
- $vk =~ s/^-//;
- $vk = lc($vk);
- $self->_assert_pass_injection_guard($vk);
- if ($vk =~ s/ [_\s]? \d+ $//x ) {
- belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
- . "You probably wanted ...-and => [ -$vk => COND1, -$vk => COND2 ... ]";
- }
- if ($vk =~ /^(?: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($vk)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref";
- }
- return +{ -op => [
- join(' ', split '_', $vk),
- $self->_expand_ident(-ident => $k),
- @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, $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($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 $self->${\($vk =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs;
-
- return +{ -op => [
- join(' ', split '_', $vk),
- $self->_expand_ident(-ident => $k),
- \@rhs
- ] };
- }
- if ($vk eq 'ident') {
- if (! defined $vv or (ref($vv) and ref($vv) eq 'ARRAY')) {
- puke "-$vk requires a single plain scalar argument (a quotable identifier) or an arrayref of identifier parts";
- }
- 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 ($vk =~ /^(and|or)$/) {
- if (ref($vv) eq 'HASH') {
- return +{ -op => [
- $vk,
- map $self->_expand_expr_hashpair($k, { $_ => $vv->{$_} }),
- sort keys %$vv
- ] };
- }
- }
- 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)
- ? @$vv
- : (-or => @$vv)
- );
- if (
- $vk =~ $self->{inequality_op}
- or join(' ', split '_', $vk) =~ $self->{not_like_op}
- ) {
- if (lc($logic) eq '-or' and @values > 1) {
- my $op = uc join ' ', split '_', $vk;
- belch "A multi-element arrayref as an argument to the inequality op '$op' "
- . 'is technically equivalent to an always-true 1=1 (you probably wanted '
- . "to say ...{ \$inequality_op => [ -and => \@values ] }... instead)"
- ;
- }
- }
- unless (@values) {
- # try to DWIM on equality operators
- my $op = join ' ', split '_', $vk;
- return
- $op =~ $self->{equality_op} ? $self->sqlfalse
- : $op =~ $self->{like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqlfalse
- : $op =~ $self->{inequality_op} ? $self->sqltrue
- : $op =~ $self->{not_like_op} ? belch("Supplying an empty arrayref to '@{[ uc $op]}' is deprecated") && $self->sqltrue
- : puke "operator '$op' applied on an empty array (field '$k')";
- }
- return +{ -op => [
- $logic =~ /^-(.*)$/,
- map $self->_expand_expr_hashpair($k => { $vk => $_ }),
- @values
- ] };
- }
- if (
- !defined($vv)
- or (
- ref($vv) eq 'HASH'
- and exists $vv->{-value}
- and not defined $vv->{-value}
- )
- ) {
- my $op = join ' ', split '_', $vk;
- my $is =
- $op =~ /^not$/i ? 'is not' # legacy
- : $op =~ $self->{equality_op} ? 'is'
- : $op =~ $self->{like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is'
- : $op =~ $self->{inequality_op} ? 'is not'
- : $op =~ $self->{not_like_op} ? belch("Supplying an undefined argument to '@{[ uc $op]}' is deprecated") && 'is not'
- : puke "unexpected operator '$op' with undef operand";
- return +{ -op => [ $is.' null', { -ident => $k } ] };
- }
- local our $Cur_Col_Meta = $k;
- return +{ -op => [
- $vk,
- { -ident => $k },
- $self->_expand_expr($vv)
- ] };
+ return $self->_expand_hashtriple($k, %$v);
}
+
+ # arrayref needs re-engineering over the elements
+
if (ref($v) eq 'ARRAY') {
return $self->sqlfalse unless @$v;
$self->_debug("ARRAY($k) means distribute over elements");
- my $this_logic = (
- $v->[0] =~ /^-((?:and|or))$/i
- ? ($v = [ @{$v}[1..$#$v] ], $1)
- : ($self->{logic} || 'or')
+ my $logic = lc(
+ $v->[0] =~ /^-(and|or)$/i
+ ? (shift(@{$v = [ @$v ]}), $1)
+ : lc($self->{logic} || 'OR')
+ );
+ return $self->_expand_op_andor(
+ $logic => $v, $k
);
- return +{ -op => [
- $this_logic,
- map $self->_expand_expr({ $k => $_ }, $this_logic), @$v
- ] };
}
+
if (my $literal = is_literal_value($v)) {
unless (length $k) {
belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
die "notreached";
}
+sub _expand_scalar {
+ my ($self, $expr) = @_;
+
+ return $self->_expand_expr({ (our $Default_Scalar_To) => $expr });
+}
+
+sub _expand_hashpair_scalar {
+ my ($self, $k, $v) = @_;
+
+ return $self->_expand_hashpair_cmp(
+ $k, $self->_expand_scalar($v),
+ );
+}
+
+sub _expand_hashpair_op {
+ my ($self, $k, $v) = @_;
+
+ $self->_assert_pass_injection_guard($k =~ /\A-(.*)\Z/s);
+
+ my $op = $self->_normalize_op($k);
+
+ if (my $exp = $self->{expand}{$op}) {
+ return $self->$exp($op, $v);
+ }
+
+ # Ops prefixed with -not_ get converted
+
+ if (my ($rest) = $op =~/^not_(.*)$/) {
+ return +{ -op => [
+ 'not',
+ $self->_expand_expr({ "-${rest}", $v })
+ ] };
+ }
+
+ { # Old SQLA compat
+
+ my $op = join(' ', split '_', $op);
+
+ # the old special op system requires illegality for top-level use
+
+ if (
+ (our $Expand_Depth) == 1
+ and (
+ List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}
+ or (
+ $self->{disable_old_special_ops}
+ and List::Util::first { $op =~ $_->{regex} } @BUILTIN_SPECIAL_OPS
+ )
+ )
+ ) {
+ puke "Illegal use of top-level '-$op'"
+ }
+
+ # the old unary op system means we should touch nothing and let it work
+
+ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
+ return { -op => [ $op, $v ] };
+ }
+ }
+
+ # an explicit node type is currently assumed to be expanded (this is almost
+ # certainly wrong and there should be expansion anyway)
+
+ if ($self->{render}{$op}) {
+ return { $k => $v };
+ }
+
+ # hashref RHS values get expanded and used as op/func args
+
+ if (
+ ref($v) eq 'HASH'
+ and keys %$v == 1
+ and (keys %$v)[0] =~ /^-/
+ ) {
+ my ($func) = $k =~ /^-(.*)$/;
+ { # Old SQLA compat
+ if (List::Util::first { $func =~ $_->{regex} } @{$self->{special_ops}}) {
+ return +{ -op => [ $func, $self->_expand_expr($v) ] };
+ }
+ }
+ return +{ -func => [
+ $func,
+ map $self->_expand_expr($_),
+ ref($v) eq 'ARRAY' ? @$v : $v
+ ] };
+ }
+
+ # scalars and literals get simply expanded
+
+ if (!ref($v) or is_literal_value($v)) {
+ return +{ -op => [ $op, $self->_expand_expr($v) ] };
+ }
+
+ die "notreached";
+}
+
+sub _expand_hashpair_cmp {
+ my ($self, $k, $v) = @_;
+ $self->_expand_hashtriple($k, $self->{cmp}, $v);
+}
+
+sub _expand_hashtriple {
+ my ($self, $k, $vk, $vv) = @_;
+
+ my $ik = $self->_expand_ident(-ident => $k);
+
+ my $op = $self->_normalize_op($vk);
+ $self->_assert_pass_injection_guard($op);
+
+ if ($op =~ s/ _? \d+ $//x ) {
+ return $self->_expand_expr($k, { $vk, $vv });
+ }
+ if (my $x = $self->{expand_op}{$op}) {
+ local our $Cur_Col_Meta = $k;
+ return $self->$x($op, $vv, $k);
+ }
+ { # Old SQLA compat
+
+ my $op = join(' ', split '_', $op);
+
+ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}) {
+ return { -op => [ $op, $ik, $vv ] };
+ }
+ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
+ return { -op => [
+ $self->{cmp},
+ $ik,
+ { -op => [ $op, $vv ] }
+ ] };
+ }
+ }
+ if (ref($vv) eq 'ARRAY') {
+ my @raw = @$vv;
+ my $logic = (defined($raw[0]) and $raw[0] =~ /^-(and|or)$/i)
+ ? (shift(@raw), $1) : 'or';
+ my @values = map +{ $vk => $_ }, @raw;
+ if (
+ $op =~ $self->{inequality_op}
+ or $op =~ $self->{not_like_op}
+ ) {
+ if (lc($logic) eq 'or' and @values > 1) {
+ belch "A multi-element arrayref as an argument to the inequality op '${\uc(join ' ', split '_', $op)}' "
+ . 'is technically equivalent to an always-true 1=1 (you probably wanted '
+ . "to say ...{ \$inequality_op => [ -and => \@values ] }... instead)"
+ ;
+ }
+ }
+ unless (@values) {
+ # try to DWIM on equality operators
+ return ($self->_dwim_op_to_is($op,
+ "Supplying an empty arrayref to '%s' is deprecated",
+ "operator '%s' applied on an empty array (field '$k')"
+ ) ? $self->sqlfalse : $self->sqltrue);
+ }
+ return $self->_expand_op_andor($logic => \@values, $k);
+ }
+ if (is_undef_value($vv)) {
+ my $is = ($self->_dwim_op_to_is($op,
+ "Supplying an undefined argument to '%s' is deprecated",
+ "unexpected operator '%s' with undef operand",
+ ) ? 'is' : 'is not');
+
+ return $self->_expand_hashpair($k => { $is, undef });
+ }
+ local our $Cur_Col_Meta = $k;
+ return +{ -op => [
+ $op,
+ $ik,
+ $self->_expand_expr($vv)
+ ] };
+}
+
+sub _dwim_op_to_is {
+ my ($self, $raw, $empty, $fail) = @_;
+
+ my $op = $self->_normalize_op($raw);
+
+ if ($op =~ /^not$/i) {
+ return 0;
+ }
+ if ($op =~ $self->{equality_op}) {
+ return 1;
+ }
+ if ($op =~ $self->{like_op}) {
+ belch(sprintf $empty, uc(join ' ', split '_', $op));
+ return 1;
+ }
+ if ($op =~ $self->{inequality_op}) {
+ return 0;
+ }
+ if ($op =~ $self->{not_like_op}) {
+ belch(sprintf $empty, uc(join ' ', split '_', $op));
+ return 0;
+ }
+ puke(sprintf $fail, $op);
+}
+
+sub _expand_func {
+ my ($self, undef, $args) = @_;
+ my ($func, @args) = @$args;
+ return { -func => [ $func, map $self->expand_expr($_), @args ] };
+}
+
sub _expand_ident {
- my ($self, undef, $body) = @_;
+ my ($self, undef, $body, $k) = @_;
+ return $self->_expand_hashpair_cmp(
+ $k, { -ident => $body }
+ ) if defined($k);
+ unless (defined($body) or (ref($body) and ref($body) eq 'ARRAY')) {
+ puke "-ident 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};
return +{ -ident => \@parts };
}
+sub _expand_value {
+ return $_[0]->_expand_hashpair_cmp(
+ $_[3], { -value => $_[2] },
+ ) if defined($_[3]);
+ +{ -bind => [ our $Cur_Col_Meta, $_[2] ] };
+}
+
+sub _expand_not {
+ +{ -op => [ 'not', $_[0]->_expand_expr($_[2]) ] };
+}
+
+sub _expand_row {
+ my ($self, undef, $args) = @_;
+ +{ -row => [ map $self->expand_expr($_), @$args ] };
+}
+
+sub _expand_op {
+ my ($self, undef, $args) = @_;
+ my ($op, @opargs) = @$args;
+ if (my $exp = $self->{expand_op}{$op}) {
+ return $self->$exp($op, \@opargs);
+ }
+ +{ -op => [ $op, map $self->expand_expr($_), @opargs ] };
+}
+
+sub _expand_bool {
+ my ($self, undef, $v) = @_;
+ if (ref($v)) {
+ return $self->_expand_expr($v);
+ }
+ puke "-bool => undef not supported" unless defined($v);
+ return $self->_expand_ident(-ident => $v);
+}
+
+sub _expand_op_andor {
+ my ($self, $logop, $v, $k) = @_;
+ if (defined $k) {
+ $v = [ map +{ $k, $_ },
+ (ref($v) eq 'HASH')
+ ? (map +{ $_ => $v->{$_} }, sort keys %$v)
+ : @$v,
+ ];
+ }
+ if (ref($v) eq 'HASH') {
+ return undef unless keys %$v;
+ return +{ -op => [
+ $logop,
+ map $self->_expand_expr({ $_ => $v->{$_} }),
+ sort keys %$v
+ ] };
+ }
+ if (ref($v) eq 'ARRAY') {
+ $logop eq 'and' or $logop eq 'or' or puke "unknown logic: $logop";
+
+ my @expr = grep {
+ (ref($_) eq 'ARRAY' and @$_)
+ or (ref($_) eq 'HASH' and %$_)
+ or 1
+ } @$v;
+
+ my @res;
+
+ while (my ($el) = splice @expr, 0, 1) {
+ puke "Supplying an empty left hand side argument is not supported in array-pairs"
+ unless defined($el) and length($el);
+ my $elref = ref($el);
+ if (!$elref) {
+ local our $Expand_Depth = 0;
+ push(@res, grep defined, $self->_expand_expr({ $el, shift(@expr) }));
+ } elsif ($elref eq 'ARRAY') {
+ push(@res, grep defined, $self->_expand_expr($el)) if @$el;
+ } elsif (my $l = is_literal_value($el)) {
+ push @res, { -literal => $l };
+ } elsif ($elref eq 'HASH') {
+ local our $Expand_Depth = 0;
+ push @res, grep defined, $self->_expand_expr($el) if %$el;
+ } else {
+ die "notreached";
+ }
+ }
+ # ???
+ # return $res[0] if @res == 1;
+ return { -op => [ $logop, @res ] };
+ }
+ die "notreached";
+}
+
+sub _expand_op_is {
+ my ($self, $op, $vv, $k) = @_;
+ ($k, $vv) = @$vv unless defined $k;
+ puke "$op can only take undef as argument"
+ if defined($vv)
+ and not (
+ ref($vv) eq 'HASH'
+ and exists($vv->{-value})
+ and !defined($vv->{-value})
+ );
+ return +{ -op => [ $op.'_null', $self->expand_expr($k, -ident) ] };
+}
+
+sub _expand_between {
+ my ($self, $op, $vv, $k) = @_;
+ $k = shift @{$vv = [ @$vv ]} unless defined $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_expr(ref($k) ? $k : { -ident => $k }),
+ @rhs
+ ] }
+}
+
+sub _expand_in {
+ my ($self, $op, $vv, $k) = @_;
+ $k = shift @{$vv = [ @$vv ]} unless defined $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_expr($k, -ident),
+ { -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($_, -value),
+ map { defined($_) ? $_: puke($undef_err) }
+ (ref($vv) eq 'ARRAY' ? @$vv : $vv);
+ return $self->${\($op =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs;
+
+ return +{ -op => [
+ $op,
+ $self->expand_expr($k, -ident),
+ @rhs
+ ] };
+}
+
+sub _expand_nest {
+ my ($self, undef, $v) = @_;
+ # DBIx::Class requires a nest warning to be emitted once but the private
+ # method it overrode to do so no longer exists
+ if ($self->{warn_once_on_nest}) {
+ unless (our $Nest_Warned) {
+ belch(
+ "-nest in search conditions is deprecated, you most probably wanted:\n"
+ .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }|
+ );
+ $Nest_Warned = 1;
+ }
+ }
+ return $self->_expand_expr($v);
+}
+
+sub _expand_bind {
+ my ($self, undef, $bind) = @_;
+ return { -bind => $bind };
+}
+
sub _recurse_where {
my ($self, $where, $logic) = @_;
return $self->_convert($self->_quote($ident));
}
-my %unop_postfix = map +($_ => 1),
- 'is null', 'is not null',
- 'asc', 'desc',
-;
-
-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 {
- my ($l, $h) = map [ $self->render_aqt($_) ], $low, $high;
- (join(' ', $l->[0], $self->_sqlcase('and'), $h->[0]),
- @{$l}[1..$#$l], @{$h}[1..$#$h])
- }
- };
- my ($lhsql, @lhbind) = $self->render_aqt($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 {
- my ($sql, @bind) = $self->render_aqt($_);
- push @in_bind, @bind;
- $sql;
- } @$rhs;
- my ($lhsql, @lbind) = $self->render_aqt($lhs);
- return (
- $lhsql.' '.$self->_sqlcase($op).' ( '
- .join(', ', @in_sql)
- .' )',
- @lbind, @in_bind
- );
- }
- }), 'in', 'not in'),
-);
-
-sub _render_op {
- my ($self, $v) = @_;
- my ($op, @args) = @$v;
- $op =~ s/^-// if length($op) > 1;
- $op = lc($op);
- if (my $h = $special{$op}) {
- return $self->$h(\@args);
- }
- my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
- if ($us and @args > 1) {
- puke "Special op '${op}' requires first value to be identifier"
- unless my ($k) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0];
- local our $Expand_Depth = 1;
- 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_aqt($args[0]);
- my $op_sql = $self->_sqlcase($final_op);
- my $final_sql = (
- $unop_postfix{lc($final_op)}
- ? "${expr_sql} ${op_sql}"
- : "${op_sql} ${expr_sql}"
- );
- return (($op eq 'not' || $us ? '('.$final_sql.')' : $final_sql), @bind);
- } else {
- my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @args;
- return '' unless @parts;
- my $is_andor = !!($op =~ /^(and|or)$/);
- return @{$parts[0]} if $is_andor and @parts == 1;
- my ($final_sql) = map +($is_andor ? "( ${_} )" : $_), join(
- ' '.$self->_sqlcase($final_op).' ',
- map $_->[0], @parts
- );
- return (
- $final_sql,
- map @{$_}[1..$#$_], @parts
- );
- }
- die "unhandled";
-}
-
-sub _render_list {
- my ($self, $list) = @_;
- my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @$list;
- return join(', ', map $_->[0], @parts), map @{$_}[1..$#$_], @parts;
+sub _render_row {
+ my ($self, $values) = @_;
+ my ($sql, @bind) = $self->_render_op([ ',', @$values ]);
+ return "($sql)", @bind;
}
sub _render_func {
return @$literal;
}
+sub _render_op {
+ my ($self, $v) = @_;
+ my ($op, @args) = @$v;
+ if (my $r = $self->{render_op}{$op}) {
+ return $self->$r($op, \@args);
+ }
+
+ { # Old SQLA compat
+
+ my $op = join(' ', split '_', $op);
+
+ my $ss = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
+ if ($ss and @args > 1) {
+ puke "Special op '${op}' requires first value to be identifier"
+ unless my ($ident) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0];
+ my $k = join(($self->{name_sep}||'.'), @$ident);
+ local our $Expand_Depth = 1;
+ return $self->${\($ss->{handler})}($k, $op, $args[1]);
+ }
+ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
+ return $self->${\($us->{handler})}($op, $args[0]);
+ }
+ if ($ss) {
+ return $self->_render_unop_paren($op, \@args);
+ }
+ }
+ if (@args == 1) {
+ return $self->_render_unop_prefix($op, \@args);
+ } else {
+ return $self->_render_op_multop($op, \@args);
+ }
+ die "notreached";
+}
+
+
+sub _render_op_between {
+ my ($self, $op, $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 {
+ my ($l, $h) = map [ $self->render_aqt($_) ], $low, $high;
+ (join(' ', $l->[0], $self->_sqlcase('and'), $h->[0]),
+ @{$l}[1..$#$l], @{$h}[1..$#$h])
+ }
+ };
+ my ($lhsql, @lhbind) = $self->render_aqt($left);
+ return (
+ join(' ',
+ '(', $lhsql,
+ $self->_sqlcase(join ' ', split '_', $op),
+ $rhsql, ')'
+ ),
+ @lhbind, @rhbind
+ );
+}
+
+sub _render_op_in {
+ my ($self, $op, $args) = @_;
+ my ($lhs, @rhs) = @$args;
+ my @in_bind;
+ my @in_sql = map {
+ my ($sql, @bind) = $self->render_aqt($_);
+ push @in_bind, @bind;
+ $sql;
+ } @rhs;
+ my ($lhsql, @lbind) = $self->render_aqt($lhs);
+ return (
+ $lhsql.' '.$self->_sqlcase(join ' ', split '_', $op).' ( '
+ .join(', ', @in_sql)
+ .' )',
+ @lbind, @in_bind
+ );
+}
+
+sub _render_op_andor {
+ my ($self, $op, $args) = @_;
+ my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @$args;
+ return '' unless @parts;
+ return @{$parts[0]} if @parts == 1;
+ my ($sql, @bind) = $self->_join_parts(' '.$self->_sqlcase($op).' ', @parts);
+ return '( '.$sql.' )', @bind;
+}
+
+sub _render_op_multop {
+ my ($self, $op, $args) = @_;
+ my @parts = grep length($_->[0]), map [ $self->render_aqt($_) ], @$args;
+ return '' unless @parts;
+ return @{$parts[0]} if @parts == 1;
+ my $join = ($op eq ','
+ ? ', '
+ : ' '.$self->_sqlcase(join ' ', split '_', $op).' '
+ );
+ return $self->_join_parts($join, @parts);
+}
+
+sub _join_parts {
+ my ($self, $join, @parts) = @_;
+ return (
+ join($join, map $_->[0], @parts),
+ (wantarray ? (map @{$_}[1..$#$_], @parts) : ()),
+ );
+}
+
+sub _render_unop_paren {
+ my ($self, $op, $v) = @_;
+ my ($sql, @bind) = $self->_render_unop_prefix($op, $v);
+ return "(${sql})", @bind;
+}
+
+sub _render_unop_prefix {
+ my ($self, $op, $v) = @_;
+ my ($expr_sql, @bind) = $self->render_aqt($v->[0]);
+
+ my $op_sql = $self->_sqlcase($op); # join ' ', split '_', $op);
+ return ("${op_sql} ${expr_sql}", @bind);
+}
+
+sub _render_unop_postfix {
+ my ($self, $op, $v) = @_;
+ my ($expr_sql, @bind) = $self->render_aqt($v->[0]);
+ my $op_sql = $self->_sqlcase(join ' ', split '_', $op);
+ return ($expr_sql.' '.$op_sql, @bind);
+}
+
# Some databases (SQLite) treat col IN (1, 2) different from
# col IN ( (1, 2) ). Use this to strip all outer parens while
# adding them back in the corresponding method
return unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg);
+ return $self->_expand_maybe_list_expr($arg)
+ if ref($arg) eq 'HASH' and ($arg->{-op}||[''])->[0] eq ',';
+
my $expander = sub {
my ($self, $dir, $expr) = @_;
my @to_expand = ref($expr) eq 'ARRAY' ? @$expr : $expr;
puke "ordering direction hash passed to order by must have exactly one key (-asc or -desc)";
}
}
- my @exp = map +(defined($dir) ? { -op => [ $dir => $_ ] } : $_),
+ my @exp = map +(
+ defined($dir) ? { -op => [ $dir =~ /^-?(.*)$/ ,=> $_ ] } : $_
+ ),
map $self->expand_expr($_, -ident),
map ref($_) eq 'ARRAY' ? @$_ : $_, @to_expand;
- return (@exp > 1 ? { -list => \@exp } : $exp[0]);
+ return undef unless @exp;
+ return undef if @exp == 1 and not defined($exp[0]);
+ return +{ -op => [ ',', @exp ] };
};
- local @{$self->{expand_unary}}{qw(-asc -desc)} = (
- sub { shift->$expander(asc => @_) },
- sub { shift->$expander(desc => @_) },
- );
+ local @{$self->{expand}}{qw(asc desc)} = (($expander) x 2);
return $self->$expander(undef, $arg);
}
if $expanded->{-ident} or @{$expanded->{-literal}||[]} == 1;
for ($expanded) {
- if (ref() eq 'HASH' and my $l = $_->{-list}) {
- return map $self->_chunkify_order_by($_), @$l;
+ if (ref() eq 'HASH' and $_->{-op} and $_->{-op}[0] eq ',') {
+ my ($comma, @list) = @{$_->{-op}};
+ return map $self->_chunkify_order_by($_), @list;
}
return [ $self->render_aqt($_) ];
}
my $self = shift;
my $from = shift;
($self->render_aqt(
- $self->_expand_maybe_list_expr($from, undef, -ident)
+ $self->_expand_maybe_list_expr($from, -ident)
))[0];
}
#======================================================================
sub _expand_maybe_list_expr {
- my ($self, $expr, $logic, $default) = @_;
- my $e = do {
- if (ref($expr) eq 'ARRAY') {
- return { -list => [
- map $self->expand_expr($_, $default), @$expr
- ] } if @$expr > 1;
- $expr->[0]
- } else {
- $expr
- }
- };
- return $self->expand_expr($e, $default);
+ my ($self, $expr, $default) = @_;
+ return { -op => [
+ ',', map $self->expand_expr($_, $default),
+ @{$expr->{-op}}[1..$#{$expr->{-op}}]
+ ] } if ref($expr) eq 'HASH' and ($expr->{-op}||[''])->[0] eq ',';
+ return +{ -op => [ ',',
+ map $self->expand_expr($_, $default),
+ ref($expr) eq 'ARRAY' ? @$expr : $expr
+ ] };
}
# highly optimized, as it's called way too often