@@ -22,11 +22,14 @@ module Database.PostgreSQL.Simple.ToField
22 , inQuotes
22 , inQuotes
25−import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
26−import Blaze.ByteString.Builder.Char8 (fromChar)
27−import Blaze.Text (integral, double, float)
28import qualified Data.Aeson as JSON
25import 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
30import Data.Int (Int8, Int16, Int32, Int64)
33import Data.Int (Int8, Int16, Int32, Int64)
31import Data.List (intersperse)
34import Data.List (intersperse)
32import Data.Monoid (mappend)
35import Data.Monoid (mappend)
@@ -35,7 +38,8 @@ import Data.Typeable (Typeable)
35import Data.Word (Word, Word8, Word16, Word32, Word64)
38import Data.Word (Word, Word8, Word16, Word32, Word64)
36import {-# SOURCE #-} Database.PostgreSQL.Simple.ToRow
39import {-# SOURCE #-} Database.PostgreSQL.Simple.ToRow
37import Database.PostgreSQL.Simple.Types
40import Database.PostgreSQL.Simple.Types
38−import qualified Blaze.ByteString.Builder.Char.Utf8 as Utf8
41+import Database.PostgreSQL.Simple.Compat (toByteString)
39import qualified Data.ByteString as SB
43import qualified Data.ByteString as SB
40import qualified Data.ByteString.Lazy as LB
44import qualified Data.ByteString.Lazy as LB
41import qualified Data.Text as ST
45import qualified Data.Text as ST
@@ -54,6 +58,7 @@ import Data.Text.Lazy.Builder.Scientific (scientificBuilder)
55import Data.Scientific (scientificBuilder)
59import Data.Scientific (scientificBuilder)
61+import Foreign.C.Types (CUInt(..))
58-- | How to render an element when substituting it into a query.
63-- | How to render an element when substituting it into a query.
59data Action =
64data Action =
@@ -99,84 +104,84 @@ instance (ToField a) => ToField (Maybe a) where
99 {-# INLINE toField #-}
104 {-# INLINE toField #-}
101instance (ToField a) => ToField (In [a]) where
106instance (ToField a) => ToField (In [a]) where
102− toField (In []) = Plain $ fromByteString "(null)"
107+ toField (In []) = Plain $ byteString "(null)"
103 toField (In xs) = Many $
108 toField (In xs) = Many $
104− Plain (fromChar '(') :
109+ Plain (char8 '(') :
105− (intersperse (Plain (fromChar ',')) . map toField $ xs) ++
110+ (intersperse (Plain (char8 ',')) . map toField $ xs) ++
106− [Plain (fromChar ')')]
111+ [Plain (char8 ')')]
108renderNull :: Action
113renderNull :: Action
109−renderNull = Plain (fromByteString "null")
114+renderNull = Plain (byteString "null")
111instance ToField Null where
116instance ToField Null where
112 toField _ = renderNull
117 toField _ = renderNull
113 {-# INLINE toField #-}
118 {-# INLINE toField #-}
115instance ToField Default where
120instance ToField Default where
116− toField _ = Plain (fromByteString "default")
121+ toField _ = Plain (byteString "default")
117 {-# INLINE toField #-}
122 {-# INLINE toField #-}
119instance ToField Bool where
124instance ToField Bool where
120− toField True = Plain (fromByteString "true")
125+ toField True = Plain (byteString "true")
121− toField False = Plain (fromByteString "false")
126+ toField False = Plain (byteString "false")
122 {-# INLINE toField #-}
127 {-# INLINE toField #-}
124instance ToField Int8 where
129instance ToField Int8 where
125− toField = Plain . integral
130+ toField = Plain . int8Dec
126 {-# INLINE toField #-}
131 {-# INLINE toField #-}
128instance ToField Int16 where
133instance ToField Int16 where
129− toField = Plain . integral
134+ toField = Plain . int16Dec
130 {-# INLINE toField #-}
135 {-# INLINE toField #-}
132instance ToField Int32 where
137instance ToField Int32 where
133− toField = Plain . integral
138+ toField = Plain . int32Dec
134 {-# INLINE toField #-}
139 {-# INLINE toField #-}
136instance ToField Int where
141instance ToField Int where
137− toField = Plain . integral
142+ toField = Plain . intDec
138 {-# INLINE toField #-}
143 {-# INLINE toField #-}
140instance ToField Int64 where
145instance ToField Int64 where
141− toField = Plain . integral
146+ toField = Plain . int64Dec
142 {-# INLINE toField #-}
147 {-# INLINE toField #-}
144instance ToField Integer where
149instance ToField Integer where
145− toField = Plain . integral
150+ toField = Plain . integerDec
146 {-# INLINE toField #-}
151 {-# INLINE toField #-}
148instance ToField Word8 where
153instance ToField Word8 where
149− toField = Plain . integral
154+ toField = Plain . word8Dec
150 {-# INLINE toField #-}
155 {-# INLINE toField #-}
152instance ToField Word16 where
157instance ToField Word16 where
153− toField = Plain . integral
158+ toField = Plain . word16Dec
154 {-# INLINE toField #-}
159 {-# INLINE toField #-}
156instance ToField Word32 where
161instance ToField Word32 where
157− toField = Plain . integral
162+ toField = Plain . word32Dec
158 {-# INLINE toField #-}
163 {-# INLINE toField #-}
160instance ToField Word where
165instance ToField Word where
161− toField = Plain . integral
166+ toField = Plain . wordDec
162 {-# INLINE toField #-}
167 {-# INLINE toField #-}
164instance ToField Word64 where
169instance ToField Word64 where
165− toField = Plain . integral
170+ toField = Plain . word64Dec
166 {-# INLINE toField #-}
171 {-# INLINE toField #-}
168instance ToField PQ.Oid where
173instance ToField PQ.Oid where
169− toField = Plain . integral . \(PQ.Oid x) -> x
174+ toField = Plain . \(PQ.Oid (CUInt x)) -> word32Dec x
170 {-# INLINE toField #-}
175 {-# INLINE toField #-}
172instance ToField Float where
177instance ToField Float where
173− toField v | isNaN v || isInfinite v = Plain (inQuotes (float v))
178+ toField v | isNaN v || isInfinite v = Plain (inQuotes (floatDec v))
174− | otherwise = Plain (float v)
179+ | otherwise = Plain (floatDec v)
175 {-# INLINE toField #-}
180 {-# INLINE toField #-}
177instance ToField Double where
182instance ToField Double where
178− toField v | isNaN v || isInfinite v = Plain (inQuotes (double v))
183+ toField v | isNaN v || isInfinite v = Plain (inQuotes (doubleDec v))
179− | otherwise = Plain (double v)
184+ | otherwise = Plain (doubleDec v)
180 {-# INLINE toField #-}
185 {-# INLINE toField #-}
182instance ToField Scientific where
187instance ToField Scientific where
@@ -198,7 +203,7 @@ instance ToField Identifier where
198instance ToField QualifiedIdentifier where
203instance ToField QualifiedIdentifier where
199 toField (QualifiedIdentifier (Just s) t) =
204 toField (QualifiedIdentifier (Just s) t) =
200 Many [ EscapeIdentifier (ST.encodeUtf8 s)
205 Many [ EscapeIdentifier (ST.encodeUtf8 s)
201− , Plain (fromChar '.')
206+ , Plain (char8 '.')
202 , EscapeIdentifier (ST.encodeUtf8 t)
207 , EscapeIdentifier (ST.encodeUtf8 t)
204 toField (QualifiedIdentifier Nothing t) =
209 toField (QualifiedIdentifier Nothing t) =
@@ -218,7 +223,7 @@ instance ToField ST.Text where
218 {-# INLINE toField #-}
223 {-# INLINE toField #-}
220instance ToField [Char] where
225instance ToField [Char] where
221− toField = Escape . toByteString . Utf8.fromString
226+ toField = Escape . toByteString . stringUtf8
222 {-# INLINE toField #-}
227 {-# INLINE toField #-}
224instance ToField LT.Text where
229instance ToField LT.Text where
@@ -268,9 +273,9 @@ instance ToField NominalDiffTime where
269instance (ToField a) => ToField (PGArray a) where
274instance (ToField a) => ToField (PGArray a) where
270 toField xs = Many $
275 toField xs = Many $
271− Plain (fromByteString "ARRAY[") :
276+ Plain (byteString "ARRAY[") :
272− (intersperse (Plain (fromChar ',')) . map toField $ fromPGArray xs) ++
277+ (intersperse (Plain (char8 ',')) . map toField $ fromPGArray xs) ++
273− [Plain (fromChar ']')]
278+ [Plain (char8 ']')]
274 -- Because the ARRAY[...] input syntax is being used, it is possible
279 -- Because the ARRAY[...] input syntax is being used, it is possible
275 -- that the use of type-specific separator characters is unnecessary.
280 -- that the use of type-specific separator characters is unnecessary.
@@ -278,7 +283,7 @@ instance (ToField a) => ToField (Vector a) where
278 toField = toField . PGArray . V.toList
283 toField = toField . PGArray . V.toList
280instance ToField UUID where
285instance ToField UUID where
281− toField = Plain . inQuotes . fromByteString . UUID.toASCIIBytes
286+ toField = Plain . inQuotes . byteString . UUID.toASCIIBytes
283instance ToField JSON.Value where
288instance ToField JSON.Value where
284 toField = toField . JSON.encode
289 toField = toField . JSON.encode
@@ -297,7 +302,7 @@ toJSONField = toField . JSON.toJSON
297-- This function /does not/ perform any other escaping.
302-- This function /does not/ perform any other escaping.
298inQuotes :: Builder -> Builder
303inQuotes :: Builder -> Builder
299inQuotes b = quote `mappend` b `mappend` quote
304inQuotes b = quote `mappend` b `mappend` quote
300− where quote = Utf8.fromChar '\''
305+ where quote = char8 '\''
302interleaveFoldr :: (a -> [b] -> [b]) -> b -> [b] -> [a] -> [b]
307interleaveFoldr :: (a -> [b] -> [b]) -> b -> [b] -> [a] -> [b]
303interleaveFoldr f b bs as = foldr (\a bs -> b : f a bs) bs as
308interleaveFoldr f b bs as = foldr (\a bs -> b : f a bs) bs as
@@ -318,8 +323,8 @@ instance ToRow a => ToField (Values a) where
318 funcname = "Database.PostgreSQL.Simple.toField :: Values a -> Action"
323 funcname = "Database.PostgreSQL.Simple.toField :: Values a -> Action"
319 norows = funcname ++ " either values or types must be non-empty"
324 norows = funcname ++ " either values or types must be non-empty"
320 emptyrow = funcname ++ " each row must contain at least one column"
325 emptyrow = funcname ++ " each row must contain at least one column"
321− lit = Plain . fromByteString
326+ lit = Plain . byteString
322− litC = Plain . fromChar
327+ litC = Plain . char8
323 values x = Many (lit "(VALUES ": x)
328 values x = Many (lit "(VALUES ": x)
325 typedField :: (Action, QualifiedIdentifier) -> [Action] -> [Action]
330 typedField :: (Action, QualifiedIdentifier) -> [Action] -> [Action]