nest doesn't need a node expander either
[scpubgit/Q-Branch.git] / lib / SQL / Abstract.pm
index e7cc40c..82f0f56 100644 (file)
@@ -139,23 +139,13 @@ 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',
@@ -243,11 +233,6 @@ sub new {
   # special operators
   $opt{special_ops} ||= [];
 
-  if ($class->isa('DBIx::Class::SQLMaker')) {
-    $opt{warn_once_on_nest} = 1;
-    $opt{disable_old_special_ops} = 1;
-  }
-
   # unary operators
   $opt{unary_ops} ||= [];
 
@@ -271,14 +256,38 @@ sub new {
     $opt{$name} = { %{$Defaults{$name}}, %{$opt{$name}||{}} };
   }
 
-  foreach my $type (qw(insert update delete)) {
-    my $method = "_${type}_returning";
-    if ($class ne __PACKAGE__
-      and __PACKAGE__->can($method) ne $class->can($method)) {
-      my $clause = "${type}.returning";
-      $opt{expand_clause}{$clause} = sub { $_[2] },
-      $opt{render_clause}{$clause}
-        = sub { [ $_[0]->$method($_[3]) ] };
+  if ($class ne __PACKAGE__) {
+
+    # check for overriden methods
+
+    foreach my $type (qw(insert update delete)) {
+      my $method = "_${type}_returning";
+      if (__PACKAGE__->can($method) ne $class->can($method)) {
+        my $clause = "${type}.returning";
+        $opt{expand_clause}{$clause} = sub { $_[2] },
+        $opt{render_clause}{$clause}
+          = sub { [ $_[0]->$method($_[3]) ] };
+      }
+    }
+    if (__PACKAGE__->can('_table') ne $class->can('_table')) {
+      $opt{expand_clause}{'select.from'} = sub {
+        return +{ -literal => [ $_[0]->_table($_[2]) ] };
+      };
+    }
+    if (__PACKAGE__->can('_order_by') ne $class->can('_order_by')) {
+      $opt{expand_clause}{'select.order_by'} = sub { $_[2] };
+      $opt{render_clause}{'select.order_by'} = sub {
+        [ $_[0]->_order_by($_[2]) ];
+      };
+    }
+    if ($class->isa('DBIx::Class::SQLMaker')) {
+      $opt{warn_once_on_nest} = 1;
+      $opt{disable_old_special_ops} = 1;
+      $opt{render_clause}{'select.where'} = sub {
+        my ($sql, @bind) = $_[0]->where($_[2]);
+        s/\A\s+//, s/\s+\Z// for $sql;
+        return [ $sql, @bind ];
+      };
     }
   }
 
@@ -401,12 +410,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?
 }
@@ -425,7 +434,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 {
@@ -486,7 +495,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);
 }
@@ -556,7 +565,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;
@@ -580,7 +589,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 {
@@ -593,7 +602,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
@@ -630,12 +639,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 {
@@ -654,8 +663,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 {
@@ -689,7 +698,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')
   ) };
 }
 
@@ -717,13 +726,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 {
@@ -1001,7 +1010,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);
   }
 
@@ -1025,14 +1034,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
 
@@ -1042,19 +1048,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 {
@@ -1188,10 +1196,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 ] };
@@ -1347,9 +1351,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 {
@@ -1536,7 +1540,9 @@ sub join_query_parts {
       : ((ref($_) eq 'ARRAY') ? $_ : [ $_ ])
   ), @parts;
   return [
-    $self->{join_sql_parts}->($join, grep defined, map $_->[0], @final),
+    $self->{join_sql_parts}->(
+      $join, grep defined && length, map $_->[0], @final
+    ),
     (map @{$_}[1..$#$_], @final),
   ];
 }
@@ -1602,7 +1608,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 {
@@ -1681,7 +1687,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];
 }
 
@@ -1690,7 +1696,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),