1{-# language DataKinds #-}
2{-# language FlexibleContexts #-}
3{-# language FlexibleInstances #-}
4{-# language MultiParamTypeClasses #-}
5{-# language NamedFieldPuns #-}
6{-# language RankNTypes #-}
7{-# language StandaloneKindSignatures #-}
8{-# language TypeFamilies #-}
9{-# language UndecidableInstances #-}
12 ( Aggregate(..), zipOutputs
13 , Aggregator(..), unsafeMakeAggregate
19import Control.Applicative ( liftA2 )
20import Data.Functor.Identity ( Identity( Identity ) )
21import Data.Kind ( Constraint, Type )
25import qualified Opaleye.Internal.Aggregate as Opaleye
26import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
27import qualified Opaleye.Internal.PackMap as Opaleye
30import Rel8.Expr ( Expr )
31import Rel8.Schema.HTable.Identity ( HIdentity(..) )
32import qualified Rel8.Schema.Kind as K
33import Rel8.Schema.Null ( Sql )
35 ( Table, Columns, Context, fromColumns, toColumns
36 , FromExprs, fromResult, toResult
39import Rel8.Table.Transpose ( Transposes )
40import Rel8.Type ( DBType )
43-- | 'Aggregate' is a special context used by 'Rel8.aggregate'.
44type Aggregate :: K.Context
45newtype Aggregate a = Aggregate (Opaleye.Aggregator () (Expr a))
48instance Sql DBType a => Table Aggregate (Aggregate a) where
49 type Columns (Aggregate a) = HIdentity a
50 type Context (Aggregate a) = Aggregate
51 type FromExprs (Aggregate a) = a
52 type Transpose to (Aggregate a) = to a
55 fromColumns (HIdentity a) = a
56 toResult = HIdentity . Identity
57 fromResult (HIdentity (Identity a)) = a
60-- | @Aggregates a b@ means that the columns in @a@ are all 'Aggregate's
61-- for the 'Expr' columns in @b@.
62type Aggregates :: Type -> Type -> Constraint
63class Transposes Aggregate Expr aggregates exprs => Aggregates aggregates exprs
64instance Transposes Aggregate Expr aggregates exprs => Aggregates aggregates exprs
68 => (Expr a -> Expr b -> Expr c) -> Aggregate a -> Aggregate b -> Aggregate c
69zipOutputs f (Aggregate a) (Aggregate b) = Aggregate (liftA2 f a b)
72type Aggregator :: Type
73data Aggregator = Aggregator
74 { operation :: Opaleye.AggrOp
75 , ordering :: [Opaleye.OrderExpr]
76 , distinction :: Opaleye.AggrDistinct
80unsafeMakeAggregate :: forall (input :: Type) (output :: Type). ()
81 => (Expr input -> Opaleye.PrimExpr)
82 -> (Opaleye.PrimExpr -> Expr output)
86unsafeMakeAggregate input output aggregator expr =
87 Aggregate $ Opaleye.Aggregator $ Opaleye.PackMap $ \f _ ->
88 output <$> f (tuplize <$> aggregator, input expr)
90 tuplize Aggregator {operation, ordering, distinction} =
91 (operation, ordering, distinction)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list