1{-# language DataKinds #-}
2{-# language DerivingVia #-}
3{-# language FlexibleContexts #-}
4{-# language FlexibleInstances #-}
6{-# language MultiParamTypeClasses #-}
7{-# language NamedFieldPuns #-}
8{-# language PolyKinds #-}
9{-# language StandaloneKindSignatures #-}
10{-# language TypeFamilies #-}
11{-# language UndecidableInstances #-}
12{-# language UndecidableSuperClasses #-}
15 ( Aggregate(..), foldInputs, mapInputs
16 , Aggregator(..), unsafeMakeAggregate
23import Data.Functor.Const ( Const( Const ), getConst )
24import Data.Functor.Identity ( Identity )
25import Data.Kind ( Constraint, Type )
29import qualified Opaleye.Internal.Aggregate as Opaleye
30import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
31import qualified Opaleye.Internal.PackMap as Opaleye
34import Rel8.Expr ( Expr )
35import Rel8.Schema.Context ( Interpretation(..) )
36import Rel8.Schema.HTable.Identity ( HIdentity(..), HType )
37import Rel8.Schema.Name ( Name )
38import Rel8.Schema.Null ( Sql )
39import Rel8.Schema.Reify ( notReify )
40import Rel8.Schema.Result ( Result )
41import Rel8.Schema.Spec ( Spec( Spec ) )
43 ( Table, Columns, Context, fromColumns, toColumns
46import Rel8.Table.Recontextualize ( Recontextualize )
47import Rel8.Type ( DBType )
50-- | An @Aggregate a@ describes how to aggregate @Table@s of type @a@. You can
51-- unpack an @Aggregate@ back to @a@ by running it with 'Rel8.aggregate'. As
52-- @Aggregate@ is almost an 'Applicative' functor - but there is no 'pure'
53-- operation. This means 'Aggregate' is an instance of 'Apply', and you can
54-- combine @Aggregate@s using the @<.>@ combinator.
55type Aggregate :: k -> Type
57 Aggregate :: !(Opaleye.Aggregator () (Expr a)) -> Aggregate a
60instance Interpretation Aggregate where
61 data Col Aggregate _spec where
63 => { unA :: !(Aggregate a) }
64 -> Col Aggregate ('Spec a)
67instance Sql DBType a => Table Aggregate (Aggregate a) where
68 type Columns (Aggregate a) = HType a
69 type Context (Aggregate a) = Aggregate
71 toColumns = HIdentity . A
72 fromColumns (HIdentity (A a)) = a
78instance Sql DBType a =>
79 Recontextualize Aggregate Aggregate (Aggregate a) (Aggregate a)
82instance Sql DBType a =>
83 Recontextualize Aggregate Expr (Aggregate a) (Expr a)
86instance Sql DBType a =>
87 Recontextualize Aggregate Result (Aggregate a) (Identity a)
90instance Sql DBType a =>
91 Recontextualize Aggregate Name (Aggregate a) (Name a)
94instance Sql DBType a =>
95 Recontextualize Expr Aggregate (Expr a) (Aggregate a)
98instance Sql DBType a =>
99 Recontextualize Result Aggregate (Identity a) (Aggregate a)
102instance Sql DBType a =>
103 Recontextualize Name Aggregate (Name a) (Aggregate a)
106-- | @Aggregates a b@ means that the columns in @a@ are all 'Aggregate' 'Expr's
107-- for the columns in @b@.
108type Aggregates :: Type -> Type -> Constraint
109class Recontextualize Aggregate Expr aggregates exprs => Aggregates aggregates exprs
110instance Recontextualize Aggregate Expr aggregates exprs => Aggregates aggregates exprs
113foldInputs :: Monoid b
114 => (Maybe Aggregator -> Opaleye.PrimExpr -> b) -> Aggregate a -> b
115foldInputs f (Aggregate (Opaleye.Aggregator (Opaleye.PackMap agg))) =
116 getConst $ flip agg () $ \(aggregator, a) ->
117 Const $ f (detuplize <$> aggregator) a
119 detuplize (operation, ordering, distinction) =
120 Aggregator {operation, ordering, distinction}
124 => (Opaleye.PrimExpr -> Opaleye.PrimExpr) -> Aggregate a -> Aggregate a
125mapInputs transform (Aggregate (Opaleye.Aggregator (Opaleye.PackMap agg))) =
126 Aggregate $ Opaleye.Aggregator $ Opaleye.PackMap $ agg . \f input ->
127 f (fmap transform input)
130type Aggregator :: Type
131data Aggregator = Aggregator
132 { operation :: Opaleye.AggrOp
133 , ordering :: [Opaleye.OrderExpr]
134 , distinction :: Opaleye.AggrDistinct
138unsafeMakeAggregate :: ()
139 => (Expr input -> Opaleye.PrimExpr)
140 -> (Opaleye.PrimExpr -> Expr output)
144unsafeMakeAggregate input output aggregator expr =
145 Aggregate $ Opaleye.Aggregator $ Opaleye.PackMap $ \f _ ->
146 output <$> f (tuplize <$> aggregator, input expr)
148 tuplize Aggregator {operation, ordering, distinction} =
149 (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