From: Matt S Trout Date: Sat, 14 Apr 2012 17:10:39 +0000 (+0000) Subject: initial group by support X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a10423885c9f7f4f6238171dc4326cb94892f914;p=dbsrgits%2FData-Query.git initial group by support --- diff --git a/lib/Data/Query/Constants.pm b/lib/Data/Query/Constants.pm index 7b564c7..2aa9100 100644 --- a/lib/Data/Query/Constants.pm +++ b/lib/Data/Query/Constants.pm @@ -17,6 +17,7 @@ use constant +{ DQ_DELETE => 'Delete', DQ_UPDATE => 'Update', DQ_INSERT => 'Insert', + DQ_GROUP => 'Group', )) }; diff --git a/lib/Data/Query/Renderer/SQL/Naive.pm b/lib/Data/Query/Renderer/SQL/Naive.pm index 6cfce82..96ca1f8 100644 --- a/lib/Data/Query/Renderer/SQL/Naive.pm +++ b/lib/Data/Query/Renderer/SQL/Naive.pm @@ -7,6 +7,7 @@ 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 + DQ_GROUP ); use Moo; @@ -331,9 +332,10 @@ sub _render_join { sub _render_where { my ($self, $dq) = @_; my ($from, $where) = @{$dq}{qw(from where)}; + my $keyword = ($from && $from->{type} eq DQ_GROUP) ? 'HAVING' : 'WHERE'; [ ($from ? $self->_render($from) : ()), - $self->_format_keyword('WHERE'), + $self->_format_keyword($keyword), $self->_render($where) ] } @@ -359,6 +361,18 @@ sub _render_order { \@ret; } +sub _render_group { + my ($self, $dq) = @_; + # this could also squash like order does. but I dunno whether that should + # move somewhere else just yet. + my @ret = ( + ($dq->{from} ? $self->_render($dq->{from}) : ()), + $self->_format_keyword('GROUP BY'), + intersperse(',', map $self->_render($_), @{$dq->{by}}) + ); + \@ret; +} + sub _render_delete { my ($self, $dq) = @_; my ($target, $where) = @{$dq}{qw(target where)};