From: Matt S Trout Date: Mon, 15 Apr 2019 01:27:04 +0000 (+0000) Subject: better extension api X-Git-Tag: v2.000000~3^2~234 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=71c1b4d5f33585157d347f79f15f7658e1f71f4e;p=dbsrgits%2FSQL-Abstract.git better extension api --- diff --git a/lib/SQL/Abstract/Clauses.pm b/lib/SQL/Abstract/Clauses.pm index 8fb800d..877117d 100644 --- a/lib/SQL/Abstract/Clauses.pm +++ b/lib/SQL/Abstract/Clauses.pm @@ -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: $@"; } } diff --git a/lib/SQL/Abstract/ExtraClauses.pm b/lib/SQL/Abstract/ExtraClauses.pm index 1007e4a..5e2e9a1 100644 --- a/lib/SQL/Abstract/ExtraClauses.pm +++ b/lib/SQL/Abstract/ExtraClauses.pm @@ -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; } diff --git a/xt/clauses.t b/xt/clauses.t index 80532c0..7fed67f 100644 --- a/xt/clauses.t +++ b/xt/clauses.t @@ -143,7 +143,6 @@ is_same_sql( ); { - my $sqlac = $sqlac->clone ->clauses_of( select => (