clauses transforms
[scpubgit/Q-Branch.git] / lib / SQL / Abstract / ExtraClauses.pm
index b874be5..00e4d2a 100644 (file)
@@ -8,32 +8,51 @@ use base qw(SQL::Abstract::Clauses);
 
 BEGIN { *puke = \&SQL::Abstract::puke }
 
-sub new {
-  my ($proto, @args) = @_;
-  my $new = $proto->next::method(@args);
-  $new->{clauses_of}{select} = [
-    @{$new->{clauses_of}{select}}, qw(group_by having)
-  ];
-  $new->{expand_clause}{'select.group_by'} = sub {
-    $_[0]->_expand_maybe_list_expr($_[1], -ident)
-  };
-  $new->{expand_clause}{'select.having'} = sub {
-    $_[0]->expand_expr($_[1])
-  };
-  $new->{expand}{from_list} = '_expand_from_list';
-  $new->{render}{from_list} = '_render_from_list';
-  $new->{expand}{join} = '_expand_join';
-  $new->{render}{join} = '_render_join';
-  $new->{expand_op}{as} = '_expand_op_as';
-  $new->{expand}{as} = '_expand_op_as';
-  $new->{render}{as} = '_render_as';
-  splice(@{$new->{clauses_of}{update}}, 2, 0, 'from');
-  splice(@{$new->{clauses_of}{delete}}, 1, 0, 'using');
-  $new->{expand_clause}{'update.from'} = '_expand_select_clause_from';
-  $new->{expand_clause}{'delete.using'} = sub {
-    +(using => $_[0]->_expand_from_list(undef, $_[1]));
-  };
-  return $new;
+sub register_defaults {
+  my $self = shift;
+  $self->next::method(@_);
+  $self->clauses_of(
+    select => $self->clauses_of('select'), qw(group_by having)
+  );
+  $self->clause_expanders(
+    'select.group_by', sub {
+      $_[0]->_expand_maybe_list_expr($_[1], -ident)
+    },
+    'select.having', 'expand_expr',
+  );
+  foreach my $thing (qw(join from_list)) {
+    $self->expander($thing => "_expand_${thing}")
+         ->renderer($thing => "_render_${thing}")
+  }
+  $self->op_expander(as => '_expand_op_as');
+  $self->expander(as => '_expand_op_as');
+  $self->renderer(as => '_render_as');
+
+  $self->clauses_of(update => sub {
+    my ($self, @clauses) = @_;
+    splice(@clauses, 2, 0, 'from');
+    @clauses;
+  });
+
+  $self->clauses_of(delete => sub {
+    my ($self, @clauses) = @_;
+    splice(@clauses, 1, 0, 'using');
+    @clauses;
+  });
+
+  $self->clause_expanders(
+    'update.from' => '_expand_select_clause_from',
+    'delete.using' => sub {
+      +(using => $_[0]->_expand_from_list(undef, $_[1]));
+    },
+    'insert.rowvalues' => sub {
+      +(from => $_[0]->expand_expr({ -values => $_[1] }));
+    },
+    'insert.select' => sub {
+      +(from => $_[0]->expand_expr({ -select => $_[1] }));
+    },
+  );
+  return $self;
 }
 
 sub _expand_select_clause_from {
@@ -47,7 +66,7 @@ sub _expand_from_list {
     return { -from_list => [ $self->expand_expr($args) ] };
   }
   my @list;
-  my @args = ref($args) ? @$args : ($args);
+  my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
   while (my $entry = shift @args) {
     if (!ref($entry) and $entry =~ /^-(.*)/) {
       if ($1 eq 'as') {
@@ -133,9 +152,7 @@ sub _render_as {
   my ($thing, $as, @cols) = @$args;
   return $self->_join_parts(
     ' ',
-    [ $self->render_aqt(
-        map +($_->{-ident} ? $_ : { -row => [ $_ ] }), $thing
-    ) ],
+    [ $self->render_aqt($thing) ],
     [ $self->_sqlcase('as') ],
     (@cols
       ? [ $self->_join_parts('',