on dev help sign in
from value
commit24e6a77f1e082feb104f31d731f37ac2dfdf1a4e authorEitan Chatav <eitan.chatav@gmail.com> dateFri 11 Aug 2017, 18:38 (9 years ago) parent4b18ff5
1 file changed+37−1
squeel-postgresql/src/Squeel/PostgreSQL/Binary2.hs+37−1
squeel-postgresql/src/Squeel/PostgreSQL/Binary2.hs+37−1
@@ -11,6 +11,8 @@
1111
1212module Squeel.PostgreSQL.Binary2 where
1313
14+import Data.Aeson hiding (Null)
15+import Data.Bits
1416import Data.Int
1517import Data.Scientific
1618import Data.Time
@@ -57,6 +59,8 @@ instance ToParam LocalTime 'PGTimestamp where
5759instance ToParam UTCTime 'PGTimestampTZ where
5860 toParam = K . Encoding.timestamptz_int
5961instance ToParam DiffTime 'PGInterval where toParam = K . Encoding.interval_int
62+instance ToParam Value 'PGJson where toParam = K . Encoding.json_ast
63+instance ToParam Value 'PGJsonb where toParam = K . Encoding.jsonb_ast
6064
6165class ToColumnParam x (ty :: ColumnType) where
6266 toColumnParam :: x -> K (Maybe Encoding.Encoding) ty
@@ -76,8 +80,40 @@ class ToParams x (tys :: [ColumnType]) where
7680 = htrans (Proxy :: Proxy ToColumnParam) (toColumnParam . unI)
7781 . unZ . unSOP . from
7882
79−class FromValue pg y where
83+class FromValue (pg :: PGType) y where
8084 fromValue :: proxy pg -> Decoding.Value y
85+instance FromValue 'PGBool Bool where fromValue _ = Decoding.bool
86+instance (Integral int, Bits int) => FromValue 'PGInt2 int where
87+ fromValue _ = Decoding.int
88+instance (Integral int, Bits int) => FromValue 'PGInt4 int where
89+ fromValue _ = Decoding.int
90+instance (Integral int, Bits int) => FromValue 'PGInt8 int where
91+ fromValue _ = Decoding.int
92+instance FromValue 'PGFloat4 Float where fromValue _ = Decoding.float4
93+instance FromValue 'PGFloat8 Double where fromValue _ = Decoding.float8
94+instance FromValue 'PGNumeric Scientific where fromValue _ = Decoding.numeric
95+instance FromValue 'PGUuid UUID where fromValue _ = Decoding.uuid
96+instance FromValue 'PGInet (NetAddr IP) where fromValue _ = Decoding.inet
97+instance FromValue ('PGChar 1) Char where fromValue _ = Decoding.char
98+instance FromValue 'PGText Strict.Text where fromValue _ = Decoding.text_strict
99+instance FromValue 'PGText Lazy.Text where fromValue _ = Decoding.text_lazy
100+instance FromValue 'PGBytea Strict.ByteString where
101+ fromValue _ = Decoding.bytea_strict
102+instance FromValue 'PGBytea Lazy.ByteString where
103+ fromValue _ = Decoding.bytea_lazy
104+instance FromValue 'PGDate Day where fromValue _ = Decoding.date
105+instance FromValue 'PGTime TimeOfDay where fromValue _ = Decoding.time_int
106+instance FromValue 'PGTimeTZ (TimeOfDay, TimeZone) where
107+ fromValue _ = Decoding.timetz_int
108+instance FromValue 'PGTimestamp LocalTime where
109+ fromValue _ = Decoding.timestamp_int
110+instance FromValue 'PGTimestampTZ UTCTime where
111+ fromValue _ = Decoding.timestamptz_int
112+instance FromValue 'PGInterval DiffTime where
113+ fromValue _ = Decoding.interval_int
114+instance FromValue 'PGJson Value where fromValue _ = Decoding.json_ast
115+instance FromValue 'PGJsonb Value where fromValue _ = Decoding.jsonb_ast
116+
81117class FromRow pgs y where
82118 fromRow :: NP proxy pgs -> Decoding.Value y
83119 default fromRow