}
sub _expand_expr {
- my ($self, $expr, $logic) = @_;
+ my ($self, $expr, $logic, $default_scalar_to) = @_;
+ local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to;
return undef unless defined($expr);
if (ref($expr) eq 'HASH') {
if (keys %$expr > 1) {
return +{ -literal => $literal };
}
if (!ref($expr) or Scalar::Util::blessed($expr)) {
+ if (my $d = $Default_Scalar_To) {
+ return +{ $d => $expr };
+ }
if (my $m = our $Cur_Col_Meta) {
return +{ -bind => [ $m, $expr ] };
}
# top level special ops are illegal in general
puke "Illegal use of top-level '-$op'"
- if !(defined $self->{_nested_func_lhs})
- and List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
+ if List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
}
if ($k eq '-value' and my $m = our $Cur_Col_Meta) {
return +{ -bind => [ $m, $v ] };
if ($k eq '-op' or $k eq '-ident' or $k eq '-value' or $k eq '-bind' or $k eq '-literal' or $k eq '-func') {
return { $k => $v };
}
+ if (my $custom = $self->{custom_expansions}{($k =~ /^-(.*)$/)[0]}) {
+ return $self->$custom($v);
+ }
if (
ref($v) eq 'HASH'
and keys %$v == 1
my ($self, $expr) = @_;
my ($k, $v, @rest) = %$expr;
die "No" if @rest;
- my %op = map +("-$_" => '_where_op_'.uc($_)),
+ my %op = map +("-$_" => '_render_'.$_),
qw(op func value bind ident literal);
if (my $meth = $op{$k}) {
- return $self->$meth(undef, $v);
+ return $self->$meth($v);
}
die "notreached: $k";
}
}
}
-sub _where_op_IDENT {
- my ($self, undef, $ident) = @_;
+sub _render_ident {
+ my ($self, $ident) = @_;
return $self->_convert($self->_quote($ident));
}
-sub _where_op_VALUE {
- my ($self, undef, $value) = @_;
+sub _render_value {
+ my ($self, $value) = @_;
return ($self->_convert('?'), $self->_bindtype(undef, $value));
}
-my %unop_postfix = map +($_ => 1), 'is null', 'is not null';
+my %unop_postfix = map +($_ => 1),
+ 'is null', 'is not null',
+ 'asc', 'desc',
+;
my %special = (
(map +($_ => do {
}), 'in', 'not in'),
);
-sub _where_op_OP {
- my ($self, undef, $v) = @_;
+sub _render_op {
+ my ($self, $v) = @_;
my ($op, @args) = @$v;
$op =~ s/^-// if length($op) > 1;
$op = lc($op);
- local $self->{_nested_func_lhs};
if (my $h = $special{$op}) {
return $self->$h(\@args);
}
die "unhandled";
}
-sub _where_op_FUNC {
- my ($self, undef, $rest) = @_;
+sub _render_func {
+ my ($self, $rest) = @_;
my ($func, @args) = @$rest;
my @arg_sql;
my @bind = map {
return ($self->_sqlcase($func).'('.join(', ', @arg_sql).')', @bind);
}
-sub _where_op_BIND {
- my ($self, undef, $bind) = @_;
+sub _render_bind {
+ my ($self, $bind) = @_;
return ($self->_convert('?'), $self->_bindtype(@$bind));
}
-sub _where_op_LITERAL {
- my ($self, undef, $literal) = @_;
+sub _render_literal {
+ my ($self, $literal) = @_;
$self->_assert_bindval_matches_bindtype(@{$literal}[1..$#$literal]);
return @$literal;
}
sub _order_by {
my ($self, $arg) = @_;
- my (@sql, @bind);
- for my $c ($self->_order_by_chunks($arg) ) {
- $self->_SWITCH_refkind($c, {
- SCALAR => sub { push @sql, $c },
- ARRAYREF => sub { push @sql, shift @$c; push @bind, @$c },
- });
- }
+ return '' unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg);
- my $sql = @sql
- ? sprintf('%s %s',
- $self->_sqlcase(' order by'),
- join(', ', @sql)
- )
- : ''
- ;
-
- return wantarray ? ($sql, @bind) : $sql;
-}
-
-sub _order_by_chunks {
- my ($self, $arg) = @_;
-
- return $self->_SWITCH_refkind($arg, {
-
- ARRAYREF => sub {
- map { $self->_order_by_chunks($_ ) } @$arg;
- },
-
- ARRAYREFREF => sub {
- my ($s, @b) = @$$arg;
- $self->_assert_bindval_matches_bindtype(@b);
- [ $s, @b ];
- },
+ my $expander = sub {
+ my ($self, $dir, $expr) = @_;
+ my @exp = map +(defined($dir) ? { -op => [ $dir => $_ ] } : $_),
+ map $self->_expand_expr($_, undef, -ident),
+ ref($expr) eq 'ARRAY' ? @$expr : $expr;
+ return (@exp > 1 ? { -op => [ ',', @exp ] } : $exp[0]);
+ };
- SCALAR => sub {$self->_quote($arg)},
+ local $self->{custom_expansions} = {
+ asc => sub { shift->$expander(asc => @_) },
+ desc => sub { shift->$expander(desc => @_) },
+ };
- UNDEF => sub {return () },
+ my $expanded = $self->$expander(undef, $arg);
- SCALARREF => sub {$$arg}, # literal SQL, no quoting
+ my ($sql, @bind) = $self->_render_expr($expanded);
- HASHREF => sub {
- # get first pair in hash
- my ($key, $val, @rest) = %$arg;
+ my $final_sql = $self->_sqlcase(' order by ').$sql;
- return () unless $key;
-
- if (@rest or not $key =~ /^-(desc|asc)/i) {
- puke "hash passed to _order_by must have exactly one key (-desc or -asc)";
- }
-
- my $direction = $1;
-
- my @ret;
- for my $c ($self->_order_by_chunks($val)) {
- my ($sql, @bind);
-
- $self->_SWITCH_refkind($c, {
- SCALAR => sub {
- $sql = $c;
- },
- ARRAYREF => sub {
- ($sql, @bind) = @$c;
- },
- });
-
- $sql = $sql . ' ' . $self->_sqlcase($direction);
-
- push @ret, [ $sql, @bind];
- }
-
- return @ret;
- },
- });
+ return wantarray ? ($final_sql, @bind) : $final_sql;
}
-
#======================================================================
# DATASOURCE (FOR NOW, JUST PLAIN TABLE OR LIST OF TABLES)
#======================================================================