better extension api
Matt S Trout [Mon, 15 Apr 2019 01:27:04 +0000 (01:27 +0000)]
lib/SQL/Abstract/Clauses.pm
lib/SQL/Abstract/ExtraClauses.pm
xt/clauses.t

index 8fb800d..877117d 100644 (file)
@@ -284,8 +284,16 @@ BEGIN {
     expand op_expand render op_renderer clause_expand clause_render
   )) {
     my $key = join '_', reverse split '_', $type;
-    eval qq{sub ${type}er { shift->_ext_rw($key => \@_) }; 1 }
-      or die "Method builder failed: $@";
+    my $singular = "${type}er";
+    eval qq{sub ${singular} { shift->_ext_rw($key => \@_) }; 1 }
+      or die "Method builder failed for ${type}er: $@";
+    eval qq{sub ${singular}s {
+      my (\$self, \@args) = \@_;
+      while (my (\$this_key, \$this_value) = splice(\@args, 0, 2)) {
+        \$self->{${key}}{\$this_key} = \$this_value;
+      }
+      return \$self;
+    }; 1 } or die "Method builder failed for ${singular}s: $@";
   }
 }
 
index 1007e4a..5e2e9a1 100644 (file)
@@ -14,12 +14,12 @@ sub register_defaults {
   $self->clauses_of(
     select => $self->clauses_of('select'), qw(group_by having)
   );
-  $self->clause_expander('select.group_by', sub {
-    $_[0]->_expand_maybe_list_expr($_[1], -ident)
-  });
-  $self->clause_expander('select.having', sub {
-    $_[0]->expand_expr($_[1])
-  });
+  $self->clause_expanders(
+    'select.group_by', sub {
+      $_[0]->_expand_maybe_list_expr($_[1], -ident)
+    },
+    'select.having', 'expand_expr',
+  );
   $self->${\"${_}er"}(from_list => "_${_}_from_list")
     for qw(expand render);
   $self->{expand}{join} = '_expand_join';
@@ -27,18 +27,21 @@ sub register_defaults {
   $self->{expand_op}{as} = '_expand_op_as';
   $self->{expand}{as} = '_expand_op_as';
   $self->{render}{as} = '_render_as';
+
   splice(@{$self->{clauses_of}{update}}, 2, 0, 'from');
   splice(@{$self->{clauses_of}{delete}}, 1, 0, 'using');
-  $self->{expand_clause}{'update.from'} = '_expand_select_clause_from';
-  $self->{expand_clause}{'delete.using'} = sub {
-    +(using => $_[0]->_expand_from_list(undef, $_[1]));
-  };
-  $self->{expand_clause}{'insert.rowvalues'} = sub {
-    (from => $_[0]->expand_expr({ -values => $_[1] }));
-  };
-  $self->{expand_clause}{'insert.select'} = sub {
-    (from => $_[0]->expand_expr({ -select => $_[1] }));
-  };
+  $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;
 }
 
index 80532c0..7fed67f 100644 (file)
@@ -143,7 +143,6 @@ is_same_sql(
 );
 
 {
-
   my $sqlac = $sqlac->clone
                     ->clauses_of(
                         select => (