@@ -1,14 +1,18 @@
1−{-# LANGUAGE DeriveDataTypeable, RecordWildCards, NamedFieldPuns #-}
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE NamedFieldPuns #-}
3+{-# LANGUAGE OverloadedStrings #-}
4+{-# LANGUAGE PatternGuards #-}
5+{-# LANGUAGE RecordWildCards #-}
6+{-# LANGUAGE ViewPatterns #-}
3------------------------------------------------------------------------------
8------------------------------------------------------------------------------
5-- Module: Database.PostgreSQL.Simple
10-- Module: Database.PostgreSQL.Simple
6-- Copyright: (c) 2011 MailRank, Inc.
11-- Copyright: (c) 2011 MailRank, Inc.
7−-- (c) 2011 Leon P Smith
12+-- (c) 2011-2012 Leon P Smith
8-- License: BSD3
13-- License: BSD3
9-- Maintainer: Leon P Smith <leon@melding-monads.com>
14-- Maintainer: Leon P Smith <leon@melding-monads.com>
10-- Stability: experimental
15-- Stability: experimental
11−-- Portability: portable
13-- A mid-level client library for the PostgreSQL database, aimed at ease of
17-- A mid-level client library for the PostgreSQL database, aimed at ease of
14-- use and high performance.
18-- use and high performance.
@@ -38,6 +42,9 @@ module Database.PostgreSQL.Simple
38 -- ** Modifying multiple rows at once
42 -- ** Modifying multiple rows at once
45+ -- ** @RETURNING@: modifications that return results
41 -- * Extracting results
48 -- * Extracting results
42 -- $result
49 -- $result
@@ -48,27 +55,35 @@ module Database.PostgreSQL.Simple
50 -- * Types
57 -- * Types
52− , Connection
58+ Connection
55 , Binary(..)
63 , Binary(..)
56 , Only(..)
64 , Only(..)
57 , (:.)(..)
65 , (:.)(..)
58 -- ** Exceptions
66 -- ** Exceptions
67+ , SomePostgreSqlException(..)
59 , SqlError(..)
68 , SqlError(..)
60− , FormatError(fmtMessage, fmtQuery, fmtParams)
70+ , FormatError(..)
61− , QueryError(qeMessage, qeQuery)
71+ , QueryError(..)
62− , ResultError(errSQLType, errHaskellType, errMessage)
72+ , ResultError(..)
63 -- * Connection management
73 -- * Connection management
65 , Base.connectPostgreSQL
74 , Base.connectPostgreSQL
66− , Base.postgreSQLConnectionString
67− , Base.defaultConnectInfo
68 , Base.close
75 , Base.close
78+ , Base.ConnectInfo(..)
79+ , Base.defaultConnectInfo
80+ , Base.postgreSQLConnectionString
69 -- * Queries that return results
81 -- * Queries that return results
84+ -- ** Queries taking parser as argument
72 -- * Queries that stream results
87 -- * Queries that stream results
73 , FoldOptions(..)
88 , FoldOptions(..)
74 , FetchQuantity(..)
89 , FetchQuantity(..)
@@ -79,6 +94,15 @@ module Database.PostgreSQL.Simple
79 , foldWithOptions_
94 , foldWithOptions_
81 , forEach_
96 , forEach_
98+ -- ** Queries that stream results taking a parser as an argument
100+ , foldWithOptionsAndParser
102+ , foldWithOptionsAndParser_
82 -- * Statements that do not return results
106 -- * Statements that do not return results
83 , execute
107 , execute
84 , execute_
108 , execute_
@@ -86,18 +110,9 @@ module Database.PostgreSQL.Simple
86-- , Base.insertID
110-- , Base.insertID
87 -- * Transaction handling
111 -- * Transaction handling
88 , withTransaction
112 , withTransaction
89− , TransactionMode(..)
113+ , withSavepoint
92− , defaultTransactionMode
93− , defaultIsolationLevel
94− , defaultReadWriteMode
95− , withTransactionLevel
96− , withTransactionMode
97-- , Base.autocommit
114-- , Base.autocommit
102 , rollback
117 , rollback
103 -- * Helper functions
118 -- * Helper functions
@@ -105,50 +120,27 @@ module Database.PostgreSQL.Simple
105 , formatQuery
120 , formatQuery
108−import Blaze.ByteString.Builder
123+import Data.ByteString.Builder (Builder, byteString, char8)
109− ( Builder, fromByteString, toByteString )
110−import Blaze.ByteString.Builder.Char8 (fromChar)
111−import Control.Applicative ((<$>), pure)
124+import Control.Applicative ((<$>))
112−import Control.Concurrent.MVar
125+import Control.Exception as E
113−import Control.Exception
114− ( Exception, onException, throw, throwIO, finally )
115−import Control.Monad (foldM)
116import Data.ByteString (ByteString)
126import Data.ByteString (ByteString)
117import Data.Int (Int64)
127import Data.Int (Int64)
118−import qualified Data.IntMap as IntMap
119import Data.List (intersperse)
128import Data.List (intersperse)
120−import Data.Monoid (mappend, mconcat)
129+import Data.Monoid (mconcat)
121−import Data.Typeable (Typeable)
122−import Database.PostgreSQL.Simple.BuiltinTypes
123− ( oid2builtin, builtin2typname )
124−import Database.PostgreSQL.Simple.Compat ( mask )
130+import Database.PostgreSQL.Simple.Compat ((<>), toByteString)
131+import Database.PostgreSQL.Simple.Cursor
125import Database.PostgreSQL.Simple.FromField (ResultError(..))
132import Database.PostgreSQL.Simple.FromField (ResultError(..))
126import Database.PostgreSQL.Simple.FromRow (FromRow(..))
133import Database.PostgreSQL.Simple.FromRow (FromRow(..))
127−import Database.PostgreSQL.Simple.Ok
134+import Database.PostgreSQL.Simple.ToField (Action(..))
128−import Database.PostgreSQL.Simple.ToField (Action(..), inQuotes)
129import Database.PostgreSQL.Simple.ToRow (ToRow(..))
135import Database.PostgreSQL.Simple.ToRow (ToRow(..))
130import Database.PostgreSQL.Simple.Types
136import Database.PostgreSQL.Simple.Types
131 ( Binary(..), In(..), Only(..), Query(..), (:.)(..) )
137 ( Binary(..), In(..), Only(..), Query(..), (:.)(..) )
132import Database.PostgreSQL.Simple.Internal as Base
138import Database.PostgreSQL.Simple.Internal as Base
139+import Database.PostgreSQL.Simple.Internal.PQResultUtils
140+import Database.PostgreSQL.Simple.Transaction
133import qualified Database.PostgreSQL.LibPQ as PQ
141import qualified Database.PostgreSQL.LibPQ as PQ
134−import Text.Regex.PCRE.Light (compile, caseless, match)
135import qualified Data.ByteString.Char8 as B
142import qualified Data.ByteString.Char8 as B
136−import qualified Data.Text as T
143+
137−import qualified Data.Text.Encoding as TE
138−import qualified Data.Vector as V
139−import Control.Monad.Trans.Reader
140−import Control.Monad.Trans.State.Strict
142−-- | Exception thrown if a 'Query' could not be formatted correctly.
143−-- This may occur if the number of \'@?@\' characters in the query
144−-- string does not match the number of parameters provided.
145−data FormatError = FormatError {
146− fmtMessage :: String
148− , fmtParams :: [ByteString]
149− } deriving (Eq, Show, Typeable)
151−instance Exception FormatError
153-- | Format a query string.
145-- | Format a query string.
@@ -182,56 +174,143 @@ formatQuery conn q@(Query template) qs
182formatMany :: (ToRow q) => Connection -> Query -> [q] -> IO ByteString
174formatMany :: (ToRow q) => Connection -> Query -> [q] -> IO ByteString
183formatMany _ q [] = fmtError "no rows supplied" q []
175formatMany _ q [] = fmtError "no rows supplied" q []
184formatMany conn q@(Query template) qs = do
176formatMany conn q@(Query template) qs = do
185− case match re template [] of
177+ case parseTemplate template of
186− Just [_,before,qbits,after] -> do
178+ Just (before, qbits, after) -> do
187 bs <- mapM (buildQuery conn q qbits . toRow) qs
179 bs <- mapM (buildQuery conn q qbits . toRow) qs
188− return . toByteString . mconcat $ fromByteString before :
180+ return . toByteString . mconcat $ byteString before :
189− intersperse (fromChar ',') bs ++
181+ intersperse (char8 ',') bs ++
190− [fromByteString after]
182+ [byteString after]
191− _ -> error "foo"
183+ Nothing -> fmtError "syntax error in multi-row template" q []
185+-- Split the input string into three pieces, @before@, @qbits@, and @after@,
186+-- following this grammar:
188+-- start: ^ before qbits after $
189+-- before: ([^?]* [^?\w])? 'VALUES' \s*
190+-- qbits: '(' \s* '?' \s* (',' \s* '?' \s*)* ')'
194+-- \w: [A-Z] | [a-z] | [\x80-\xFF] | '_' | '$' | [0-9]
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− re = compile "^([^?]+\\bvalues\\s*)\
204+ -- Search for the next occurrence of "VALUES"
194− \(\\(\\s*[?](?:\\s*,\\s*[?])*\\s*\\))\
205+ search bs =
195− \([^?]*)$"
206+ case B.breakSubstring "VALUES" bs of
196− [caseless]
207+ (x, y)
197−
208+ -- If "VALUES" is not present in the string, or any '?' characters
198−escapeStringConn :: Connection -> ByteString -> IO (Either ByteString ByteString)
209+ -- were encountered prior to it, fail.
199−escapeStringConn conn s =
210+ | B.null y || ('?' `B.elem` x)
200− withConnection conn $ \c ->
211+ -> Nothing
201− PQ.escapeStringConn c s >>= checkError c
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
218+ -- Otherwise, we have a legitimate "VALUES" token.
220+ -> parseQueryBits $ skipSpace $ B.drop 6 y
222+ -- Parse '(' \s* '?' \s* . If this doesn't match
223+ -- (and we don't consume a '?'), look for another "VALUES".
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.
229+ | Just ('(', skipSpace -> bs1) <- B.uncons qb
230+ , Just ('?', skipSpace -> bs2) <- B.uncons bs1
231+ = finishQueryBits qb bs2
235+ -- Parse (',' \s* '?' \s*)* ')' [^?]* .
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
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
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.
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 =
260+ (s1, source') = B.splitAt (B.length source - B.length p1) source
261+ (s2, source'') = B.splitAt (B.length p1 - B.length p2) source'
263+ toUpper_ascii c | c >= 'a' && c <= 'z' = toEnum (fromEnum c - 32)
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')
275+ -- Based on {space} in scan.l
276+ isSpace_ascii c = (c == ' ') || (c >= '\t' && c <= '\r')
278+ skipSpace = B.dropWhile isSpace_ascii
203−escapeByteaConn :: Connection -> ByteString -> IO (Either ByteString ByteString)
204−escapeByteaConn conn s =
205− withConnection conn $ \c ->
206− PQ.escapeByteaConn c s >>= checkError c
208−checkError :: PQ.Connection -> Maybe a -> IO (Either ByteString a)
209−checkError _ (Just x) = return $ Right x
210−checkError c Nothing = Left . maybe "" id <$> PQ.errorMessage c
212buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
281buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
213−buildQuery conn q template xs = zipParams (split template) <$> mapM sub xs
282+buildQuery conn q template xs =
214− where quote = either (\msg -> fmtError (utf8ToString msg) q xs)
283+ zipParams (split template) <$> mapM (buildAction conn q xs) xs
215− (inQuotes . fromByteString)
284+ where split s =
216− utf8ToString = T.unpack . TE.decodeUtf8
285+ -- This part escapes double '??'s to make literal '?'s possible
217− sub (Plain b) = pure b
286+ -- in PostgreSQL queries using the JSON operators: @?@, @?|@ and @?&@
218− sub (Escape s) = quote <$> escapeStringConn conn s
287+ let (h,t) = breakOnSingleQuestionMark s
219− sub (EscapeBytea s) = quote <$> escapeByteaConn conn s
288+ in byteString h
220− sub (Many ys) = mconcat <$> mapM sub ys
289+ : if B.null t
221− split s = fromByteString h : if B.null t then [] else split (B.tail t)
291+ else split (B.tail t)
222− where (h,t) = B.break (=='?') s
223− zipParams (t:ts) (p:ps) = t `mappend` p `mappend` zipParams ts ps
292+ zipParams (t:ts) (p:ps) = t <> p <> zipParams ts ps
224 zipParams [t] [] = t
293 zipParams [t] [] = t
225− zipParams _ _ = fmtError (show (B.count '?' template) ++
294+ zipParams _ _ = fmtError (show countSingleQs ++
226− " '?' characters, but " ++
295+ " single '?' characters, but " ++
227 show (length xs) ++ " parameters") q xs
296 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
229-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
307-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
230-- expected to return results.
308-- expected to return results.
232-- Returns the number of rows affected.
310-- Returns the number of rows affected.
234−-- Throws 'FormatError' if the query could not be formatted correctly.
312+-- Throws 'FormatError' if the query could not be formatted correctly, or
313+-- a 'SqlError' exception if the backend returns an error.
235execute :: (ToRow q) => Connection -> Query -> q -> IO Int64
314execute :: (ToRow q) => Connection -> Query -> q -> IO Int64
236execute conn template qs = do
315execute conn template qs = do
237 result <- exec conn =<< formatQuery conn template qs
316 result <- exec conn =<< formatQuery conn template qs
@@ -240,15 +319,61 @@ execute conn template qs = do
240-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not
319-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not
241-- expected to return results.
320-- expected to return results.
243−-- Returns the number of rows affected.
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.
245−-- Throws 'FormatError' if the query could not be formatted correctly.
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:
341+-- executeMany c [sql|
344+-- FROM (VALUES (?,?)) as upd(x,y)
345+-- WHERE sometable.x = upd.x
346+-- |] [(1, \"hello\"),(2, \"world\")]
246executeMany :: (ToRow q) => Connection -> Query -> [q] -> IO Int64
349executeMany :: (ToRow q) => Connection -> Query -> [q] -> IO Int64
247executeMany _ _ [] = return 0
350executeMany _ _ [] = return 0
248executeMany conn q qs = do
351executeMany conn q qs = do
249 result <- exec conn =<< formatMany conn q qs
352 result <- exec conn =<< formatMany conn q qs
250 finishExecute conn q result
353 finishExecute conn q result
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'.
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.
367+returning :: (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 []
373+returningWith parser conn q qs = do
374+ result <- exec conn =<< formatMany conn q qs
375+ finishQueryWith parser conn q result
252-- | Perform a @SELECT@ or other SQL query that is expected to return
377-- | Perform a @SELECT@ or other SQL query that is expected to return
253-- results. All results are retrieved and converted before this
378-- results. All results are retrieved and converted before this
254-- function returns.
379-- function returns.
@@ -264,17 +389,27 @@ executeMany conn q qs = do
264-- using 'execute' instead of 'query').
389-- using 'execute' instead of 'query').
266-- * 'ResultError': result conversion failed.
391-- * '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.
267−query :: (ToRow q, FromRow r)
395+query :: (ToRow q, FromRow r) => Connection -> Query -> q -> IO [r]
268− => Connection -> Query -> q -> IO [r]
396+query = queryWith fromRow
269−query conn template qs = do
270− result <- exec conn =<< formatQuery conn template qs
271− finishQuery conn template result
273-- | A version of 'query' that does not perform query substitution.
398-- | A version of 'query' that does not perform query substitution.
274query_ :: (FromRow r) => Connection -> Query -> IO [r]
399query_ :: (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]
275−query_ conn q@(Query que) = do
410+queryWith_ parser conn q@(Query que) = do
276 result <- exec conn que
411 result <- exec conn que
277− finishQuery conn q result
412+ finishQueryWith parser conn q result
279-- | Perform a @SELECT@ or other SQL query that is expected to return
414-- | Perform a @SELECT@ or other SQL query that is expected to return
280-- results. Results are streamed incrementally from the server, and
415-- results. Results are streamed incrementally from the server, and
@@ -286,6 +421,12 @@ query_ conn q@(Query que) = do
286-- This fold is /not/ strict. The stream consumer is responsible for
421-- This fold is /not/ strict. The stream consumer is responsible for
287-- forcing the evaluation of its result to avoid space leaks.
422-- 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.
289-- Exceptions that may be thrown:
430-- Exceptions that may be thrown:
291-- * 'FormatError': the query string could not be formatted correctly.
432-- * 'FormatError': the query string could not be formatted correctly.
@@ -294,7 +435,9 @@ query_ conn q@(Query que) = do
294-- using 'execute' instead of 'query').
435-- using 'execute' instead of 'query').
296-- * 'ResultError': result conversion failed.
437-- * '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.
298fold :: ( FromRow row, ToRow params )
441fold :: ( FromRow row, ToRow params )
299 => Connection
442 => Connection
@@ -304,6 +447,20 @@ fold :: ( FromRow row, ToRow params )
305fold = foldWithOptions defaultFoldOptions
448fold = 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.
307data FetchQuantity
464data FetchQuantity
308 = Automatic
465 = Automatic
309 | Fixed !Int
466 | Fixed !Int
@@ -314,12 +471,19 @@ data FoldOptions
314 transactionMode :: !TransactionMode
471 transactionMode :: !TransactionMode
474+-- | defaults to 'Automatic', and 'TransactionMode' 'ReadCommitted' 'ReadOnly'
317defaultFoldOptions :: FoldOptions
475defaultFoldOptions :: FoldOptions
318defaultFoldOptions = FoldOptions {
476defaultFoldOptions = FoldOptions {
319 fetchQuantity = Automatic,
477 fetchQuantity = Automatic,
320 transactionMode = TransactionMode ReadCommitted ReadOnly
478 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.
323foldWithOptions :: ( FromRow row, ToRow params )
487foldWithOptions :: ( FromRow row, ToRow params )
324 => FoldOptions
488 => FoldOptions
325 -> Connection
489 -> Connection
@@ -328,9 +492,21 @@ foldWithOptions :: ( FromRow row, ToRow params )
329 -> (a -> row -> IO a)
493 -> (a -> row -> IO a)
331−foldWithOptions opts conn template qs a f = do
495+foldWithOptions opts = foldWithOptionsAndParser opts fromRow
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
332 q <- formatQuery conn template qs
508 q <- formatQuery conn template qs
333− doFold opts conn template (Query q) a f
509+ doFold opts parser conn template (Query q) a f
335-- | A version of 'fold' that does not perform query substitution.
511-- | A version of 'fold' that does not perform query substitution.
336fold_ :: (FromRow r) =>
512fold_ :: (FromRow r) =>
@@ -341,6 +517,14 @@ fold_ :: (FromRow r) =>
342fold_ = foldWithOptions_ defaultFoldOptions
518fold_ = foldWithOptions_ defaultFoldOptions
520+-- | A version of 'fold_' taking a parser as an argument
521+foldWith_ :: RowParser r
527+foldWith_ = foldWithOptionsAndParser_ defaultFoldOptions
345foldWithOptions_ :: (FromRow r) =>
529foldWithOptions_ :: (FromRow r) =>
346 FoldOptions
530 FoldOptions
@@ -349,18 +533,27 @@ foldWithOptions_ :: (FromRow r) =>
349 -> a -- ^ Initial state for result consumer.
533 -> a -- ^ Initial state for result consumer.
350 -> (a -> r -> IO a) -- ^ Result consumer.
534 -> (a -> r -> IO a) -- ^ Result consumer.
352−foldWithOptions_ opts conn query a f = doFold opts conn query query a f
536+foldWithOptions_ opts conn query' a f = doFold opts fromRow conn query' query' a f
354−
538+-- | A version of 'foldWithOptions_' taking a parser as an argument
355−doFold :: ( FromRow row )
539+foldWithOptionsAndParser_ :: FoldOptions
356− => FoldOptions
540+ -> RowParser r
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
357 -> Connection
550 -> Connection
361 -> (a -> row -> IO a)
554 -> (a -> row -> IO a)
363−doFold FoldOptions{..} conn _template q a f = do
556+doFold FoldOptions{..} parser conn _template q a0 f = do
364 stat <- withConnection conn PQ.transactionStatus
557 stat <- withConnection conn PQ.transactionStatus
365 case stat of
558 case stat of
366 PQ.TransIdle -> withTransactionMode transactionMode conn go
559 PQ.TransIdle -> withTransactionMode transactionMode conn go
@@ -378,11 +571,17 @@ doFold FoldOptions{..} conn _template q a f = do
378 PQ.TransUnknown -> fail "foldWithOpts FIXME: PQ.TransUnknown"
571 PQ.TransUnknown -> fail "foldWithOpts FIXME: PQ.TransUnknown"
379 -- Not sure what this means.
572 -- Not sure what this means.
381− go = do
574+ declare =
382− -- FIXME: what about name clashes with already-declared cursors?
575+ declareCursor conn q
383− _ <- execute_ conn ("DECLARE fold NO SCROLL CURSOR FOR "
576+ fetch cursor a =
384− `mappend` q)
577+ foldForwardWithParser cursor parser chunkSize f a
385− loop a `finally` execute_ conn "CLOSE fold"
578+
579+ go = bracket declare closeCursor $ \cursor ->
580+ let loop a = fetch cursor a >>=
387-- FIXME: choose the Automatic chunkSize more intelligently
586-- FIXME: choose the Automatic chunkSize more intelligently
388-- One possibility is to use the type of the results, although this
587-- One possibility is to use the type of the results, although this
@@ -393,9 +592,6 @@ doFold FoldOptions{..} conn _template q a f = do
393 chunkSize = case fetchQuantity of
592 chunkSize = case fetchQuantity of
394 Automatic -> 256
593 Automatic -> 256
395 Fixed n -> n
594 Fixed n -> n
397− rs <- query conn "FETCH FORWARD ? FROM fold" (Only chunkSize)
398− if null rs then return a else foldM f a rs >>= loop
400-- | A version of 'fold' that does not transform a state value.
596-- | A version of 'fold' that does not transform a state value.
401forEach :: (ToRow q, FromRow r) =>
597forEach :: (ToRow q, FromRow r) =>
@@ -404,188 +600,37 @@ forEach :: (ToRow q, FromRow r) =>
404 -> q -- ^ Query parameters.
600 -> q -- ^ Query parameters.
405 -> (r -> IO ()) -- ^ Result consumer.
601 -> (r -> IO ()) -- ^ Result consumer.
407−forEach conn template qs = fold conn template qs () . const
603+forEach = forEachWith fromRow
408{-# INLINE forEach #-}
604{-# 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 #-}
410-- | A version of 'forEach' that does not perform query substitution.
617-- | A version of 'forEach' that does not perform query substitution.
411forEach_ :: (FromRow r) =>
618forEach_ :: (FromRow r) =>
412 Connection
619 Connection
413 -> Query -- ^ Query template.
620 -> Query -- ^ Query template.
414 -> (r -> IO ()) -- ^ Result consumer.
621 -> (r -> IO ()) -- ^ Result consumer.
416−forEach_ conn template = fold_ conn template () . const
623+forEach_ = forEachWith_ fromRow
417{-# INLINE forEach_ #-}
624{-# INLINE forEach_ #-}
419−forM' :: (Ord n, Num n) => n -> n -> (n -> IO a) -> IO [a]
626+forEachWith_ :: RowParser r
420−forM' lo hi m = loop hi []
627+ -> Connection
422− loop !n !as
629+ -> (r -> IO ())
423− | n < lo = return as
630+ -> IO ()
424− | otherwise = do
631+forEachWith_ parser conn template = foldWith_ parser conn template () . const
425− a <- m n
632+{-# INLINE forEachWith_ #-}
426− loop (n-1) (a:as)
633+
428−finishQuery :: (FromRow r) => Connection -> Query -> PQ.Result -> IO [r]
429−finishQuery conn q result = do
430− status <- PQ.resultStatus result
433− throwIO $ QueryError "query: Empty query" q
435− throwIO $ QueryError "query resulted in a command response" q
437− ncols <- PQ.nfields result
438− let unCol (PQ.Col x) = fromIntegral x :: Int
439− typenames <- V.generateM (unCol ncols)
440− (\(PQ.Col . fromIntegral -> col) -> do
441− getTypename conn =<< PQ.ftype result col)
442− nrows <- PQ.ntuples result
443− ncols <- PQ.nfields result
444− forM' 0 (nrows-1) $ \row -> do
445− let rw = Row row typenames result
446− case runStateT (runReaderT (unRP fromRow) rw) 0 of
447− Ok (val,col) | col == ncols -> return val
449− vals <- forM' 0 (ncols-1) $ \c -> do
450− v <- PQ.getvalue result row c
451− return ( typenames V.! unCol c
453− throw (ConversionFailed
454− (show (unCol ncols) ++ " values: " ++ show vals)
455− (show (unCol col) ++ " slots in target type")
456− "mismatch between number of columns to \
457− \convert and number in target type")
458− Errors [] -> throwIO $ ConversionFailed "" "" "unknown error"
459− Errors [x] -> throwIO x
460− Errors xs -> throwIO $ ManyErrors xs
462− throwIO $ QueryError "query: COPY TO is not supported" q
464− throwIO $ QueryError "query: COPY FROM is not supported" q
465− PQ.BadResponse -> throwResultError "query" result status
466− PQ.NonfatalError -> throwResultError "query" result status
467− PQ.FatalError -> throwResultError "query" result status
469−ellipsis :: ByteString -> ByteString
471− | B.length bs > 15 = B.take 10 bs `B.append` "[...]"
475−-- | Of the four isolation levels defined by the SQL standard,
476−-- these are the three levels distinguished by PostgreSQL as of version 9.0.
477−-- See <http://www.postgresql.org/docs/9.1/static/transaction-iso.html>
478−-- for more information. Note that prior to PostgreSQL 9.0, 'RepeatableRead'
479−-- was equivalent to 'Serializable'.
482− = DefaultIsolationLevel -- ^ the isolation level will be taken from
483− -- PostgreSQL's per-connection
484− -- @default_transaction_isolation@ variable,
485− -- which is initialized according to the
486− -- server's config. The default configuration
487− -- is 'ReadCommitted'.
491− deriving (Show, Eq, Ord, Enum, Bounded)
494− = DefaultReadWriteMode -- ^ the read-write mode will be taken from
495− -- PostgreSQL's per-connection
496− -- @default_transaction_read_only@ variable,
497− -- which is initialized according to the
498− -- server's config. The default configuration
502− deriving (Show, Eq, Ord, Enum, Bounded)
504−data TransactionMode = TransactionMode {
505− isolationLevel :: !IsolationLevel,
506− readWriteMode :: !ReadWriteMode
507− } deriving (Show, Eq)
509−defaultTransactionMode :: TransactionMode
510−defaultTransactionMode = TransactionMode
511− defaultIsolationLevel
512− defaultReadWriteMode
514−defaultIsolationLevel :: IsolationLevel
515−defaultIsolationLevel = DefaultIsolationLevel
517−defaultReadWriteMode :: ReadWriteMode
518−defaultReadWriteMode = DefaultReadWriteMode
520−-- | Execute an action inside a SQL transaction.
522−-- This function initiates a transaction with a \"@begin
523−-- transaction@\" statement, then executes the supplied action. If
524−-- the action succeeds, the transaction will be completed with
525−-- 'Base.commit' before this function returns.
527−-- If the action throws /any/ kind of exception (not just a
528−-- PostgreSQL-related exception), the transaction will be rolled back using
529−-- 'rollback', then the exception will be rethrown.
530−withTransaction :: Connection -> IO a -> IO a
531−withTransaction = withTransactionMode defaultTransactionMode
533−-- | Execute an action inside a SQL transaction with a given isolation level.
534−withTransactionLevel :: IsolationLevel -> Connection -> IO a -> IO a
535−withTransactionLevel lvl
536− = withTransactionMode defaultTransactionMode { isolationLevel = lvl }
538−-- | Execute an action inside a SQL transaction with a given transaction mode.
539−withTransactionMode :: TransactionMode -> Connection -> IO a -> IO a
540−withTransactionMode mode conn act =
541− mask $ \restore -> do
543− r <- restore act `onException` rollback conn
547−-- | Rollback a transaction.
548−rollback :: Connection -> IO ()
549−rollback conn = execute_ conn "ABORT" >> return ()
551−-- | Commit a transaction.
552−commit :: Connection -> IO ()
553−commit conn = execute_ conn "COMMIT" >> return ()
555−-- | Begin a transaction.
556−begin :: Connection -> IO ()
557−begin = beginMode defaultTransactionMode
559−-- | Begin a transaction with a given isolation level
560−beginLevel :: IsolationLevel -> Connection -> IO ()
561−beginLevel lvl = beginMode defaultTransactionMode { isolationLevel = lvl }
563−-- | Begin a transaction with a given transaction mode
564−beginMode :: TransactionMode -> Connection -> IO ()
565−beginMode mode conn = do
566− _ <- execute_ conn $! Query (B.concat ["BEGIN", isolevel, readmode])
569− isolevel = case isolationLevel mode of
570− DefaultIsolationLevel -> ""
571− ReadCommitted -> " ISOLATION LEVEL READ COMMITTED"
572− RepeatableRead -> " ISOLATION LEVEL REPEATABLE READ"
573− Serializable -> " ISOLATION LEVEL SERIALIZABLE"
574− readmode = case readWriteMode mode of
575− DefaultReadWriteMode -> ""
576− ReadWrite -> " READ WRITE"
577− ReadOnly -> " READ ONLY"
579−fmtError :: String -> Query -> [Action] -> a
580−fmtError msg q xs = throw FormatError {
583− , fmtParams = map twiddle xs
585− where twiddle (Plain b) = toByteString b
586− twiddle (Escape s) = s
587− twiddle (EscapeBytea s) = s
588− twiddle (Many ys) = B.concat (map twiddle ys)
@@ -613,9 +658,11 @@ fmtError msg q xs = throw FormatError {
614-- > import Database.PostgreSQL.Simple
659-- > import Database.PostgreSQL.Simple
616-- > hello = do
662-- > hello = do
617−-- > conn <- connect defaultConnectInfo
663+-- > conn <- connectPostgreSQL ""
618−-- > query conn "select 2 + 2"
664+-- > [Only i] <- query_ conn "select 2 + 2"
620-- A 'Query' value does not represent the actual query that will be
667-- A 'Query' value does not represent the actual query that will be
621-- executed, but is a template for constructing the final query.
668-- executed, but is a template for constructing the final query.
@@ -676,15 +723,30 @@ fmtError msg q xs = throw FormatError {
676-- The same kind of problem can arise with string literals if you have
723-- The same kind of problem can arise with string literals if you have
677-- the @OverloadedStrings@ language extension enabled. Again, just
724-- the @OverloadedStrings@ language extension enabled. Again, just
678-- use an explicit type signature if this happens.
725-- 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])
680-- $only_param
741-- $only_param
682-- Haskell lacks a single-element tuple type, so if you have just one
743-- Haskell lacks a single-element tuple type, so if you have just one
683−-- value you want substituted into a query, what should you do?
744+-- value you want substituted into a query or a single-column result,
745+-- what should you do?
685-- The obvious approach would appear to be something like this:
747-- The obvious approach would appear to be something like this:
687−-- > instance (Param a) => QueryParam a where
749+-- > instance (ToField a) => ToRow a where
690-- Unfortunately, this wreaks havoc with type inference, so we take a
752-- Unfortunately, this wreaks havoc with type inference, so we take a
@@ -695,6 +757,9 @@ fmtError msg q xs = throw FormatError {
696-- > execute conn "insert into users (first_name) values (?)"
758-- > execute conn "insert into users (first_name) values (?)"
697-- > ["Nuala"]
759-- > ["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.
@@ -712,7 +777,7 @@ fmtError msg q xs = throw FormatError {
712-- Here's an example:
777-- Here's an example:
714-- > query conn "select * from users where first_name in ?" $
779-- > query conn "select * from users where first_name in ?" $
715−-- > In ["Anna", "Boris", "Carla"]
780+-- > Only $ In ["Anna", "Boris", "Carla"]
717-- If your 'In'-wrapped list is empty, the string @\"(null)\"@ will be
782-- If your 'In'-wrapped list is empty, the string @\"(null)\"@ will be
718-- substituted instead, to ensure that your clause remains
783-- substituted instead, to ensure that your clause remains
@@ -740,7 +805,7 @@ fmtError msg q xs = throw FormatError {
740-- * There must be no other \"@?@\" characters anywhere in your
805-- * There must be no other \"@?@\" characters anywhere in your
741-- template.
806-- template.
743−-- * There must one or more \"@?@\" in the parentheses.
808+-- * There must be one or more \"@?@\" in the parentheses.
745-- * Extra white space is fine.
810-- * Extra white space is fine.
@@ -761,6 +826,22 @@ fmtError msg q xs = throw FormatError {
761-- > insert into users (first_name,last_name) values
826-- > insert into users (first_name,last_name) values
762-- > ('Boris','Karloff'),('Ed','Wood')
827-- > ('Boris','Karloff'),('Ed','Wood')
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'.
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.
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")]
764-- $result
845-- $result
766-- The 'query' and 'query_' functions return a list of values in the
847-- The 'query' and 'query_' functions return a list of values in the
@@ -795,7 +876,7 @@ fmtError msg q xs = throw FormatError {
796-- Although SQL can accommodate @NULL@ as a value for any of these
877-- Although SQL can accommodate @NULL@ as a value for any of these
797-- types, Haskell cannot. If your result contains columns that may be
878-- types, Haskell cannot. If your result contains columns that may be
798−-- @NULL@, be sure that you use 'Maybe' in those positions of of your
879+-- @NULL@, be sure that you use 'Maybe' in those positions of your
801-- > (Text, Maybe Int, Int)
882-- > (Text, Maybe Int, Int)
@@ -821,7 +902,7 @@ fmtError msg q xs = throw FormatError {
821-- all values of the given PostgreSQL type is considered \"compatible\".
902-- all values of the given PostgreSQL type is considered \"compatible\".
822-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
903-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
823-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
904-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
824−-- represent a @SMALLINT@, so it is considered compatble with those types.
905+-- represent a @SMALLINT@, so it is considered compatible with those types.
826-- * A numeric compatibility check is based only on the type of a
907-- * A numeric compatibility check is based only on the type of a
827-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
908-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
@@ -835,25 +916,3 @@ fmtError msg q xs = throw FormatError {
835-- UTF-8. If you use some other encoding, decoding may fail or give
916-- UTF-8. If you use some other encoding, decoding may fail or give
836-- wrong results. In such cases, write a @newtype@ wrapper and a
917-- wrong results. In such cases, write a @newtype@ wrapper and a
837-- custom 'Result' instance to handle your encoding.
918-- custom 'Result' instance to handle your encoding.
839−getTypename :: Connection -> PQ.Oid -> IO ByteString
840−getTypename conn@Connection{..} oid =
841− case oid2builtin oid of
842− Just builtin -> return $! builtin2typname builtin
843− Nothing -> modifyMVar connectionObjects $ \oidmap -> do
844− case IntMap.lookup (oid2int oid) oidmap of
845− Just name -> return (oidmap, name)
847− names <- query conn "SELECT typname FROM pg_type WHERE oid=?"
849− name <- case names of
850− [] -> return $ throw SqlError {
851− sqlNativeError = -1,
852− sqlErrorMsg = "invalid type oid",
855− [Only x] -> return x
856− _ -> fail "typename query returned more than one result"
857− -- oid is a primary key, so the query should
858− -- never return more than one result
859− return (IntMap.insert (oid2int oid) name oidmap, name)