on master help sign in
Always use `ROW(NULL)` and not `NULL`
commit3017f59f7c20c900c38737526782724ec3f08346 authorShane O'Brien <shane.obrien@circuithub.com> dateMon 14 Jun 2021, 13:34 (5 years ago) parent4475085
2 files changed+21−15
src/Rel8/Expr/Aggregate.hs+3−3 src/Rel8/Type/Array.hs+18−12
src/Rel8/Expr/Aggregate.hs+3−3
@@ -40,7 +40,7 @@ import Rel8.Expr.Null ( null )
4040import Rel8.Expr.Serialize ( litExpr )
4141import Rel8.Schema.Null ( Sql, Unnullify )
4242import Rel8.Type ( DBType, typeInformation )
43−import Rel8.Type.Array ( wrap )
43+import Rel8.Type.Array ( encodeArrayElement )
4444import Rel8.Type.Eq ( DBEq )
4545import Rel8.Type.Information ( TypeInformation )
4646import Rel8.Type.Num ( DBNum )
@@ -189,7 +189,7 @@ slistAggExpr info = unsafeMakeAggregate to fromPrimExpr $ Just
189189 , distinction = Opaleye.AggrAll
190190 }
191191 where
192− to = wrap info . toPrimExpr
192+ to = encodeArrayElement info . toPrimExpr
193193
194194
195195snonEmptyAggExpr :: ()
@@ -201,4 +201,4 @@ snonEmptyAggExpr info = unsafeMakeAggregate to fromPrimExpr $ Just
201201 , distinction = Opaleye.AggrAll
202202 }
203203 where
204− to = wrap info . toPrimExpr
204+ to = encodeArrayElement info . toPrimExpr
src/Rel8/Type/Array.hs+18−12
@@ -5,7 +5,7 @@
55{-# language ViewPatterns #-}
66
77module Rel8.Type.Array
8− ( array, wrap
8+ ( array, encodeArrayElement
99 , listTypeInformation
1010 , nonEmptyTypeInformation
1111 )
@@ -31,7 +31,7 @@ array :: Foldable f
3131 => TypeInformation a -> f Opaleye.PrimExpr -> Opaleye.PrimExpr
3232array info =
3333 Opaleye.CastExpr (arrayType info <> "[]") .
34− Opaleye.ArrayExpr . map (wrap info) . toList
34+ Opaleye.ArrayExpr . map (encodeArrayElement info) . toList
3535{-# INLINABLE array #-}
3636
3737
@@ -42,11 +42,17 @@ listTypeInformation :: ()
4242listTypeInformation nullity info@TypeInformation {encode, decode} =
4343 TypeInformation
4444 { decode = case nullity of
45− Null -> Hasql.listArray (Hasql.nullable (unwrap info decode))
46− NotNull -> Hasql.listArray (Hasql.nonNullable (unwrap info decode))
45+ Null ->
46+ Hasql.listArray (decodeArrayElement info (Hasql.nullable decode))
47+ NotNull ->
48+ Hasql.listArray (decodeArrayElement info (Hasql.nonNullable decode))
4749 , encode = case nullity of
48− Null -> Opaleye.ArrayExpr . fmap (wrap info . maybe null encode)
49− NotNull -> Opaleye.ArrayExpr . fmap (wrap info . encode)
50+ Null ->
51+ Opaleye.ArrayExpr .
52+ fmap (encodeArrayElement info . maybe null encode)
53+ NotNull ->
54+ Opaleye.ArrayExpr .
55+ fmap (encodeArrayElement info . encode)
5056 , typeName = arrayType info <> "[]"
5157 }
5258 where
@@ -76,13 +82,13 @@ arrayType info
7682 | otherwise = typeName info
7783
7884
79−wrap :: TypeInformation a -> Opaleye.PrimExpr -> Opaleye.PrimExpr
80−wrap info
81− | isArray info = Opaleye.UnExpr (Opaleye.UnOpOther "ROW")
85+decodeArrayElement :: TypeInformation a -> Hasql.NullableOrNot Hasql.Value x -> Hasql.NullableOrNot Hasql.Value x
86+decodeArrayElement info
87+ | isArray info = Hasql.nonNullable . Hasql.composite . Hasql.field
8288 | otherwise = id
8389
8490
85−unwrap :: TypeInformation a -> Hasql.Value x -> Hasql.Value x
86−unwrap info
87− | isArray info = Hasql.composite . Hasql.field . Hasql.nonNullable
91+encodeArrayElement :: TypeInformation a -> Opaleye.PrimExpr -> Opaleye.PrimExpr
92+encodeArrayElement info
93+ | isArray info = Opaleye.UnExpr (Opaleye.UnOpOther "ROW")
8894 | otherwise = id