@@ -4,6 +4,7 @@
4{-# LANGUAGE PatternGuards #-}
4{-# LANGUAGE PatternGuards #-}
5{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE ViewPatterns #-}
6{-# LANGUAGE ViewPatterns #-}
7+{-# LANGUAGE QuasiQuotes #-}
8------------------------------------------------------------------------------
9------------------------------------------------------------------------------
@@ -13,6 +14,7 @@
13-- License: BSD3
14-- License: BSD3
14-- Maintainer: Leon P Smith <leon@melding-monads.com>
15-- Maintainer: Leon P Smith <leon@melding-monads.com>
15-- Stability: experimental
16-- Stability: experimental
17+-- Portability: portable
17-- A mid-level client library for the PostgreSQL database, aimed at ease of
19-- A mid-level client library for the PostgreSQL database, aimed at ease of
18-- use and high performance.
20-- use and high performance.
@@ -42,7 +44,7 @@ module Database.PostgreSQL.Simple
42 -- ** Modifying multiple rows at once
44 -- ** Modifying multiple rows at once
45− -- ** @RETURNING@: modifications that return results
47+ -- ** @RETURNING@: modifications that returns results
46 -- $returning
48 -- $returning
48 -- * Extracting results
50 -- * Extracting results
@@ -55,7 +57,8 @@ module Database.PostgreSQL.Simple
57 -- * Types
59 -- * Types
58− Connection
61+ , Connection
@@ -64,26 +67,20 @@ module Database.PostgreSQL.Simple
64 , Only(..)
67 , Only(..)
65 , (:.)(..)
68 , (:.)(..)
66 -- ** Exceptions
69 -- ** Exceptions
67− , SomePostgreSqlException(..)
68 , SqlError(..)
70 , SqlError(..)
69 , PQ.ExecStatus(..)
71 , PQ.ExecStatus(..)
70− , FormatError(..)
72+ , FormatError(fmtMessage, fmtQuery, fmtParams)
71− , QueryError(..)
73+ , QueryError(qeMessage, qeQuery)
72− , ResultError(..)
74+ , ResultError(errSQLType, errHaskellType, errMessage)
73 -- * Connection management
75 -- * Connection management
74− , Base.connectPostgreSQL
76 , Base.connect
76 , Base.connect
77− , Base.withConnect
77+ , Base.connectPostgreSQL
78− , Base.ConnectInfo(..)
79− , Base.defaultConnectInfo
80 , Base.postgreSQLConnectionString
78 , Base.postgreSQLConnectionString
79+ , Base.defaultConnectInfo
81 -- * Queries that return results
81 -- * Queries that return results
84− -- ** Queries taking parser as argument
87 -- * Queries that stream results
84 -- * Queries that stream results
88 , FoldOptions(..)
85 , FoldOptions(..)
89 , FetchQuantity(..)
86 , FetchQuantity(..)
@@ -95,14 +92,6 @@ module Database.PostgreSQL.Simple
96 , forEach_
93 , forEach_
97 , returning
94 , returning
98− -- ** Queries that stream results taking a parser as an argument
100− , foldWithOptionsAndParser
102− , foldWithOptionsAndParser_
106 -- * Statements that do not return results
95 -- * Statements that do not return results
107 , execute
96 , execute
108 , execute_
97 , execute_
@@ -110,9 +99,20 @@ module Database.PostgreSQL.Simple
110-- , Base.insertID
99-- , Base.insertID
111 -- * Transaction handling
100 -- * Transaction handling
112 , withTransaction
101 , withTransaction
113− , withSavepoint
102+ , withTransactionSerializable
103+ , TransactionMode(..)
104+ , IsolationLevel(..)
106+ , defaultTransactionMode
107+ , defaultIsolationLevel
108+ , defaultReadWriteMode
109+ , withTransactionLevel
110+ , withTransactionMode
111+ , withTransactionModeRetry
114-- , Base.autocommit
112-- , Base.autocommit
117 , rollback
117 , rollback
118 -- * Helper functions
118 -- * Helper functions
@@ -120,27 +120,50 @@ module Database.PostgreSQL.Simple
120 , formatQuery
120 , formatQuery
123−import Data.ByteString.Builder (Builder, byteString, char8)
123+import Blaze.ByteString.Builder
124+ ( Builder, fromByteString, toByteString )
125+import Blaze.ByteString.Builder.Char8 (fromChar)
124−import Control.Applicative ((<$>))
126+import Control.Applicative ((<$>), pure)
125−import Control.Exception as E
127+import Control.Concurrent.MVar
128+import Control.Exception
129+ ( Exception, onException, throw, throwIO, finally
130+ , try, SomeException, fromException )
131+import Control.Monad (foldM)
126import Data.ByteString (ByteString)
132import Data.ByteString (ByteString)
127import Data.Int (Int64)
133import Data.Int (Int64)
134+import qualified Data.IntMap as IntMap
128import Data.List (intersperse)
135import Data.List (intersperse)
129import Data.Monoid (mconcat)
136import Data.Monoid (mconcat)
137+import Data.Typeable (Typeable)
138+import Database.PostgreSQL.Simple.BuiltinTypes ( oid2typname )
130−import Database.PostgreSQL.Simple.Compat ((<>), toByteString)
139+import Database.PostgreSQL.Simple.Compat ( mask, (<>) )
131−import Database.PostgreSQL.Simple.Cursor
132import Database.PostgreSQL.Simple.FromField (ResultError(..))
140import Database.PostgreSQL.Simple.FromField (ResultError(..))
133import Database.PostgreSQL.Simple.FromRow (FromRow(..))
141import Database.PostgreSQL.Simple.FromRow (FromRow(..))
134−import Database.PostgreSQL.Simple.ToField (Action(..))
142+import Database.PostgreSQL.Simple.Ok
143+import Database.PostgreSQL.Simple.ToField (Action(..), inQuotes)
135import Database.PostgreSQL.Simple.ToRow (ToRow(..))
144import Database.PostgreSQL.Simple.ToRow (ToRow(..))
136import Database.PostgreSQL.Simple.Types
145import Database.PostgreSQL.Simple.Types
137 ( Binary(..), In(..), Only(..), Query(..), (:.)(..) )
146 ( Binary(..), In(..), Only(..), Query(..), (:.)(..) )
138import Database.PostgreSQL.Simple.Internal as Base
147import Database.PostgreSQL.Simple.Internal as Base
139−import Database.PostgreSQL.Simple.Internal.PQResultUtils
148+import Database.PostgreSQL.Simple.SqlQQ (sql)
140−import Database.PostgreSQL.Simple.Transaction
141import qualified Database.PostgreSQL.LibPQ as PQ
149import qualified Database.PostgreSQL.LibPQ as PQ
142import qualified Data.ByteString.Char8 as B
150import qualified Data.ByteString.Char8 as B
143−
151+import qualified Data.Text as T
152+import qualified Data.Text.Encoding as TE
153+import qualified Data.Vector as V
154+import Control.Monad.Trans.Reader
155+import Control.Monad.Trans.State.Strict
157+-- | Exception thrown if a 'Query' could not be formatted correctly.
158+-- This may occur if the number of \'@?@\' characters in the query
159+-- string does not match the number of parameters provided.
160+data FormatError = FormatError {
161+ fmtMessage :: String
163+ , fmtParams :: [ByteString]
164+ } deriving (Eq, Show, Typeable)
166+instance Exception FormatError
145-- | Format a query string.
168-- | Format a query string.
@@ -177,9 +200,9 @@ formatMany conn q@(Query template) qs = do
177 case parseTemplate template of
200 case parseTemplate template of
178 Just (before, qbits, after) -> do
201 Just (before, qbits, after) -> do
179 bs <- mapM (buildQuery conn q qbits . toRow) qs
202 bs <- mapM (buildQuery conn q qbits . toRow) qs
180− return . toByteString . mconcat $ byteString before :
203+ return . toByteString . mconcat $ fromByteString before :
181− intersperse (char8 ',') bs ++
204+ intersperse (fromChar ',') bs ++
182− [byteString after]
205+ [fromByteString after]
183 Nothing -> fmtError "syntax error in multi-row template" q []
206 Nothing -> fmtError "syntax error in multi-row template" q []
185-- Split the input string into three pieces, @before@, @qbits@, and @after@,
208-- Split the input string into three pieces, @before@, @qbits@, and @after@,
@@ -277,40 +300,43 @@ parseTemplate template =
278 skipSpace = B.dropWhile isSpace_ascii
301 skipSpace = B.dropWhile isSpace_ascii
303+escapeStringConn :: Connection -> ByteString -> IO (Either ByteString ByteString)
304+escapeStringConn conn s =
305+ withConnection conn $ \c ->
306+ PQ.escapeStringConn c s >>= checkError c
308+escapeByteaConn :: Connection -> ByteString -> IO (Either ByteString ByteString)
309+escapeByteaConn conn s =
310+ withConnection conn $ \c ->
311+ PQ.escapeByteaConn c s >>= checkError c
313+checkError :: PQ.Connection -> Maybe a -> IO (Either ByteString a)
314+checkError _ (Just x) = return $ Right x
315+checkError c Nothing = Left . maybe "" id <$> PQ.errorMessage c
281buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
317buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
282−buildQuery conn q template xs =
318+buildQuery conn q template xs = zipParams (split template) <$> mapM sub xs
283− zipParams (split template) <$> mapM (buildAction conn q xs) xs
319+ where quote = either (\msg -> fmtError (utf8ToString msg) q xs)
284− where split s =
320+ (inQuotes . fromByteString)
285− -- This part escapes double '??'s to make literal '?'s possible
321+ utf8ToString = T.unpack . TE.decodeUtf8
286− -- in PostgreSQL queries using the JSON operators: @?@, @?|@ and @?&@
322+ sub (Plain b) = pure b
287− let (h,t) = breakOnSingleQuestionMark s
323+ sub (Escape s) = quote <$> escapeStringConn conn s
288− in byteString h
324+ sub (EscapeByteA s) = quote <$> escapeByteaConn conn s
289− : if B.null t
325+ sub (Many ys) = mconcat <$> mapM sub ys
291− else split (B.tail t)
326+ split s = fromByteString h : if B.null t then [] else split (B.tail t)
327+ where (h,t) = B.break (=='?') s
292 zipParams (t:ts) (p:ps) = t <> p <> zipParams ts ps
328 zipParams (t:ts) (p:ps) = t <> p <> zipParams ts ps
293 zipParams [t] [] = t
329 zipParams [t] [] = t
294− zipParams _ _ = fmtError (show countSingleQs ++
330+ zipParams _ _ = fmtError (show (B.count '?' template) ++
295− " single '?' characters, but " ++
331+ " '?' characters, but " ++
296 show (length xs) ++ " parameters") q xs
332 show (length xs) ++ " parameters") q xs
297− countSingleQs = go 0 template
298− where go i "" = (i :: Int)
299− go i bs = case qms of
300− ("?","?") -> go i nextQMBS
301− ("?",_) -> go (i+1) nextQMBS
303− where qms = B.splitAt 1 qmBS
304− (qmBS,nextQMBS) = B.splitAt 2 qmBS'
305− qmBS' = B.dropWhile (/= '?') bs
307-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
334-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
308-- expected to return results.
335-- expected to return results.
310-- Returns the number of rows affected.
337-- Returns the number of rows affected.
312−-- Throws 'FormatError' if the query could not be formatted correctly, or
339+-- Throws 'FormatError' if the query could not be formatted correctly.
313−-- a 'SqlError' exception if the backend returns an error.
314execute :: (ToRow q) => Connection -> Query -> q -> IO Int64
340execute :: (ToRow q) => Connection -> Query -> q -> IO Int64
315execute conn template qs = do
341execute conn template qs = do
316 result <- exec conn =<< formatQuery conn template qs
342 result <- exec conn =<< formatQuery conn template qs
@@ -319,33 +345,9 @@ execute conn template qs = do
319-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not
345-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not
320-- expected to return results.
346-- expected to return results.
322−-- Returns the number of rows affected. If the list of parameters is empty,
348+-- Returns the number of rows affected.
323−-- this function will simply return 0 without issuing the query to the backend.
324−-- If this is not desired, consider using the 'Values' constructor instead.
326−-- Throws 'FormatError' if the query could not be formatted correctly, or
327−-- a 'SqlError' exception if the backend returns an error.
329−-- For example, here's a command that inserts two rows into a table
333−-- executeMany c [sql|
334−-- INSERT INTO sometable VALUES (?,?)
335−-- |] [(1, \"hello\"),(2, \"world\")]
338−-- Here's an canonical example of a multi-row update command:
340−-- @
350+-- Throws 'FormatError' if the query could not be formatted correctly.
341−-- executeMany c [sql|
344−-- FROM (VALUES (?,?)) as upd(x,y)
345−-- WHERE sometable.x = upd.x
346−-- |] [(1, \"hello\"),(2, \"world\")]
349executeMany :: (ToRow q) => Connection -> Query -> [q] -> IO Int64
351executeMany :: (ToRow q) => Connection -> Query -> [q] -> IO Int64
350executeMany _ _ [] = return 0
352executeMany _ _ [] = return 0
351executeMany conn q qs = do
353executeMany conn q qs = do
@@ -359,20 +361,12 @@ executeMany conn q qs = do
359-- in cases where you are only inserting a single row, and do not need
361-- in cases where you are only inserting a single row, and do not need
360-- functionality analogous to 'executeMany'.
362-- functionality analogous to 'executeMany'.
362−-- If the list of parameters is empty, this function will simply return @[]@
363−-- without issuing the query to the backend. If this is not desired,
364−-- consider using the 'Values' constructor instead.
366-- Throws 'FormatError' if the query could not be formatted correctly.
364-- Throws 'FormatError' if the query could not be formatted correctly.
367returning :: (ToRow q, FromRow r) => Connection -> Query -> [q] -> IO [r]
365returning :: (ToRow q, FromRow r) => Connection -> Query -> [q] -> IO [r]
368−returning = returningWith fromRow
370−-- | A version of 'returning' taking parser as argument
371−returningWith :: (ToRow q) => RowParser r -> Connection -> Query -> [q] -> IO [r]
372−returningWith _ _ _ [] = return []
366+returning _ _ [] = return []
373−returningWith parser conn q qs = do
367+returning conn q qs = do
374 result <- exec conn =<< formatMany conn q qs
368 result <- exec conn =<< formatMany conn q qs
375− finishQueryWith parser conn q result
369+ finishQuery conn q result
377-- | Perform a @SELECT@ or other SQL query that is expected to return
371-- | Perform a @SELECT@ or other SQL query that is expected to return
378-- results. All results are retrieved and converted before this
372-- results. All results are retrieved and converted before this
@@ -389,27 +383,17 @@ returningWith parser conn q qs = do
389-- using 'execute' instead of 'query').
383-- using 'execute' instead of 'query').
391-- * 'ResultError': result conversion failed.
385-- * 'ResultError': result conversion failed.
393−-- * 'SqlError': the postgresql backend returned an error, e.g.
394−-- a syntax or type error, or an incorrect table or column name.
395−query :: (ToRow q, FromRow r) => Connection -> Query -> q -> IO [r]
386+query :: (ToRow q, FromRow r)
396−query = queryWith fromRow
387+ => Connection -> Query -> q -> IO [r]
388+query conn template qs = do
389+ result <- exec conn =<< formatQuery conn template qs
390+ finishQuery conn template result
398-- | A version of 'query' that does not perform query substitution.
392-- | A version of 'query' that does not perform query substitution.
399query_ :: (FromRow r) => Connection -> Query -> IO [r]
393query_ :: (FromRow r) => Connection -> Query -> IO [r]
400−query_ = queryWith_ fromRow
402−-- | A version of 'query' taking parser as argument
403−queryWith :: ToRow q => RowParser r -> Connection -> Query -> q -> IO [r]
404−queryWith parser conn template qs = do
405− result <- exec conn =<< formatQuery conn template qs
406− finishQueryWith parser conn template result
408−-- | A version of 'query_' taking parser as argument
409−queryWith_ :: RowParser r -> Connection -> Query -> IO [r]
410−queryWith_ parser conn q@(Query que) = do
394+query_ conn q@(Query que) = do
411 result <- exec conn que
395 result <- exec conn que
412− finishQueryWith parser conn q result
396+ finishQuery conn q result
414-- | Perform a @SELECT@ or other SQL query that is expected to return
398-- | Perform a @SELECT@ or other SQL query that is expected to return
415-- results. Results are streamed incrementally from the server, and
399-- results. Results are streamed incrementally from the server, and
@@ -421,12 +405,6 @@ queryWith_ parser conn q@(Query que) = do
421-- This fold is /not/ strict. The stream consumer is responsible for
405-- This fold is /not/ strict. The stream consumer is responsible for
422-- forcing the evaluation of its result to avoid space leaks.
406-- forcing the evaluation of its result to avoid space leaks.
424−-- This is implemented using a database cursor. As such, this requires
425−-- a transaction. This function will detect whether or not there is a
426−-- transaction in progress, and will create a 'ReadCommitted' 'ReadOnly'
427−-- transaction if needed. The cursor is given a unique temporary name,
428−-- so the consumer may itself call fold.
430-- Exceptions that may be thrown:
408-- Exceptions that may be thrown:
432-- * 'FormatError': the query string could not be formatted correctly.
410-- * 'FormatError': the query string could not be formatted correctly.
@@ -435,9 +413,7 @@ queryWith_ parser conn q@(Query que) = do
435-- using 'execute' instead of 'query').
413-- using 'execute' instead of 'query').
437-- * 'ResultError': result conversion failed.
415-- * 'ResultError': result conversion failed.
439−-- * 'SqlError': the postgresql backend returned an error, e.g.
440−-- a syntax or type error, or an incorrect table or column name.
441fold :: ( FromRow row, ToRow params )
417fold :: ( FromRow row, ToRow params )
442 => Connection
418 => Connection
@@ -447,20 +423,6 @@ fold :: ( FromRow row, ToRow params )
448fold = foldWithOptions defaultFoldOptions
424fold = foldWithOptions defaultFoldOptions
450−-- | A version of 'fold' taking a parser as an argument
451−foldWith :: ( ToRow params )
457− -> (a -> row -> IO a)
459−foldWith = foldWithOptionsAndParser defaultFoldOptions
461−-- | Number of rows to fetch at a time. 'Automatic' currently defaults
462−-- to 256 rows, although it might be nice to make this more intelligent
463−-- based on e.g. the average size of the rows.
464data FetchQuantity
426data FetchQuantity
465 = Automatic
427 = Automatic
466 | Fixed !Int
428 | Fixed !Int
@@ -471,19 +433,12 @@ data FoldOptions
471 transactionMode :: !TransactionMode
433 transactionMode :: !TransactionMode
474−-- | defaults to 'Automatic', and 'TransactionMode' 'ReadCommitted' 'ReadOnly'
475defaultFoldOptions :: FoldOptions
436defaultFoldOptions :: FoldOptions
476defaultFoldOptions = FoldOptions {
437defaultFoldOptions = FoldOptions {
477 fetchQuantity = Automatic,
438 fetchQuantity = Automatic,
478 transactionMode = TransactionMode ReadCommitted ReadOnly
439 transactionMode = TransactionMode ReadCommitted ReadOnly
481−-- | The same as 'fold', but this provides a bit more control over
482−-- lower-level details. Currently, the number of rows fetched per
483−-- round-trip to the server and the transaction mode may be adjusted
484−-- accordingly. If the connection is already in a transaction,
485−-- then the existing transaction is used and thus the 'transactionMode'
486−-- option is ignored.
487foldWithOptions :: ( FromRow row, ToRow params )
442foldWithOptions :: ( FromRow row, ToRow params )
488 => FoldOptions
443 => FoldOptions
489 -> Connection
444 -> Connection
@@ -492,21 +447,9 @@ foldWithOptions :: ( FromRow row, ToRow params )
493 -> (a -> row -> IO a)
448 -> (a -> row -> IO a)
495−foldWithOptions opts = foldWithOptionsAndParser opts fromRow
450+foldWithOptions opts conn template qs a f = do
497−-- | A version of 'foldWithOptions' taking a parser as an argument
498−foldWithOptionsAndParser :: (ToRow params)
505− -> (a -> row -> IO a)
507−foldWithOptionsAndParser opts parser conn template qs a f = do
508 q <- formatQuery conn template qs
451 q <- formatQuery conn template qs
509− doFold opts parser conn template (Query q) a f
452+ doFold opts conn template (Query q) a f
511-- | A version of 'fold' that does not perform query substitution.
454-- | A version of 'fold' that does not perform query substitution.
512fold_ :: (FromRow r) =>
455fold_ :: (FromRow r) =>
@@ -517,14 +460,6 @@ fold_ :: (FromRow r) =>
518fold_ = foldWithOptions_ defaultFoldOptions
461fold_ = foldWithOptions_ defaultFoldOptions
520−-- | A version of 'fold_' taking a parser as an argument
521−foldWith_ :: RowParser r
527−foldWith_ = foldWithOptionsAndParser_ defaultFoldOptions
529foldWithOptions_ :: (FromRow r) =>
464foldWithOptions_ :: (FromRow r) =>
530 FoldOptions
465 FoldOptions
@@ -533,27 +468,18 @@ foldWithOptions_ :: (FromRow r) =>
533 -> a -- ^ Initial state for result consumer.
468 -> a -- ^ Initial state for result consumer.
534 -> (a -> r -> IO a) -- ^ Result consumer.
469 -> (a -> r -> IO a) -- ^ Result consumer.
536−foldWithOptions_ opts conn query' a f = doFold opts fromRow conn query' query' a f
471+foldWithOptions_ opts conn query a f = doFold opts conn query query a f
538−-- | A version of 'foldWithOptions_' taking a parser as an argument
473+
539−foldWithOptionsAndParser_ :: FoldOptions
474+doFold :: ( FromRow row )
540− -> RowParser r
475+ => FoldOptions
542− -> Query -- ^ Query.
543− -> a -- ^ Initial state for result consumer.
544− -> (a -> r -> IO a) -- ^ Result consumer.
546−foldWithOptionsAndParser_ opts parser conn query' a f = doFold opts parser conn query' query' a f
548−doFold :: FoldOptions
550 -> Connection
476 -> Connection
554 -> (a -> row -> IO a)
480 -> (a -> row -> IO a)
556−doFold FoldOptions{..} parser conn _template q a0 f = do
482+doFold FoldOptions{..} conn _template q a f = do
557 stat <- withConnection conn PQ.transactionStatus
483 stat <- withConnection conn PQ.transactionStatus
558 case stat of
484 case stat of
559 PQ.TransIdle -> withTransactionMode transactionMode conn go
485 PQ.TransIdle -> withTransactionMode transactionMode conn go
@@ -571,17 +497,10 @@ doFold FoldOptions{..} parser conn _template q a0 f = do
571 PQ.TransUnknown -> fail "foldWithOpts FIXME: PQ.TransUnknown"
497 PQ.TransUnknown -> fail "foldWithOpts FIXME: PQ.TransUnknown"
572 -- Not sure what this means.
498 -- Not sure what this means.
574− declare =
500+ go = do
575− declareCursor conn q
501+ -- FIXME: what about name clashes with already-declared cursors?
576− fetch cursor a =
502+ _ <- execute_ conn ("DECLARE fold NO SCROLL CURSOR FOR " <> q)
577− foldForwardWithParser cursor parser chunkSize f a
503+ loop a `finally` execute_ conn "CLOSE fold"
579− go = bracket declare closeCursor $ \cursor ->
580− let loop a = fetch cursor a >>=
586-- FIXME: choose the Automatic chunkSize more intelligently
505-- FIXME: choose the Automatic chunkSize more intelligently
587-- One possibility is to use the type of the results, although this
506-- One possibility is to use the type of the results, although this
@@ -592,6 +511,9 @@ doFold FoldOptions{..} parser conn _template q a0 f = do
592 chunkSize = case fetchQuantity of
511 chunkSize = case fetchQuantity of
593 Automatic -> 256
512 Automatic -> 256
594 Fixed n -> n
513 Fixed n -> n
515+ rs <- query conn "FETCH FORWARD ? FROM fold" (Only chunkSize)
516+ if null rs then return a else foldM f a rs >>= loop
596-- | A version of 'fold' that does not transform a state value.
518-- | A version of 'fold' that does not transform a state value.
597forEach :: (ToRow q, FromRow r) =>
519forEach :: (ToRow q, FromRow r) =>
@@ -600,37 +522,245 @@ forEach :: (ToRow q, FromRow r) =>
600 -> q -- ^ Query parameters.
522 -> q -- ^ Query parameters.
601 -> (r -> IO ()) -- ^ Result consumer.
523 -> (r -> IO ()) -- ^ Result consumer.
603−forEach = forEachWith fromRow
525+forEach conn template qs = fold conn template qs () . const
604{-# INLINE forEach #-}
526{-# INLINE forEach #-}
606−-- | A version of 'forEach' taking a parser as an argument
607−forEachWith :: ( ToRow q )
614−forEachWith parser conn template qs = foldWith parser conn template qs () . const
615−{-# INLINE forEachWith #-}
617-- | A version of 'forEach' that does not perform query substitution.
528-- | A version of 'forEach' that does not perform query substitution.
618forEach_ :: (FromRow r) =>
529forEach_ :: (FromRow r) =>
619 Connection
530 Connection
620 -> Query -- ^ Query template.
531 -> Query -- ^ Query template.
621 -> (r -> IO ()) -- ^ Result consumer.
532 -> (r -> IO ()) -- ^ Result consumer.
623−forEach_ = forEachWith_ fromRow
534+forEach_ conn template = fold_ conn template () . const
624{-# INLINE forEach_ #-}
535{-# INLINE forEach_ #-}
626−forEachWith_ :: RowParser r
537+forM' :: (Ord n, Num n) => n -> n -> (n -> IO a) -> IO [a]
627− -> Connection
538+forM' lo hi m = loop hi []
629− -> (r -> IO ())
540+ loop !n !as
630− -> IO ()
541+ | n < lo = return as
631−forEachWith_ parser conn template = foldWith_ parser conn template () . const
542+ | otherwise = do
632−{-# INLINE forEachWith_ #-}
543+ a <- m n
633−
544+ loop (n-1) (a:as)
546+finishQuery :: (FromRow r) => Connection -> Query -> PQ.Result -> IO [r]
547+finishQuery conn q result = do
548+ status <- PQ.resultStatus result
551+ throwIO $ QueryError "query: Empty query" q
553+ throwIO $ QueryError "query resulted in a command response" q
555+ ncols <- PQ.nfields result
556+ let unCol (PQ.Col x) = fromIntegral x :: Int
557+ typeinfos <- V.generateM (unCol ncols)
558+ (\(PQ.Col . fromIntegral -> col) -> do
559+ getTypeInfo conn =<< PQ.ftype result col)
560+ nrows <- PQ.ntuples result
561+ ncols <- PQ.nfields result
562+ forM' 0 (nrows-1) $ \row -> do
563+ let rw = Row row typeinfos result
564+ case runStateT (runReaderT (unRP fromRow) rw) 0 of
565+ Ok (val,col) | col == ncols -> return val
567+ vals <- forM' 0 (ncols-1) $ \c -> do
568+ v <- PQ.getvalue result row c
569+ return ( typeinfos V.! unCol c
571+ throw (ConversionFailed
572+ (show (unCol ncols) ++ " values: " ++ show vals)
575+ (show (unCol col) ++ " slots in target type")
576+ "mismatch between number of columns to \
577+ \convert and number in target type")
578+ Errors [] -> throwIO $ ConversionFailed "" Nothing "" "" "unknown error"
579+ Errors [x] -> throwIO x
580+ Errors xs -> throwIO $ ManyErrors xs
582+ throwIO $ QueryError "query: COPY TO is not supported" q
584+ throwIO $ QueryError "query: COPY FROM is not supported" q
585+ PQ.BadResponse -> throwResultError "query" result status
586+ PQ.NonfatalError -> throwResultError "query" result status
587+ PQ.FatalError -> throwResultError "query" result status
589+ellipsis :: ByteString -> ByteString
591+ | B.length bs > 15 = B.take 10 bs `B.append` "[...]"
595+-- | Of the four isolation levels defined by the SQL standard,
596+-- these are the three levels distinguished by PostgreSQL as of version 9.0.
597+-- See <http://www.postgresql.org/docs/9.1/static/transaction-iso.html>
598+-- for more information. Note that prior to PostgreSQL 9.0, 'RepeatableRead'
599+-- was equivalent to 'Serializable'.
602+ = DefaultIsolationLevel -- ^ the isolation level will be taken from
603+ -- PostgreSQL's per-connection
604+ -- @default_transaction_isolation@ variable,
605+ -- which is initialized according to the
606+ -- server's config. The default configuration
607+ -- is 'ReadCommitted'.
611+ deriving (Show, Eq, Ord, Enum, Bounded)
614+ = DefaultReadWriteMode -- ^ the read-write mode will be taken from
615+ -- PostgreSQL's per-connection
616+ -- @default_transaction_read_only@ variable,
617+ -- which is initialized according to the
618+ -- server's config. The default configuration
622+ deriving (Show, Eq, Ord, Enum, Bounded)
624+data TransactionMode = TransactionMode {
625+ isolationLevel :: !IsolationLevel,
626+ readWriteMode :: !ReadWriteMode
627+ } deriving (Show, Eq)
629+defaultTransactionMode :: TransactionMode
630+defaultTransactionMode = TransactionMode
631+ defaultIsolationLevel
632+ defaultReadWriteMode
634+defaultIsolationLevel :: IsolationLevel
635+defaultIsolationLevel = DefaultIsolationLevel
637+defaultReadWriteMode :: ReadWriteMode
638+defaultReadWriteMode = DefaultReadWriteMode
640+-- | Execute an action inside a SQL transaction.
642+-- This function initiates a transaction with a \"@begin
643+-- transaction@\" statement, then executes the supplied action. If
644+-- the action succeeds, the transaction will be completed with
645+-- 'Base.commit' before this function returns.
647+-- If the action throws /any/ kind of exception (not just a
648+-- PostgreSQL-related exception), the transaction will be rolled back using
649+-- 'rollback', then the exception will be rethrown.
650+withTransaction :: Connection -> IO a -> IO a
651+withTransaction = withTransactionMode defaultTransactionMode
653+-- | Execute an action inside of a 'Serializable' transaction. If a
654+-- serialization failure occurs, roll back the transaction and try again.
655+-- Be warned that this may execute the IO action multiple times.
657+-- A 'Serializable' transaction creates the illusion that your program has
658+-- exclusive access to the database. This means that, even in a concurrent
659+-- setting, you can perform queries in sequence without having to worry about
660+-- what might happen between one statement and the next.
662+-- Think of it as STM, but without @retry@.
663+withTransactionSerializable :: Connection -> IO a -> IO a
664+withTransactionSerializable =
665+ withTransactionModeRetry
667+ { isolationLevel = Serializable
668+ , readWriteMode = ReadWrite
670+ retryOnNotSerializable
672+ retryOnNotSerializable exception =
674+ SqlError{..} | sqlState == serialization_failure
677+ -- http://www.postgresql.org/docs/current/static/errcodes-appendix.html
678+ serialization_failure = "40001"
680+-- | Execute an action inside a SQL transaction with a given isolation level.
681+withTransactionLevel :: IsolationLevel -> Connection -> IO a -> IO a
682+withTransactionLevel lvl
683+ = withTransactionMode defaultTransactionMode { isolationLevel = lvl }
685+-- | Execute an action inside a SQL transaction with a given transaction mode.
686+withTransactionMode :: TransactionMode -> Connection -> IO a -> IO a
687+withTransactionMode mode conn act =
688+ mask $ \restore -> do
690+ r <- restore act `onException` rollback conn
694+-- | Like 'withTransactionMode', but also takes a custom callback to
695+-- determine if a transaction should be retried if an 'SqlError' occurs.
696+-- If the callback returns True, then the transaction will be retried.
697+-- If the callback returns False, or an exception other than an 'SqlError'
698+-- occurs then the transaction will be rolled back and the exception rethrown.
700+-- This is used to implement 'withTransactionSerializable'.
701+withTransactionModeRetry :: TransactionMode -> (SqlError -> Bool) -> Connection -> IO a -> IO a
702+withTransactionModeRetry mode shouldRetry conn act =
704+ retryLoop $ try $ do
709+ retryLoop :: IO (Either SomeException a) -> IO a
716+ case fmap shouldRetry (fromException e) of
717+ Just True -> retryLoop act'
722+-- | Rollback a transaction.
723+rollback :: Connection -> IO ()
724+rollback conn = execute_ conn "ABORT" >> return ()
726+-- | Commit a transaction.
727+commit :: Connection -> IO ()
728+commit conn = execute_ conn "COMMIT" >> return ()
730+-- | Begin a transaction.
731+begin :: Connection -> IO ()
732+begin = beginMode defaultTransactionMode
734+-- | Begin a transaction with a given isolation level
735+beginLevel :: IsolationLevel -> Connection -> IO ()
736+beginLevel lvl = beginMode defaultTransactionMode { isolationLevel = lvl }
738+-- | Begin a transaction with a given transaction mode
739+beginMode :: TransactionMode -> Connection -> IO ()
740+beginMode mode conn = do
741+ _ <- execute_ conn $! Query (B.concat ["BEGIN", isolevel, readmode])
744+ isolevel = case isolationLevel mode of
745+ DefaultIsolationLevel -> ""
746+ ReadCommitted -> " ISOLATION LEVEL READ COMMITTED"
747+ RepeatableRead -> " ISOLATION LEVEL REPEATABLE READ"
748+ Serializable -> " ISOLATION LEVEL SERIALIZABLE"
749+ readmode = case readWriteMode mode of
750+ DefaultReadWriteMode -> ""
751+ ReadWrite -> " READ WRITE"
752+ ReadOnly -> " READ ONLY"
754+fmtError :: String -> Query -> [Action] -> a
755+fmtError msg q xs = throw FormatError {
758+ , fmtParams = map twiddle xs
760+ where twiddle (Plain b) = toByteString b
761+ twiddle (Escape s) = s
762+ twiddle (EscapeByteA s) = s
763+ twiddle (Many ys) = B.concat (map twiddle ys)
@@ -658,11 +788,9 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
659-- > import Database.PostgreSQL.Simple
789-- > import Database.PostgreSQL.Simple
662-- > hello = do
791-- > hello = do
663−-- > conn <- connectPostgreSQL ""
792+-- > conn <- connect defaultConnectInfo
664−-- > [Only i] <- query_ conn "select 2 + 2"
793+-- > query conn "select 2 + 2"
667-- A 'Query' value does not represent the actual query that will be
795-- A 'Query' value does not represent the actual query that will be
668-- executed, but is a template for constructing the final query.
796-- executed, but is a template for constructing the final query.
@@ -723,30 +851,15 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
723-- The same kind of problem can arise with string literals if you have
851-- The same kind of problem can arise with string literals if you have
724-- the @OverloadedStrings@ language extension enabled. Again, just
852-- the @OverloadedStrings@ language extension enabled. Again, just
725-- use an explicit type signature if this happens.
853-- use an explicit type signature if this happens.
727−-- Finally, remember that the compiler must be able to infer the type
728−-- of a query's /results/ as well as its parameters. We might like
729−-- the following example to work:
731−-- > print =<< query_ conn "select 2 + 2"
733−-- Unfortunately, while a quick glance tells us that the result type
734−-- should be a single row containing a single numeric column, the
735−-- compiler has no way to infer what the types are. We can easily fix
736−-- this by providing an explicit type annotation:
738−-- > xs <- query_ conn "select 2 + 2"
739−-- > print (xs :: [Only Int])
741-- $only_param
855-- $only_param
743-- Haskell lacks a single-element tuple type, so if you have just one
857-- Haskell lacks a single-element tuple type, so if you have just one
744−-- value you want substituted into a query or a single-column result,
858+-- value you want substituted into a query, what should you do?
745−-- what should you do?
747-- The obvious approach would appear to be something like this:
860-- The obvious approach would appear to be something like this:
749−-- > instance (ToField a) => ToRow a where
862+-- > instance (Param a) => QueryParam a where
752-- Unfortunately, this wreaks havoc with type inference, so we take a
865-- Unfortunately, this wreaks havoc with type inference, so we take a
@@ -757,9 +870,6 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
758-- > execute conn "insert into users (first_name) values (?)"
871-- > execute conn "insert into users (first_name) values (?)"
759-- > ["Nuala"]
872-- > ["Nuala"]
761−-- A row of /n/ query results is represented using an /n/-tuple, so
762−-- you should use 'Only' to represent a single-column result.
@@ -777,7 +887,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
777-- Here's an example:
887-- Here's an example:
779-- > query conn "select * from users where first_name in ?" $
889-- > query conn "select * from users where first_name in ?" $
780−-- > Only $ In ["Anna", "Boris", "Carla"]
890+-- > In ["Anna", "Boris", "Carla"]
782-- If your 'In'-wrapped list is empty, the string @\"(null)\"@ will be
892-- If your 'In'-wrapped list is empty, the string @\"(null)\"@ will be
783-- substituted instead, to ensure that your clause remains
893-- substituted instead, to ensure that your clause remains
@@ -805,7 +915,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
805-- * There must be no other \"@?@\" characters anywhere in your
915-- * There must be no other \"@?@\" characters anywhere in your
806-- template.
916-- template.
808−-- * There must be one or more \"@?@\" in the parentheses.
918+-- * There must one or more \"@?@\" in the parentheses.
810-- * Extra white space is fine.
920-- * Extra white space is fine.
@@ -828,10 +938,9 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
829-- $returning
939-- $returning
831−-- PostgreSQL supports returning values from data manipulation statements
941+-- The 'returning' function is similar to 'executeMany' but is
832−-- such as @INSERT@ and @UPDATE@. You can use these statements by
942+-- intended for use with multi-tuple @INSERT@ or @UPDATE@ statements
833−-- using 'query' instead of 'execute'. For multi-tuple inserts,
943+-- that make use of @RETURNING@.
834−-- use 'returning' instead of 'executeMany'.
836-- For example, were there an auto-incrementing @id@ column and
945-- For example, were there an auto-incrementing @id@ column and
837-- timestamp column @t@ that defaulted to the present time for the
946-- timestamp column @t@ that defaulted to the present time for the
@@ -839,8 +948,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
839-- sales records and also return their new @id@s and timestamps.
948-- sales records and also return their new @id@s and timestamps.
841-- > let q = "insert into sales (amount, label) values (?,?) returning id, t"
950-- > let q = "insert into sales (amount, label) values (?,?) returning id, t"
842−-- > xs :: [(Int, UTCTime)] <- query conn q (15,"Sawdust")
843−-- > ys :: [(Int, UTCTime)] <- returning conn q [(20,"Chips"),(300,"Wood")]
951+-- > xs :: [(Int, UTCTime)] <- returning conn q [(20,"Chips"),(300,"Wood")]
845-- $result
953-- $result
@@ -876,7 +984,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
877-- Although SQL can accommodate @NULL@ as a value for any of these
985-- Although SQL can accommodate @NULL@ as a value for any of these
878-- types, Haskell cannot. If your result contains columns that may be
986-- types, Haskell cannot. If your result contains columns that may be
879−-- @NULL@, be sure that you use 'Maybe' in those positions of your
987+-- @NULL@, be sure that you use 'Maybe' in those positions of of your
882-- > (Text, Maybe Int, Int)
990-- > (Text, Maybe Int, Int)
@@ -902,7 +1010,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
902-- all values of the given PostgreSQL type is considered \"compatible\".
1010-- all values of the given PostgreSQL type is considered \"compatible\".
903-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
1011-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
904-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
1012-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
905−-- represent a @SMALLINT@, so it is considered compatible with those types.
1013+-- represent a @SMALLINT@, so it is considered compatble with those types.
907-- * A numeric compatibility check is based only on the type of a
1015-- * A numeric compatibility check is based only on the type of a
908-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
1016-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
@@ -916,3 +1024,33 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
916-- UTF-8. If you use some other encoding, decoding may fail or give
1024-- UTF-8. If you use some other encoding, decoding may fail or give
917-- wrong results. In such cases, write a @newtype@ wrapper and a
1025-- wrong results. In such cases, write a @newtype@ wrapper and a
918-- custom 'Result' instance to handle your encoding.
1026-- custom 'Result' instance to handle your encoding.
1028+getTypeInfo :: Connection -> PQ.Oid -> IO TypeInfo
1029+getTypeInfo conn@Connection{..} oid =
1030+ case oid2typname oid of
1031+ Just name -> return $! TypeInfo { typ = NamedOid oid name
1032+ , typelem = Nothing
1034+ Nothing -> modifyMVar connectionObjects $ \oidmap -> do
1035+ case IntMap.lookup (oid2int oid) oidmap of
1036+ Just typeinfo -> return (oidmap, typeinfo)
1038+ names <- query conn
1039+ [sql| SELECT p.oid, p.typname, c.oid, c.typname
1040+ FROM pg_type AS p LEFT OUTER JOIN pg_type AS c
1041+ ON c.oid = p.typelem
1044+ typinf <- case names of
1045+ [] -> return $ throw (fatalError "invalid type oid")
1046+ [(pOid, pTypName, mbCOid, mbCTypName)] ->
1047+ return $! TypeInfo { typ = NamedOid pOid pTypName
1050+ cTypName <- mbCTypName
1051+ return $ NamedOid cOid cTypName
1053+ _ -> fail "typename query returned more than one result"
1054+ -- oid is a primary key, so the query should
1055+ -- never return more than one result
1056+ return (IntMap.insert (oid2int oid) typinf oidmap, typinf)