From: Matt S Trout Date: Sat, 25 Aug 2012 18:47:08 +0000 (+0000) Subject: fixups for HAVING clauses X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5a0586236846b07ec90b3ea1e28f8a6f7c0fa8bf;p=dbsrgits%2FData-Query.git fixups for HAVING clauses --- diff --git a/lib/Data/Query/ExprHelpers.pm b/lib/Data/Query/ExprHelpers.pm index ad1f529..6020be6 100644 --- a/lib/Data/Query/ExprHelpers.pm +++ b/lib/Data/Query/ExprHelpers.pm @@ -68,7 +68,7 @@ foreach my $name (values %Data::Query::Constants::CONST) { my $sub = "is_${name}"; *$sub = sub { my $dq = $_[0]||$_; - $dq->{type} eq $name + $dq->{type} and $dq->{type} eq $name }; push @EXPORT, $sub; if ($map{$name}) { @@ -84,6 +84,10 @@ foreach my $name (values %Data::Query::Constants::CONST) { } } +sub is_Having { is_Where($_[0]) and is_Group($_[0]->{from}) } + +push @EXPORT, 'is_Having'; + sub compose (&@) { my $code = shift; require Scalar::Util; diff --git a/lib/Data/Query/Renderer/SQL/Naive.pm b/lib/Data/Query/Renderer/SQL/Naive.pm index 96abe9c..492323b 100644 --- a/lib/Data/Query/Renderer/SQL/Naive.pm +++ b/lib/Data/Query/Renderer/SQL/Naive.pm @@ -144,7 +144,7 @@ sub _render_operator { sub _maybe_parenthesise { my ($self, $dq) = @_; for ($dq) { - return is_Select() || is_Group() || is_Slice() + return is_Select() || is_Group() || is_Slice() || is_Having() ? [ '(', $self->_render($dq), ')' ] : $self->_render($dq); } diff --git a/lib/Data/Query/Renderer/SQL/Slice/GenericSubquery.pm b/lib/Data/Query/Renderer/SQL/Slice/GenericSubquery.pm index c637729..4caeabf 100644 --- a/lib/Data/Query/Renderer/SQL/Slice/GenericSubquery.pm +++ b/lib/Data/Query/Renderer/SQL/Slice/GenericSubquery.pm @@ -11,6 +11,8 @@ sub _render_slice { unless is_Select my $orig_select = $dq->{from}; my %remapped = $self->_subquery_remap($orig_select); my $first_from = $remapped{inner_body}; + # Should we simply strip until we reach a join/alias/etc. here? + $first_from = $first_from->{from}{from} if is_Having($first_from); $first_from = $first_from->{from} if is_Where($first_from); while (is_Join $first_from) { $first_from = $first_from->{left};