@@ -21,13 +21,10 @@ module Database.PostgreSQL.Simple.ToField
21 , inQuotes
21 , inQuotes
24−import Blaze.ByteString.Builder (Builder, fromByteString, fromLazyByteString,
24+import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
26import Blaze.ByteString.Builder.Char8 (fromChar)
25import Blaze.ByteString.Builder.Char8 (fromChar)
27import Blaze.Text (integral, double, float)
26import Blaze.Text (integral, double, float)
28import Data.ByteString (ByteString)
27import Data.ByteString (ByteString)
29−import qualified Data.ByteString.Base16 as B16
30−import qualified Data.ByteString.Base16.Lazy as L16
31import Data.Int (Int8, Int16, Int32, Int64)
28import Data.Int (Int8, Int16, Int32, Int64)
32import Data.List (intersperse)
29import Data.List (intersperse)
33import Data.Monoid (mappend)
30import Data.Monoid (mappend)
@@ -58,14 +55,18 @@ data Action =
58 -- ^ Escape and enclose in quotes before substituting. Use for all
55 -- ^ Escape and enclose in quotes before substituting. Use for all
59 -- text-like types, and anything else that may contain unsafe
56 -- text-like types, and anything else that may contain unsafe
60 -- characters when rendered.
57 -- characters when rendered.
58+ | EscapeBytea ByteString
59+ -- ^ Escape binary data for use as a @bytea@ literal. Include surrounding
60+ -- quotes. This is used by the 'Binary' newtype wrapper.
61 | Many [Action]
61 | Many [Action]
62 -- ^ Concatenate a series of rendering actions.
62 -- ^ Concatenate a series of rendering actions.
63 deriving (Typeable)
63 deriving (Typeable)
65instance Show Action where
65instance Show Action where
66− show (Plain b) = "Plain " ++ show (toByteString b)
66+ show (Plain b) = "Plain " ++ show (toByteString b)
67− show (Escape b) = "Escape " ++ show b
67+ show (Escape b) = "Escape " ++ show b
68+ show (EscapeBytea b) = "EscapeBytea " ++ show b
68− show (Many b) = "Many " ++ show b
69+ show (Many b) = "Many " ++ show b
70-- | A type that may be used as a single parameter to a SQL query.
71-- | A type that may be used as a single parameter to a SQL query.
71class ToField a where
72class ToField a where
@@ -88,16 +89,6 @@ instance (ToField a) => ToField (In [a]) where
88 (intersperse (Plain (fromChar ',')) . map toField $ xs) ++
89 (intersperse (Plain (fromChar ',')) . map toField $ xs) ++
89 [Plain (fromChar ')')]
90 [Plain (fromChar ')')]
91−instance ToField (Binary SB.ByteString) where
92− toField (Binary bs) = Plain $ fromByteString "'\\x" `mappend`
93− fromByteString (B16.encode bs) `mappend`
96−instance ToField (Binary LB.ByteString) where
97− toField (Binary bs) = Plain $ fromByteString "'\\x" `mappend`
98− fromLazyByteString (L16.encode bs) `mappend`
101renderNull :: Action
92renderNull :: Action
102renderNull = Plain (fromByteString "null")
93renderNull = Plain (fromByteString "null")
@@ -168,6 +159,14 @@ instance ToField Double where
168 | otherwise = Plain (double v)
159 | otherwise = Plain (double v)
169 {-# INLINE toField #-}
160 {-# INLINE toField #-}
162+instance ToField (Binary SB.ByteString) where
163+ toField (Binary bs) = EscapeBytea bs
164+ {-# INLINE toField #-}
166+instance ToField (Binary LB.ByteString) where
167+ toField (Binary bs) = (EscapeBytea . SB.concat . LB.toChunks) bs
168+ {-# INLINE toField #-}
171instance ToField SB.ByteString where
170instance ToField SB.ByteString where
172 toField = Escape
171 toField = Escape
173 {-# INLINE toField #-}
172 {-# INLINE toField #-}