@@ -22,11 +22,14 @@ module Database.PostgreSQL.Simple.ToField
2222 , inQuotes
2323 ) where
2424
25−import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
26−import Blaze.ByteString.Builder.Char8 (fromChar)
27−import Blaze.Text (integral, double, float)
2825import qualified Data.Aeson as JSON
29−import Data.ByteString (ByteString)
26+import Data.ByteString (ByteString)
27+import Data.ByteString.Builder
28+ ( Builder, byteString, char8, stringUtf8
29+ , intDec, int8Dec, int16Dec, int32Dec, int64Dec, integerDec
30+ , wordDec, word8Dec, word16Dec, word32Dec, word64Dec
31+ , floatDec, doubleDec
32+ )
3033import Data.Int (Int8, Int16, Int32, Int64)
3134import Data.List (intersperse)
3235import Data.Monoid (mappend)
@@ -35,7 +38,8 @@ import Data.Typeable (Typeable)
3538import Data.Word (Word, Word8, Word16, Word32, Word64)
3639import {-# SOURCE #-} Database.PostgreSQL.Simple.ToRow
3740import Database.PostgreSQL.Simple.Types
38−import qualified Blaze.ByteString.Builder.Char.Utf8 as Utf8
41+import Database.PostgreSQL.Simple.Compat (toByteString)
42+
3943import qualified Data.ByteString as SB
4044import qualified Data.ByteString.Lazy as LB
4145import qualified Data.Text as ST
@@ -54,6 +58,7 @@ import Data.Text.Lazy.Builder.Scientific (scientificBuilder)
5458#else
5559import Data.Scientific (scientificBuilder)
5660#endif
61+import Foreign.C.Types (CUInt(..))
5762
5863-- | How to render an element when substituting it into a query.
5964data Action =
@@ -99,84 +104,84 @@ instance (ToField a) => ToField (Maybe a) where
99104 {-# INLINE toField #-}
100105
101106instance (ToField a) => ToField (In [a]) where
102− toField (In []) = Plain $ fromByteString "(null)"
107+ toField (In []) = Plain $ byteString "(null)"
103108 toField (In xs) = Many $
104− Plain (fromChar '(') :
105− (intersperse (Plain (fromChar ',')) . map toField $ xs) ++
106− [Plain (fromChar ')')]
109+ Plain (char8 '(') :
110+ (intersperse (Plain (char8 ',')) . map toField $ xs) ++
111+ [Plain (char8 ')')]
107112
108113renderNull :: Action
109−renderNull = Plain (fromByteString "null")
114+renderNull = Plain (byteString "null")
110115
111116instance ToField Null where
112117 toField _ = renderNull
113118 {-# INLINE toField #-}
114119
115120instance ToField Default where
116− toField _ = Plain (fromByteString "default")
121+ toField _ = Plain (byteString "default")
117122 {-# INLINE toField #-}
118123
119124instance ToField Bool where
120− toField True = Plain (fromByteString "true")
121− toField False = Plain (fromByteString "false")
125+ toField True = Plain (byteString "true")
126+ toField False = Plain (byteString "false")
122127 {-# INLINE toField #-}
123128
124129instance ToField Int8 where
125− toField = Plain . integral
130+ toField = Plain . int8Dec
126131 {-# INLINE toField #-}
127132
128133instance ToField Int16 where
129− toField = Plain . integral
134+ toField = Plain . int16Dec
130135 {-# INLINE toField #-}
131136
132137instance ToField Int32 where
133− toField = Plain . integral
138+ toField = Plain . int32Dec
134139 {-# INLINE toField #-}
135140
136141instance ToField Int where
137− toField = Plain . integral
142+ toField = Plain . intDec
138143 {-# INLINE toField #-}
139144
140145instance ToField Int64 where
141− toField = Plain . integral
146+ toField = Plain . int64Dec
142147 {-# INLINE toField #-}
143148
144149instance ToField Integer where
145− toField = Plain . integral
150+ toField = Plain . integerDec
146151 {-# INLINE toField #-}
147152
148153instance ToField Word8 where
149− toField = Plain . integral
154+ toField = Plain . word8Dec
150155 {-# INLINE toField #-}
151156
152157instance ToField Word16 where
153− toField = Plain . integral
158+ toField = Plain . word16Dec
154159 {-# INLINE toField #-}
155160
156161instance ToField Word32 where
157− toField = Plain . integral
162+ toField = Plain . word32Dec
158163 {-# INLINE toField #-}
159164
160165instance ToField Word where
161− toField = Plain . integral
166+ toField = Plain . wordDec
162167 {-# INLINE toField #-}
163168
164169instance ToField Word64 where
165− toField = Plain . integral
170+ toField = Plain . word64Dec
166171 {-# INLINE toField #-}
167172
168173instance ToField PQ.Oid where
169− toField = Plain . integral . \(PQ.Oid x) -> x
174+ toField = Plain . \(PQ.Oid (CUInt x)) -> word32Dec x
170175 {-# INLINE toField #-}
171176
172177instance ToField Float where
173− toField v | isNaN v || isInfinite v = Plain (inQuotes (float v))
174− | otherwise = Plain (float v)
178+ toField v | isNaN v || isInfinite v = Plain (inQuotes (floatDec v))
179+ | otherwise = Plain (floatDec v)
175180 {-# INLINE toField #-}
176181
177182instance ToField Double where
178− toField v | isNaN v || isInfinite v = Plain (inQuotes (double v))
179− | otherwise = Plain (double v)
183+ toField v | isNaN v || isInfinite v = Plain (inQuotes (doubleDec v))
184+ | otherwise = Plain (doubleDec v)
180185 {-# INLINE toField #-}
181186
182187instance ToField Scientific where
@@ -198,7 +203,7 @@ instance ToField Identifier where
198203instance ToField QualifiedIdentifier where
199204 toField (QualifiedIdentifier (Just s) t) =
200205 Many [ EscapeIdentifier (ST.encodeUtf8 s)
201− , Plain (fromChar '.')
206+ , Plain (char8 '.')
202207 , EscapeIdentifier (ST.encodeUtf8 t)
203208 ]
204209 toField (QualifiedIdentifier Nothing t) =
@@ -218,7 +223,7 @@ instance ToField ST.Text where
218223 {-# INLINE toField #-}
219224
220225instance ToField [Char] where
221− toField = Escape . toByteString . Utf8.fromString
226+ toField = Escape . toByteString . stringUtf8
222227 {-# INLINE toField #-}
223228
224229instance ToField LT.Text where
@@ -268,9 +273,9 @@ instance ToField NominalDiffTime where
268273
269274instance (ToField a) => ToField (PGArray a) where
270275 toField xs = Many $
271− Plain (fromByteString "ARRAY[") :
272− (intersperse (Plain (fromChar ',')) . map toField $ fromPGArray xs) ++
273− [Plain (fromChar ']')]
276+ Plain (byteString "ARRAY[") :
277+ (intersperse (Plain (char8 ',')) . map toField $ fromPGArray xs) ++
278+ [Plain (char8 ']')]
274279 -- Because the ARRAY[...] input syntax is being used, it is possible
275280 -- that the use of type-specific separator characters is unnecessary.
276281
@@ -278,7 +283,7 @@ instance (ToField a) => ToField (Vector a) where
278283 toField = toField . PGArray . V.toList
279284
280285instance ToField UUID where
281− toField = Plain . inQuotes . fromByteString . UUID.toASCIIBytes
286+ toField = Plain . inQuotes . byteString . UUID.toASCIIBytes
282287
283288instance ToField JSON.Value where
284289 toField = toField . JSON.encode
@@ -297,7 +302,7 @@ toJSONField = toField . JSON.toJSON
297302-- This function /does not/ perform any other escaping.
298303inQuotes :: Builder -> Builder
299304inQuotes b = quote `mappend` b `mappend` quote
300− where quote = Utf8.fromChar '\''
305+ where quote = char8 '\''
301306
302307interleaveFoldr :: (a -> [b] -> [b]) -> b -> [b] -> [a] -> [b]
303308interleaveFoldr f b bs as = foldr (\a bs -> b : f a bs) bs as
@@ -318,8 +323,8 @@ instance ToRow a => ToField (Values a) where
318323 funcname = "Database.PostgreSQL.Simple.toField :: Values a -> Action"
319324 norows = funcname ++ " either values or types must be non-empty"
320325 emptyrow = funcname ++ " each row must contain at least one column"
321− lit = Plain . fromByteString
322− litC = Plain . fromChar
326+ lit = Plain . byteString
327+ litC = Plain . char8
323328 values x = Many (lit "(VALUES ": x)
324329
325330 typedField :: (Action, QualifiedIdentifier) -> [Action] -> [Action]