1{-# language DataKinds #-}
2{-# language ScopedTypeVariables #-}
3{-# language TypeApplications #-}
4{-# language TypeFamilies #-}
6{-# options_ghc -fno-warn-redundant-constraints #-}
8module Rel8.Expr.Aggregate
9 ( count, countDistinct, countStar, countWhere
15 , listAggExpr, nonEmptyAggExpr
21import Data.Int ( Int64 )
22import Data.List.NonEmpty ( NonEmpty )
23import Prelude hiding ( and, max, min, null, or, sum )
26import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
29import Rel8.Aggregate ( Aggregate, Aggregator(..), unsafeMakeAggregate )
30import Rel8.Expr ( Expr )
31import Rel8.Expr.Bool ( caseExpr )
32import Rel8.Expr.Opaleye
38import Rel8.Expr.Null ( null )
39import Rel8.Expr.Serialize ( litExpr )
40import Rel8.Schema.Nullability ( Nullability, Nullabilizes, nullabilization )
41import Rel8.Type.Array ( fromPrimArray )
42import Rel8.Type.Eq ( DBEq )
43import Rel8.Type.Num ( DBNum )
44import Rel8.Type.Ord ( DBMax, DBMin )
45import Rel8.Type.String ( DBString )
46import Rel8.Type.Sum ( DBSum )
49count :: Expr a -> Aggregate (Expr Int64)
50count = unsafeMakeAggregate toPrimExpr fromPrimExpr $
52 { operation = Opaleye.AggrCount
54 , distinction = Opaleye.AggrAll
58countDistinct :: DBEq a => Expr a -> Aggregate (Expr Int64)
59countDistinct = unsafeMakeAggregate toPrimExpr fromPrimExpr $
61 { operation = Opaleye.AggrCount
63 , distinction = Opaleye.AggrDistinct
67countStar :: Aggregate (Expr Int64)
68countStar = count (litExpr True)
71countWhere :: Expr Bool -> Aggregate (Expr Int64)
72countWhere condition = count (caseExpr [(condition, litExpr (Just True))] null)
75and :: Expr Bool -> Aggregate (Expr Bool)
76and = unsafeMakeAggregate toPrimExpr fromPrimExpr $
78 { operation = Opaleye.AggrBoolAnd
80 , distinction = Opaleye.AggrAll
84or :: Expr Bool -> Aggregate (Expr Bool)
85or = unsafeMakeAggregate toPrimExpr fromPrimExpr $
87 { operation = Opaleye.AggrBoolOr
89 , distinction = Opaleye.AggrAll
93-- | Produce an aggregation for @Expr a@ using the @max@ function.
94max :: (DBMax db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
95max = unsafeMakeAggregate toPrimExpr fromPrimExpr $
97 { operation = Opaleye.AggrSum
99 , distinction = Opaleye.AggrAll
103-- | Produce an aggregation for @Expr a@ using the @max@ function.
104min :: (DBMin db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
105min = unsafeMakeAggregate toPrimExpr fromPrimExpr $
107 { operation = Opaleye.AggrSum
109 , distinction = Opaleye.AggrAll
112-- | Corresponds to @sum@.
113sum :: (DBSum db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
114sum = unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
116 { operation = Opaleye.AggrSum
118 , distinction = Opaleye.AggrAll
122sumWhere :: (DBNum db, DBSum db, Nullabilizes db a)
123 => Expr Bool -> Expr a -> Aggregate (Expr a)
124sumWhere condition a = sum (caseExpr [(condition, a)] 0)
127stringAgg :: (DBString db, Nullabilizes db a)
128 => Expr db -> Expr a -> Aggregate (Expr a)
130 unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
132 { operation = Opaleye.AggrStringAggr (toPrimExpr delimiter)
134 , distinction = Opaleye.AggrAll
138-- | Aggregate a value by grouping by it.
139groupByExpr :: (DBEq db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
140groupByExpr = sgroupByExpr nullabilization
143listAggExpr :: Expr a -> Aggregate (Expr [a])
144listAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
146 { operation = Opaleye.AggrArr
148 , distinction = Opaleye.AggrAll
151 from = fromPrimExpr . fromPrimArray
154nonEmptyAggExpr :: Expr a -> Aggregate (Expr (NonEmpty a))
155nonEmptyAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
157 { operation = Opaleye.AggrArr
159 , distinction = Opaleye.AggrAll
162 from = fromPrimExpr . fromPrimArray
165sgroupByExpr :: DBEq db => Nullability db a -> Expr a -> Aggregate (Expr a)
166sgroupByExpr _ = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing
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