From: Matt S Trout Date: Sun, 31 Jul 2011 15:03:46 +0000 (+0000) Subject: refactor DELETE and add UPDATE X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=87867dadad5c03bf5c9cedb8370175b289181932;p=dbsrgits%2FData-Query.git refactor DELETE and add UPDATE --- diff --git a/lib/Data/Query/Constants.pm b/lib/Data/Query/Constants.pm index 0850fbb..37c4443 100644 --- a/lib/Data/Query/Constants.pm +++ b/lib/Data/Query/Constants.pm @@ -15,6 +15,7 @@ use constant +{ DQ_ORDER => 'Order', DQ_WHERE => 'Where', DQ_DELETE => 'Delete', + DQ_UPDATE => 'Update', )) }; diff --git a/lib/Data/Query/Renderer/SQL/Naive.pm b/lib/Data/Query/Renderer/SQL/Naive.pm index 6c2375a..a3e681c 100644 --- a/lib/Data/Query/Renderer/SQL/Naive.pm +++ b/lib/Data/Query/Renderer/SQL/Naive.pm @@ -1,6 +1,9 @@ package Data::Query::Renderer::SQL::Naive; use strictures 1; + +sub intersperse { my $i = shift; my @i = map +($_, $i), @_; pop @i; @i } + use SQL::ReservedWords; use Data::Query::Constants qw( DQ_IDENTIFIER DQ_OPERATOR DQ_VALUE DQ_JOIN DQ_ALIAS DQ_ORDER DQ_LITERAL @@ -358,7 +361,29 @@ sub _render_order { sub _render_delete { my ($self, $dq) = @_; - [ $self->_format_keyword('DELETE FROM'), @{$self->_render($dq->{from})} ]; + my ($target, $where) = @{$dq}{qw(target where)}; + [ $self->_format_keyword('DELETE FROM'), + $self->_render($target), + ($where + ? ($self->_format_keyword('WHERE'), $self->_render($where)) + : ()) + ]; +} + +sub _render_update { + my ($self, $dq) = @_; + my ($target, $set, $where) = @{$dq}{qw(target set where)}; + my @rendered_set = intersperse( + ',', map [ intersperse('=', map $self->_render($_), @$_) ], @{$set} + ); + [ $self->_format_keyword('UPDATE'), + $self->_render($target), + $self->_format_keyword('SET'), + @rendered_set, + ($where + ? ($self->_format_keyword('WHERE'), $self->_render($where)) + : ()) + ]; } 1;