@@ -1,8 +1,14 @@
1{-# LANGUAGE
1{-# LANGUAGE
3 , FlexibleInstances
3 , FlexibleInstances
4+ , FunctionalDependencies
4 , KindSignatures
5 , KindSignatures
5 , MultiParamTypeClasses
6 , MultiParamTypeClasses
8module Squeel.PostgreSQL.Value where
14module Squeel.PostgreSQL.Value where
@@ -11,10 +17,13 @@ import Control.Arrow (left)
11import Data.Aeson (FromJSON, ToJSON, eitherDecodeStrict, encode)
17import Data.Aeson (FromJSON, ToJSON, eitherDecodeStrict, encode)
12import Data.Bits (Bits)
18import Data.Bits (Bits)
13import Data.ByteString (ByteString)
19import Data.ByteString (ByteString)
20+import Data.ByteString.Builder
14import qualified Data.ByteString.Lazy as Lazy (ByteString, toStrict)
21import qualified Data.ByteString.Lazy as Lazy (ByteString, toStrict)
15import Data.Int (Int16,Int32,Int64)
22import Data.Int (Int16,Int32,Int64)
25+import Data.Vinyl.Functor
16import PostgreSQL.Binary.Decoder (Decoder)
26import PostgreSQL.Binary.Decoder (Decoder)
17−import PostgreSQL.Binary.Encoder (Encoder)
18import qualified PostgreSQL.Binary.Decoder as Decoder
27import qualified PostgreSQL.Binary.Decoder as Decoder
19import qualified PostgreSQL.Binary.Encoder as Encoder
28import qualified PostgreSQL.Binary.Encoder as Encoder
20import Data.Proxy (Proxy)
29import Data.Proxy (Proxy)
@@ -73,7 +82,7 @@ instance FromValue ('PGType "interval") DiffTime where
73 fromValue _ = Decoder.interval_int
82 fromValue _ = Decoder.interval_int
75class ToValue x (pg :: PGType) where
84class ToValue x (pg :: PGType) where
76− toValue :: Proxy pg -> Encoder x
85+ toValue :: Proxy pg -> x -> Builder
77instance ToValue Int16 ('PGType "int2") where
86instance ToValue Int16 ('PGType "int2") where
78 toValue _ = Encoder.int2_int16
87 toValue _ = Encoder.int2_int16
79instance ToValue Int32 ('PGType "int4") where
88instance ToValue Int32 ('PGType "int4") where
@@ -123,6 +132,10 @@ instance ToValue UTCTime ('PGType "timestamptz") where
123instance ToValue DiffTime ('PGType "interval") where
132instance ToValue DiffTime ('PGType "interval") where
124 toValue _ = Encoder.interval_int
133 toValue _ = Encoder.interval_int
126−type family ToParameters ps xs where
135+class ToValues xs pgs where
127− ToParameters '[] '[] = ()
136+ toValues :: Proxy pgs -> Rec Identity xs -> [ByteString]
128− ToParameters (x ': xs) (p ': ps) = (ToValue x p, ToParameters xs ps)
137+instance ToValues '[] '[] where toValues _ _ = []
138+instance (ToValue x pg, ToValues xs pgs)
139+ => ToValues (x ': xs) (pg ': pgs) where
140+ toValues (_ :: Proxy (pg ': pgs)) (Identity x :& xs) =
141+ Encoder.run (toValue (Proxy @pg)) x : toValues (Proxy @pgs) xs