1{-# language DataKinds #-}
2{-# language FlexibleContexts #-}
3{-# language FlexibleInstances #-}
4{-# language MultiParamTypeClasses #-}
5{-# language RankNTypes #-}
6{-# language StandaloneKindSignatures #-}
7{-# language TypeFamilies #-}
8{-# language UndecidableInstances #-}
11 ( Aggregate(..), zipOutputs
18import Control.Applicative ( liftA2 )
19import Data.Functor.Identity ( Identity( Identity ) )
20import Data.Kind ( Constraint, Type )
24import Data.Profunctor ( dimap )
27import qualified Opaleye.Aggregate as Opaleye
28import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
29import qualified Opaleye.Internal.Column as Opaleye
32import Rel8.Expr ( Expr )
33import Rel8.Schema.HTable.Identity ( HIdentity(..) )
34import qualified Rel8.Schema.Kind as K
35import Rel8.Schema.Null ( Sql )
37 ( Table, Columns, Context, fromColumns, toColumns
38 , FromExprs, fromResult, toResult
41import Rel8.Table.Transpose ( Transposes )
42import Rel8.Type ( DBType )
45-- | 'Aggregate' is a special context used by 'Rel8.aggregate'.
46type Aggregate :: K.Context
47newtype Aggregate a = Aggregate (Opaleye.Aggregator () (Expr a))
50instance Sql DBType a => Table Aggregate (Aggregate a) where
51 type Columns (Aggregate a) = HIdentity a
52 type Context (Aggregate a) = Aggregate
53 type FromExprs (Aggregate a) = a
54 type Transpose to (Aggregate a) = to a
57 fromColumns (HIdentity a) = a
58 toResult = HIdentity . Identity
59 fromResult (HIdentity (Identity a)) = a
62-- | @Aggregates a b@ means that the columns in @a@ are all 'Aggregate's
63-- for the 'Expr' columns in @b@.
64type Aggregates :: Type -> Type -> Constraint
65class Transposes Aggregate Expr aggregates exprs => Aggregates aggregates exprs
66instance Transposes Aggregate Expr aggregates exprs => Aggregates aggregates exprs
70 => (Expr a -> Expr b -> Expr c) -> Aggregate a -> Aggregate b -> Aggregate c
71zipOutputs f (Aggregate a) (Aggregate b) = Aggregate (liftA2 f a b)
74unsafeMakeAggregate :: forall (input :: Type) (output :: Type) n n' a a'. ()
75 => (Expr input -> Opaleye.PrimExpr)
76 -> (Opaleye.PrimExpr -> Expr output)
77 -> Opaleye.Aggregator (Opaleye.Field_ n a) (Opaleye.Field_ n' a')
80unsafeMakeAggregate input output aggregator expr =
81 Aggregate $ dimap in_ out aggregator
82 where out = output . Opaleye.unColumn
83 in_ = Opaleye.Column . input . const expr
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