From: Matt S Trout Date: Mon, 15 Apr 2019 00:18:09 +0000 (+0000) Subject: initial extension api X-Git-Tag: v2.000000~3^2~238 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8637f8699bd222b15f74bf6afa97c1af4330db39;p=dbsrgits%2FSQL-Abstract.git initial extension api --- diff --git a/lib/SQL/Abstract/Clauses.pm b/lib/SQL/Abstract/Clauses.pm index 1b9bfd5..07d1264 100644 --- a/lib/SQL/Abstract/Clauses.pm +++ b/lib/SQL/Abstract/Clauses.pm @@ -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; diff --git a/xt/clauses.t b/xt/clauses.t index e6268ea..80532c0 100644 --- a/xt/clauses.t +++ b/xt/clauses.t @@ -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 => '*',