on dev help sign in
example & some instance fixes
commitda4c51ec65987fcfd4006d755ba74c7d2eb6934d authorEitan Chatav <eitan.chatav@gmail.com> dateWed 22 Aug 2018, 16:39 (8 years ago) parent0ed8712
1 file changed+46−11
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+46−11
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+46−11
@@ -44,7 +44,46 @@ True
4444
4545In addition to being able to encode and decode basic Haskell types like `Int16` and `Text`,
4646Squeal permits you to encode and decode Haskell types which are equivalent to
47−Postgres enumerated and composite types.
47+Postgres array, enumerated and composite types.
48+
49+>>> :{
50+let
51+ query :: Query '[]
52+ '[ 'NotNull ('PGvararray ('NotNull 'PGint2))
53+ , 'NotNull ('PGfixarray 2 ('Null 'PGint2))
54+ , 'NotNull ('PGfixarray 3 ('NotNull ('PGfixarray 2 ('NotNull 'PGint2))))
55+ ]
56+ '[ "v1" ::: 'NotNull ('PGvararray ('NotNull 'PGint2))
57+ , "v2" ::: 'NotNull ('PGfixarray 2 ('Null 'PGint2))
58+ , "v3" ::: 'NotNull ('PGfixarray 3 ('NotNull ('PGfixarray 2 ('NotNull 'PGint2))))
59+ ]
60+ query = values_ (param @1 `as` #v1 :* param @2 `as` #v2 :* param @3 `as` #v3)
61+:}
62+
63+>>> :{
64+data VRow = VRow
65+ { v1 :: Vector Int16
66+ , v2 :: (Maybe Int16,Maybe Int16)
67+ , v3 :: ((Int16,Int16),(Int16,Int16),(Int16,Int16))
68+ } deriving (Eq, GHC.Generic)
69+:}
70+
71+>>> instance Generic VRow
72+>>> instance HasDatatypeInfo VRow
73+>>> :set -XOverloadedLists
74+>>> let vparams = VRow [1,2] (Just 1,Nothing) ((1,2), (3,4), (5,6))
75+
76+>>> :{
77+let
78+ roundtrip :: IO ()
79+ roundtrip = void . withConnection "host=localhost port=5432 dbname=exampledb" $ do
80+ result <- runQueryParams query vparams
81+ Just row <- firstRow result
82+ liftBase . print $ row == vparams
83+:}
84+
85+>>> roundtrip
86+True
4887
4988Enumerated (enum) types are data types that comprise a static, ordered set of values.
5089They are equivalent to Haskell algebraic data types whose constructors are nullary.
@@ -433,16 +472,12 @@ instance FromValue 'PGinterval DiffTime where
433472 fromValue = Decoding.interval_int
434473instance FromValue 'PGjson Value where fromValue = Decoding.json_ast
435474instance FromValue 'PGjsonb Value where fromValue = Decoding.jsonb_ast
436−instance FromValue pg y
437− => FromValue ('PGvararray ('Null pg)) (Vector (Maybe y)) where
438− fromValue = Decoding.array
439− (Decoding.dimensionArray Vector.replicateM
440− (Decoding.nullableValueArray (fromValue @pg)))
441−instance FromValue pg y
442− => FromValue ('PGvararray ('NotNull pg)) (Vector y) where
443− fromValue = Decoding.array
444− (Decoding.dimensionArray Vector.replicateM
445− (Decoding.valueArray (fromValue @pg)))
475+instance FromArray ('NotNull ('PGvararray ty)) y
476+ => FromValue ('PGvararray ty) y where
477+ fromValue = Decoding.array (fromArray @('NotNull ('PGvararray ty)) @y)
478+instance FromArray ('NotNull ('PGfixarray n ty)) y
479+ => FromValue ('PGfixarray n ty) y where
480+ fromValue = Decoding.array (fromArray @('NotNull ('PGfixarray n ty)) @y)
446481instance
447482 ( IsEnumType y
448483 , HasDatatypeInfo y