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
93max :: (DBMax db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
94max = unsafeMakeAggregate toPrimExpr fromPrimExpr $
96 { operation = Opaleye.AggrSum
98 , distinction = Opaleye.AggrAll
102min :: (DBMin db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
103min = unsafeMakeAggregate toPrimExpr fromPrimExpr $
105 { operation = Opaleye.AggrSum
107 , distinction = Opaleye.AggrAll
111sum :: (DBSum db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
112sum = unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
114 { operation = Opaleye.AggrSum
116 , distinction = Opaleye.AggrAll
120sumWhere :: (DBNum db, DBSum db, Nullabilizes db a)
121 => Expr Bool -> Expr a -> Aggregate (Expr a)
122sumWhere condition a = sum (caseExpr [(condition, a)] 0)
125stringAgg :: (DBString db, Nullabilizes db a)
126 => Expr db -> Expr a -> Aggregate (Expr a)
128 unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
130 { operation = Opaleye.AggrStringAggr (toPrimExpr delimiter)
132 , distinction = Opaleye.AggrAll
136groupByExpr :: (DBEq db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
137groupByExpr = sgroupByExpr nullabilization
140listAggExpr :: Expr a -> Aggregate (Expr [a])
141listAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
143 { operation = Opaleye.AggrArr
145 , distinction = Opaleye.AggrAll
148 from = fromPrimExpr . fromPrimArray
151nonEmptyAggExpr :: Expr a -> Aggregate (Expr (NonEmpty a))
152nonEmptyAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
154 { operation = Opaleye.AggrArr
156 , distinction = Opaleye.AggrAll
159 from = fromPrimExpr . fromPrimArray
162sgroupByExpr :: DBEq db => Nullability db a -> Expr a -> Aggregate (Expr a)
163sgroupByExpr _ = 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