@@ -13,21 +13,16 @@
1313
1414module Squeel.PostgreSQL.Binary where
1515
16−import Control.Arrow (left)
17−import Data.Aeson (FromJSON, ToJSON, eitherDecodeStrict, encode)
1816import Data.ByteString (ByteString)
1917import Data.Int (Int16,Int32,Int64)
2018import Generics.SOP
21−-- import GHC.Generics
22−-- import PostgreSQL.Binary.Decoding
2319import Data.Scientific (Scientific)
2420import Data.Text (Text)
2521import Data.Time (Day, TimeOfDay, TimeZone, LocalTime, UTCTime, DiffTime)
2622import Data.UUID (UUID)
27−-- import GHC.Exts
2823
29−import qualified Data.ByteString.Lazy as Lazy (ByteString, toStrict)
30−import qualified Data.Text as Text (pack)
24+import qualified Data.Aeson as Aeson
25+import qualified Data.ByteString.Lazy as Lazy (ByteString)
3126import qualified Data.Text.Lazy as Lazy (Text)
3227import qualified PostgreSQL.Binary.Decoding as Decoder
3328import qualified PostgreSQL.Binary.Encoding as Encoder
@@ -39,47 +34,47 @@ decodeValue p = Decoder.valueParser $ decoding p
3934
4035class HasDecoding pg x where
4136 decoding :: proxy pg -> Decoder.Value x
42−instance HasDecoding 'PGInt2 Int16 where
37+instance HasDecoding (column ::: 'Required ('NotNull 'PGInt2)) Int16 where
4338 decoding _ = Decoder.int
44−instance HasDecoding 'PGInt4 Int32 where
39+instance HasDecoding (column ::: 'Required ('NotNull 'PGInt4)) Int32 where
4540 decoding _ = Decoder.int
46−instance HasDecoding 'PGInt8 Int64 where
41+instance HasDecoding (column ::: 'Required ('NotNull 'PGInt8)) Int64 where
4742 decoding _ = Decoder.int
48−instance HasDecoding 'PGFloat4 Float where
43+instance HasDecoding (column ::: 'Required ('NotNull 'PGFloat4)) Float where
4944 decoding _ = Decoder.float4
50−instance HasDecoding 'PGFloat8 Double where
45+instance HasDecoding (column ::: 'Required ('NotNull 'PGFloat8)) Double where
5146 decoding _ = Decoder.float8
52−instance HasDecoding 'PGBool Bool where
47+instance HasDecoding (column ::: 'Required ('NotNull 'PGBool)) Bool where
5348 decoding _ = Decoder.bool
54−instance HasDecoding 'PGBytea ByteString where
49+instance HasDecoding (column ::: 'Required ('NotNull 'PGBytea)) ByteString where
5550 decoding _ = Decoder.bytea_strict
56−instance HasDecoding 'PGBytea Lazy.ByteString where
51+instance HasDecoding (column ::: 'Required ('NotNull 'PGBytea)) Lazy.ByteString where
5752 decoding _ = Decoder.bytea_lazy
58−instance HasDecoding 'PGText Text where
53+instance HasDecoding (column ::: 'Required ('NotNull 'PGText)) Text where
5954 decoding _ = Decoder.text_strict
60−instance HasDecoding 'PGText Lazy.Text where
55+instance HasDecoding (column ::: 'Required ('NotNull 'PGText)) Lazy.Text where
6156 decoding _ = Decoder.text_lazy
62−instance HasDecoding ('PGChar 1) Char where
57+instance HasDecoding (column ::: 'Required ('NotNull ('PGChar 1))) Char where
6358 decoding _ = Decoder.char
64−instance HasDecoding 'PGNumeric Scientific where
59+instance HasDecoding (column ::: 'Required ('NotNull 'PGNumeric)) Scientific where
6560 decoding _ = Decoder.numeric
66−instance HasDecoding 'PGUuid UUID where
61+instance HasDecoding (column ::: 'Required ('NotNull 'PGUuid)) UUID where
6762 decoding _ = Decoder.uuid
68−instance FromJSON x => HasDecoding 'PGJson x where
69− decoding _ = Decoder.json_bytes (left Text.pack . eitherDecodeStrict)
70−instance FromJSON x => HasDecoding 'PGJsonb x where
71− decoding _ = Decoder.jsonb_bytes (left Text.pack . eitherDecodeStrict)
72−instance HasDecoding 'PGDate Day where
63+instance HasDecoding (column ::: 'Required ('NotNull 'PGJson)) Aeson.Value where
64+ decoding _ = Decoder.json_ast
65+instance HasDecoding (column ::: 'Required ('NotNull 'PGJsonb)) Aeson.Value where
66+ decoding _ = Decoder.jsonb_ast
67+instance HasDecoding (column ::: 'Required ('NotNull 'PGDate)) Day where
7368 decoding _ = Decoder.date
74−instance HasDecoding 'PGTime TimeOfDay where
69+instance HasDecoding (column ::: 'Required ('NotNull 'PGTime)) TimeOfDay where
7570 decoding _ = Decoder.time_int
76−instance HasDecoding 'PGTimeTZ (TimeOfDay, TimeZone) where
71+instance HasDecoding (column ::: 'Required ('NotNull 'PGTimeTZ)) (TimeOfDay, TimeZone) where
7772 decoding _ = Decoder.timetz_int
78−instance HasDecoding 'PGTimestamp LocalTime where
73+instance HasDecoding (column ::: 'Required ('NotNull 'PGTimestamp)) LocalTime where
7974 decoding _ = Decoder.timestamp_int
80−instance HasDecoding 'PGTimestampTZ UTCTime where
75+instance HasDecoding (column ::: 'Required ('NotNull 'PGTimestampTZ)) UTCTime where
8176 decoding _ = Decoder.timestamptz_int
82−instance HasDecoding 'PGInterval DiffTime where
77+instance HasDecoding (column ::: 'Required ('NotNull 'PGInterval)) DiffTime where
8378 decoding _ = Decoder.interval_int
8479
8580decodings
@@ -93,53 +88,53 @@ decodings (_ :: proxy pgs) (K bytestring :* bytestrings) =
9388 <$> (I <$> Decoder.valueParser (decoding proxy) bytestring)
9489 <*> decodings proxies bytestrings
9590
96−class HasEncoding x pg where
91+class HasEncoding pg x where
9792 encoding :: proxy pg -> x -> Encoder.Encoding
98−instance HasEncoding Int16 'PGInt2 where
93+instance HasEncoding ('Required ('NotNull 'PGInt2)) Int16 where
9994 encoding _ = Encoder.int2_int16
100−instance HasEncoding Int32 'PGInt4 where
95+instance HasEncoding ('Required ('NotNull 'PGInt4)) Int32 where
10196 encoding _ = Encoder.int4_int32
102−instance HasEncoding Int64 'PGInt8 where
97+instance HasEncoding ('Required ('NotNull 'PGInt8)) Int64 where
10398 encoding _ = Encoder.int8_int64
104−instance HasEncoding Float 'PGFloat4 where
99+instance HasEncoding ('Required ('NotNull 'PGFloat4)) Float where
105100 encoding _ = Encoder.float4
106−instance HasEncoding Double 'PGFloat8 where
101+instance HasEncoding ('Required ('NotNull 'PGFloat8)) Double where
107102 encoding _ = Encoder.float8
108−instance HasEncoding Bool 'PGBool where
103+instance HasEncoding ('Required ('NotNull 'PGBool)) Bool where
109104 encoding _ = Encoder.bool
110−instance HasEncoding ByteString 'PGBytea where
105+instance HasEncoding ('Required ('NotNull 'PGBytea)) ByteString where
111106 encoding _ = Encoder.bytea_strict
112−instance HasEncoding Lazy.ByteString 'PGBytea where
107+instance HasEncoding ('Required ('NotNull 'PGBytea)) Lazy.ByteString where
113108 encoding _ = Encoder.bytea_lazy
114−instance HasEncoding Text 'PGText where
109+instance HasEncoding ('Required ('NotNull 'PGText)) Text where
115110 encoding _ = Encoder.text_strict
116−instance HasEncoding Lazy.Text 'PGText where
111+instance HasEncoding ('Required ('NotNull 'PGText)) Lazy.Text where
117112 encoding _ = Encoder.text_lazy
118−instance HasEncoding Char ('PGChar 1) where
113+instance HasEncoding ('Required ('NotNull ('PGChar 1))) Char where
119114 encoding _ = Encoder.char_utf8
120−instance HasEncoding Scientific 'PGNumeric where
115+instance HasEncoding ('Required ('NotNull 'PGNumeric)) Scientific where
121116 encoding _ = Encoder.numeric
122−instance HasEncoding UUID 'PGUuid where
117+instance HasEncoding ('Required ('NotNull 'PGUuid)) UUID where
123118 encoding _ = Encoder.uuid
124−instance ToJSON x => HasEncoding x 'PGJson where
125− encoding _ = Encoder.json_bytes . Lazy.toStrict . encode
126−instance ToJSON x => HasEncoding x 'PGJsonb where
127− encoding _ = Encoder.jsonb_bytes . Lazy.toStrict . encode
128−instance HasEncoding Day 'PGDate where
119+instance HasEncoding ('Required ('NotNull 'PGJson)) Aeson.Value where
120+ encoding _ = Encoder.json_ast
121+instance HasEncoding ('Required ('NotNull 'PGJsonb)) Aeson.Value where
122+ encoding _ = Encoder.jsonb_ast
123+instance HasEncoding ('Required ('NotNull 'PGDate)) Day where
129124 encoding _ = Encoder.date
130−instance HasEncoding TimeOfDay 'PGTime where
125+instance HasEncoding ('Required ('NotNull 'PGTime)) TimeOfDay where
131126 encoding _ = Encoder.time_int
132−instance HasEncoding (TimeOfDay, TimeZone) 'PGTimeTZ where
127+instance HasEncoding ('Required ('NotNull 'PGTimeTZ)) (TimeOfDay, TimeZone) where
133128 encoding _ = Encoder.timetz_int
134−instance HasEncoding LocalTime 'PGTimestamp where
129+instance HasEncoding ('Required ('NotNull 'PGTimestamp)) LocalTime where
135130 encoding _ = Encoder.timestamp_int
136−instance HasEncoding UTCTime 'PGTimestampTZ where
131+instance HasEncoding ('Required ('NotNull 'PGTimestampTZ)) UTCTime where
137132 encoding _ = Encoder.timestamptz_int
138−instance HasEncoding DiffTime 'PGInterval where
133+instance HasEncoding ('Required ('NotNull 'PGInterval)) DiffTime where
139134 encoding _ = Encoder.interval_int
140135
141136encodings
142− :: AllZip HasEncoding xs pgs
137+ :: AllZip HasEncoding pgs xs
143138 => proxy pgs
144139 -> NP I xs
145140 -> [ByteString]