no-op expander and hashpair op expansion cleanup
[scpubgit/Q-Branch.git] / lib / SQL / Abstract.pm
index 7a03936..f518e0a 100644 (file)
@@ -139,23 +139,14 @@ sub is_plain_value ($) {
 
 our %Defaults = (
   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',
     values => '_expand_values',
+    bind => '_expand_noop',
+    literal => '_expand_noop',
   },
   expand_op => {
     'between' => '_expand_between',
@@ -420,12 +411,12 @@ sub insert {
 }
 
 sub _expand_insert_clause_target {
-  +(target => $_[0]->_expand_maybe_list_expr($_[2], -ident));
+  +(target => $_[0]->expand_maybe_list_expr($_[2], -ident));
 }
 
 sub _expand_insert_clause_fields {
   return +{ -row => [
-    $_[0]->_expand_maybe_list_expr($_[2], -ident)
+    $_[0]->expand_maybe_list_expr($_[2], -ident)
   ] } if ref($_[2]) eq 'ARRAY';
   return $_[2]; # should maybe still expand somewhat?
 }
@@ -444,7 +435,7 @@ sub _expand_insert_clause_from {
 }
 
 sub _expand_insert_clause_returning {
-  +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
+  +(returning => $_[0]->expand_maybe_list_expr($_[2], -ident));
 }
 
 sub _expand_insert_values {
@@ -505,7 +496,7 @@ sub _returning {
   my $f = $options->{returning};
 
   my ($sql, @bind) = @{ $self->render_aqt(
-    $self->_expand_maybe_list_expr($f, -ident)
+    $self->expand_maybe_list_expr($f, -ident)
   ) };
   return ($self->_sqlcase(' returning ').$sql, @bind);
 }
@@ -575,7 +566,7 @@ sub _update_set_values {
 
 sub _expand_update_set_values {
   my ($self, undef, $data) = @_;
-  $self->_expand_maybe_list_expr( [
+  $self->expand_maybe_list_expr( [
     map {
       my ($k, $set) = @$_;
       $set = { -bind => $_ } unless defined $set;
@@ -599,7 +590,7 @@ sub _expand_update_set_values {
 
 sub _expand_update_clause_target {
   my ($self, undef, $target) = @_;
-  +(target => $self->_expand_maybe_list_expr($target, -ident));
+  +(target => $self->expand_maybe_list_expr($target, -ident));
 }
 
 sub _expand_update_clause_set {
@@ -612,7 +603,7 @@ sub _expand_update_clause_where {
 }
 
 sub _expand_update_clause_returning {
-  +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
+  +(returning => $_[0]->expand_maybe_list_expr($_[2], -ident));
 }
 
 # So that subclasses can override UPDATE ... RETURNING separately from
@@ -649,12 +640,12 @@ sub select {
 
 sub _expand_select_clause_select {
   my ($self, undef, $select) = @_;
-  +(select => $self->_expand_maybe_list_expr($select, -ident));
+  +(select => $self->expand_maybe_list_expr($select, -ident));
 }
 
 sub _expand_select_clause_from {
   my ($self, undef, $from) = @_;
-  +(from => $self->_expand_maybe_list_expr($from, -ident));
+  +(from => $self->expand_maybe_list_expr($from, -ident));
 }
 
 sub _expand_select_clause_where {
@@ -673,8 +664,8 @@ sub _expand_select_clause_where {
         };
       };
       $self->clone
-           ->wrap_expanders(map +($_ => $_wrap), qw(ident value bind))
-           ->wrap_op_expanders(map +($_ => $_wrap), qw(ident value bind))
+           ->wrap_expander(bind => $_wrap)
+           ->wrap_op_expanders(map +($_ => $_wrap), qw(ident value))
            ->wrap_expander(func => sub {
                my $orig = shift;
                sub {
@@ -708,7 +699,7 @@ sub _select_fields {
   my ($self, $fields) = @_;
   return $fields unless ref($fields);
   return @{ $self->render_aqt(
-    $self->_expand_maybe_list_expr($fields, '-ident')
+    $self->expand_maybe_list_expr($fields, '-ident')
   ) };
 }
 
@@ -736,13 +727,13 @@ sub delete {
 sub _delete_returning { shift->_returning(@_) }
 
 sub _expand_delete_clause_target {
-  +(target => $_[0]->_expand_maybe_list_expr($_[2], -ident));
+  +(target => $_[0]->expand_maybe_list_expr($_[2], -ident));
 }
 
 sub _expand_delete_clause_where { +(where => $_[0]->expand_expr($_[2])); }
 
 sub _expand_delete_clause_returning {
-  +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
+  +(returning => $_[0]->expand_maybe_list_expr($_[2], -ident));
 }
 
 sub _render_delete_clause_target {
@@ -1020,7 +1011,7 @@ sub _expand_hashpair_op {
     }
   }
 
-  if (my $exp = $self->{expand}{$op}) {
+  if (my $exp = $self->{expand}{$op}||$self->{expand_op}{$op}) {
     return $self->$exp($op, $v);
   }
 
@@ -1044,14 +1035,11 @@ sub _expand_hashpair_op {
     }
   }
 
-  # 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 };
-  }
-
-  my $type = $self->{unknown_unop_always_func} ? -func : -op;
+  my $type = (
+    $self->{unknown_unop_always_func} && !$self->{render_op}{$op}
+      ? -func
+      : -op
+  );
 
   { # Old SQLA compat
 
@@ -1061,19 +1049,21 @@ sub _expand_hashpair_op {
       and (keys %$v)[0] =~ /^-/
     ) {
       $type = (
-        (List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}})
+        (
+          (List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}})
+          or $self->{render_op}{$op}
+        )
           ? -op
           : -func
       )
     }
   }
 
-  return +{ $type => [
-    $op,
-    ($type eq -func and ref($v) eq 'ARRAY')
-      ? map $self->_expand_expr($_), @$v
-      : $self->_expand_expr($v)
-  ] };
+  if ($type eq -func and ref($v) eq 'ARRAY') {
+    return $self->_expand_expr({ -func => [ $op, @$v ] });
+  }
+
+  return $self->_expand_expr({ $type => [ $op, $v ] });
 }
 
 sub _expand_hashpair_cmp {
@@ -1207,10 +1197,6 @@ sub _expand_value {
   +{ -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 ] };
@@ -1366,9 +1352,9 @@ sub _expand_nest {
   return $self->_expand_expr($v);
 }
 
-sub _expand_bind {
-  my ($self, undef, $bind) = @_;
-  return { -bind => $bind };
+sub _expand_noop {
+  my ($self, $type, $v) = @_;
+  return { "-${type}" => $v };
 }
 
 sub _expand_values {
@@ -1623,7 +1609,7 @@ sub _expand_order_by {
 
   return unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg);
 
-  return $self->_expand_maybe_list_expr($arg)
+  return $self->expand_maybe_list_expr($arg)
     if ref($arg) eq 'HASH' and ($arg->{-op}||[''])->[0] eq ',';
 
   my $expander = sub {
@@ -1702,7 +1688,7 @@ sub _table  {
   my $self = shift;
   my $from = shift;
   $self->render_aqt(
-    $self->_expand_maybe_list_expr($from, -ident)
+    $self->expand_maybe_list_expr($from, -ident)
   )->[0];
 }
 
@@ -1711,7 +1697,7 @@ sub _table  {
 # UTILITY FUNCTIONS
 #======================================================================
 
-sub _expand_maybe_list_expr {
+sub expand_maybe_list_expr {
   my ($self, $expr, $default) = @_;
   return { -op => [
     ',', map $self->expand_expr($_, $default),