op => '_expand_op',
func => '_expand_func',
values => '_expand_values',
- bind => '_expand_noop',
- literal => '_expand_noop',
},
expand_op => {
- 'between' => '_expand_between',
- 'not_between' => '_expand_between',
- 'in' => '_expand_in',
- 'not_in' => '_expand_in',
- 'nest' => '_expand_nest',
+ (map +($_ => __PACKAGE__->make_binop_expander('_expand_between')),
+ qw(between not_between)),
+ (map +($_ => __PACKAGE__->make_binop_expander('_expand_in')),
+ qw(in not_in)),
(map +($_ => '_expand_op_andor'), ('and', 'or')),
(map +($_ => '_expand_op_is'), ('is', 'is_not')),
- 'ident' => '_expand_ident',
- 'value' => '_expand_value',
+ (map +($_ => __PACKAGE__->make_unop_expander("_expand_${_}")),
+ qw(ident value nest)),
},
render => {
- (map +($_, "_render_$_"), qw(op func bind ident literal row values)),
+ (map +($_, "_render_$_"),
+ qw(op func bind ident literal row values keyword)),
},
render_op => {
(map +($_ => '_render_op_between'), 'between', 'not_between'),
return $self;
}
+sub make_unop_expander {
+ my (undef, $exp) = @_;
+ sub {
+ my ($self, $name, $body, $k) = @_;
+ return $self->_expand_hashpair_cmp($k, { "-${name}" => $body })
+ if defined($k);
+ return $self->$exp($name, $body);
+ }
+}
+
+sub make_binop_expander {
+ my (undef, $exp) = @_;
+ sub {
+ my ($self, $name, $body, $k) = @_;
+ $k = shift @{$body = [ @$body ]} unless defined $k;
+ $k = ref($k) ? $k : { -ident => $k };
+ return $self->$exp($name, $body, $k);
+ }
+}
+
BEGIN {
foreach my $type (qw(
expand op_expand render op_render clause_expand clause_render
if (ref($data) eq 'HASH' and (keys(%$data))[0] =~ /^-/) {
return $self->expand_expr($data);
}
- return $data if ref($data) eq 'HASH' and $data->{-row};
my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data);
return (
from => { -values => [ $v_aqt ] },
sub _render_insert_clause_target {
my ($self, undef, $from) = @_;
- $self->join_query_parts(' ', $self->format_keyword('insert into'), $from);
+ $self->join_query_parts(' ', { -keyword => 'insert into' }, $from);
}
sub _render_insert_clause_from {
sub _render_update_clause_target {
my ($self, undef, $target) = @_;
- $self->join_query_parts(' ', $self->format_keyword('update'), $target);
+ $self->join_query_parts(' ', { -keyword => 'update' }, $target);
}
sub _update_set_values {
sub _render_delete_clause_target {
my ($self, undef, $from) = @_;
- $self->join_query_parts(' ', $self->format_keyword('delete from'), $from);
+ $self->join_query_parts(' ', { -keyword => 'delete from' }, $from);
}
#======================================================================
my $r = $self->render_aqt($clause_expr, 1);
next unless defined $r->[0] and length $r->[0];
$self->join_query_parts(' ',
- $self->format_keyword($clause),
+ { -keyword => $clause },
$r
);
}
my $op = $self->_normalize_op($k);
- { # Old SQLA compat
+ my $wsop = join(' ', split '_', $op);
- my $op = join(' ', split '_', $op);
+ my $is_special = List::Util::first { $wsop =~ $_->{regex} }
+ @{$self->{special_ops}};
+
+ { # Old SQLA compat
# 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}}
+ $is_special
or (
$self->{disable_old_special_ops}
- and List::Util::first { $op =~ $_->{regex} } @BUILTIN_SPECIAL_OPS
+ and List::Util::first { $wsop =~ $_->{regex} } @BUILTIN_SPECIAL_OPS
)
)
) {
- puke "Illegal use of top-level '-$op'"
+ puke "Illegal use of top-level '-$wsop'"
}
}
return $self->$exp($op, $v);
}
+ if ($self->{render}{$op}) {
+ return { "-${op}" => $v };
+ }
+
# Ops prefixed with -not_ get converted
if (my ($rest) = $op =~/^not_(.*)$/) {
}
}
- my $type = (
- $self->{unknown_unop_always_func} && !$self->{render_op}{$op}
- ? -func
- : -op
- );
+ my $type = $is_special || $self->{render_op}{$op} ? -op : -func;
- { # Old SQLA compat
+ if ($self->{restore_old_unop_handling}) {
+
+ # Old SQLA compat
if (
ref($v) eq 'HASH'
and keys %$v == 1
and (keys %$v)[0] =~ /^-/
+ and not $self->{render_op}{$op}
+ and not $is_special
) {
- $type = (
- (
- (List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}})
- or $self->{render_op}{$op}
- )
- ? -op
- : -func
- )
+ $type = -func;
+ } else {
+ $type = -op;
}
}
}
sub _expand_ident {
- my ($self, undef, $body, $k) = @_;
- return $self->_expand_hashpair_cmp(
- $k, { -ident => $body }
- ) if defined($k);
+ my ($self, undef, $body) = @_;
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";
}
}
sub _expand_value {
- return $_[0]->_expand_hashpair_cmp(
- $_[3], { -value => $_[2] },
- ) if defined($_[3]);
+{ -bind => [ our $Cur_Col_Meta, $_[2] ] };
}
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 (
}
return +{ -op => [
$op,
- $self->expand_expr(ref($k) ? $k : { -ident => $k }),
- @rhs
+ $self->expand_expr($k),
+ map $self->expand_expr($_, -value), @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 $self->_expand_expr($v);
}
-sub _expand_noop {
- my ($self, $type, $v) = @_;
- return { "-${type}" => $v };
-}
-
sub _expand_values {
my ($self, undef, $values) = @_;
return { -values => [
}
sub _recurse_where {
- my ($self, $where, $logic) = @_;
+ my ($self, $where) = @_;
# Special case: top level simple string treated as literal
my $where_exp = (ref($where)
- ? $self->_expand_expr($where, $logic)
+ ? $self->_expand_select_clause_where(undef, $where)
: { -literal => [ $where ] });
# dispatch expanded expression
sub _render_ident {
my ($self, undef, $ident) = @_;
- return [ $self->_convert($self->_quote($ident)) ];
+ return [ $self->_quote($ident) ];
}
sub _render_row {
sub _render_bind {
my ($self, undef, $bind) = @_;
- return [ $self->_convert('?'), $self->_bindtype(@$bind) ];
+ return [ '?', $self->_bindtype(@$bind) ];
}
sub _render_literal {
return $literal;
}
+sub _render_keyword {
+ my ($self, undef, $keyword) = @_;
+ return [ $self->_sqlcase(
+ ref($keyword) ? $$keyword : join ' ', split '_', $keyword
+ ) ];
+}
+
sub _render_op {
my ($self, undef, $v) = @_;
my ($op, @args) = @$v;
unless $low->{-literal};
$low;
} else {
- +($low, $self->format_keyword('and'), $high);
+ +($low, { -keyword => 'and' }, $high);
}
};
return $self->join_query_parts(' ',
- '(', $left, $self->format_keyword($op), @rh, ')',
+ '(', $left, { -keyword => $op }, @rh, ')',
);
}
return $self->join_query_parts(' ',
$lhs,
- $self->format_keyword($op),
+ { -keyword => $op },
$self->join_query_parts(' ',
'(',
$self->join_query_parts(', ', @rhs),
return $self->render_aqt($parts[0]) if @parts == 1;
my $join = ($op eq ','
? ', '
- : ' '.$self->format_keyword($op).' '
+ : { -keyword => " ${op} " }
);
return $self->join_query_parts($join, @parts);
}
sub _render_values {
my ($self, undef, $values) = @_;
my $inner = $self->join_query_parts(' ',
- $self->format_keyword('values'),
+ { -keyword => 'values' },
$self->join_query_parts(', ',
ref($values) eq 'ARRAY' ? @$values : $values
),
sub join_query_parts {
my ($self, $join, @parts) = @_;
+ if (ref($join) eq 'HASH') {
+ $join = $self->render_aqt($join)->[0];
+ }
my @final = map +(
ref($_) eq 'HASH'
? $self->render_aqt($_)
sub _render_unop_prefix {
my ($self, $op, $v) = @_;
+ my $op_sql = $self->{restore_old_unop_handling}
+ ? $self->_sqlcase($op)
+ : { -keyword => $op };
return $self->join_query_parts(' ',
- $self->_sqlcase($op), $v->[0]
+ ($self->{restore_old_unop_handling}
+ ? $self->_sqlcase($op)
+ : { -keyword => \$op }),
+ $v->[0]
);
}
sub _render_unop_postfix {
my ($self, $op, $v) = @_;
return $self->join_query_parts(' ',
- $v->[0], $self->format_keyword($op),
+ $v->[0], { -keyword => $op },
);
}
#my ($self, $arg) = @_;
if (my $conv = $_[0]->{convert_where}) {
return @{ $_[0]->join_query_parts('',
- $_[0]->format_keyword($conv),
+ $_[0]->_sqlcase($conv),
'(' , $_[1] , ')'
) };
}
}
}
-sub _join_sql_clauses {
- my ($self, $logic, $clauses_aref, $bind_aref) = @_;
-
- if (@$clauses_aref > 1) {
- my $join = " " . $self->_sqlcase($logic) . " ";
- my $sql = '( ' . join($join, @$clauses_aref) . ' )';
- return ($sql, @$bind_aref);
- }
- elsif (@$clauses_aref) {
- return ($clauses_aref->[0], @$bind_aref); # no parentheses
- }
- else {
- return (); # if no SQL, ignore @$bind_aref
- }
-}
-
-
# Fix SQL case, if so requested
sub _sqlcase {
# LDNOTE: if $self->{case} is true, then it contains 'lower', so we
return $_[0]->{case} ? $_[1] : uc($_[1]);
}
-sub format_keyword { $_[0]->_sqlcase(join ' ', split '_', $_[1]) }
-
#======================================================================
# DISPATCHING FROM REFKIND
#======================================================================