From: Matt S Trout Date: Sat, 13 Apr 2019 18:39:27 +0000 (+0000) Subject: make INSERT SELECT work X-Git-Tag: v1.90_01~223 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f8d50921370b4bbdf550737442f9fa9e8b4e9d8d;p=dbsrgits%2FSQL-Abstract.git make INSERT SELECT work --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 8fbd784..cd79541 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -1054,9 +1054,9 @@ sub _expand_values { return { -values => [ map +( ref($_) eq 'HASH' - ? $_ + ? $self->expand_expr($_) : +{ -row => [ map $self->expand_expr($_), @$_ ] } - ), @$values + ), ref($values) eq 'ARRAY' ? @$values : $values ] }; } diff --git a/lib/SQL/Abstract/Clauses.pm b/lib/SQL/Abstract/Clauses.pm index b12dbca..686f6e3 100644 --- a/lib/SQL/Abstract/Clauses.pm +++ b/lib/SQL/Abstract/Clauses.pm @@ -66,6 +66,9 @@ sub register_defaults { my ($sql, @bind) = $self->render_aqt($from); ($self->_sqlcase('insert into ').$sql, @bind); }; + $self->{render_clause}{'insert.values'} = sub { + return $_[0]->render_aqt($_[1]); + }; return $self; } @@ -213,9 +216,12 @@ sub _expand_insert_clause_target { sub _expand_insert_clause_values { my ($self, $data) = @_; + if (ref($data) eq 'HASH' and (keys(%$data))[0] =~ /^-/) { + return $self->expand_expr($data); + } return $data if ref($data) eq 'HASH' and $data->{-row}; my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data); - return (values => $v_aqt, ($f_aqt ? (fields => $f_aqt) : ())); + return (values => { -values => $v_aqt }, ($f_aqt ? (fields => $f_aqt) : ())); } 1; diff --git a/xt/clauses.t b/xt/clauses.t index 8b7427f..d47e113 100644 --- a/xt/clauses.t +++ b/xt/clauses.t @@ -96,4 +96,12 @@ is_same_sql( q{SELECT x.* FROM (SELECT * FROM y) AS x}, ); +is_same_sql( + $sqlac->insert({ + into => 'foo', + values => { -select => { select => '*', from => 'bar' } } + }), + q{INSERT INTO foo SELECT * FROM bar} +); + done_testing;