expand value node type to bind, remove value render code
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract.pm
index 8745cc4..cba900c 100644 (file)
@@ -160,10 +160,11 @@ sub new {
   # regexes are applied in order, thus push after user-defines
   push @{$opt{special_ops}}, @BUILTIN_SPECIAL_OPS;
 
-  if ($class =~ /^DBIx::Class::SQLMaker/) {
+  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;
   }
 
   # unary operators
@@ -185,7 +186,7 @@ sub new {
 
   $opt{node_types} = +{
     map +("-$_" => '_render_'.$_),
-      qw(op func value bind ident literal)
+      qw(op func bind ident literal list)
   };
 
   $opt{expand_unary} = {};
@@ -557,18 +558,16 @@ sub _expand_expr {
   }
   if (!ref($expr) or Scalar::Util::blessed($expr)) {
     if (my $d = $Default_Scalar_To) {
-      return +{ $d => $expr };
+      return $self->_expand_expr({ $d => $expr });
     }
     if (my $m = our $Cur_Col_Meta) {
       return +{ -bind => [ $m, $expr ] };
     }
-    return +{ -value => $expr };
+    return +{ -bind => [ undef, $expr ] };
   }
   die "notreached";
 }
 
-my $Nest_Warned = 0;
-
 sub _expand_expr_hashpair {
   my ($self, $k, $v, $logic) = @_;
   unless (defined($k) and length($k)) {
@@ -587,8 +586,8 @@ sub _expand_expr_hashpair {
     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 (ref($self) =~ /^DBIx::Class::SQLMaker/) {
-        unless ($Nest_Warned) {
+      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. ], ... }|
@@ -640,8 +639,8 @@ sub _expand_expr_hashpair {
         return { -op => [ $op, $v ] };
       }
     }
-    if ($k eq '-value' and my $m = our $Cur_Col_Meta) {
-      return +{ -bind => [ $m, $v ] };
+    if ($k eq '-value') {
+      return +{ -bind => [ our $Cur_Col_Meta, $v ] };
     }
     if (my $custom = $self->{expand_unary}{$k}) {
       return $self->$custom($v);
@@ -896,21 +895,13 @@ sub _render_expr {
 sub _recurse_where {
   my ($self, $where, $logic) = @_;
 
-#print STDERR Data::Dumper::Concise::Dumper([ $where, $logic ]);
-
   # Special case: top level simple string treated as literal
 
   my $where_exp = (ref($where)
                     ? $self->_expand_expr($where, $logic)
                     : { -literal => [ $where ] });
-#::Dwarn([ EXPANDED => $where_exp ]);
-
-#print STDERR Data::Dumper::Concise::Dumper([ EXP => $where_exp ]);
 
-  # dispatch on appropriate method according to refkind of $where
-#  my $method = $self->_METHOD_FOR_refkind("_where", $where_exp);
-
-#  my ($sql, @bind) =  $self->$method($where_exp, $logic);
+  # dispatch expanded expression
 
   my ($sql, @bind) = defined($where_exp) ? $self->_render_expr($where_exp) : (undef);
   # DBIx::Class used to call _recurse_where in scalar context
@@ -930,12 +921,6 @@ sub _render_ident {
   return $self->_convert($self->_quote($ident));
 }
 
-sub _render_value {
-  my ($self, $value) = @_;
-
-  return ($self->_convert('?'), $self->_bindtype(undef, $value));
-}
-
 my %unop_postfix = map +($_ => 1),
   'is null', 'is not null',
   'asc', 'desc',
@@ -1023,7 +1008,7 @@ sub _render_op {
      my $is_andor = !!($op =~ /^(and|or)$/);
      return @{$parts[0]} if $is_andor and @parts == 1;
      my ($final_sql) = map +($is_andor ? "( ${_} )" : $_), join(
-       ($final_op eq ',' ? '' : ' ').$self->_sqlcase($final_op).' ',
+       ' '.$self->_sqlcase($final_op).' ',
        map $_->[0], @parts
      );
      return (
@@ -1034,6 +1019,12 @@ sub _render_op {
   die "unhandled";
 }
 
+sub _render_list {
+  my ($self, $list) = @_;
+  my @parts = grep length($_->[0]), map [ $self->_render_expr($_) ], @$list;
+  return join(', ', map $_->[0], @parts), map @{$_}[1..$#$_], @parts;
+}
+
 sub _render_func {
   my ($self, $rest) = @_;
   my ($func, @args) = @$rest;
@@ -1112,7 +1103,7 @@ sub _expand_order_by {
     my @exp = map +(defined($dir) ? { -op => [ $dir => $_ ] } : $_),
                 map $self->_expand_expr($_, undef, -ident),
                 map ref($_) eq 'ARRAY' ? @$_ : $_, @to_expand;
-    return (@exp > 1 ? { -op => [ ',', @exp ] } : $exp[0]);
+    return (@exp > 1 ? { -list => \@exp } : $exp[0]);
   };
 
   local @{$self->{expand_unary}}{qw(-asc -desc)} = (
@@ -1154,10 +1145,8 @@ sub _chunkify_order_by {
     if $expanded->{-ident} or @{$expanded->{-literal}||[]} == 1;
 
   for ($expanded) {
-    if (ref() eq 'HASH' and my $op = $_->{-op}) {
-      if ($op->[0] eq ',') {
-        return map $self->_chunkify_order_by($_), @{$op}[1..$#$op];
-      }
+    if (ref() eq 'HASH' and my $l = $_->{-list}) {
+      return map $self->_chunkify_order_by($_), @$l;
     }
     return [ $self->_render_expr($_) ];
   }
@@ -1184,8 +1173,8 @@ sub _expand_maybe_list_expr {
   my ($self, $expr, $logic, $default) = @_;
   my $e = do {
     if (ref($expr) eq 'ARRAY') {
-      return { -op => [
-        ',', map $self->_expand_expr($_, $logic, $default), @$expr
+      return { -list => [
+        map $self->_expand_expr($_, $logic, $default), @$expr
       ] } if @$expr > 1;
       $expr->[0]
     } else {