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