on master help sign in
Add countRows
commit5b5d29f89c32aa2390b0d8ce268274c78f06cfb3 authorShane O'Brien <shane.obrien@circuithub.com> dateMon 29 Mar 2021, 12:33 (6 years ago) parentbde3d54
2 files changed+12−1
src/Rel8.hs+1 src/Rel8/Query/Aggregate.hs+11−1
src/Rel8.hs+1
@@ -183,6 +183,7 @@ module Rel8
183183 -- ** Aggregation
184184 , Aggregate
185185 , aggregate
186+ , countRows
186187 , listAgg, listAggExpr
187188 , nonEmptyAgg, nonEmptyAggExpr
188189 , groupBy
src/Rel8/Query/Aggregate.hs+11−1
@@ -2,22 +2,32 @@
22
33module Rel8.Query.Aggregate
44 ( aggregate
5+ , countRows
56 )
67where
78
89-- base
9−import Prelude ()
10+import Data.Int ( Int64 )
11+import Prelude
1012
1113-- opaleye
1214import qualified Opaleye.Aggregate as Opaleye
1315
1416-- rel8
1517import Rel8.Aggregate ( Aggregate )
18+import Rel8.Expr ( Expr )
19+import Rel8.Expr.Aggregate ( countStar )
1620import Rel8.Query ( Query )
21+import Rel8.Query.Maybe ( optional )
1722import Rel8.Query.Opaleye ( mapOpaleye )
1823import Rel8.Table.Opaleye ( aggregator )
24+import Rel8.Table.Maybe ( maybeTable )
1925
2026
2127-- | Apply an aggregation to all rows returned by a 'Query'.
2228aggregate :: Query (Aggregate exprs) -> Query exprs
2329aggregate = mapOpaleye (Opaleye.aggregate aggregator)
30+
31+
32+countRows :: Query a -> Query (Expr Int64)
33+countRows = fmap (maybeTable 0 id) . optional . aggregate . fmap (const countStar)