on master help sign in
Add array1DAggExpr
b0e6053Shane O'Brien
src/Rel8.hs+1
@@ -172,6 +172,7 @@ module Rel8
172 , Aggregate
172 , Aggregate
173 , aggregate
173 , aggregate
174 , countRows
174 , countRows
175+ , array1DAggExpr
175 , listAgg, listAggExpr
176 , listAgg, listAggExpr
176 , nonEmptyAgg, nonEmptyAggExpr
177 , nonEmptyAgg, nonEmptyAggExpr
177 , groupBy
178 , groupBy
src/Rel8/Expr/Aggregate.hs+14−1
@@ -12,7 +12,7 @@ module Rel8.Expr.Aggregate
12 , sum, sumWhere
12 , sum, sumWhere
13 , stringAgg
13 , stringAgg
14 , groupByExpr
14 , groupByExpr
15− , listAggExpr, nonEmptyAggExpr
15+ , array1DAggExpr, listAggExpr, nonEmptyAggExpr
16 )
16 )
17where
17where
18
18
@@ -38,6 +38,7 @@ import Rel8.Expr.Null ( null )
38import Rel8.Expr.Serialize ( litExpr )
38import Rel8.Expr.Serialize ( litExpr )
39import Rel8.Schema.Null ( Sql )
39import Rel8.Schema.Null ( Sql )
40import Rel8.Type.Array ( fromPrimArray )
40import Rel8.Type.Array ( fromPrimArray )
41+import Rel8.Type.Array1D ( Array1D, NotArray )
41import Rel8.Type.Eq ( DBEq )
42import Rel8.Type.Eq ( DBEq )
42import Rel8.Type.Num ( DBNum )
43import Rel8.Type.Num ( DBNum )
43import Rel8.Type.Ord ( DBMax, DBMin )
44import Rel8.Type.Ord ( DBMax, DBMin )
@@ -152,6 +153,18 @@ groupByExpr :: Sql DBEq a => Expr a -> Aggregate (Expr a)
152groupByExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing
153groupByExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing
153
154
154
155
156+-- | Collect expressions values as an array.
157+array1DAggExpr :: NotArray a => Expr a -> Aggregate (Expr (Array1D a))
158+array1DAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
159+ Aggregator
160+ { operation = Opaleye.AggrArr
161+ , ordering = []
162+ , distinction = Opaleye.AggrAll
163+ }
164+ where
165+ from = fromPrimExpr . fromPrimArray
166+
167+
155-- | Collect expressions values as a list.
168-- | Collect expressions values as a list.
156listAggExpr :: Expr a -> Aggregate (Expr [a])
169listAggExpr :: Expr a -> Aggregate (Expr [a])
157listAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
170listAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
src/Rel8/Type/Array1D.hs+15−8
@@ -1,6 +1,8 @@
1{-# language DataKinds #-}
1{-# language DataKinds #-}
2{-# language DeriveTraversable #-}
2{-# language DeriveTraversable #-}
3{-# language DerivingStrategies #-}
3{-# language DerivingStrategies #-}
4+{-# language FlexibleContexts #-}
5+{-# language FlexibleInstances #-}
4{-# language GeneralizedNewtypeDeriving #-}
6{-# language GeneralizedNewtypeDeriving #-}
5{-# language NamedFieldPuns #-}
7{-# language NamedFieldPuns #-}
6{-# language StandaloneKindSignatures #-}
8{-# language StandaloneKindSignatures #-}
@@ -11,6 +13,7 @@
11
13
12module Rel8.Type.Array1D
14module Rel8.Type.Array1D
13 ( Array1D( Array1D )
15 ( Array1D( Array1D )
16+ , NotArray
14 , getArray1D
17 , getArray1D
15 )
18 )
16where
19where
@@ -89,7 +92,11 @@ type family IsArray1D a where
89 IsArray1D _ = 'False
92 IsArray1D _ = 'False
90
93
91
94
95+class IsArray1D a ~ 'False => NotArray a
96+instance IsArray1D a ~ 'False => NotArray a
97+
98+
92−array1DTypeInformation :: IsArray1D (Unnullify a) ~ 'False
99+array1DTypeInformation :: Sql NotArray a
93 => Nullity a
100 => Nullity a
94 -> TypeInformation (Unnullify a)
101 -> TypeInformation (Unnullify a)
95 -> TypeInformation (Array1D a)
102 -> TypeInformation (Array1D a)
@@ -108,25 +115,25 @@ array1DTypeInformation nullity info =
108 null = Opaleye.ConstExpr Opaleye.NullLit
115 null = Opaleye.ConstExpr Opaleye.NullLit
109
116
110
117
111−instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBType (Array1D a) where
118+instance (Sql DBType a, Sql NotArray a) => DBType (Array1D a) where
112 typeInformation = array1DTypeInformation nullable typeInformation
119 typeInformation = array1DTypeInformation nullable typeInformation
113
120
114
121
115−instance (Sql DBEq a, IsArray1D (Unnullify a) ~ 'False) => DBEq (Array1D a)
122+instance (Sql DBEq a, Sql NotArray a) => DBEq (Array1D a)
116
123
117
124
118−instance (Sql DBOrd a, IsArray1D (Unnullify a) ~ 'False) => DBOrd (Array1D a)
125+instance (Sql DBOrd a, Sql NotArray a) => DBOrd (Array1D a)
119
126
120
127
121−instance (Sql DBMax a, IsArray1D (Unnullify a) ~ 'False) => DBMax (Array1D a)
128+instance (Sql DBMax a, Sql NotArray a) => DBMax (Array1D a)
122
129
123
130
124−instance (Sql DBMin a, IsArray1D (Unnullify a) ~ 'False) => DBMin (Array1D a)
131+instance (Sql DBMin a, Sql NotArray a) => DBMin (Array1D a)
125
132
126
133
127−instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBSemigroup (Array1D a) where
134+instance (Sql DBType a, Sql NotArray a) => DBSemigroup (Array1D a) where
128 (<>.) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:||))
135 (<>.) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:||))
129
136
130
137
131−instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBMonoid (Array1D a) where
138+instance (Sql DBType a, Sql NotArray a) => DBMonoid (Array1D a) where
132 memptyExpr = litExpr mempty
139 memptyExpr = litExpr mempty