X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FAbstract%2FExtraClauses.pm;h=8ff2b748759567fff29d521bd00574ecb2cda214;hb=583c795782989e08ee7fabdb9626c93428131e1e;hp=22817ce3125df3adb2e43c8fbcbc3150f46898bb;hpb=9a20a32d6450e2f88fb51e61e83f1db3f7d8a159;p=scpubgit%2FQ-Branch.git diff --git a/lib/SQL/Abstract/ExtraClauses.pm b/lib/SQL/Abstract/ExtraClauses.pm index 22817ce..8ff2b74 100644 --- a/lib/SQL/Abstract/ExtraClauses.pm +++ b/lib/SQL/Abstract/ExtraClauses.pm @@ -5,8 +5,7 @@ use Moo; has sqla => ( is => 'ro', init_arg => undef, handles => [ qw( - expand_expr expand_maybe_list_expr render_aqt - format_keyword join_query_parts + expand_expr expand_maybe_list_expr render_aqt join_query_parts ) ], ); @@ -42,8 +41,7 @@ sub apply_to { $sqla->expander($thing => $self->cb("_expand_${thing}")) ->renderer($thing => $self->cb("_render_${thing}")) } - $sqla->op_expander(as => $self->cb('_expand_op_as')); - $sqla->expander(as => $self->cb('_expand_op_as')); + $sqla->binop_expander(as => $self->cb('_expand_op_as')); $sqla->renderer(as => $self->cb('_render_as')); $sqla->expander(alias => $self->cb('_expand_alias')); $sqla->renderer(alias => $self->cb('_render_alias')); @@ -61,7 +59,7 @@ sub apply_to { }); $sqla->clause_expanders( - 'update.from' => $self->cb('_expand_select_clause_from'), + 'update.from' => $self->cb('_expand_from_list'), 'delete.using' => $self->cb('_expand_from_list'), 'insert.rowvalues' => $self->cb(sub { +(from => $_[0]->expand_expr({ -values => $_[2] })); @@ -76,10 +74,9 @@ sub apply_to { $self->cb('_expand_select', $_[0], \@before_setop); }); - $sqla->clause_renderer('select.setop' => $self->cb(sub { - my ($self, undef, $setop) = @_; - $self->render_aqt($setop); - })); + $sqla->clause_renderer( + 'select.setop' => $self->cb(sub { $_[0]->render_aqt($_[2]); }) + ); foreach my $setop (qw(union intersect except)) { $sqla->expander($setop => $self->cb('_expand_setop')); @@ -111,7 +108,7 @@ sub apply_to { $sqla->expander(cast => $self->cb('_expand_cast')); $sqla->clause_expanders( - "select.from", $self->cb('_expand_select_clause_from'), + "select.from", $self->cb('_expand_from_list'), "update.target", $self->cb('_expand_update_clause_target'), "update.update", $self->cb('_expand_update_clause_target'), ); @@ -131,11 +128,6 @@ sub _expand_select { return $exp; } -sub _expand_select_clause_from { - my ($self, undef, $from) = @_; - +(from => $self->_expand_from_list(undef, $from)); -} - sub _expand_from_list { my ($self, undef, $args) = @_; if (ref($args) eq 'HASH') { @@ -194,14 +186,14 @@ sub _render_join { my @parts = ( $args->{from}, - $self->format_keyword(join '_', ($args->{type}||()), 'join'), + { -keyword => join '_', ($args->{type}||()), 'join' }, (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}), ($args->{on} ? ( - $self->format_keyword('on') , + { -keyword => 'on' }, $args->{on}, ) : ()), ($args->{using} ? ( - $self->format_keyword('using'), + { -keyword => 'using' }, '(', $args->{using}, ')', ) : ()), ); @@ -211,7 +203,6 @@ sub _render_join { sub _expand_op_as { my ($self, undef, $vv, $k) = @_; my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv); - $k ||= shift @vv; my $ik = $self->expand_expr($k, -ident); return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] } if @vv == 1 and ref($vv[0]) eq 'HASH'; @@ -226,7 +217,7 @@ sub _render_as { return $self->join_query_parts( ' ', $thing, - $self->format_keyword('as'), + { -keyword => 'as' }, $alias, ); } @@ -307,13 +298,13 @@ sub _render_with { my ($alias, $query) = @$_; $self->join_query_parts(' ', $alias, - $self->format_keyword('as'), + { -keyword => 'as' }, $query, ) } @{$with->{queries}} ); return $self->join_query_parts(' ', - $self->format_keyword(join '_', 'with', ($with->{type}||'')), + { -keyword => join '_', 'with', ($with->{type}||'') }, $q_part, ); } @@ -329,7 +320,7 @@ sub _expand_setop { sub _render_setop { my ($self, $setop, $args) = @_; $self->join_query_parts( - ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ', + { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' }, @{$args->{queries}} ); } @@ -346,3 +337,101 @@ sub _expand_clause_setop { } 1; + +__END__ + +=head1 NAME + +SQL::Abstract::ExtraClauses - new/experimental additions to L + +=head1 SYNOPSIS + + my $sqla = SQL::Abstract->new; + SQL::Abstract::ExtraClauses->apply_to($sqla); + +=head1 METHODS + +=head2 apply_to + +Applies the plugin to an L object. + +=head2 cb + +For plugin authors, creates a callback to call a method on the plugin. + +=head2 sqla + +Available only during plugin callback executions, contains the currently +active L object. + +=head1 NODE TYPES + +=head2 alias + +Represents a table alias. Expands name and column names with ident as default. + + # expr + { -alias => [ 't', 'x', 'y', 'z' ] } + + # aqt + { -alias => [ + { -ident => [ 't' ] }, { -ident => [ 'x' ] }, + { -ident => [ 'y' ] }, { -ident => [ 'z' ] }, + ] } + + # query + t(x, y, z) + [] + +=head2 as + +Represents an sql AS. LHS is expanded with ident as default, RHS is treated +as a list of arguments for the alias node. + + # expr + { foo => { -as => 'bar' } } + + # aqt + { -as => + [ + { -ident => [ 'foo' ] }, + { -alias => [ { -ident => [ 'bar' ] } ] }, + ] + } + + # query + foo AS bar + [] + + # expr + { -as => [ { -select => { _ => 'blah' } }, 't', 'blah' ] } + + # aqt + { -as => [ + { -select => + { select => { -op => [ ',', { -ident => [ 'blah' ] } ] } } + }, + { -alias => [ { -ident => [ 't' ] }, { -ident => [ 'blah' ] } ] }, + ] } + + # query + (SELECT blah) AS t(blah) + [] + +=head2 cast + + # expr + { -cast => [ { -ident => 'birthday' }, 'date' ] } + + # aqt + { -func => [ + 'cast', { + -as => [ { -ident => [ 'birthday' ] }, { -ident => [ 'date' ] } ] + }, + ] } + + # query + CAST(birthday AS date) + [] + +=cut