@@ -287,14 +287,21 @@ parseTemplate template =
287287 skipSpace = B.dropWhile isSpace_ascii
288288
289289escapeStringConn :: Connection -> ByteString -> IO (Either ByteString ByteString)
290−escapeStringConn conn s =
291− withConnection conn $ \c ->
292− PQ.escapeStringConn c s >>= checkError c
290+escapeStringConn = escapeWrap PQ.escapeStringConn
291+
292+escapeIdentifier :: Connection -> ByteString -> IO (Either ByteString ByteString)
293+escapeIdentifier = escapeWrap PQ.escapeIdentifier
293294
294295escapeByteaConn :: Connection -> ByteString -> IO (Either ByteString ByteString)
295−escapeByteaConn conn s =
296+escapeByteaConn = escapeWrap PQ.escapeByteaConn
297+
298+escapeWrap :: (PQ.Connection -> ByteString -> IO (Maybe ByteString))
299+ -> Connection
300+ -> ByteString
301+ -> IO (Either ByteString ByteString)
302+escapeWrap f conn s =
296303 withConnection conn $ \c ->
297− PQ.escapeByteaConn c s >>= checkError c
304+ f c s >>= checkError c
298305
299306checkError :: PQ.Connection -> Maybe a -> IO (Either ByteString a)
300307checkError _ (Just x) = return $ Right x
@@ -302,13 +309,15 @@ checkError c Nothing = Left . maybe "" id <$> PQ.errorMessage c
302309
303310buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
304311buildQuery conn q template xs = zipParams (split template) <$> mapM sub xs
305− where quote = either (\msg -> fmtError (utf8ToString msg) q xs)
306− (inQuotes . fromByteString)
312+ where err' msg = fmtError (utf8ToString msg) q xs
313+ quote = either err' (inQuotes . fromByteString)
307314 utf8ToString = T.unpack . TE.decodeUtf8
308− sub (Plain b) = pure b
309− sub (Escape s) = quote <$> escapeStringConn conn s
310− sub (EscapeByteA s) = quote <$> escapeByteaConn conn s
311− sub (Many ys) = mconcat <$> mapM sub ys
315+ sub (Plain b) = pure b
316+ sub (Escape s) = quote <$> escapeStringConn conn s
317+ sub (EscapeByteA s) = quote <$> escapeByteaConn conn s
318+ sub (EscapeIdentifier s) = either err' fromByteString <$>
319+ escapeIdentifier conn s
320+ sub (Many ys) = mconcat <$> mapM sub ys
312321 split s = fromByteString h : if B.null t then [] else split (B.tail t)
313322 where (h,t) = B.break (=='?') s
314323 zipParams (t:ts) (p:ps) = t <> p <> zipParams ts ps
@@ -606,10 +615,11 @@ fmtError msg q xs = throw FormatError {
606615 , fmtQuery = q
607616 , fmtParams = map twiddle xs
608617 }
609− where twiddle (Plain b) = toByteString b
610− twiddle (Escape s) = s
611− twiddle (EscapeByteA s) = s
612− twiddle (Many ys) = B.concat (map twiddle ys)
618+ where twiddle (Plain b) = toByteString b
619+ twiddle (Escape s) = s
620+ twiddle (EscapeByteA s) = s
621+ twiddle (EscapeIdentifier s) = s
622+ twiddle (Many ys) = B.concat (map twiddle ys)
613623
614624-- $use
615625--