@@ -1,14 +1,18 @@
1−{-# LANGUAGE BangPatterns, DeriveDataTypeable, OverloadedStrings #-}
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE NamedFieldPuns #-}
3+{-# LANGUAGE OverloadedStrings #-}
4+{-# LANGUAGE PatternGuards #-}
2−{-# LANGUAGE DoAndIfThenElse, RecordWildCards, NamedFieldPuns #-}
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,26 +55,35 @@ module Database.PostgreSQL.Simple
50 -- * Types
57 -- * Types
52− , Connection
58+ Connection
55 , Binary(..)
63 , Binary(..)
56 , Only(..)
64 , Only(..)
57 -- ** Exceptions
66 -- ** Exceptions
67+ , SomePostgreSqlException(..)
58 , SqlError(..)
68 , SqlError(..)
59− , FormatError(fmtMessage, fmtQuery, fmtParams)
70+ , FormatError(..)
60− , QueryError(qeMessage, qeQuery)
71+ , QueryError(..)
61− , ResultError(errSQLType, errHaskellType, errMessage)
72+ , ResultError(..)
62 -- * Connection management
73 -- * Connection management
64 , Base.connectPostgreSQL
74 , Base.connectPostgreSQL
65− , Base.postgreSQLConnectionString
66− , Base.defaultConnectInfo
67 , Base.close
75 , Base.close
78+ , Base.ConnectInfo(..)
79+ , Base.defaultConnectInfo
80+ , Base.postgreSQLConnectionString
68 -- * Queries that return results
81 -- * Queries that return results
84+ -- ** Queries taking parser as argument
71 -- * Queries that stream results
87 -- * Queries that stream results
72 , FoldOptions(..)
88 , FoldOptions(..)
73 , FetchQuantity(..)
89 , FetchQuantity(..)
@@ -78,6 +94,15 @@ module Database.PostgreSQL.Simple
78 , foldWithOptions_
94 , foldWithOptions_
80 , forEach_
96 , forEach_
98+ -- ** Queries that stream results taking a parser as an argument
100+ , foldWithOptionsAndParser
102+ , foldWithOptionsAndParser_
81 -- * Statements that do not return results
106 -- * Statements that do not return results
82 , execute
107 , execute
83 , execute_
108 , execute_
@@ -85,18 +110,9 @@ module Database.PostgreSQL.Simple
85-- , Base.insertID
110-- , Base.insertID
86 -- * Transaction handling
111 -- * Transaction handling
87 , withTransaction
112 , withTransaction
88− , TransactionMode(..)
113+ , withSavepoint
91− , defaultTransactionMode
92− , defaultIsolationLevel
93− , defaultReadWriteMode
94− , withTransactionLevel
95− , withTransactionMode
96-- , Base.autocommit
114-- , Base.autocommit
101 , rollback
117 , rollback
102 -- * Helper functions
118 -- * Helper functions
@@ -104,50 +120,27 @@ module Database.PostgreSQL.Simple
104 , formatQuery
120 , formatQuery
107−import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
123+import Data.ByteString.Builder (Builder, byteString, char8)
108−import Blaze.ByteString.Builder.Char8 (fromChar)
109−import Control.Applicative ((<$>), pure)
124+import Control.Applicative ((<$>))
110−import Control.Concurrent.MVar
125+import Control.Exception as E
111−import Control.Exception (Exception, onException, throw, throwIO, finally)
112−import Control.Monad (foldM)
113−import Data.ByteString (ByteString)
126+import Data.ByteString (ByteString)
114−import Data.Char(ord)
115−import Data.Int (Int64)
127+import Data.Int (Int64)
116−import qualified Data.IntMap as IntMap
117−import Data.List (intersperse)
128+import Data.List (intersperse)
118−import Data.Monoid (mappend, mconcat)
129+import Data.Monoid (mconcat)
119−import Data.Typeable (Typeable)
120−import Database.PostgreSQL.Simple.BuiltinTypes (oid2builtin, builtin2typname)
121−import Database.PostgreSQL.Simple.Compat (mask)
130+import Database.PostgreSQL.Simple.Compat ((<>), toByteString)
131+import Database.PostgreSQL.Simple.Cursor
132+import Database.PostgreSQL.Simple.FromField (ResultError(..))
122−import Database.PostgreSQL.Simple.Param (Action(..), inQuotes)
133+import Database.PostgreSQL.Simple.FromRow (FromRow(..))
123−import Database.PostgreSQL.Simple.QueryParams (QueryParams(..))
134+import Database.PostgreSQL.Simple.ToField (Action(..))
124−import Database.PostgreSQL.Simple.Result (ResultError(..))
125−import Database.PostgreSQL.Simple.QueryResults (QueryResults(..))
135+import Database.PostgreSQL.Simple.ToRow (ToRow(..))
126−import Database.PostgreSQL.Simple.Types (Binary(..), In(..), Only(..), Query(..))
136+import Database.PostgreSQL.Simple.Types
137+ ( Binary(..), In(..), Only(..), Query(..), (:.)(..) )
127−import Database.PostgreSQL.Simple.Internal as Base
138+import Database.PostgreSQL.Simple.Internal as Base
139+import Database.PostgreSQL.Simple.Internal.PQResultUtils
140+import Database.PostgreSQL.Simple.Transaction
128import qualified Database.PostgreSQL.LibPQ as PQ
141import qualified Database.PostgreSQL.LibPQ as PQ
129−import Text.Regex.PCRE.Light (compile, caseless, match)
130import qualified Data.ByteString.Char8 as B
142import qualified Data.ByteString.Char8 as B
132−-- | Exception thrown if a 'Query' could not be formatted correctly.
133−-- This may occur if the number of \'@?@\' characters in the query
134−-- string does not match the number of parameters provided.
135−data FormatError = FormatError {
136− fmtMessage :: String
138− , fmtParams :: [ByteString]
139− } deriving (Eq, Show, Typeable)
141−instance Exception FormatError
143−-- | Exception thrown if 'query' is used to perform an @INSERT@-like
144−-- operation, or 'execute' is used to perform a @SELECT@-like operation.
145−data QueryError = QueryError {
148− } deriving (Eq, Show, Typeable)
150−instance Exception QueryError
152-- | Format a query string.
145-- | Format a query string.
@@ -159,11 +152,11 @@ instance Exception QueryError
160-- Throws 'FormatError' if the query string could not be formatted
153-- Throws 'FormatError' if the query string could not be formatted
161-- correctly.
154-- correctly.
162−formatQuery :: QueryParams q => Connection -> Query -> q -> IO ByteString
155+formatQuery :: ToRow q => Connection -> Query -> q -> IO ByteString
163formatQuery conn q@(Query template) qs
156formatQuery conn q@(Query template) qs
164 | null xs && '?' `B.notElem` template = return template
157 | null xs && '?' `B.notElem` template = return template
165 | otherwise = toByteString <$> buildQuery conn q template xs
158 | otherwise = toByteString <$> buildQuery conn q template xs
166− where xs = renderParams qs
159+ where xs = toRow qs
168-- | Format a query string with a variable number of rows.
161-- | Format a query string with a variable number of rows.
@@ -178,106 +171,208 @@ formatQuery conn q@(Query template) qs
179-- Throws 'FormatError' if the query string could not be formatted
172-- Throws 'FormatError' if the query string could not be formatted
180-- correctly.
173-- correctly.
181−formatMany :: (QueryParams q) => Connection -> Query -> [q] -> IO ByteString
174+formatMany :: (ToRow q) => Connection -> Query -> [q] -> IO ByteString
182formatMany _ q [] = fmtError "no rows supplied" q []
175formatMany _ q [] = fmtError "no rows supplied" q []
183formatMany conn q@(Query template) qs = do
176formatMany conn q@(Query template) qs = do
184− case match re template [] of
177+ case parseTemplate template of
185− Just [_,before,qbits,after] -> do
178+ Just (before, qbits, after) -> do
186− bs <- mapM (buildQuery conn q qbits . renderParams) qs
179+ bs <- mapM (buildQuery conn q qbits . toRow) qs
187− return . toByteString . mconcat $ fromByteString before :
180+ return . toByteString . mconcat $ byteString before :
188− intersperse (fromChar ',') bs ++
181+ intersperse (char8 ',') bs ++
189− [fromByteString after]
182+ [byteString after]
190− _ -> 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
192− re = compile "^([^?]+\\bvalues\\s*)\
204+ -- Search for the next occurrence of "VALUES"
193− \(\\(\\s*[?](?:\\s*,\\s*[?])*\\s*\\))\
205+ search bs =
194− \([^?]*)$"
206+ case B.breakSubstring "VALUES" bs of
195− [caseless]
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)
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
197−escapeStringConn :: Connection -> ByteString -> IO (Maybe ByteString)
198−escapeStringConn conn s = withConnection conn $ \c -> do
199− PQ.escapeStringConn c s
201buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
281buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
202−buildQuery conn q template xs = zipParams (split template) <$> mapM sub xs
282+buildQuery conn q template xs =
203− where quote = inQuotes . fromByteString . maybe undefined id
283+ zipParams (split template) <$> mapM (buildAction conn q xs) xs
204− sub (Plain b) = pure b
284+ where split s =
205− sub (Escape s) = quote <$> escapeStringConn conn s
285+ -- This part escapes double '??'s to make literal '?'s possible
206− sub (Many ys) = mconcat <$> mapM sub ys
286+ -- in PostgreSQL queries using the JSON operators: @?@, @?|@ and @?&@
287+ let (h,t) = breakOnSingleQuestionMark s
207− split s = fromByteString h : if B.null t then [] else split (B.tail t)
291+ else split (B.tail t)
208− where (h,t) = B.break (=='?') s
209− zipParams (t:ts) (p:ps) = t `mappend` p `mappend` zipParams ts ps
292+ zipParams (t:ts) (p:ps) = t <> p <> zipParams ts ps
210 zipParams [t] [] = t
293 zipParams [t] [] = t
211− zipParams _ _ = fmtError (show (B.count '?' template) ++
294+ zipParams _ _ = fmtError (show countSingleQs ++
212− " '?' characters, but " ++
295+ " single '?' characters, but " ++
213 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
215-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
307-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
216-- expected to return results.
308-- expected to return results.
218-- Returns the number of rows affected.
310-- Returns the number of rows affected.
220−-- 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.
221−execute :: (QueryParams q) => Connection -> Query -> q -> IO Int64
314+execute :: (ToRow q) => Connection -> Query -> q -> IO Int64
222execute conn template qs = do
315execute conn template qs = do
223 result <- exec conn =<< formatQuery conn template qs
316 result <- exec conn =<< formatQuery conn template qs
224 finishExecute conn template result
317 finishExecute conn template result
226−-- | A version of 'execute' that does not perform query substitution.
227−execute_ :: Connection -> Query -> IO Int64
228−execute_ conn q@(Query stmt) = do
229− result <- exec conn stmt
230− finishExecute conn q result
232-- | 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
233-- expected to return results.
320-- expected to return results.
235−-- 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.
237−-- 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\")]
238−executeMany :: (QueryParams q) => Connection -> Query -> [q] -> IO Int64
349+executeMany :: (ToRow q) => Connection -> Query -> [q] -> IO Int64
239executeMany _ _ [] = return 0
350executeMany _ _ [] = return 0
240executeMany conn q qs = do
351executeMany conn q qs = do
241 result <- exec conn =<< formatMany conn q qs
352 result <- exec conn =<< formatMany conn q qs
242 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.
244−finishExecute :: Connection -> Query -> PQ.Result -> IO Int64
367+returning :: (ToRow q, FromRow r) => Connection -> Query -> [q] -> IO [r]
245−finishExecute _conn q result = do
368+returning = returningWith fromRow
246− status <- PQ.resultStatus result
249− ncols <- PQ.nfields result
251− then throwIO $ QueryError ("execute resulted in " ++ show ncols ++
254− nstr <- PQ.cmdTuples result
255− return $ case nstr of
256− Nothing -> 0 -- is this appropriate?
257− Just str -> toInteger str
259− ncols <- PQ.nfields result
260− throwIO $ QueryError ("execute resulted in " ++ show ncols ++
262− PQ.CopyIn -> fail "FIXME: postgresql-simple does not currently support COPY IN"
263− PQ.CopyOut -> fail "FIXME: postgresql-simple does not currently support COPY OUT"
265− errormsg <- maybe "" id <$> PQ.resultErrorMessage result
266− statusmsg <- PQ.resStatus status
267− state <- maybe "" id <$> PQ.resultErrorField result PQ.DiagSqlstate
268− throwIO $ SqlError { sqlState = state
269− , sqlNativeError = fromEnum status
270− , sqlErrorMsg = B.concat [ "execute: ", statusmsg
273− toInteger str = B.foldl' delta 0 str
276− if '0' <= c && c <= '9'
277− then 10 * acc + fromIntegral (ord c - ord '0')
278− else error ("finishExecute: not an int: " ++ B.unpack str)
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
282-- | 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
283-- results. All results are retrieved and converted before this
378-- results. All results are retrieved and converted before this
@@ -294,17 +389,27 @@ finishExecute _conn q result = do
294-- using 'execute' instead of 'query').
389-- using 'execute' instead of 'query').
296-- * 'ResultError': result conversion failed.
391-- * 'ResultError': result conversion failed.
297−query :: (QueryParams q, QueryResults r)
392+--
393+-- * 'SqlError': the postgresql backend returned an error, e.g.
394+-- a syntax or type error, or an incorrect table or column name.
298− => Connection -> Query -> q -> IO [r]
395+query :: (ToRow q, FromRow r) => Connection -> Query -> q -> IO [r]
299−query conn template qs = do
396+query = queryWith fromRow
300− result <- exec conn =<< formatQuery conn template qs
301− finishQuery conn template result
303-- | A version of 'query' that does not perform query substitution.
398-- | A version of 'query' that does not perform query substitution.
304−query_ :: (QueryResults r) => Connection -> Query -> IO [r]
399+query_ :: (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]
305−query_ conn q@(Query que) = do
410+queryWith_ parser conn q@(Query que) = do
306 result <- exec conn que
411 result <- exec conn que
307− finishQuery conn q result
412+ finishQueryWith parser conn q result
309-- | 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
310-- results. Results are streamed incrementally from the server, and
415-- results. Results are streamed incrementally from the server, and
@@ -316,6 +421,12 @@ query_ conn q@(Query que) = do
316-- This fold is /not/ strict. The stream consumer is responsible for
421-- This fold is /not/ strict. The stream consumer is responsible for
317-- 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.
319-- Exceptions that may be thrown:
430-- Exceptions that may be thrown:
321-- * 'FormatError': the query string could not be formatted correctly.
432-- * 'FormatError': the query string could not be formatted correctly.
@@ -324,8 +435,10 @@ query_ conn q@(Query que) = do
324-- using 'execute' instead of 'query').
435-- using 'execute' instead of 'query').
326-- * '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.
328−fold :: ( QueryResults row, QueryParams params )
441+fold :: ( FromRow row, ToRow params )
329 => Connection
442 => Connection
331 -> params
444 -> params
@@ -334,6 +447,20 @@ fold :: ( QueryResults row, QueryParams params )
335fold = 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.
337data FetchQuantity
464data FetchQuantity
338 = Automatic
465 = Automatic
339 | Fixed !Int
466 | Fixed !Int
@@ -344,13 +471,20 @@ data FoldOptions
344 transactionMode :: !TransactionMode
471 transactionMode :: !TransactionMode
474+-- | defaults to 'Automatic', and 'TransactionMode' 'ReadCommitted' 'ReadOnly'
347defaultFoldOptions :: FoldOptions
475defaultFoldOptions :: FoldOptions
348defaultFoldOptions = FoldOptions {
476defaultFoldOptions = FoldOptions {
349 fetchQuantity = Automatic,
477 fetchQuantity = Automatic,
350 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.
353−foldWithOptions :: ( QueryResults row, QueryParams params )
487+foldWithOptions :: ( FromRow row, ToRow params )
354 => FoldOptions
488 => FoldOptions
355 -> Connection
489 -> Connection
@@ -358,12 +492,24 @@ foldWithOptions :: ( QueryResults row, QueryParams params )
359 -> (a -> row -> IO a)
493 -> (a -> row -> IO a)
361−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
362 q <- formatQuery conn template qs
508 q <- formatQuery conn template qs
363− doFold opts conn template (Query q) a f
509+ doFold opts parser conn template (Query q) a f
365-- | A version of 'fold' that does not perform query substitution.
511-- | A version of 'fold' that does not perform query substitution.
366−fold_ :: (QueryResults r) =>
512+fold_ :: (FromRow r) =>
367 Connection
513 Connection
368 -> Query -- ^ Query.
514 -> Query -- ^ Query.
369 -> a -- ^ Initial state for result consumer.
515 -> a -- ^ Initial state for result consumer.
@@ -371,26 +517,43 @@ fold_ :: (QueryResults r) =>
372fold_ = foldWithOptions_ defaultFoldOptions
518fold_ = foldWithOptions_ defaultFoldOptions
520+-- | A version of 'fold_' taking a parser as an argument
521+foldWith_ :: RowParser r
527+foldWith_ = foldWithOptionsAndParser_ defaultFoldOptions
375−foldWithOptions_ :: (QueryResults r) =>
529+foldWithOptions_ :: (FromRow r) =>
376 FoldOptions
530 FoldOptions
377 -> Connection
531 -> Connection
378 -> Query -- ^ Query.
532 -> Query -- ^ Query.
379 -> a -- ^ Initial state for result consumer.
533 -> a -- ^ Initial state for result consumer.
380 -> (a -> r -> IO a) -- ^ Result consumer.
534 -> (a -> r -> IO a) -- ^ Result consumer.
382−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
384−
538+-- | A version of 'foldWithOptions_' taking a parser as an argument
385−doFold :: ( QueryResults row )
539+foldWithOptionsAndParser_ :: FoldOptions
386− => 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
387 -> Connection
550 -> Connection
391 -> (a -> row -> IO a)
554 -> (a -> row -> IO a)
393−doFold FoldOptions{..} conn _template q a f = do
556+doFold FoldOptions{..} parser conn _template q a0 f = do
394 stat <- withConnection conn PQ.transactionStatus
557 stat <- withConnection conn PQ.transactionStatus
395 case stat of
558 case stat of
396 PQ.TransIdle -> withTransactionMode transactionMode conn go
559 PQ.TransIdle -> withTransactionMode transactionMode conn go
@@ -408,11 +571,17 @@ doFold FoldOptions{..} conn _template q a f = do
408 PQ.TransUnknown -> fail "foldWithOpts FIXME: PQ.TransUnknown"
571 PQ.TransUnknown -> fail "foldWithOpts FIXME: PQ.TransUnknown"
409 -- Not sure what this means.
572 -- Not sure what this means.
411− go = do
574+ declare =
412− -- FIXME: what about name clashes with already-declared cursors?
575+ declareCursor conn q
413− _ <- execute_ conn ("DECLARE fold NO SCROLL CURSOR FOR "
576+ fetch cursor a =
414− `mappend` q)
577+ foldForwardWithParser cursor parser chunkSize f a
415− loop a `finally` execute_ conn "CLOSE fold"
578+
579+ go = bracket declare closeCursor $ \cursor ->
580+ let loop a = fetch cursor a >>=
417-- FIXME: choose the Automatic chunkSize more intelligently
586-- FIXME: choose the Automatic chunkSize more intelligently
418-- 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
@@ -423,168 +592,45 @@ doFold FoldOptions{..} conn _template q a f = do
423 chunkSize = case fetchQuantity of
592 chunkSize = case fetchQuantity of
424 Automatic -> 256
593 Automatic -> 256
425 Fixed n -> n
594 Fixed n -> n
427− rs <- query conn "FETCH FORWARD ? FROM fold" (Only chunkSize)
428− if null rs then return a else foldM f a rs >>= loop
430-- | A version of 'fold' that does not transform a state value.
596-- | A version of 'fold' that does not transform a state value.
431−forEach :: (QueryParams q, QueryResults r) =>
597+forEach :: (ToRow q, FromRow r) =>
432 Connection
598 Connection
433 -> Query -- ^ Query template.
599 -> Query -- ^ Query template.
434 -> q -- ^ Query parameters.
600 -> q -- ^ Query parameters.
435 -> (r -> IO ()) -- ^ Result consumer.
601 -> (r -> IO ()) -- ^ Result consumer.
437−forEach conn template qs = fold conn template qs () . const
603+forEach = forEachWith fromRow
438{-# 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 #-}
440-- | A version of 'forEach' that does not perform query substitution.
617-- | A version of 'forEach' that does not perform query substitution.
441−forEach_ :: (QueryResults r) =>
618+forEach_ :: (FromRow r) =>
442 Connection
619 Connection
443 -> Query -- ^ Query template.
620 -> Query -- ^ Query template.
444 -> (r -> IO ()) -- ^ Result consumer.
621 -> (r -> IO ()) -- ^ Result consumer.
446−forEach_ conn template = fold_ conn template () . const
623+forEach_ = forEachWith_ fromRow
447{-# INLINE forEach_ #-}
624{-# INLINE forEach_ #-}
449−forM' :: (Ord n, Num n) => n -> n -> (n -> IO a) -> IO [a]
626+forEachWith_ :: RowParser r
450−forM' lo hi m = loop hi []
627+ -> Connection
452− loop !n !as
629+ -> (r -> IO ())
453− | n < lo = return as
630+ -> IO ()
454− | otherwise = do
631+forEachWith_ parser conn template = foldWith_ parser conn template () . const
455− a <- m n
632+{-# INLINE forEachWith_ #-}
456− loop (n-1) (a:as)
633+
458−finishQuery :: (QueryResults r) => Connection -> Query -> PQ.Result -> IO [r]
459−finishQuery conn q result = do
460− status <- PQ.resultStatus result
463− throwIO $ QueryError "query resulted in a command response" q
465− ncols <- PQ.nfields result
466− fields <- forM' 0 (ncols-1) $ \column -> do
467− type_oid <- PQ.ftype result column
468− typename <- getTypename conn type_oid
470− nrows <- PQ.ntuples result
471− forM' 0 (nrows-1) $ \row -> do
472− values <- forM' 0 (ncols-1) (\col -> PQ.getvalue result row col)
473− case convertResults fields values of
474− Left err -> throwIO err
476− PQ.CopyIn -> fail "FIXME: postgresql-simple does not currently support COPY IN"
477− PQ.CopyOut -> fail "FIXME: postgresql-simple does not currently support COPY OUT"
479− errormsg <- maybe "" id <$> PQ.resultErrorMessage result
480− statusmsg <- PQ.resStatus status
481− state <- maybe "" id <$> PQ.resultErrorField result PQ.DiagSqlstate
482− throwIO $ SqlError { sqlState = state
483− , sqlNativeError = fromEnum status
484− , sqlErrorMsg = B.concat [ "query: ", statusmsg
487−-- | Of the four isolation levels defined by the SQL standard,
488−-- these are the three levels distinguished by PostgreSQL as of version 9.0.
489−-- See <http://www.postgresql.org/docs/9.1/static/transaction-iso.html>
490−-- for more information.
496− deriving (Show, Eq, Ord, Enum, Bounded)
501− deriving (Show, Eq, Ord, Enum, Bounded)
503−data TransactionMode = TransactionMode {
504− isolationLevel :: !IsolationLevel,
505− readWriteMode :: !ReadWriteMode
506− } deriving (Show, Eq)
508−defaultTransactionMode :: TransactionMode
509−defaultTransactionMode = TransactionMode ReadCommitted ReadWrite
511−defaultIsolationLevel :: IsolationLevel
512−defaultIsolationLevel = ReadCommitted
514−defaultReadWriteMode :: ReadWriteMode
515−defaultReadWriteMode = ReadWrite
517−-- | Execute an action inside a SQL transaction.
519−-- This function initiates a transaction with a \"@begin
520−-- transaction@\" statement, then executes the supplied action. If
521−-- the action succeeds, the transaction will be completed with
522−-- 'Base.commit' before this function returns.
524−-- If the action throws /any/ kind of exception (not just a
525−-- PostgreSQL-related exception), the transaction will be rolled back using
526−-- 'rollback', then the exception will be rethrown.
527−withTransaction :: Connection -> IO a -> IO a
528−withTransaction = withTransactionMode defaultTransactionMode
530−-- | Execute an action inside a SQL transaction with a given isolation level.
531−withTransactionLevel :: IsolationLevel -> Connection -> IO a -> IO a
532−withTransactionLevel lvl
533− = withTransactionMode defaultTransactionMode { isolationLevel = lvl }
535−-- | Execute an action inside a SQL transaction with a given transaction mode.
536−withTransactionMode :: TransactionMode -> Connection -> IO a -> IO a
537−withTransactionMode mode conn act =
538− mask $ \restore -> do
540− r <- restore act `onException` rollback conn
544−-- | Rollback a transaction.
545−rollback :: Connection -> IO ()
546−rollback conn = execute_ conn "ABORT" >> return ()
548−-- | Commit a transaction.
549−commit :: Connection -> IO ()
550−commit conn = execute_ conn "COMMIT" >> return ()
552−-- | Begin a transaction.
553−begin :: Connection -> IO ()
554−begin = beginMode defaultTransactionMode
556−-- | Begin a transaction with a given isolation level
557−beginLevel :: IsolationLevel -> Connection -> IO ()
558−beginLevel lvl = beginMode defaultTransactionMode { isolationLevel = lvl }
560−-- | Begin a transaction with a given transaction mode
561−beginMode :: TransactionMode -> Connection -> IO ()
562−beginMode mode conn = do
563− _ <- execute_ conn $!
565− TransactionMode ReadCommitted ReadWrite ->
567− TransactionMode ReadCommitted ReadOnly ->
569− TransactionMode RepeatableRead ReadWrite ->
570− "BEGIN ISOLATION LEVEL REPEATABLE READ"
571− TransactionMode RepeatableRead ReadOnly ->
572− "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"
573− TransactionMode Serializable ReadWrite ->
574− "BEGIN ISOLATION LEVEL SERIALIZABLE"
575− TransactionMode Serializable ReadOnly ->
576− "BEGIN ISOLATION LEVEL SERIALIZABLE 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 (Many ys) = B.concat (map twiddle ys)
@@ -612,9 +658,11 @@ fmtError msg q xs = throw FormatError {
613-- > import Database.PostgreSQL.Simple
659-- > import Database.PostgreSQL.Simple
615-- > hello = do
662-- > hello = do
616−-- > conn <- connect defaultConnectInfo
663+-- > conn <- connectPostgreSQL ""
617−-- > query conn "select 2 + 2"
664+-- > [Only i] <- query_ conn "select 2 + 2"
619-- 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
620-- executed, but is a template for constructing the final query.
668-- executed, but is a template for constructing the final query.
@@ -661,7 +709,7 @@ fmtError msg q xs = throw FormatError {
661-- Automated type inference means that you will often be able to avoid
709-- Automated type inference means that you will often be able to avoid
662-- supplying explicit type signatures for the elements of a tuple.
710-- supplying explicit type signatures for the elements of a tuple.
663-- However, sometimes the compiler will not be able to infer your
711-- However, sometimes the compiler will not be able to infer your
664−-- types. Consider a care where you write a numeric literal in a
712+-- types. Consider a case where you write a numeric literal in a
665-- parameter tuple:
713-- parameter tuple:
667-- > query conn "select ? + ?" (40,2)
715-- > query conn "select ? + ?" (40,2)
@@ -675,15 +723,30 @@ fmtError msg q xs = throw FormatError {
675-- 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
676-- the @OverloadedStrings@ language extension enabled. Again, just
724-- the @OverloadedStrings@ language extension enabled. Again, just
677-- 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])
679-- $only_param
741-- $only_param
681-- 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
682−-- 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?
684-- The obvious approach would appear to be something like this:
747-- The obvious approach would appear to be something like this:
686−-- > instance (Param a) => QueryParam a where
749+-- > instance (ToField a) => ToRow a where
689-- Unfortunately, this wreaks havoc with type inference, so we take a
752-- Unfortunately, this wreaks havoc with type inference, so we take a
@@ -694,6 +757,9 @@ fmtError msg q xs = throw FormatError {
695-- > execute conn "insert into users (first_name) values (?)"
758-- > execute conn "insert into users (first_name) values (?)"
696-- > ["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.
@@ -711,7 +777,7 @@ fmtError msg q xs = throw FormatError {
711-- Here's an example:
777-- Here's an example:
713-- > query conn "select * from users where first_name in ?" $
779-- > query conn "select * from users where first_name in ?" $
714−-- > In ["Anna", "Boris", "Carla"]
780+-- > Only $ In ["Anna", "Boris", "Carla"]
716-- 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
717-- substituted instead, to ensure that your clause remains
783-- substituted instead, to ensure that your clause remains
@@ -739,7 +805,7 @@ fmtError msg q xs = throw FormatError {
739-- * There must be no other \"@?@\" characters anywhere in your
805-- * There must be no other \"@?@\" characters anywhere in your
740-- template.
806-- template.
742−-- * There must one or more \"@?@\" in the parentheses.
808+-- * There must be one or more \"@?@\" in the parentheses.
744-- * Extra white space is fine.
810-- * Extra white space is fine.
@@ -760,10 +826,26 @@ fmtError msg q xs = throw FormatError {
760-- > insert into users (first_name,last_name) values
826-- > insert into users (first_name,last_name) values
761-- > ('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")]
763-- $result
845-- $result
765-- 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
766−-- 'QueryResults' typeclass. This class performs automatic extraction
848+-- 'FromRow' typeclass. This class performs automatic extraction
767-- and type conversion of rows from a query result.
849-- and type conversion of rows from a query result.
769-- Here is a simple example of how to extract results:
851-- Here is a simple example of how to extract results:
@@ -794,7 +876,7 @@ fmtError msg q xs = throw FormatError {
795-- 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
796-- types, Haskell cannot. If your result contains columns that may be
878-- types, Haskell cannot. If your result contains columns that may be
797−-- @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
800-- > (Text, Maybe Int, Int)
882-- > (Text, Maybe Int, Int)
@@ -820,7 +902,7 @@ fmtError msg q xs = throw FormatError {
820-- all values of the given PostgreSQL type is considered \"compatible\".
902-- all values of the given PostgreSQL type is considered \"compatible\".
821-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
903-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
822-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
904-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
823−-- represent a @SMALLINT@, so it is considered compatble with those types.
905+-- represent a @SMALLINT@, so it is considered compatible with those types.
825-- * 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
826-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
908-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
@@ -834,25 +916,3 @@ fmtError msg q xs = throw FormatError {
834-- 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
835-- wrong results. In such cases, write a @newtype@ wrapper and a
917-- wrong results. In such cases, write a @newtype@ wrapper and a
836-- custom 'Result' instance to handle your encoding.
918-- custom 'Result' instance to handle your encoding.
838−getTypename :: Connection -> PQ.Oid -> IO ByteString
839−getTypename conn@Connection{..} oid =
840− case oid2builtin oid of
841− Just builtin -> return $! builtin2typname builtin
842− Nothing -> modifyMVar connectionObjects $ \oidmap -> do
843− case IntMap.lookup (oid2int oid) oidmap of
844− Just name -> return (oidmap, name)
846− names <- query conn "SELECT typname FROM pg_type WHERE oid=?"
848− name <- case names of
849− [] -> return $ throw SqlError {
850− sqlNativeError = -1,
851− sqlErrorMsg = "invalid type oid",
854− [Only x] -> return x
855− _ -> fail "typename query returned more than one result"
856− -- oid is a primary key, so the query should
857− -- never return more than one result
858− return (IntMap.insert (oid2int oid) name oidmap, name)