From cb5d2b7c68f67804027d6fca697ab72f15525804 Mon Sep 17 00:00:00 2001
From: miker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Fri, 22 Apr 2011 21:34:56 +0000
Subject: [PATCH] address the empty-query case

git-svn-id: svn://svn.open-ils.org/ILS/trunk@20295 dcc99617-32d9-48b4-a31d-7c20da2025e4
---
 .../lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm     | 9 ++++++++-
 .../src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm  | 2 +-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm
index 191542434c..b858fd55bd 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm
@@ -585,6 +585,13 @@ sub toSQL {
 
     my $core_limit = $self->QueryParser->core_limit || 25000;
 
+    my $flat_where = $$flat_plan{where};
+    if ($flat_where eq '()') {
+        $flat_where = '';
+    } else {
+        $flat_where = "AND $flat_where";
+    }
+
     my $sql = <<SQL;
 SELECT  $key AS id,
         ARRAY_ACCUM(DISTINCT m.source) AS records,
@@ -600,7 +607,7 @@ SELECT  $key AS id,
         $during
         $between
         $combined_dyn_filters
-        AND $$flat_plan{where}
+        $flat_where
   GROUP BY 1
   ORDER BY 4 $desc NULLS LAST, 5 DESC NULLS LAST, 3 DESC
   LIMIT $core_limit
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm
index 479bd40198..1b3a37f5fd 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm
@@ -685,7 +685,7 @@ sub decompose {
 
     }
 
-    $struct = undef if (scalar(@{$struct->query_nodes}) == 0);
+    $struct = undef if (scalar(@{$struct->query_nodes}) == 0 && !$struct->top_plan);
 
     return $struct if !wantarray;
     return ($struct, $remainder);
-- 
2.11.0