{-# language FlexibleContexts #-}
{-# language MonoLocalBinds #-}
module Rel8.Query.Aggregate
( aggregate
, countRows
)
where
import Data.Int ( Int64 )
import Prelude
import qualified Opaleye.Aggregate as Opaleye
import Rel8.Aggregate ( Aggregates )
import Rel8.Expr ( Expr )
import Rel8.Expr.Aggregate ( countStar )
import Rel8.Query ( Query )
import Rel8.Query.Maybe ( optional )
import Rel8.Query.Opaleye ( mapOpaleye )
import Rel8.Table.Opaleye ( aggregator )
import Rel8.Table.Maybe ( maybeTable )
aggregate :: Aggregates aggregates exprs => Query aggregates -> Query exprs
aggregate :: Query aggregates -> Query exprs
aggregate = (Select aggregates -> Select exprs)
-> Query aggregates -> Query exprs
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye (Aggregator aggregates exprs -> Select aggregates -> Select exprs
forall a b. Aggregator a b -> Select a -> Select b
Opaleye.aggregate Aggregator aggregates exprs
forall aggregates exprs.
Aggregates aggregates exprs =>
Aggregator aggregates exprs
aggregator)
countRows :: Query a -> Query (Expr Int64)
countRows :: Query a -> Query (Expr Int64)
countRows = (MaybeTable (Expr Int64) -> Expr Int64)
-> Query (MaybeTable (Expr Int64)) -> Query (Expr Int64)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Expr Int64
-> (Expr Int64 -> Expr Int64)
-> MaybeTable (Expr Int64)
-> Expr Int64
forall b a. Table Expr b => b -> (a -> b) -> MaybeTable a -> b
maybeTable Expr Int64
0 Expr Int64 -> Expr Int64
forall a. a -> a
id) (Query (MaybeTable (Expr Int64)) -> Query (Expr Int64))
-> (Query a -> Query (MaybeTable (Expr Int64)))
-> Query a
-> Query (Expr Int64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query (Expr Int64) -> Query (MaybeTable (Expr Int64))
forall a. Query a -> Query (MaybeTable a)
optional (Query (Expr Int64) -> Query (MaybeTable (Expr Int64)))
-> (Query a -> Query (Expr Int64))
-> Query a
-> Query (MaybeTable (Expr Int64))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query (Aggregate Int64) -> Query (Expr Int64)
forall aggregates exprs.
Aggregates aggregates exprs =>
Query aggregates -> Query exprs
aggregate (Query (Aggregate Int64) -> Query (Expr Int64))
-> (Query a -> Query (Aggregate Int64))
-> Query a
-> Query (Expr Int64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Aggregate Int64) -> Query a -> Query (Aggregate Int64)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Aggregate Int64 -> a -> Aggregate Int64
forall a b. a -> b -> a
const Aggregate Int64
countStar)