From: Matt S Trout Date: Sun, 31 Jul 2011 19:39:06 +0000 (+0000) Subject: INSERT support X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a5c0a2d5f87261b93baa621b1af4b805bffa47b2;p=dbsrgits%2FData-Query.git INSERT support --- diff --git a/lib/Data/Query/Constants.pm b/lib/Data/Query/Constants.pm index 37c4443..7b564c7 100644 --- a/lib/Data/Query/Constants.pm +++ b/lib/Data/Query/Constants.pm @@ -16,6 +16,7 @@ use constant +{ DQ_WHERE => 'Where', DQ_DELETE => 'Delete', DQ_UPDATE => 'Update', + DQ_INSERT => 'Insert', )) }; diff --git a/lib/Data/Query/Renderer/SQL/Naive.pm b/lib/Data/Query/Renderer/SQL/Naive.pm index a3e681c..92b1617 100644 --- a/lib/Data/Query/Renderer/SQL/Naive.pm +++ b/lib/Data/Query/Renderer/SQL/Naive.pm @@ -108,9 +108,7 @@ sub _render_identifier { } sub _render_value { - defined($_[1]->{value}) - ? [ '?', $_[1] ] - : [ 'NULL' ]; + [ '?', $_[1] ] } sub _operator_type { 'SQL.Naive' } @@ -373,6 +371,13 @@ sub _render_delete { sub _render_update { my ($self, $dq) = @_; my ($target, $set, $where) = @{$dq}{qw(target set where)}; + unless ($set) { + die "Must have set key - names+value keys not yet tested"; + my ($names, $value) = @{$dq}{qw(names value)}; + die "Must have names and value or set" unless $names and $value; + die "names and value must be same size" unless @$names == @$value; + $set = [ map [ $names->[$_], $value->[$_] ], 0..$#$names ]; + } my @rendered_set = intersperse( ',', map [ intersperse('=', map $self->_render($_), @$_) ], @{$set} ); @@ -386,4 +391,28 @@ sub _render_update { ]; } +sub _render_insert { + my ($self, $dq) = @_; + my ($target, $names, $values, $returning) + = @{$dq}{qw(target names values returning)}; + unless ($values) { + die "Must have values key - sets key not yet implemented"; + } + [ $self->_format_keyword('INSERT INTO'), + $self->_render($target), + ($names + ? ('(', intersperse(',', map $self->_render($_), @$names), ')') + : ()), + $self->_format_keyword('VALUES'), + intersperse(',', + map [ '(', intersperse(',', map $self->_render($_), @$_), ')' ], + @$values + ), + ($returning + ? ($self->_format_keyword('RETURNING'), + intersperse(',', map $self->_render($_), @$returning)) + : ()), + ]; +} + 1;