on dev help sign in
from value
24e6a77Eitan Chatav
squeel-postgresql/src/Squeel/PostgreSQL/Binary2.hs+37−1
@@ -11,6 +11,8 @@
11
11
12module Squeel.PostgreSQL.Binary2 where
12module Squeel.PostgreSQL.Binary2 where
13
13
14+import Data.Aeson hiding (Null)
15+import Data.Bits
14import Data.Int
16import Data.Int
15import Data.Scientific
17import Data.Scientific
16import Data.Time
18import Data.Time
@@ -57,6 +59,8 @@ instance ToParam LocalTime 'PGTimestamp where
57instance ToParam UTCTime 'PGTimestampTZ where
59instance ToParam UTCTime 'PGTimestampTZ where
58 toParam = K . Encoding.timestamptz_int
60 toParam = K . Encoding.timestamptz_int
59instance ToParam DiffTime 'PGInterval where toParam = K . Encoding.interval_int
61instance 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
60
64
61class ToColumnParam x (ty :: ColumnType) where
65class ToColumnParam x (ty :: ColumnType) where
62 toColumnParam :: x -> K (Maybe Encoding.Encoding) ty
66 toColumnParam :: x -> K (Maybe Encoding.Encoding) ty
@@ -76,8 +80,40 @@ class ToParams x (tys :: [ColumnType]) where
76 = htrans (Proxy :: Proxy ToColumnParam) (toColumnParam . unI)
80 = htrans (Proxy :: Proxy ToColumnParam) (toColumnParam . unI)
77 . unZ . unSOP . from
81 . unZ . unSOP . from
78
82
79−class FromValue pg y where
83+class FromValue (pg :: PGType) y where
80 fromValue :: proxy pg -> Decoding.Value y
84 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+
81class FromRow pgs y where
117class FromRow pgs y where
82 fromRow :: NP proxy pgs -> Decoding.Value y
118 fromRow :: NP proxy pgs -> Decoding.Value y
83 default fromRow
119 default fromRow