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.Eq ( DBEq )
42import Rel8.Type.Num ( DBNum )
43import Rel8.Type.Ord ( DBMax, DBMin )
44import Rel8.Type.String ( DBString )
45import Rel8.Type.Sum ( DBSum )
48count :: Expr a -> Aggregate (Expr Int64)
49count = unsafeMakeAggregate unsafeToPrimExpr unsafeFromPrimExpr $
51 { operation = Opaleye.AggrCount
53 , distinction = Opaleye.AggrAll
57countDistinct :: DBEq a => Expr a -> Aggregate (Expr Int64)
58countDistinct = unsafeMakeAggregate unsafeToPrimExpr unsafeFromPrimExpr $
60 { operation = Opaleye.AggrCount
62 , distinction = Opaleye.AggrDistinct
66countStar :: Aggregate (Expr Int64)
67countStar = count (litExpr True)
70countWhere :: Expr Bool -> Aggregate (Expr Int64)
71countWhere condition = count (caseExpr [(condition, litExpr (Just True))] null)
74and :: Expr Bool -> Aggregate (Expr Bool)
75and = unsafeMakeAggregate unsafeToPrimExpr unsafeFromPrimExpr $
77 { operation = Opaleye.AggrBoolAnd
79 , distinction = Opaleye.AggrAll
83or :: Expr Bool -> Aggregate (Expr Bool)
84or = unsafeMakeAggregate unsafeToPrimExpr unsafeFromPrimExpr $
86 { operation = Opaleye.AggrBoolOr
88 , distinction = Opaleye.AggrAll
92max :: (DBMax db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
93max = unsafeMakeAggregate unsafeToPrimExpr unsafeFromPrimExpr $
95 { operation = Opaleye.AggrSum
97 , distinction = Opaleye.AggrAll
101min :: (DBMin db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
102min = unsafeMakeAggregate unsafeToPrimExpr unsafeFromPrimExpr $
104 { operation = Opaleye.AggrSum
106 , distinction = Opaleye.AggrAll
110sum :: (DBSum db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
111sum = unsafeMakeAggregate unsafeToPrimExpr (castExpr . unsafeFromPrimExpr) $
113 { operation = Opaleye.AggrSum
115 , distinction = Opaleye.AggrAll
119sumWhere :: (DBNum db, DBSum db, Nullabilizes db a)
120 => Expr Bool -> Expr a -> Aggregate (Expr a)
121sumWhere condition a = sum (caseExpr [(condition, a)] 0)
124stringAgg :: (DBString db, Nullabilizes db a)
125 => Expr db -> Expr a -> Aggregate (Expr a)
127 unsafeMakeAggregate unsafeToPrimExpr (castExpr . unsafeFromPrimExpr) $
129 { operation = Opaleye.AggrStringAggr (unsafeToPrimExpr delimiter)
131 , distinction = Opaleye.AggrAll
135groupByExpr :: (DBEq db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
136groupByExpr = sgroupByExpr nullabilization
139listAggExpr :: Expr a -> Aggregate (Expr [a])
140listAggExpr = unsafeMakeAggregate unsafeToPrimExpr fromPrimExpr $ Just
142 { operation = Opaleye.AggrArr
144 , distinction = Opaleye.AggrAll
148nonEmptyAggExpr :: Expr a -> Aggregate (Expr (NonEmpty a))
149nonEmptyAggExpr = unsafeMakeAggregate unsafeToPrimExpr fromPrimExpr $ Just
151 { operation = Opaleye.AggrArr
153 , distinction = Opaleye.AggrAll
157sgroupByExpr :: DBEq db => Nullability db a -> Expr a -> Aggregate (Expr a)
158sgroupByExpr _ = unsafeMakeAggregate unsafeToPrimExpr unsafeFromPrimExpr 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