restore expand stability for extraclauses
[scpubgit/Q-Branch.git] / lib / SQL / Abstract / ExtraClauses.pm
index f7da280..9af44a5 100644 (file)
@@ -116,22 +116,33 @@ sub register_defaults {
           qw(union intersect except)
   );
 
+  my $expand_alias = sub {
+    my ($self, $to_expand) = @_;
+    [ map $self->expand_expr($_, -ident),
+        ref($to_expand) eq 'ARRAY'
+          ? @$to_expand
+          : $to_expand
+    ]
+  };
   $self->clause_expander('select.with' => my $with_expander = sub {
     my ($self, $name, $with) = @_;
     my (undef, $type) = split '_', $name;
     if (ref($with) eq 'HASH') {
       return +{
         %$with,
-        queries => [ map $self->expand_expr($_), @{$with->{queries}} ]
+        queries => [
+          map +[
+            $self->$expand_alias($_->[0]),
+            $self->expand_expr($_->[1]),
+          ], @{$with->{queries}}
+        ]
       }
     }
     my @with = @$with;
     my @exp;
-    while (my ($name, $query) = splice @with, 0, 2) {
-      my @n = map $self->expand_expr($_, -ident),
-                ref($name) eq 'ARRAY' ? @$name : $name;
+    while (my ($alias, $query) = splice @with, 0, 2) {
       push @exp, [
-        \@n,
+        $self->$expand_alias($alias),
         $self->expand_expr($query)
       ];
     }
@@ -140,18 +151,18 @@ sub register_defaults {
   $self->clause_expander('select.with_recursive', $with_expander);
   $self->clause_renderer('select.with' => sub {
     my ($self, undef, $with) = @_;
-    my $q_part = [ $self->join_query_parts(', ',
+    my $q_part = $self->join_query_parts(', ',
       map {
         my ($alias, $query) = @$_;
-        [ $self->join_query_parts(' ',
-            [ $self->_render_alias($alias) ],
-            [ $self->format_keyword('as') ],
+        $self->join_query_parts(' ',
+            $self->_render_alias($alias),
+            $self->format_keyword('as'),
             $query,
-        ) ]
+        )
       } @{$with->{queries}}
-    ) ];
+    );
     return $self->join_query_parts(' ',
-      [ $self->format_keyword(join '_', 'with', ($with->{type}||'')) ],
+      $self->format_keyword(join '_', 'with', ($with->{type}||'')),
       $q_part,
     );
   });
@@ -167,6 +178,7 @@ sub _expand_select_clause_from {
 sub _expand_from_list {
   my ($self, undef, $args) = @_;
   if (ref($args) eq 'HASH') {
+    return $args if $args->{-from_list};
     return { -from_list => [ $self->expand_expr($args) ] };
   }
   my @list;
@@ -247,9 +259,9 @@ sub _render_as {
   my ($thing, @alias) = @$args;
   return $self->join_query_parts(
     ' ',
-    [ $self->render_aqt($thing) ],
-    [ $self->format_keyword('as') ],
-    [ $self->_render_alias(\@alias) ],
+    $self->render_aqt($thing),
+    $self->format_keyword('as'),
+    $self->_render_alias(\@alias),
   );
 }
 
@@ -259,12 +271,12 @@ sub _render_alias {
   return (@cols
     ? $self->join_query_parts('',
          $as,
-         [ '(' ],
-         [ $self->join_query_parts(
-             ', ',
-             @cols
-         ) ],
-         [ ')' ],
+         '(',
+         $self->join_query_parts(
+           ', ',
+           @cols
+         ),
+         ')',
       )
     : $self->render_aqt($as)
   );