initial extension api
Matt S Trout [Mon, 15 Apr 2019 00:18:09 +0000 (00:18 +0000)]
lib/SQL/Abstract/Clauses.pm
xt/clauses.t

index 1b9bfd5..07d1264 100644 (file)
@@ -272,5 +272,42 @@ sub _render_values {
   );
 }
 
+sub _ext_rw {
+  my ($self, $name, $key, $value) = @_;
+  return $self->{$name}{$key} unless @_ > 2;
+  $self->{$name}{$key} = $value;
+  return $self;
+}
+
+sub expander { shift->_ext_rw(expand => @_) }
+sub op_expander { shift->_ext_rw(expand_op => @_) }
+sub renderer { shift->_ext_rw(render => @_) }
+sub op_renderer { shift->_ext_rw(expand_op => @_) }
+sub clause_expander { shift->_ext_rw(expand_cluse => @_) }
+sub clause_renderer { shift->_ext_rw(render_clause => @_) }
+
+sub clauses_of {
+  my ($self, $of, @clauses) = @_;
+  unless (@clauses) {
+    return @{$self->{clauses_of}{$of}||[]};
+  }
+  $self->{clauses_of}{$of} = \@clauses;
+  return $self;
+}
+
+sub clone {
+  my ($self) = @_;
+  bless(
+    {
+      (map +($_ => (
+        ref($self->{$_}) eq 'HASH'
+          ? { %{$self->{$_}} }
+          : $self->{$_}
+      )), keys %$self),
+    },
+    ref($self)
+  );
+}
+
 
 1;
index e6268ea..80532c0 100644 (file)
@@ -143,9 +143,14 @@ is_same_sql(
 );
 
 {
-  local $sqlac->{clauses_of}{select} = [
-    @{$sqlac->{clauses_of}{select}}, qw(limit offset)
-  ];
+
+  my $sqlac = $sqlac->clone
+                    ->clauses_of(
+                        select => (
+                          $sqlac->clauses_of('select'),
+                          qw(limit offset),
+                        )
+                      );
 
   ($sql, @bind) = $sqlac->select({
     select => '*',