From: Matt S Trout Date: Sun, 14 Apr 2019 02:36:20 +0000 (+0000) Subject: sugar for select and multi-row insert X-Git-Tag: v2.000000~3^2~243 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bea59460f3bd2ca184e1a2e8652e39ad2ca341f2;p=dbsrgits%2FSQL-Abstract.git sugar for select and multi-row insert --- diff --git a/lib/SQL/Abstract/Clauses.pm b/lib/SQL/Abstract/Clauses.pm index 686f6e3..86f74b9 100644 --- a/lib/SQL/Abstract/Clauses.pm +++ b/lib/SQL/Abstract/Clauses.pm @@ -42,7 +42,7 @@ sub register_defaults { ($self->_sqlcase('delete from ').$sql, @bind); }; $self->{clauses_of}{insert} = [ - 'target', 'fields', 'values', 'returning' + 'target', 'fields', 'from', 'returning' ]; $self->{expand}{insert} = sub { shift->_expand_statement(@_) }; $self->{render}{insert} = sub { shift->_render_statement(insert => @_) }; @@ -66,7 +66,7 @@ sub register_defaults { my ($sql, @bind) = $self->render_aqt($from); ($self->_sqlcase('insert into ').$sql, @bind); }; - $self->{render_clause}{'insert.values'} = sub { + $self->{render_clause}{'insert.from'} = sub { return $_[0]->render_aqt($_[1]); }; return $self; @@ -133,7 +133,7 @@ sub _expand_statement { @exp } } else { - ($_ => $val) + ($_ => $self->expand_expr($val)) } } sort keys %$args } }; @@ -221,7 +221,7 @@ sub _expand_insert_clause_values { } return $data if ref($data) eq 'HASH' and $data->{-row}; my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data); - return (values => { -values => $v_aqt }, ($f_aqt ? (fields => $f_aqt) : ())); + return (from => { -values => $v_aqt }, ($f_aqt ? (fields => $f_aqt) : ())); } 1; diff --git a/lib/SQL/Abstract/ExtraClauses.pm b/lib/SQL/Abstract/ExtraClauses.pm index b874be5..e668de8 100644 --- a/lib/SQL/Abstract/ExtraClauses.pm +++ b/lib/SQL/Abstract/ExtraClauses.pm @@ -33,6 +33,12 @@ sub new { $new->{expand_clause}{'delete.using'} = sub { +(using => $_[0]->_expand_from_list(undef, $_[1])); }; + $new->{expand_clause}{'insert.rowvalues'} = sub { + (from => $_[0]->expand_expr({ -values => $_[1] })); + }; + $new->{expand_clause}{'insert.select'} = sub { + (from => $_[0]->expand_expr({ -select => $_[1] })); + }; return $new; } diff --git a/xt/clauses.t b/xt/clauses.t index d47e113..4e934f8 100644 --- a/xt/clauses.t +++ b/xt/clauses.t @@ -99,9 +99,20 @@ is_same_sql( is_same_sql( $sqlac->insert({ into => 'foo', - values => { -select => { select => '*', from => 'bar' } } + select => { select => '*', from => 'bar' } }), q{INSERT INTO foo SELECT * FROM bar} ); +($sql, @bind) = $sqlac->insert({ + into => 'eh', + rowvalues => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +}); + +is_same_sql_bind( + $sql, \@bind, + q{INSERT INTO eh VALUES (?, ?), (?, ?), (?, ?)}, + [ 1..6 ], +); + done_testing;