on 269974e help sign in
rel8/src/Rel8/Query/Aggregate.hs
37 lines 1.0 kB history raw
1{-# language FlexibleContexts #-}
2{-# language MonoLocalBinds #-}
4module Rel8.Query.Aggregate
5 ( aggregate
6 , countRows
7 )
8where
10-- base
11import Data.Int ( Int64 )
12import Prelude
14-- opaleye
15import qualified Opaleye.Aggregate as Opaleye
17-- rel8
18import Rel8.Aggregate ( Aggregates )
19import Rel8.Expr ( Expr )
20import Rel8.Expr.Aggregate ( countStar )
21import Rel8.Query ( Query )
22import Rel8.Query.Maybe ( optional )
23import Rel8.Query.Opaleye ( mapOpaleye )
24import Rel8.Table.Opaleye ( aggregator )
25import Rel8.Table.Maybe ( maybeTable )
28-- | Apply an aggregation to all rows returned by a 'Query'.
29aggregate :: Aggregates aggregates exprs => Query aggregates -> Query exprs
30aggregate = mapOpaleye (Opaleye.aggregate aggregator)
33-- | Count the number of rows returned by a query. Note that this is different
34-- from @countStar@, as even if the given query yields no rows, @countRows@
35-- will return @0@.
36countRows :: Query a -> Query (Expr Int64)
37countRows = fmap (maybeTable 0 id) . optional . aggregate . fmap (const countStar)