@@ -1,18 +1,14 @@
1−{-# LANGUAGE DeriveDataTypeable #-}
2−{-# LANGUAGE NamedFieldPuns #-}
3−{-# LANGUAGE OverloadedStrings #-}
4−{-# LANGUAGE PatternGuards #-}
5−{-# LANGUAGE RecordWildCards #-}
6−{-# LANGUAGE ViewPatterns #-}
1+{-# LANGUAGE DeriveDataTypeable, RecordWildCards, NamedFieldPuns #-}
72
83------------------------------------------------------------------------------
94-- |
105-- Module: Database.PostgreSQL.Simple
116-- Copyright: (c) 2011 MailRank, Inc.
12−-- (c) 2011-2012 Leon P Smith
7+-- (c) 2011 Leon P Smith
138-- License: BSD3
149-- Maintainer: Leon P Smith <leon@melding-monads.com>
1510-- Stability: experimental
11+-- Portability: portable
1612--
1713-- A mid-level client library for the PostgreSQL database, aimed at ease of
1814-- use and high performance.
@@ -42,9 +38,6 @@ module Database.PostgreSQL.Simple
4238 -- ** Modifying multiple rows at once
4339 -- $many
4440
45− -- ** @RETURNING@: modifications that return results
46− -- $returning
47−
4841 -- * Extracting results
4942 -- $result
5043
@@ -55,35 +48,26 @@ module Database.PostgreSQL.Simple
5548 -- $types
5649
5750 -- * Types
58− Connection
51+ Base.ConnectInfo(..)
52+ , Connection
5953 , Query
60− , ToRow
61− , FromRow
6254 , In(..)
6355 , Binary(..)
6456 , Only(..)
65− , (:.)(..)
6657 -- ** Exceptions
67− , SomePostgreSqlException(..)
6858 , SqlError(..)
69− , PQ.ExecStatus(..)
70− , FormatError(..)
71− , QueryError(..)
72− , ResultError(..)
59+ , FormatError(fmtMessage, fmtQuery, fmtParams)
60+ , QueryError(qeMessage, qeQuery)
61+ , ResultError(errSQLType, errHaskellType, errMessage)
7362 -- * Connection management
74− , Base.connectPostgreSQL
75− , Base.close
7663 , Base.connect
77− , Base.withConnect
78− , Base.ConnectInfo(..)
79− , Base.defaultConnectInfo
64+ , Base.connectPostgreSQL
8065 , Base.postgreSQLConnectionString
66+ , Base.defaultConnectInfo
67+ , Base.close
8168 -- * Queries that return results
8269 , query
8370 , query_
84− -- ** Queries taking parser as argument
85− , queryWith
86− , queryWith_
8771 -- * Queries that stream results
8872 , FoldOptions(..)
8973 , FetchQuantity(..)
@@ -94,15 +78,6 @@ module Database.PostgreSQL.Simple
9478 , foldWithOptions_
9579 , forEach
9680 , forEach_
97− , returning
98− -- ** Queries that stream results taking a parser as an argument
99− , foldWith
100− , foldWithOptionsAndParser
101− , foldWith_
102− , foldWithOptionsAndParser_
103− , forEachWith
104− , forEachWith_
105− , returningWith
10681 -- * Statements that do not return results
10782 , execute
10883 , execute_
@@ -110,9 +85,18 @@ module Database.PostgreSQL.Simple
11085-- , Base.insertID
11186 -- * Transaction handling
11287 , withTransaction
113− , withSavepoint
88+ , TransactionMode(..)
89+ , IsolationLevel(..)
90+ , ReadWriteMode(..)
91+ , defaultTransactionMode
92+ , defaultIsolationLevel
93+ , defaultReadWriteMode
94+ , withTransactionLevel
95+ , withTransactionMode
11496-- , Base.autocommit
11597 , begin
98+ , beginLevel
99+ , beginMode
116100 , commit
117101 , rollback
118102 -- * Helper functions
@@ -120,27 +104,59 @@ module Database.PostgreSQL.Simple
120104 , formatQuery
121105 ) where
122106
123−import Data.ByteString.Builder (Builder, byteString, char8)
124−import Control.Applicative ((<$>))
125−import Control.Exception as E
107+import Blaze.ByteString.Builder
108+ ( Builder, fromByteString, toByteString )
109+import Blaze.ByteString.Builder.Char8 (fromChar)
110+import Control.Applicative ((<$>), pure)
111+import Control.Concurrent.MVar
112+import Control.Exception
113+ ( Exception, bracket, onException, throw, throwIO, finally )
114+import Control.Monad (foldM)
115+import Control.Monad.Fix (fix)
126116import Data.ByteString (ByteString)
117+import Data.Char(ord)
127118import Data.Int (Int64)
119+import qualified Data.IntMap as IntMap
128120import Data.List (intersperse)
129−import Data.Monoid (mconcat)
130−import Database.PostgreSQL.Simple.Compat ((<>), toByteString)
131−import Database.PostgreSQL.Simple.Cursor
121+import Data.Monoid (mappend, mconcat)
122+import Data.Typeable (Typeable)
123+import Database.PostgreSQL.Simple.BuiltinTypes
124+ ( oid2builtin, builtin2typname )
132125import Database.PostgreSQL.Simple.FromField (ResultError(..))
133126import Database.PostgreSQL.Simple.FromRow (FromRow(..))
134−import Database.PostgreSQL.Simple.ToField (Action(..))
135−import Database.PostgreSQL.Simple.ToRow (ToRow(..))
127+import Database.PostgreSQL.Simple.Ok
128+import Database.PostgreSQL.Simple.Param (Action(..), inQuotes)
129+import Database.PostgreSQL.Simple.QueryParams (QueryParams(..))
136130import Database.PostgreSQL.Simple.Types
137− ( Binary(..), In(..), Only(..), Query(..), (:.)(..) )
131+ ( Binary(..), In(..), Only(..), Query(..) )
138132import Database.PostgreSQL.Simple.Internal as Base
139−import Database.PostgreSQL.Simple.Internal.PQResultUtils
140−import Database.PostgreSQL.Simple.Transaction
141133import qualified Database.PostgreSQL.LibPQ as PQ
134+import Text.Regex.PCRE.Light (compile, caseless, match)
142135import qualified Data.ByteString.Char8 as B
143−
136+import qualified Data.Vector as V
137+import Control.Exception
138+import Control.Monad.Trans.Reader
139+import Control.Monad.Trans.State.Strict
140+
141+-- | Exception thrown if a 'Query' could not be formatted correctly.
142+-- This may occur if the number of \'@?@\' characters in the query
143+-- string does not match the number of parameters provided.
144+data FormatError = FormatError {
145+ fmtMessage :: String
146+ , fmtQuery :: Query
147+ , fmtParams :: [ByteString]
148+ } deriving (Eq, Show, Typeable)
149+
150+instance Exception FormatError
151+
152+-- | Exception thrown if 'query' is used to perform an @INSERT@-like
153+-- operation, or 'execute' is used to perform a @SELECT@-like operation.
154+data QueryError = QueryError {
155+ qeMessage :: String
156+ , qeQuery :: Query
157+ } deriving (Eq, Show, Typeable)
158+
159+instance Exception QueryError
144160
145161-- | Format a query string.
146162--
@@ -152,11 +168,11 @@ import qualified Data.ByteString.Char8 as B
152168--
153169-- Throws 'FormatError' if the query string could not be formatted
154170-- correctly.
155−formatQuery :: ToRow q => Connection -> Query -> q -> IO ByteString
171+formatQuery :: QueryParams q => Connection -> Query -> q -> IO ByteString
156172formatQuery conn q@(Query template) qs
157173 | null xs && '?' `B.notElem` template = return template
158174 | otherwise = toByteString <$> buildQuery conn q template xs
159− where xs = toRow qs
175+ where xs = renderParams qs
160176
161177-- | Format a query string with a variable number of rows.
162178--
@@ -171,208 +187,105 @@ formatQuery conn q@(Query template) qs
171187--
172188-- Throws 'FormatError' if the query string could not be formatted
173189-- correctly.
174−formatMany :: (ToRow q) => Connection -> Query -> [q] -> IO ByteString
190+formatMany :: (QueryParams q) => Connection -> Query -> [q] -> IO ByteString
175191formatMany _ q [] = fmtError "no rows supplied" q []
176192formatMany conn q@(Query template) qs = do
177− case parseTemplate template of
178− Just (before, qbits, after) -> do
179− bs <- mapM (buildQuery conn q qbits . toRow) qs
180− return . toByteString . mconcat $ byteString before :
181− intersperse (char8 ',') bs ++
182− [byteString after]
183− Nothing -> fmtError "syntax error in multi-row template" q []
184−
185−-- Split the input string into three pieces, @before@, @qbits@, and @after@,
186−-- following this grammar:
187−--
188−-- start: ^ before qbits after $
189−-- before: ([^?]* [^?\w])? 'VALUES' \s*
190−-- qbits: '(' \s* '?' \s* (',' \s* '?' \s*)* ')'
191−-- after: [^?]*
192−--
193−-- \s: [ \t\n\r\f]
194−-- \w: [A-Z] | [a-z] | [\x80-\xFF] | '_' | '$' | [0-9]
195−--
196−-- This would be much more concise with some sort of regex engine.
197−-- 'formatMany' used to use pcre-light instead of this hand-written parser,
198−-- but pcre is a hassle to install on Windows.
199−parseTemplate :: ByteString -> Maybe (ByteString, ByteString, ByteString)
200−parseTemplate template =
201− -- Convert input string to uppercase, to facilitate searching.
202− search $ B.map toUpper_ascii template
193+ case match re template [] of
194+ Just [_,before,qbits,after] -> do
195+ bs <- mapM (buildQuery conn q qbits . renderParams) qs
196+ return . toByteString . mconcat $ fromByteString before :
197+ intersperse (fromChar ',') bs ++
198+ [fromByteString after]
199+ _ -> error "foo"
203200 where
204− -- Search for the next occurrence of "VALUES"
205− search bs =
206− case B.breakSubstring "VALUES" bs of
207− (x, y)
208− -- If "VALUES" is not present in the string, or any '?' characters
209− -- were encountered prior to it, fail.
210− | B.null y || ('?' `B.elem` x)
211− -> Nothing
212−
213− -- If "VALUES" is preceded by an identifier character (a.k.a. \w),
214− -- try the next occurrence.
215− | not (B.null x) && isIdent (B.last x)
216− -> search $ B.drop 6 y
217−
218− -- Otherwise, we have a legitimate "VALUES" token.
219− | otherwise
220− -> parseQueryBits $ skipSpace $ B.drop 6 y
221−
222− -- Parse '(' \s* '?' \s* . If this doesn't match
223− -- (and we don't consume a '?'), look for another "VALUES".
224− --
225− -- qb points to the open paren (if present), meaning it points to the
226− -- beginning of the "qbits" production described above. This is why we
227− -- pass it down to finishQueryBits.
228− parseQueryBits qb
229− | Just ('(', skipSpace -> bs1) <- B.uncons qb
230− , Just ('?', skipSpace -> bs2) <- B.uncons bs1
231− = finishQueryBits qb bs2
232− | otherwise
233− = search qb
234−
235− -- Parse (',' \s* '?' \s*)* ')' [^?]* .
236− --
237− -- Since we've already consumed at least one '?', there's no turning back.
238− -- The parse has to succeed here, or the whole thing fails
239− -- (because we don't allow '?' to appear outside of the VALUES list).
240− finishQueryBits qb bs0
241− | Just (')', bs1) <- B.uncons bs0
242− = if '?' `B.elem` bs1
243− then Nothing
244− else Just $ slice3 template qb bs1
245− | Just (',', skipSpace -> bs1) <- B.uncons bs0
246− , Just ('?', skipSpace -> bs2) <- B.uncons bs1
247− = finishQueryBits qb bs2
248− | otherwise
249− = Nothing
250−
251− -- Slice a string into three pieces, given the start offset of the second
252− -- and third pieces. Each "offset" is actually a tail of the uppercase
253− -- version of the template string. Its length is used to infer the offset.
254− --
255− -- It is important to note that we only slice the original template.
256− -- We don't want our all-caps trick messing up the actual query string.
257− slice3 source p1 p2 =
258− (s1, s2, source'')
259− where
260− (s1, source') = B.splitAt (B.length source - B.length p1) source
261− (s2, source'') = B.splitAt (B.length p1 - B.length p2) source'
262−
263− toUpper_ascii c | c >= 'a' && c <= 'z' = toEnum (fromEnum c - 32)
264− | otherwise = c
265−
266− -- Based on the definition of {ident_cont} in src/backend/parser/scan.l
267− -- in the PostgreSQL source. No need to check [a-z], since we converted
268− -- the whole string to uppercase.
269− isIdent c = (c >= '0' && c <= '9')
270− || (c >= 'A' && c <= 'Z')
271− || (c >= '\x80' && c <= '\xFF')
272− || c == '_'
273− || c == '$'
274−
275− -- Based on {space} in scan.l
276− isSpace_ascii c = (c == ' ') || (c >= '\t' && c <= '\r')
277−
278− skipSpace = B.dropWhile isSpace_ascii
201+ re = compile "^([^?]+\\bvalues\\s*)\
202+ \(\\(\\s*[?](?:\\s*,\\s*[?])*\\s*\\))\
203+ \([^?]*)$"
204+ [caseless]
279205
206+escapeStringConn conn s = withConnection conn $ \c -> do
207+ PQ.escapeStringConn c s
280208
281209buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
282−buildQuery conn q template xs =
283− zipParams (split template) <$> mapM (buildAction conn q xs) xs
284− where split s =
285− -- This part escapes double '??'s to make literal '?'s possible
286− -- in PostgreSQL queries using the JSON operators: @?@, @?|@ and @?&@
287− let (h,t) = breakOnSingleQuestionMark s
288− in byteString h
289− : if B.null t
290− then []
291− else split (B.tail t)
292− zipParams (t:ts) (p:ps) = t <> p <> zipParams ts ps
210+buildQuery conn q template xs = zipParams (split template) <$> mapM sub xs
211+ where quote = inQuotes . fromByteString . maybe undefined id
212+ sub (Plain b) = pure b
213+ sub (Escape s) = quote <$> escapeStringConn conn s
214+ sub (Many ys) = mconcat <$> mapM sub ys
215+ split s = fromByteString h : if B.null t then [] else split (B.tail t)
216+ where (h,t) = B.break (=='?') s
217+ zipParams (t:ts) (p:ps) = t `mappend` p `mappend` zipParams ts ps
293218 zipParams [t] [] = t
294− zipParams _ _ = fmtError (show countSingleQs ++
295− " single '?' characters, but " ++
219+ zipParams _ _ = fmtError (show (B.count '?' template) ++
220+ " '?' characters, but " ++
296221 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
302− _ -> i
303− where qms = B.splitAt 1 qmBS
304− (qmBS,nextQMBS) = B.splitAt 2 qmBS'
305− qmBS' = B.dropWhile (/= '?') bs
306222
307223-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
308224-- expected to return results.
309225--
310226-- Returns the number of rows affected.
311227--
312−-- Throws 'FormatError' if the query could not be formatted correctly, or
313−-- a 'SqlError' exception if the backend returns an error.
314−execute :: (ToRow q) => Connection -> Query -> q -> IO Int64
228+-- Throws 'FormatError' if the query could not be formatted correctly.
229+execute :: (QueryParams q) => Connection -> Query -> q -> IO Int64
315230execute conn template qs = do
316231 result <- exec conn =<< formatQuery conn template qs
317232 finishExecute conn template result
318233
234+-- | A version of 'execute' that does not perform query substitution.
235+execute_ :: Connection -> Query -> IO Int64
236+execute_ conn q@(Query stmt) = do
237+ result <- exec conn stmt
238+ finishExecute conn q result
239+
319240-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not
320241-- expected to return results.
321242--
322−-- Returns the number of rows affected. If the list of parameters is empty,
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.
325−--
326−-- Throws 'FormatError' if the query could not be formatted correctly, or
327−-- a 'SqlError' exception if the backend returns an error.
328−--
329−-- For example, here's a command that inserts two rows into a table
330−-- with two columns:
331−--
332−-- @
333−-- executeMany c [sql|
334−-- INSERT INTO sometable VALUES (?,?)
335−-- |] [(1, \"hello\"),(2, \"world\")]
336−-- @
337−--
338−-- Here's an canonical example of a multi-row update command:
243+-- Returns the number of rows affected.
339244--
340−-- @
341−-- executeMany c [sql|
342−-- UPDATE sometable
343−-- SET y = upd.y
344−-- FROM (VALUES (?,?)) as upd(x,y)
345−-- WHERE sometable.x = upd.x
346−-- |] [(1, \"hello\"),(2, \"world\")]
347−-- @
348−
349−executeMany :: (ToRow q) => Connection -> Query -> [q] -> IO Int64
245+-- Throws 'FormatError' if the query could not be formatted correctly.
246+executeMany :: (QueryParams q) => Connection -> Query -> [q] -> IO Int64
350247executeMany _ _ [] = return 0
351248executeMany conn q qs = do
352249 result <- exec conn =<< formatMany conn q qs
353250 finishExecute conn q result
354251
355−-- | Execute @INSERT ... RETURNING@, @UPDATE ... RETURNING@, or other SQL
356−-- query that accepts multi-row input and is expected to return results.
357−-- Note that it is possible to write
358−-- @'query' conn "INSERT ... RETURNING ..." ...@
359−-- in cases where you are only inserting a single row, and do not need
360−-- functionality analogous to 'executeMany'.
361−--
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.
365−--
366−-- Throws 'FormatError' if the query could not be formatted correctly.
367−returning :: (ToRow q, FromRow r) => Connection -> Query -> [q] -> IO [r]
368−returning = returningWith fromRow
252+finishExecute :: Connection -> Query -> PQ.Result -> IO Int64
253+finishExecute conn q result = do
254+ status <- PQ.resultStatus result
255+ case status of
256+ PQ.CommandOk -> do
257+ ncols <- PQ.nfields result
258+ if ncols /= 0
259+ then throwIO $ QueryError ("execute resulted in " ++ show ncols ++
260+ "-column result") q
261+ else do
262+ nstr <- PQ.cmdTuples result
263+ return $ case nstr of
264+ Nothing -> 0 -- is this appropriate?
265+ Just str -> toInteger str
266+ PQ.TuplesOk -> do
267+ ncols <- PQ.nfields result
268+ throwIO $ QueryError ("execute resulted in " ++ show ncols ++
269+ "-column result") q
270+ PQ.CopyIn -> fail "FIXME: postgresql-simple does not currently support COPY IN"
271+ PQ.CopyOut -> fail "FIXME: postgresql-simple does not currently support COPY OUT"
272+ _ -> do
273+ errormsg <- maybe "" id <$> PQ.resultErrorMessage result
274+ statusmsg <- PQ.resStatus status
275+ state <- maybe "" id <$> PQ.resultErrorField result PQ.DiagSqlstate
276+ throwIO $ SqlError { sqlState = state
277+ , sqlNativeError = fromEnum status
278+ , sqlErrorMsg = B.concat [ "execute: ", statusmsg
279+ , ": ", errormsg ]}
280+ where
281+ toInteger str = B.foldl' delta 0 str
282+ where
283+ delta acc c =
284+ if '0' <= c && c <= '9'
285+ then 10 * acc + fromIntegral (ord c - ord '0')
286+ else error ("finishExecute: not an int: " ++ B.unpack str)
287+
369288
370−-- | A version of 'returning' taking parser as argument
371−returningWith :: (ToRow q) => RowParser r -> Connection -> Query -> [q] -> IO [r]
372−returningWith _ _ _ [] = return []
373−returningWith parser conn q qs = do
374− result <- exec conn =<< formatMany conn q qs
375− finishQueryWith parser conn q result
376289
377290-- | Perform a @SELECT@ or other SQL query that is expected to return
378291-- results. All results are retrieved and converted before this
@@ -389,27 +302,17 @@ returningWith parser conn q qs = do
389302-- using 'execute' instead of 'query').
390303--
391304-- * 'ResultError': result conversion failed.
392−--
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]
396−query = queryWith fromRow
305+query :: (QueryParams q, FromRow r)
306+ => Connection -> Query -> q -> IO [r]
307+query conn template qs = do
308+ result <- exec conn =<< formatQuery conn template qs
309+ finishQuery conn template result
397310
398311-- | A version of 'query' that does not perform query substitution.
399312query_ :: (FromRow r) => Connection -> Query -> IO [r]
400−query_ = queryWith_ fromRow
401−
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
407−
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
313+query_ conn q@(Query que) = do
411314 result <- exec conn que
412− finishQueryWith parser conn q result
315+ finishQuery conn q result
413316
414317-- | Perform a @SELECT@ or other SQL query that is expected to return
415318-- results. Results are streamed incrementally from the server, and
@@ -421,12 +324,6 @@ queryWith_ parser conn q@(Query que) = do
421324-- This fold is /not/ strict. The stream consumer is responsible for
422325-- forcing the evaluation of its result to avoid space leaks.
423326--
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.
429−--
430327-- Exceptions that may be thrown:
431328--
432329-- * 'FormatError': the query string could not be formatted correctly.
@@ -435,10 +332,8 @@ queryWith_ parser conn q@(Query que) = do
435332-- using 'execute' instead of 'query').
436333--
437334-- * 'ResultError': result conversion failed.
438−--
439−-- * 'SqlError': the postgresql backend returned an error, e.g.
440−-- a syntax or type error, or an incorrect table or column name.
441−fold :: ( FromRow row, ToRow params )
335+
336+fold :: ( FromRow row, QueryParams params )
442337 => Connection
443338 -> Query
444339 -> params
@@ -447,20 +342,6 @@ fold :: ( FromRow row, ToRow params )
447342 -> IO a
448343fold = foldWithOptions defaultFoldOptions
449344
450−-- | A version of 'fold' taking a parser as an argument
451−foldWith :: ( ToRow params )
452− => RowParser row
453− -> Connection
454− -> Query
455− -> params
456− -> a
457− -> (a -> row -> IO a)
458− -> IO a
459−foldWith = foldWithOptionsAndParser defaultFoldOptions
460−
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.
464345data FetchQuantity
465346 = Automatic
466347 | Fixed !Int
@@ -471,20 +352,12 @@ data FoldOptions
471352 transactionMode :: !TransactionMode
472353 }
473354
474−-- | defaults to 'Automatic', and 'TransactionMode' 'ReadCommitted' 'ReadOnly'
475−defaultFoldOptions :: FoldOptions
476355defaultFoldOptions = FoldOptions {
477356 fetchQuantity = Automatic,
478357 transactionMode = TransactionMode ReadCommitted ReadOnly
479358 }
480359
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.
487−foldWithOptions :: ( FromRow row, ToRow params )
360+foldWithOptions :: ( FromRow row, QueryParams params )
488361 => FoldOptions
489362 -> Connection
490363 -> Query
@@ -492,21 +365,9 @@ foldWithOptions :: ( FromRow row, ToRow params )
492365 -> a
493366 -> (a -> row -> IO a)
494367 -> IO a
495−foldWithOptions opts = foldWithOptionsAndParser opts fromRow
496−
497−-- | A version of 'foldWithOptions' taking a parser as an argument
498−foldWithOptionsAndParser :: (ToRow params)
499− => FoldOptions
500− -> RowParser row
501− -> Connection
502− -> Query
503− -> params
504− -> a
505− -> (a -> row -> IO a)
506− -> IO a
507−foldWithOptionsAndParser opts parser conn template qs a f = do
368+foldWithOptions opts conn template qs a f = do
508369 q <- formatQuery conn template qs
509− doFold opts parser conn template (Query q) a f
370+ doFold opts conn template (Query q) a f
510371
511372-- | A version of 'fold' that does not perform query substitution.
512373fold_ :: (FromRow r) =>
@@ -517,14 +378,6 @@ fold_ :: (FromRow r) =>
517378 -> IO a
518379fold_ = foldWithOptions_ defaultFoldOptions
519380
520−-- | A version of 'fold_' taking a parser as an argument
521−foldWith_ :: RowParser r
522− -> Connection
523− -> Query
524− -> a
525− -> (a -> r -> IO a)
526− -> IO a
527−foldWith_ = foldWithOptionsAndParser_ defaultFoldOptions
528381
529382foldWithOptions_ :: (FromRow r) =>
530383 FoldOptions
@@ -533,27 +386,18 @@ foldWithOptions_ :: (FromRow r) =>
533386 -> a -- ^ Initial state for result consumer.
534387 -> (a -> r -> IO a) -- ^ Result consumer.
535388 -> IO a
536−foldWithOptions_ opts conn query' a f = doFold opts fromRow conn query' query' a f
537−
538−-- | A version of 'foldWithOptions_' taking a parser as an argument
539−foldWithOptionsAndParser_ :: FoldOptions
540− -> RowParser r
541− -> Connection
542− -> Query -- ^ Query.
543− -> a -- ^ Initial state for result consumer.
544− -> (a -> r -> IO a) -- ^ Result consumer.
545− -> IO a
546−foldWithOptionsAndParser_ opts parser conn query' a f = doFold opts parser conn query' query' a f
547−
548−doFold :: FoldOptions
549− -> RowParser row
389+foldWithOptions_ opts conn query a f = doFold opts conn query query a f
390+
391+
392+doFold :: ( FromRow row )
393+ => FoldOptions
550394 -> Connection
551395 -> Query
552396 -> Query
553397 -> a
554398 -> (a -> row -> IO a)
555399 -> IO a
556−doFold FoldOptions{..} parser conn _template q a0 f = do
400+doFold FoldOptions{..} conn _template q a f = do
557401 stat <- withConnection conn PQ.transactionStatus
558402 case stat of
559403 PQ.TransIdle -> withTransactionMode transactionMode conn go
@@ -571,17 +415,11 @@ doFold FoldOptions{..} parser conn _template q a0 f = do
571415 PQ.TransUnknown -> fail "foldWithOpts FIXME: PQ.TransUnknown"
572416 -- Not sure what this means.
573417 where
574− declare =
575− declareCursor conn q
576− fetch cursor a =
577− foldForwardWithParser cursor parser chunkSize f a
578−
579− go = bracket declare closeCursor $ \cursor ->
580− let loop a = fetch cursor a >>=
581− \r -> case r of
582− Left x -> return x
583− Right x -> loop x
584− in loop a0
418+ go = do
419+ -- FIXME: what about name clashes with already-declared cursors?
420+ _ <- execute_ conn ("DECLARE fold NO SCROLL CURSOR FOR "
421+ `mappend` q)
422+ loop a `finally` execute_ conn "CLOSE fold"
585423
586424-- FIXME: choose the Automatic chunkSize more intelligently
587425-- One possibility is to use the type of the results, although this
@@ -592,45 +430,198 @@ doFold FoldOptions{..} parser conn _template q a0 f = do
592430 chunkSize = case fetchQuantity of
593431 Automatic -> 256
594432 Fixed n -> n
433+ loop a = do
434+ rs <- query conn "FETCH FORWARD ? FROM fold" (Only chunkSize)
435+ if null rs then return a else foldM f a rs >>= loop
595436
596437-- | A version of 'fold' that does not transform a state value.
597−forEach :: (ToRow q, FromRow r) =>
438+forEach :: (QueryParams q, FromRow r) =>
598439 Connection
599440 -> Query -- ^ Query template.
600441 -> q -- ^ Query parameters.
601442 -> (r -> IO ()) -- ^ Result consumer.
602443 -> IO ()
603−forEach = forEachWith fromRow
444+forEach conn template qs = fold conn template qs () . const
604445{-# INLINE forEach #-}
605446
606−-- | A version of 'forEach' taking a parser as an argument
607−forEachWith :: ( ToRow q )
608− => RowParser r
609− -> Connection
610− -> Query
611− -> q
612− -> (r -> IO ())
613− -> IO ()
614−forEachWith parser conn template qs = foldWith parser conn template qs () . const
615−{-# INLINE forEachWith #-}
616−
617447-- | A version of 'forEach' that does not perform query substitution.
618448forEach_ :: (FromRow r) =>
619449 Connection
620450 -> Query -- ^ Query template.
621451 -> (r -> IO ()) -- ^ Result consumer.
622452 -> IO ()
623−forEach_ = forEachWith_ fromRow
453+forEach_ conn template = fold_ conn template () . const
624454{-# INLINE forEach_ #-}
625455
626−forEachWith_ :: RowParser r
627− -> Connection
628− -> Query
629− -> (r -> IO ())
630− -> IO ()
631−forEachWith_ parser conn template = foldWith_ parser conn template () . const
632−{-# INLINE forEachWith_ #-}
633−
456+forM' :: (Ord n, Num n) => n -> n -> (n -> IO a) -> IO [a]
457+forM' lo hi m = loop hi []
458+ where
459+ loop !n !as
460+ | n < lo = return as
461+ | otherwise = do
462+ a <- m n
463+ loop (n-1) (a:as)
464+
465+finishQuery :: (FromRow r) => Connection -> Query -> PQ.Result -> IO [r]
466+finishQuery conn q result = do
467+ status <- PQ.resultStatus result
468+ case status of
469+ PQ.CommandOk -> do
470+ throwIO $ QueryError "query resulted in a command response" q
471+ PQ.TuplesOk -> do
472+ ncols <- PQ.nfields result
473+ let unCol (PQ.Col x) = fromIntegral x
474+ typenames <- V.generateM (unCol ncols)
475+ (\(PQ.Col . fromIntegral -> col) -> do
476+ getTypename conn =<< PQ.ftype result col)
477+ nrows <- PQ.ntuples result
478+ ncols <- PQ.nfields result
479+ forM' 0 (nrows-1) $ \row -> do
480+ let rw = Row row typenames result
481+ case runStateT (runReaderT (unRP fromRow) rw) 0 of
482+ Ok (val,col) | col == ncols -> return val
483+ | otherwise -> do
484+ vals <- forM' 0 (ncols-1) $ \c -> do
485+ v <- PQ.getvalue result row c
486+ return ( typenames V.! unCol c
487+ , fmap ellipsis v )
488+ throw (ConversionFailed
489+ (show (unCol ncols) ++ " values: " ++ show vals)
490+ (show (unCol col) ++ " slots in target type")
491+ "mismatch between number of columns to \
492+ \convert and number in target type")
493+ Errors [] -> throwIO $ ConversionFailed "" "" "unknown error"
494+ Errors [x] -> throwIO x
495+ Errors xs -> throwIO $ ManyErrors xs
496+
497+ellipsis :: ByteString -> ByteString
498+ellipsis bs
499+ | B.length bs > 15 = B.take 10 bs `B.append` "[...]"
500+ | otherwise = bs
501+
502+-- | Throw a 'ConversionFailed' exception, indicating a mismatch
503+-- between the number of columns in the 'Field' and row, and the
504+-- number in the collection to be converted to.
505+convertError :: [Field]
506+ -- ^ Descriptors of fields to be converted.
507+ -> [Maybe ByteString]
508+ -- ^ Contents of the row to be converted.
509+ -> Int
510+ -- ^ Number of columns expected for conversion. For
511+ -- instance, if converting to a 3-tuple, the number to
512+ -- provide here would be 3.
513+ -> Either SomeException a
514+convertError fs vs n = Left . SomeException $ ConversionFailed
515+ (show (length fs) ++ " values: " ++ show (zip (map typename fs)
516+ (map (fmap ellipsis) vs)))
517+ (show n ++ " slots in target type")
518+ "mismatch between number of columns to convert and number in target type"
519+
520+
521+-- | Of the four isolation levels defined by the SQL standard,
522+-- these are the three levels distinguished by PostgreSQL as of version 9.0.
523+-- See <http://www.postgresql.org/docs/9.1/static/transaction-iso.html>
524+-- for more information.
525+
526+data IsolationLevel
527+ = DefaultIsolationLevel
528+ | ReadCommitted
529+ | RepeatableRead
530+ | Serializable
531+ deriving (Show, Eq, Ord, Enum, Bounded)
532+
533+data ReadWriteMode
534+ = ReadWrite
535+ | ReadOnly
536+ deriving (Show, Eq, Ord, Enum, Bounded)
537+
538+data TransactionMode = TransactionMode {
539+ isolationLevel :: !IsolationLevel,
540+ readWriteMode :: !ReadWriteMode
541+ } deriving (Show, Eq)
542+
543+defaultTransactionMode :: TransactionMode
544+defaultTransactionMode = TransactionMode DefaultIsolationLevel ReadWrite
545+
546+defaultIsolationLevel :: IsolationLevel
547+defaultIsolationLevel = ReadCommitted
548+
549+defaultReadWriteMode :: ReadWriteMode
550+defaultReadWriteMode = ReadWrite
551+
552+-- | Execute an action inside a SQL transaction.
553+--
554+-- This function initiates a transaction with a \"@begin
555+-- transaction@\" statement, then executes the supplied action. If
556+-- the action succeeds, the transaction will be completed with
557+-- 'Base.commit' before this function returns.
558+--
559+-- If the action throws /any/ kind of exception (not just a
560+-- PostgreSQL-related exception), the transaction will be rolled back using
561+-- 'rollback', then the exception will be rethrown.
562+withTransaction :: Connection -> IO a -> IO a
563+withTransaction = withTransactionMode defaultTransactionMode
564+
565+-- | Execute an action inside a SQL transaction with a given isolation level.
566+withTransactionLevel :: IsolationLevel -> Connection -> IO a -> IO a
567+withTransactionLevel lvl
568+ = withTransactionMode defaultTransactionMode { isolationLevel = lvl }
569+
570+-- | Execute an action inside a SQL transaction with a given transaction mode.
571+withTransactionMode :: TransactionMode -> Connection -> IO a -> IO a
572+withTransactionMode mode conn act = do
573+ beginMode mode conn
574+ r <- act `onException` rollback conn
575+ commit conn
576+ return r
577+
578+-- | Rollback a transaction.
579+rollback :: Connection -> IO ()
580+rollback conn = execute_ conn "ABORT" >> return ()
581+
582+-- | Commit a transaction.
583+commit :: Connection -> IO ()
584+commit conn = execute_ conn "COMMIT" >> return ()
585+
586+-- | Begin a transaction.
587+begin :: Connection -> IO ()
588+begin = beginMode defaultTransactionMode
589+
590+-- | Begin a transaction with a given isolation level
591+beginLevel :: IsolationLevel -> Connection -> IO ()
592+beginLevel lvl = beginMode defaultTransactionMode { isolationLevel = lvl }
593+
594+-- | Begin a transaction with a given transaction mode
595+beginMode :: TransactionMode -> Connection -> IO ()
596+beginMode mode conn = do
597+ execute_ conn $! case mode of
598+ TransactionMode DefaultIsolationLevel ReadWrite ->
599+ "BEGIN"
600+ TransactionMode DefaultIsolationLevel ReadOnly ->
601+ "BEGIN READ ONLY"
602+ TransactionMode ReadCommitted ReadWrite ->
603+ "BEGIN ISOLATION LEVEL READ COMMITTED"
604+ TransactionMode ReadCommitted ReadOnly ->
605+ "BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY"
606+ TransactionMode RepeatableRead ReadWrite ->
607+ "BEGIN ISOLATION LEVEL REPEATABLE READ"
608+ TransactionMode RepeatableRead ReadOnly ->
609+ "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"
610+ TransactionMode Serializable ReadWrite ->
611+ "BEGIN ISOLATION LEVEL SERIALIZABLE"
612+ TransactionMode Serializable ReadOnly ->
613+ "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"
614+ return ()
615+
616+fmtError :: String -> Query -> [Action] -> a
617+fmtError msg q xs = throw FormatError {
618+ fmtMessage = msg
619+ , fmtQuery = q
620+ , fmtParams = map twiddle xs
621+ }
622+ where twiddle (Plain b) = toByteString b
623+ twiddle (Escape s) = s
624+ twiddle (Many ys) = B.concat (map twiddle ys)
634625
635626-- $use
636627--
@@ -658,11 +649,9 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
658649-- >
659650-- > import Database.PostgreSQL.Simple
660651-- >
661−-- > hello :: IO Int
662652-- > hello = do
663−-- > conn <- connectPostgreSQL ""
664−-- > [Only i] <- query_ conn "select 2 + 2"
665−-- > return i
653+-- > conn <- connect defaultConnectInfo
654+-- > query conn "select 2 + 2"
666655--
667656-- A 'Query' value does not represent the actual query that will be
668657-- executed, but is a template for constructing the final query.
@@ -709,7 +698,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
709698-- Automated type inference means that you will often be able to avoid
710699-- supplying explicit type signatures for the elements of a tuple.
711700-- However, sometimes the compiler will not be able to infer your
712−-- types. Consider a case where you write a numeric literal in a
701+-- types. Consider a care where you write a numeric literal in a
713702-- parameter tuple:
714703--
715704-- > query conn "select ? + ?" (40,2)
@@ -723,30 +712,15 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
723712-- The same kind of problem can arise with string literals if you have
724713-- the @OverloadedStrings@ language extension enabled. Again, just
725714-- use an explicit type signature if this happens.
726−--
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:
730−--
731−-- > print =<< query_ conn "select 2 + 2"
732−--
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:
737−--
738−-- > xs <- query_ conn "select 2 + 2"
739−-- > print (xs :: [Only Int])
740715
741716-- $only_param
742717--
743718-- 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,
745−-- what should you do?
719+-- value you want substituted into a query, what should you do?
746720--
747721-- The obvious approach would appear to be something like this:
748722--
749−-- > instance (ToField a) => ToRow a where
723+-- > instance (Param a) => QueryParam a where
750724-- > ...
751725--
752726-- Unfortunately, this wreaks havoc with type inference, so we take a
@@ -757,9 +731,6 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
757731--
758732-- > execute conn "insert into users (first_name) values (?)"
759733-- > ["Nuala"]
760−--
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.
763734
764735-- $in
765736--
@@ -777,7 +748,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
777748-- Here's an example:
778749--
779750-- > query conn "select * from users where first_name in ?" $
780−-- > Only $ In ["Anna", "Boris", "Carla"]
751+-- > In ["Anna", "Boris", "Carla"]
781752--
782753-- If your 'In'-wrapped list is empty, the string @\"(null)\"@ will be
783754-- substituted instead, to ensure that your clause remains
@@ -805,7 +776,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
805776-- * There must be no other \"@?@\" characters anywhere in your
806777-- template.
807778--
808−-- * There must be one or more \"@?@\" in the parentheses.
779+-- * There must one or more \"@?@\" in the parentheses.
809780--
810781-- * Extra white space is fine.
811782--
@@ -826,22 +797,6 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
826797-- > insert into users (first_name,last_name) values
827798-- > ('Boris','Karloff'),('Ed','Wood')
828799
829−-- $returning
830−--
831−-- PostgreSQL supports returning values from data manipulation statements
832−-- such as @INSERT@ and @UPDATE@. You can use these statements by
833−-- using 'query' instead of 'execute'. For multi-tuple inserts,
834−-- use 'returning' instead of 'executeMany'.
835−--
836−-- For example, were there an auto-incrementing @id@ column and
837−-- timestamp column @t@ that defaulted to the present time for the
838−-- @sales@ table, then the following query would insert two new
839−-- sales records and also return their new @id@s and timestamps.
840−--
841−-- > 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")]
844−
845800-- $result
846801--
847802-- The 'query' and 'query_' functions return a list of values in the
@@ -876,7 +831,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
876831--
877832-- Although SQL can accommodate @NULL@ as a value for any of these
878833-- types, Haskell cannot. If your result contains columns that may be
879−-- @NULL@, be sure that you use 'Maybe' in those positions of your
834+-- @NULL@, be sure that you use 'Maybe' in those positions of of your
880835-- tuple.
881836--
882837-- > (Text, Maybe Int, Int)
@@ -902,7 +857,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
902857-- all values of the given PostgreSQL type is considered \"compatible\".
903858-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
904859-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
905−-- represent a @SMALLINT@, so it is considered compatible with those types.
860+-- represent a @SMALLINT@, so it is considered compatble with those types.
906861--
907862-- * A numeric compatibility check is based only on the type of a
908863-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
@@ -916,3 +871,25 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
916871-- UTF-8. If you use some other encoding, decoding may fail or give
917872-- wrong results. In such cases, write a @newtype@ wrapper and a
918873-- custom 'Result' instance to handle your encoding.
874+
875+getTypename :: Connection -> PQ.Oid -> IO ByteString
876+getTypename conn@Connection{..} oid =
877+ case oid2builtin oid of
878+ Just builtin -> return $! builtin2typname builtin
879+ Nothing -> modifyMVar connectionObjects $ \oidmap -> do
880+ case IntMap.lookup (oid2int oid) oidmap of
881+ Just name -> return (oidmap, name)
882+ Nothing -> do
883+ names <- query conn "SELECT typname FROM pg_type WHERE oid=?"
884+ (Only oid)
885+ name <- case names of
886+ [] -> return $ throw SqlError {
887+ sqlNativeError = -1,
888+ sqlErrorMsg = "invalid type oid",
889+ sqlState = ""
890+ }
891+ [Only x] -> return x
892+ _ -> fail "typename query returned more than one result"
893+ -- oid is a primary key, so the query should
894+ -- never return more than one result
895+ return (IntMap.insert (oid2int oid) name oidmap, name)