consolidate with_recursive
[scpubgit/Q-Branch.git] / lib / SQL / Abstract / ExtraClauses.pm
index ad49758..6a27d43 100644 (file)
@@ -98,26 +98,27 @@ sub register_defaults {
     );
   }) for qw(union intersect except);
 
-  foreach my $setop (qw(union intersect except)) {
+  my $setop_expander = sub {
+    my ($self, $setop, $args) = @_;
+    my ($op, $type) = split '_', $setop;
+    +(setop => $self->expand_expr({
+      "-${op}" => {
+        ($type ? (type => $type) : ()),
+        queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
+      }
+    }));
+  };
+
+  $self->clause_expanders(
+    map +($_ => $setop_expander),
+      map "select.${_}",
+        map +($_, "${_}_all", "${_}_distinct"),
+          qw(union intersect except)
+  );
 
-    $self->clause_expander("select.${setop}" => sub {
-      +(setop => $_[0]->expand_expr({
-                   "-${setop}" => {
-                     queries => (ref($_[2]) eq 'ARRAY' ? $_[2] : [ $_[2] ]),
-                   }
-                 }));
-    });
-    $self->clause_expander("select.${setop}_all" => sub {
-      +(setop => $_[0]->expand_expr({
-                   "-${setop}" => {
-                     type => 'all',
-                     queries => (ref($_[2]) eq 'ARRAY' ? $_[2] : [ $_[2] ]),
-                   }
-                 }));
-    });
-  }
   $self->clause_expander('select.with' => my $with_expander = sub {
-    my ($self, undef, $with) = @_;
+    my ($self, $name, $with) = @_;
+    my (undef, $type) = split '_', $name;
     if (ref($with) eq 'HASH') {
       return +{
         %$with,
@@ -134,16 +135,9 @@ sub register_defaults {
         $self->expand_expr($query)
       ];
     }
-    return +{ queries => \@exp };
-  });
-  $self->clause_expander('select.with_recursive' => sub {
-    my ($self, undef, $with) = @_;
-    my $exp = $self->$with_expander(undef, $with);
-    return +(with => +{
-      %$exp,
-      type => 'recursive'
-    });
+    return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
   });
+  $self->clause_expander('select.with_recursive', $with_expander);
   $self->clause_renderer('select.with' => sub {
     my ($self, undef, $with) = @_;
     my $q_part = [ $self->join_clauses(', ',