on dev help sign in
array nullability
commit739a943cea446a8b22b54a22954f229cdbec15c5 authorEitan Chatav <eitan.chatav@gmail.com> dateTue 10 Jul 2018, 10:05 (8 years ago) parent4deaa68
4 files changed+43−26
squeal-postgresql/exe/Example.hs+3−3 squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+25−9 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+13−12 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+2−2
squeal-postgresql/exe/Example.hs+3−3
@@ -29,7 +29,7 @@ type Schema =
2929 '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=>
3030 '[ "id" ::: 'Def :=> 'NotNull 'PGint4
3131 , "name" ::: 'NoDef :=> 'NotNull 'PGtext
32− , "vec" ::: 'NoDef :=> 'NotNull ('PGvararray 'PGint2)
32+ , "vec" ::: 'NoDef :=> 'NotNull ('PGvararray ('Null 'PGint2))
3333 ])
3434 , "emails" ::: 'Table (
3535 '[ "pk_emails" ::: 'PrimaryKey '["id"]
@@ -60,7 +60,7 @@ setup =
6060teardown :: Definition Schema '[]
6161teardown = dropTable #emails >>> dropTable #users
6262
63−insertUser :: Manipulation Schema '[ 'NotNull 'PGtext, 'NotNull ('PGvararray 'PGint2)]
63+insertUser :: Manipulation Schema '[ 'NotNull 'PGtext, 'NotNull ('PGvararray ('Null 'PGint2))]
6464 '[ "fromOnly" ::: 'NotNull 'PGint4 ]
6565insertUser = insertRows #users
6666 (Default `as` #id :* Set (param @1) `as` #name :* Set (param @2) `as` #vec) []
@@ -76,7 +76,7 @@ insertEmail = insertRows #emails
7676getUsers :: Query Schema '[]
7777 '[ "userName" ::: 'NotNull 'PGtext
7878 , "userEmail" ::: 'Null 'PGtext
79− , "userVec" ::: 'NotNull ('PGvararray 'PGint2)]
79+ , "userVec" ::: 'NotNull ('PGvararray ('Null 'PGint2))]
8080getUsers = select
8181 (#u ! #name `as` #userName :* #e ! #email `as` #userEmail :* #u ! #vec `as` #userVec)
8282 ( from (table (#users `as` #u)
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+25−9
@@ -235,9 +235,13 @@ instance ToParam DiffTime 'PGinterval where toParam = K . Encoding.interval_int
235235instance ToParam Value 'PGjson where toParam = K . Encoding.json_ast
236236instance ToParam Value 'PGjsonb where toParam = K . Encoding.jsonb_ast
237237instance (HasOid pg, ToParam x pg)
238− => ToParam (Vector (Maybe x)) ('PGvararray pg) where
238+ => ToParam (Vector (Maybe x)) ('PGvararray ('Null pg)) where
239239 toParam = K . Encoding.nullableArray_vector
240240 (oid @pg) (unK . toParam @x @pg)
241+instance (HasOid pg, ToParam x pg)
242+ => ToParam (Vector x) ('PGvararray ('NotNull pg)) where
243+ toParam = K . Encoding.array_vector
244+ (oid @pg) (unK . toParam @x @pg)
241245instance
242246 ( IsEnumType x
243247 , HasDatatypeInfo x
@@ -386,14 +390,26 @@ instance FromValue 'PGinterval DiffTime where
386390 fromValue _ = Decoding.interval_int
387391instance FromValue 'PGjson Value where fromValue _ = Decoding.json_ast
388392instance FromValue 'PGjsonb Value where fromValue _ = Decoding.jsonb_ast
389−instance FromValue pg y => FromValue ('PGvararray pg) (Vector (Maybe y)) where
390− fromValue _ = Decoding.array
391− (Decoding.dimensionArray Vector.replicateM
392− (Decoding.nullableValueArray (fromValue (Proxy @pg))))
393−instance FromValue pg y => FromValue ('PGfixarray n pg) (Vector (Maybe y)) where
394− fromValue _ = Decoding.array
395− (Decoding.dimensionArray Vector.replicateM
396− (Decoding.nullableValueArray (fromValue (Proxy @pg))))
393+instance FromValue pg y
394+ => FromValue ('PGvararray ('Null pg)) (Vector (Maybe y)) where
395+ fromValue _ = Decoding.array
396+ (Decoding.dimensionArray Vector.replicateM
397+ (Decoding.nullableValueArray (fromValue (Proxy @pg))))
398+instance FromValue pg y
399+ => FromValue ('PGvararray ('NotNull pg)) (Vector y) where
400+ fromValue _ = Decoding.array
401+ (Decoding.dimensionArray Vector.replicateM
402+ (Decoding.valueArray (fromValue (Proxy @pg))))
403+instance FromValue pg y
404+ => FromValue ('PGfixarray n ('Null pg)) (Vector (Maybe y)) where
405+ fromValue _ = Decoding.array
406+ (Decoding.dimensionArray Vector.replicateM
407+ (Decoding.nullableValueArray (fromValue (Proxy @pg))))
408+instance FromValue pg y
409+ => FromValue ('PGfixarray n ('NotNull pg)) (Vector y) where
410+ fromValue _ = Decoding.array
411+ (Decoding.dimensionArray Vector.replicateM
412+ (Decoding.valueArray (fromValue (Proxy @pg))))
397413instance
398414 ( IsEnumType y
399415 , HasDatatypeInfo y
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+13−12
@@ -382,7 +382,7 @@ nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
382382-- | >>> printSQL $ array [null_, notNull false, notNull true]
383383-- ARRAY[NULL, FALSE, TRUE]
384384array
385− :: [Expression schema relations grouping params ('Null ty)]
385+ :: [Expression schema relations grouping params ty]
386386 -- ^ array elements
387387 -> Expression schema relations grouping params (nullity ('PGvararray ty))
388388array xs = UnsafeExpression $
@@ -1206,19 +1206,18 @@ jsonb = UnsafeTypeExpression "jsonb"
12061206-- | variable length array
12071207vararray
12081208 :: TypeExpression schema pg
1209− -> TypeExpression schema ('PGvararray pg)
1209+ -> TypeExpression schema ('PGvararray (nullity pg))
12101210vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
12111211-- | fixed length array
12121212--
12131213-- >>> renderTypeExpression (fixarray (Proxy @2) json)
12141214-- "json[2]"
12151215fixarray
1216− :: KnownNat n
1217− => proxy n
1218− -> TypeExpression schema pg
1219− -> TypeExpression schema ('PGfixarray n pg)
1220−fixarray p ty = UnsafeTypeExpression $
1221− renderTypeExpression ty <> "[" <> renderNat p <> "]"
1216+ :: forall n schema nullity pg. KnownNat n
1217+ => TypeExpression schema pg
1218+ -> TypeExpression schema ('PGfixarray n (nullity pg))
1219+fixarray ty = UnsafeTypeExpression $
1220+ renderTypeExpression ty <> "[" <> renderNat (Proxy @n) <> "]"
12221221
12231222-- | `pgtype` is a demoted version of a `PGType`
12241223class PGTyped schema (ty :: PGType) where pgtype :: TypeExpression schema ty
@@ -1244,7 +1243,9 @@ instance PGTyped schema 'PGinterval where pgtype = interval
12441243instance PGTyped schema 'PGuuid where pgtype = uuid
12451244instance PGTyped schema 'PGjson where pgtype = json
12461245instance PGTyped schema 'PGjsonb where pgtype = jsonb
1247−instance PGTyped schema ty => PGTyped schema ('PGvararray ty) where
1248− pgtype = vararray (pgtype @schema @ty)
1249−instance (KnownNat n, PGTyped schema ty) => PGTyped schema ('PGfixarray n ty) where
1250− pgtype = fixarray (Proxy @n) (pgtype @schema @ty)
1246+instance PGTyped schema ty
1247+ => PGTyped schema ('PGvararray (nullity ty)) where
1248+ pgtype = vararray (pgtype @schema @ty)
1249+instance (KnownNat n, PGTyped schema ty)
1250+ => PGTyped schema ('PGfixarray n (nullity ty)) where
1251+ pgtype = fixarray @n (pgtype @schema @ty)
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+2−2
@@ -174,8 +174,8 @@ data PGType
174174 | PGinet -- ^ IPv4 or IPv6 host address
175175 | PGjson -- ^ textual JSON data
176176 | PGjsonb -- ^ binary JSON data, decomposed
177− | PGvararray PGType -- ^ variable length array
178− | PGfixarray Nat PGType -- ^ fixed length array
177+ | PGvararray NullityType -- ^ variable length array
178+ | PGfixarray Nat NullityType -- ^ fixed length array
179179 | PGenum [Symbol] -- ^ enumerated (enum) types are data types that comprise a static, ordered set of values.
180180 | PGcomposite [(Symbol, PGType)] -- ^ a composite type represents the structure of a row or record; it is essentially just a list of field names and their data types.
181181 | UnsafePGType Symbol -- ^ an escape hatch for unsupported PostgreSQL types