From: Matt S Trout Date: Mon, 15 Apr 2019 00:18:09 +0000 (+0000) Subject: initial extension api X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=345700930861933507c595d05a5a0995a7356b0b;p=scpubgit%2FQ-Branch.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 => '*',