From: Matt S Trout Date: Sat, 13 Apr 2019 02:54:04 +0000 (+0000) Subject: add, expand, and use in from, VALUES X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4a2c263bbfb6a7a2ac842fea2d3495d7ddfbbab1;p=scpubgit%2FQ-Branch.git add, expand, and use in from, VALUES --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 6025daf..8fbd784 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -209,6 +209,7 @@ sub new { ident => '_expand_ident', value => '_expand_value', func => '_expand_func', + values => '_expand_values', }; $opt{expand_op} = { @@ -1048,6 +1049,17 @@ sub _expand_bind { return { -bind => $bind }; } +sub _expand_values { + my ($self, undef, $values) = @_; + return { -values => [ + map +( + ref($_) eq 'HASH' + ? $_ + : +{ -row => [ map $self->expand_expr($_), @$_ ] } + ), @$values + ] }; +} + sub _recurse_where { my ($self, $where, $logic) = @_; diff --git a/lib/SQL/Abstract/ExtraClauses.pm b/lib/SQL/Abstract/ExtraClauses.pm index b6f83f2..78680da 100644 --- a/lib/SQL/Abstract/ExtraClauses.pm +++ b/lib/SQL/Abstract/ExtraClauses.pm @@ -94,7 +94,9 @@ sub _render_join { .'join' ) ], - [ $self->render_aqt($args->{to}) ], + [ $self->render_aqt( + map +($_->{-ident} or $_->{-as} ? $_ : { -row => [ $_ ] }), $args->{to} + ) ], ($args->{on} ? ( [ $self->_sqlcase('on') ], [ $self->render_aqt($args->{on}) ], @@ -119,12 +121,22 @@ sub _render_as { my ($thing, $as, @cols) = @$args; return $self->_join_parts( ' ', - [ $self->render_aqt($thing) ], + [ $self->render_aqt( + map +($_->{-ident} ? $_ : { -row => [ $_ ] }), $thing + ) ], [ $self->_sqlcase('as') ], - [ @cols - ? $self->render_aqt({ -func => [ $as, @cols ] }) - : $self->render_aqt($as) - ], + (@cols + ? [ $self->_join_parts('', + [ $self->render_aqt($as) ], + [ '(' ], + [ $self->_join_parts( + ', ', + map [ $self->render_aqt($_) ], @cols + ) ], + [ ')' ], + ) ] + : [ $self->render_aqt($as) ] + ), ); } diff --git a/xt/clauses.t b/xt/clauses.t index 4fabc98..5546d86 100644 --- a/xt/clauses.t +++ b/xt/clauses.t @@ -1,7 +1,7 @@ use strict; use warnings; use Test::More; -use SQL::Abstract::Test import => [ qw(is_same_sql_bind) ]; +use SQL::Abstract::Test import => [ qw(is_same_sql_bind is_same_sql) ]; use SQL::Abstract::ExtraClauses; my $sqlac = SQL::Abstract::ExtraClauses->new; @@ -31,4 +31,11 @@ is_same_sql_bind( [ [ 'Rock' ], 3 ] ); +($sql) = $sqlac->select({ + select => [ 'a' ], + from => [ { -values => [ [ 1, 2 ], [ 3, 4 ] ] }, -as => [ qw(t a b) ] ], +}); + +is_same_sql($sql, q{SELECT a FROM (VALUES (1, 2), (3, 4)) AS t(a,b)}); + done_testing;