From 5d5c2393b69f0be505db8f725f7fcb1a4770c64c Mon Sep 17 00:00:00 2001 From: Joe Nelson Date: Tue, 5 Aug 2014 12:28:40 -0700 Subject: [PATCH] Adjust total for active filter Including total = 0 --- PgQuery.hs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/PgQuery.hs b/PgQuery.hs index 9be82ff1f..3712eef41 100644 --- a/PgQuery.hs +++ b/PgQuery.hs @@ -32,7 +32,7 @@ type QuotedSql = (String, [SqlValue]) getRows :: String -> String -> Net.Query -> Maybe R.NonnegRange -> Connection -> IO RangedResult getRows schema table qq range conn = do query <- populateSql conn - $ globalAndLimitedCounts schema table <> + $ globalAndLimitedCounts schema table qq <> jsonArrayRows (selectStarClause schema table <> whereClause qq @@ -40,6 +40,7 @@ getRows schema table qq range conn = do r <- quickQuery conn query [] return $ case r of + [[_, _, SqlNull]] -> RangedResult 0 0 0 "" [[total, limited_total, json]] -> RangedResult offset (offset + fromSql limited_total - 1) (fromSql total) (fromSql json) @@ -81,9 +82,12 @@ limitClause range = limit = fromMaybe "ALL" $ show <$> (R.limit =<< range) offset = fromMaybe 0 $ R.offset <$> range -globalAndLimitedCounts :: String -> String -> QuotedSql -globalAndLimitedCounts schema table = - (" select (select count(1) from %I.%I), count(t), ", map toSql [schema, table]) +globalAndLimitedCounts :: String -> String -> Net.Query -> QuotedSql +globalAndLimitedCounts schema table qq = + (" select ", []) + <> ("(select count(1) from %I.%I ", map toSql [schema, table]) + <> whereClause qq + <> ("), count(t), ", []) selectStarClause :: String -> String -> QuotedSql selectStarClause schema table =