@@ -1,23 +1,16 @@
1−{-# LANGUAGE DeriveDataTypeable #-}
2−{-# LANGUAGE NamedFieldPuns #-}
3−{-# LANGUAGE OverloadedStrings #-}
4−{-# LANGUAGE PatternGuards #-}
5−{-# LANGUAGE RecordWildCards #-}
6−{-# LANGUAGE ViewPatterns #-}
7−
8−------------------------------------------------------------------------------
1+{-# LANGUAGE BangPatterns, DeriveDataTypeable, OverloadedStrings, DoAndIfThenElse, RecordWildCards, NamedFieldPuns #-}
2+
93-- |
104-- Module: Database.PostgreSQL.Simple
115-- Copyright: (c) 2011 MailRank, Inc.
12−-- (c) 2011-2012 Leon P Smith
6+-- (c) 2011 Leon P Smith
137-- License: BSD3
148-- Maintainer: Leon P Smith <leon@melding-monads.com>
159-- Stability: experimental
10+-- Portability: portable
1611--
17−-- A mid-level client library for the PostgreSQL database, aimed at ease of
12+-- A mid-level client library for the MySQL database, aimed at ease of
1813-- use and high performance.
19−--
20−------------------------------------------------------------------------------
2114
2215module Database.PostgreSQL.Simple
2316 (
@@ -42,9 +35,6 @@ module Database.PostgreSQL.Simple
4235 -- ** Modifying multiple rows at once
4336 -- $many
4437
45− -- ** @RETURNING@: modifications that return results
46− -- $returning
47−
4838 -- * Extracting results
4939 -- $result
5040
@@ -55,54 +45,32 @@ module Database.PostgreSQL.Simple
5545 -- $types
5646
5747 -- * Types
58− Connection
48+ Base.ConnectInfo(..)
49+ , Connection
5950 , Query
60− , ToRow
61− , FromRow
6251 , In(..)
6352 , Binary(..)
6453 , Only(..)
65− , (:.)(..)
6654 -- ** Exceptions
67− , SomePostgreSqlException(..)
68− , SqlError(..)
69− , PQ.ExecStatus(..)
70− , FormatError(..)
71− , QueryError(..)
72− , ResultError(..)
55+ , FormatError(fmtMessage, fmtQuery, fmtParams)
56+ , QueryError(qeMessage, qeQuery)
57+ , ResultError(errSQLType, errHaskellType, errMessage)
7358 -- * Connection management
74− , Base.connectPostgreSQL
75− , Base.close
7659 , Base.connect
77− , Base.withConnect
78− , Base.ConnectInfo(..)
79− , Base.defaultConnectInfo
60+ , Base.connectPostgreSQL
8061 , Base.postgreSQLConnectionString
62+ , Base.defaultConnectInfo
63+ , Base.close
8164 -- * Queries that return results
8265 , query
8366 , query_
84− -- ** Queries taking parser as argument
85− , queryWith
86− , queryWith_
67+{--
8768 -- * Queries that stream results
88− , FoldOptions(..)
89− , FetchQuantity(..)
90− , defaultFoldOptions
9169 , fold
92− , foldWithOptions
9370 , fold_
94− , foldWithOptions_
9571 , forEach
9672 , 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
73+--}
10674 -- * Statements that do not return results
10775 , execute
10876 , execute_
@@ -110,7 +78,6 @@ module Database.PostgreSQL.Simple
11078-- , Base.insertID
11179 -- * Transaction handling
11280 , withTransaction
113− , withSavepoint
11481-- , Base.autocommit
11582 , begin
11683 , commit
@@ -120,27 +87,55 @@ module Database.PostgreSQL.Simple
12087 , formatQuery
12188 ) where
12289
123−import Data.ByteString.Builder (Builder, byteString, char8)
124−import Control.Applicative ((<$>))
125−import Control.Exception as E
126−import Data.ByteString (ByteString)
127−import Data.Int (Int64)
128−import Data.List (intersperse)
129−import Data.Monoid (mconcat)
130−import Database.PostgreSQL.Simple.Compat ((<>), toByteString)
131−import Database.PostgreSQL.Simple.Cursor
132−import Database.PostgreSQL.Simple.FromField (ResultError(..))
133−import Database.PostgreSQL.Simple.FromRow (FromRow(..))
134−import Database.PostgreSQL.Simple.ToField (Action(..))
135−import Database.PostgreSQL.Simple.ToRow (ToRow(..))
136−import Database.PostgreSQL.Simple.Types
137− ( Binary(..), In(..), Only(..), Query(..), (:.)(..) )
138−import Database.PostgreSQL.Simple.Internal as Base
139−import Database.PostgreSQL.Simple.Internal.PQResultUtils
140−import Database.PostgreSQL.Simple.Transaction
90+import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
91+import Blaze.ByteString.Builder.Char8 (fromChar)
92+import Control.Applicative ((<$>), pure)
93+import Control.Concurrent.MVar
94+import Control.Exception (Exception, bracket, onException, throw, throwIO)
95+import Control.Monad (forM)
96+import Control.Monad.Fix (fix)
97+import Data.ByteString (ByteString)
98+-- import qualified Data.ByteString as B (unpack)
99+import Data.Char(ord)
100+import Data.Int (Int64)
101+import qualified Data.IntMap as IntMap
102+import Data.List (intersperse)
103+import Data.Monoid (mappend, mconcat)
104+import Data.Typeable (Typeable)
105+--import Database.MySQL.Base (Connection, Result)
106+--import Database.MySQL.Base.Types (Field)
107+import Database.PostgreSQL.Simple.BuiltinTypes (oid2builtin, builtin2typname)
108+import Database.PostgreSQL.Simple.Param (Action(..), inQuotes)
109+import Database.PostgreSQL.Simple.QueryParams (QueryParams(..))
110+import Database.PostgreSQL.Simple.Result (ResultError(..))
111+import Database.PostgreSQL.Simple.QueryResults (QueryResults(..))
112+-- import Database.PostgreSQL.Simple.Result (ResultError(..))
113+import Database.PostgreSQL.Simple.Types (Binary(..), In(..), Only(..), Query(..))
114+import Database.PostgreSQL.Simple.Implementation as Base
141115import qualified Database.PostgreSQL.LibPQ as PQ
116+import Text.Regex.PCRE.Light (compile, caseless, match)
142117import qualified Data.ByteString.Char8 as B
118+--import qualified Database.MySQL.Base as Base
119+
120+-- | Exception thrown if a 'Query' could not be formatted correctly.
121+-- This may occur if the number of \'@?@\' characters in the query
122+-- string does not match the number of parameters provided.
123+data FormatError = FormatError {
124+ fmtMessage :: String
125+ , fmtQuery :: Query
126+ , fmtParams :: [ByteString]
127+ } deriving (Eq, Show, Typeable)
143128
129+instance Exception FormatError
130+
131+-- | Exception thrown if 'query' is used to perform an @INSERT@-like
132+-- operation, or 'execute' is used to perform a @SELECT@-like operation.
133+data QueryError = QueryError {
134+ qeMessage :: String
135+ , qeQuery :: Query
136+ } deriving (Eq, Show, Typeable)
137+
138+instance Exception QueryError
144139
145140-- | Format a query string.
146141--
@@ -152,11 +147,11 @@ import qualified Data.ByteString.Char8 as B
152147--
153148-- Throws 'FormatError' if the query string could not be formatted
154149-- correctly.
155−formatQuery :: ToRow q => Connection -> Query -> q -> IO ByteString
150+formatQuery :: QueryParams q => Connection -> Query -> q -> IO ByteString
156151formatQuery conn q@(Query template) qs
157152 | null xs && '?' `B.notElem` template = return template
158153 | otherwise = toByteString <$> buildQuery conn q template xs
159− where xs = toRow qs
154+ where xs = renderParams qs
160155
161156-- | Format a query string with a variable number of rows.
162157--
@@ -171,208 +166,106 @@ formatQuery conn q@(Query template) qs
171166--
172167-- Throws 'FormatError' if the query string could not be formatted
173168-- correctly.
174−formatMany :: (ToRow q) => Connection -> Query -> [q] -> IO ByteString
169+formatMany :: (QueryParams q) => Connection -> Query -> [q] -> IO ByteString
175170formatMany _ q [] = fmtError "no rows supplied" q []
176171formatMany 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
172+ case match re template [] of
173+ Just [_,before,qbits,after] -> do
174+ bs <- mapM (buildQuery conn q qbits . renderParams) qs
175+ return . toByteString . mconcat $ fromByteString before :
176+ intersperse (fromChar ',') bs ++
177+ [fromByteString after]
178+ _ -> error "foo"
203179 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
180+ re = compile "^([^?]+\\bvalues\\s*)\
181+ \(\\(\\s*[?](?:\\s*,\\s*[?])*\\s*\\))\
182+ \([^?]*)$"
183+ [caseless]
279184
185+escapeStringConn conn s = withConnection conn $ \c -> do
186+ PQ.escapeStringConn c s
280187
281188buildQuery :: 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
189+buildQuery conn q template xs = zipParams (split template) <$> mapM sub xs
190+ where quote = inQuotes . fromByteString . maybe undefined id
191+ sub (Plain b) = pure b
192+ sub (Escape s) = quote <$> escapeStringConn conn s
193+ sub (Many ys) = mconcat <$> mapM sub ys
194+ split s = fromByteString h : if B.null t then [] else split (B.tail t)
195+ where (h,t) = B.break (=='?') s
196+ zipParams (t:ts) (p:ps) = t `mappend` p `mappend` zipParams ts ps
293197 zipParams [t] [] = t
294− zipParams _ _ = fmtError (show countSingleQs ++
295− " single '?' characters, but " ++
198+ zipParams _ _ = fmtError (show (B.count '?' template) ++
199+ " '?' characters, but " ++
296200 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
306201
307202-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
308203-- expected to return results.
309204--
310205-- Returns the number of rows affected.
311206--
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
207+-- Throws 'FormatError' if the query could not be formatted correctly.
208+execute :: (QueryParams q) => Connection -> Query -> q -> IO Int64
315209execute conn template qs = do
316210 result <- exec conn =<< formatQuery conn template qs
317211 finishExecute conn template result
318212
213+-- | A version of 'execute' that does not perform query substitution.
214+execute_ :: Connection -> Query -> IO Int64
215+execute_ conn q@(Query stmt) = do
216+ result <- exec conn stmt
217+ finishExecute conn q result
218+
319219-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not
320220-- expected to return results.
321221--
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:
222+-- Returns the number of rows affected.
339223--
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
224+-- Throws 'FormatError' if the query could not be formatted correctly.
225+executeMany :: (QueryParams q) => Connection -> Query -> [q] -> IO Int64
350226executeMany _ _ [] = return 0
351227executeMany conn q qs = do
352228 result <- exec conn =<< formatMany conn q qs
353229 finishExecute conn q result
354230
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
231+finishExecute :: Connection -> Query -> PQ.Result -> IO Int64
232+finishExecute conn q result = do
233+ status <- PQ.resultStatus result
234+ case status of
235+ PQ.EmptyQuery -> fail "empty query" -- FIXME: error message
236+ PQ.CommandOk -> do
237+ ncols <- PQ.nfields result
238+ if ncols /= 0
239+ then throwIO $ QueryError ("execute resulted in " ++ show ncols ++
240+ "-column result") q
241+ else do
242+ nstr <- PQ.cmdTuples result
243+ return $ case nstr of
244+ Nothing -> 0 -- is this appropriate?
245+ Just str -> toInteger str
246+ PQ.TuplesOk -> do
247+ ncols <- PQ.nfields result
248+ throwIO $ QueryError ("execute resulted in " ++ show ncols ++
249+ "-column result") q
250+ PQ.CopyIn -> fail "FIXME: postgresql-simple does not currently support COPY IN"
251+ PQ.CopyOut -> fail "FIXME: postgresql-simple does not currently support COPY OUT"
252+ _ -> do
253+ errormsg <- maybe "" id <$> PQ.resultErrorMessage result
254+ statusmsg <- PQ.resStatus status
255+ state <- maybe "" id <$> PQ.resultErrorField result PQ.DiagSqlstate
256+ throw $ SqlError { sqlState = state
257+ , sqlNativeError = 0 -- FIXME
258+ , sqlErrorMsg = B.concat [ "execute: ", statusmsg
259+ , ": ", errormsg ]}
260+ where
261+ toInteger str = B.foldl' delta 0 str
262+ where
263+ delta acc c =
264+ if '0' <= c && c <= '9'
265+ then 10 * acc + fromIntegral (ord c - ord '0')
266+ else error ("finishExecute: not an int: " ++ B.unpack str)
267+
369268
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
376269
377270-- | Perform a @SELECT@ or other SQL query that is expected to return
378271-- results. All results are retrieved and converted before this
@@ -389,44 +282,33 @@ returningWith parser conn q qs = do
389282-- using 'execute' instead of 'query').
390283--
391284-- * '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
397−
398−-- | A version of 'query' that does not perform query substitution.
399−query_ :: (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
285+query :: (QueryParams q, QueryResults r)
286+ => Connection -> Query -> q -> IO [r]
287+query conn template qs = do
405288 result <- exec conn =<< formatQuery conn template qs
406− finishQueryWith parser conn template result
289+ finishQuery conn template result
407290
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
291+-- | A version of 'query' that does not perform query substitution.
292+query_ :: (QueryResults r) => Connection -> Query -> IO [r]
293+query_ conn q@(Query que) = do
411294 result <- exec conn que
412− finishQueryWith parser conn q result
413−
295+ finishQuery conn q result
296+{--
414297-- | Perform a @SELECT@ or other SQL query that is expected to return
415298-- results. Results are streamed incrementally from the server, and
416299-- consumed via a left fold.
417300--
301+-- The result consumer must be carefully written to execute
302+-- quickly. If the consumer is slow, server resources will be tied up,
303+-- and other clients may not be able to update the tables from which
304+-- the results are being streamed.
305+--
418306-- When dealing with small results, it may be simpler (and perhaps
419307-- faster) to use 'query' instead.
420308--
421309-- This fold is /not/ strict. The stream consumer is responsible for
422310-- forcing the evaluation of its result to avoid space leaks.
423311--
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−--
430312-- Exceptions that may be thrown:
431313--
432314-- * 'FormatError': the query string could not be formatted correctly.
@@ -435,202 +317,164 @@ queryWith_ parser conn q@(Query que) = do
435317-- using 'execute' instead of 'query').
436318--
437319-- * '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 )
442− => Connection
443− -> Query
444− -> params
445− -> a
446− -> (a -> row -> IO a)
447− -> IO a
448−fold = foldWithOptions defaultFoldOptions
449−
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.
464−data FetchQuantity
465− = Automatic
466− | Fixed !Int
467−
468−data FoldOptions
469− = FoldOptions {
470− fetchQuantity :: !FetchQuantity,
471− transactionMode :: !TransactionMode
472− }
473−
474−-- | defaults to 'Automatic', and 'TransactionMode' 'ReadCommitted' 'ReadOnly'
475−defaultFoldOptions :: FoldOptions
476−defaultFoldOptions = FoldOptions {
477− fetchQuantity = Automatic,
478− transactionMode = TransactionMode ReadCommitted ReadOnly
479− }
480−
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 )
488− => FoldOptions
489− -> Connection
490− -> Query
491− -> params
492− -> a
493− -> (a -> row -> IO a)
494− -> 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
508− q <- formatQuery conn template qs
509− doFold opts parser conn template (Query q) a f
510−
320+fold :: (QueryParams q, QueryResults r) =>
321+ Connection
322+ -> Query -- ^ Query template.
323+ -> q -- ^ Query parameters.
324+ -> a -- ^ Initial state for result consumer.
325+ -> (a -> r -> IO a) -- ^ Result consumer.
326+ -> IO a
327+fold conn template qs z f = do
328+ Base.query conn =<< formatQuery conn template qs
329+ finishFold conn template z f
330+--}
331+{--
511332-- | A version of 'fold' that does not perform query substitution.
512−fold_ :: (FromRow r) =>
333+fold_ :: (QueryResults r) =>
513334 Connection
514335 -> Query -- ^ Query.
515336 -> a -- ^ Initial state for result consumer.
516337 -> (a -> r -> IO a) -- ^ Result consumer.
517338 -> IO a
518−fold_ = foldWithOptions_ defaultFoldOptions
519−
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
528−
529−foldWithOptions_ :: (FromRow r) =>
530− FoldOptions
531− -> Connection
532− -> Query -- ^ Query.
533− -> a -- ^ Initial state for result consumer.
534− -> (a -> r -> IO a) -- ^ Result consumer.
535− -> 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
550− -> Connection
551− -> Query
552− -> Query
553− -> a
554− -> (a -> row -> IO a)
555− -> IO a
556−doFold FoldOptions{..} parser conn _template q a0 f = do
557− stat <- withConnection conn PQ.transactionStatus
558− case stat of
559− PQ.TransIdle -> withTransactionMode transactionMode conn go
560− PQ.TransInTrans -> go
561− PQ.TransActive -> fail "foldWithOpts FIXME: PQ.TransActive"
562− -- This _shouldn't_ occur in the current incarnation of
563− -- the library, as we aren't using libpq asynchronously.
564− -- However, it could occur in future incarnations of
565− -- this library or if client code uses the Internal module
566− -- to use raw libpq commands on postgresql-simple connections.
567− PQ.TransInError -> fail "foldWithOpts FIXME: PQ.TransInError"
568− -- This should be turned into a better error message.
569− -- It is probably a bad idea to automatically roll
570− -- back the transaction and start another.
571− PQ.TransUnknown -> fail "foldWithOpts FIXME: PQ.TransUnknown"
572− -- Not sure what this means.
573− 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
585−
586−-- FIXME: choose the Automatic chunkSize more intelligently
587−-- One possibility is to use the type of the results, although this
588−-- still isn't a perfect solution, given that common types (e.g. text)
589−-- are of highly variable size.
590−-- A refinement of this technique is to pick this number adaptively
591−-- as results are read in from the database.
592− chunkSize = case fetchQuantity of
593− Automatic -> 256
594− Fixed n -> n
595−
339+fold_ conn q@(Query que) z f = do
340+ Base.query conn que
341+ finishFold conn q z f
342+--}
343+{--
596344-- | A version of 'fold' that does not transform a state value.
597−forEach :: (ToRow q, FromRow r) =>
345+forEach :: (QueryParams q, QueryResults r) =>
598346 Connection
599347 -> Query -- ^ Query template.
600348 -> q -- ^ Query parameters.
601349 -> (r -> IO ()) -- ^ Result consumer.
602350 -> IO ()
603−forEach = forEachWith fromRow
351+forEach conn template qs = fold conn template qs () . const
604352{-# INLINE forEach #-}
605353
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−
617354-- | A version of 'forEach' that does not perform query substitution.
618−forEach_ :: (FromRow r) =>
355+forEach_ :: (QueryResults r) =>
619356 Connection
620357 -> Query -- ^ Query template.
621358 -> (r -> IO ()) -- ^ Result consumer.
622359 -> IO ()
623−forEach_ = forEachWith_ fromRow
360+forEach_ conn template = fold_ conn template () . const
624361{-# INLINE forEach_ #-}
362+--}
625363
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−
364+forM' :: (Ord n, Num n) => n -> n -> (n -> IO a) -> IO [a]
365+forM' lo hi m = loop hi []
366+ where
367+ loop !n !as
368+ | n < lo = return as
369+ | otherwise = do
370+ a <- m n
371+ loop (n-1) (a:as)
372+
373+finishQuery :: (QueryResults r) => Connection -> Query -> PQ.Result -> IO [r]
374+finishQuery conn q result = do
375+ status <- PQ.resultStatus result
376+ case status of
377+ PQ.EmptyQuery -> fail "empty query" -- FIXME: error message
378+ PQ.CommandOk -> do
379+ throwIO $ QueryError "query resulted in a command response" q
380+ PQ.TuplesOk -> do
381+ ncols <- PQ.nfields result
382+ fields <- forM' 0 (ncols-1) $ \column -> do
383+ type_oid <- PQ.ftype result column
384+ typename <- getTypename conn type_oid
385+ return Field{..}
386+ nrows <- PQ.ntuples result
387+ forM' 0 (nrows-1) $ \row -> do
388+ values <- forM' 0 (ncols-1) (\col -> PQ.getvalue' result row col)
389+ case convertResults fields values of
390+ Fail err -> throwIO err
391+ Success a -> return a
392+ PQ.CopyIn -> fail "FIXME: postgresql-simple does not currently support COPY IN"
393+ PQ.CopyOut -> fail "FIXME: postgresql-simple does not currently support COPY OUT"
394+ _ -> do
395+ errormsg <- maybe "" id <$> PQ.resultErrorMessage result
396+ statusmsg <- PQ.resStatus status
397+ state <- maybe "" id <$> PQ.resultErrorField result PQ.DiagSqlstate
398+ throw $ SqlError { sqlState = state
399+ , sqlNativeError = 0 -- FIXME
400+ , sqlErrorMsg = B.concat [ "execute: ", statusmsg
401+ , ": ", errormsg ]}
402+
403+{--
404+withResult (Base.storeResult conn) q $ \r fs -> do
405+ status <-
406+/ flip fix [] $ \loop acc -> do
407+ row <- Base.fetchRow r
408+ case row of
409+ [] -> return (reverse acc)
410+ _ -> let !c = convertResults fs row
411+ in loop (c:acc)
412+--}
413+{--
414+finishFold :: (QueryResults r) =>
415+ Connection -> Query -> a -> (a -> r -> IO a) -> IO a
416+finishFold conn q z0 f = withResult (Base.useResult conn) q $ \r fs ->
417+ flip fix z0 $ \loop z -> do
418+ row <- Base.fetchRow r
419+ case row of
420+ [] -> return z
421+ _ -> (f z $! convertResults fs row) >>= loop
422+--}
423+{--
424+withResult :: (IO Result) -> Query -> (Result -> [Field] -> IO a) -> IO a
425+withResult fetchResult q act = bracket fetchResult Base.freeResult $ \r -> do
426+ ncols <- Base.fieldCount (Right r)
427+ if ncols == 0
428+ then throwIO $ QueryError "query resulted in zero-column result" q
429+ else act r =<< Base.fetchFields r
430+--}
431+-- | Execute an action inside a SQL transaction.
432+--
433+-- This function initiates a transaction with a \"@begin
434+-- transaction@\" statement, then executes the supplied action. If
435+-- the action succeeds, the transaction will be completed with
436+-- 'Base.commit' before this function returns.
437+--
438+-- If the action throws /any/ kind of exception (not just a
439+-- MySQL-related exception), the transaction will be rolled back using
440+-- 'Base.rollback', then the exception will be rethrown.
441+withTransaction :: Connection -> IO a -> IO a
442+withTransaction conn act = do
443+ _ <- begin conn
444+ r <- act `onException` rollback conn
445+ commit conn
446+ return r
447+
448+-- | Rollback a transaction.
449+-- rollback :: (MonadCatchIO m,MonadIO m) => Connection -> m ()
450+rollback :: Connection -> IO ()
451+rollback conn = do
452+ _ <- execute_ conn "ABORT;"
453+ return ()
454+
455+-- | Commit a transaction.
456+-- commit :: (MonadCatchIO m,MonadIO m) => Connection -> m ()
457+commit :: Connection -> IO ()
458+commit conn = do
459+ _ <- execute_ conn "COMMIT;"
460+ return ()
461+
462+-- | Begin a transaction.
463+-- begin :: (MonadCatchIO m,MonadIO m) => Connection -> m ()
464+begin :: Connection -> IO ()
465+begin conn = do
466+ _ <- execute_ conn "BEGIN;"
467+ return ()
468+
469+fmtError :: String -> Query -> [Action] -> a
470+fmtError msg q xs = throw FormatError {
471+ fmtMessage = msg
472+ , fmtQuery = q
473+ , fmtParams = map twiddle xs
474+ }
475+ where twiddle (Plain b) = toByteString b
476+ twiddle (Escape s) = s
477+ twiddle (Many ys) = B.concat (map twiddle ys)
634478
635479-- $use
636480--
@@ -656,13 +500,11 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
656500--
657501-- > {-# LANGUAGE OverloadedStrings #-}
658502-- >
659−-- > import Database.PostgreSQL.Simple
503+-- > import Database.MySQL.Simple
660504-- >
661−-- > hello :: IO Int
662505-- > hello = do
663−-- > conn <- connectPostgreSQL ""
664−-- > [Only i] <- query_ conn "select 2 + 2"
665−-- > return i
506+-- > conn <- connect defaultConnectInfo
507+-- > query conn "select 2 + 2"
666508--
667509-- A 'Query' value does not represent the actual query that will be
668510-- executed, but is a template for constructing the final query.
@@ -709,7 +551,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
709551-- Automated type inference means that you will often be able to avoid
710552-- supplying explicit type signatures for the elements of a tuple.
711553-- However, sometimes the compiler will not be able to infer your
712−-- types. Consider a case where you write a numeric literal in a
554+-- types. Consider a care where you write a numeric literal in a
713555-- parameter tuple:
714556--
715557-- > query conn "select ? + ?" (40,2)
@@ -723,30 +565,15 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
723565-- The same kind of problem can arise with string literals if you have
724566-- the @OverloadedStrings@ language extension enabled. Again, just
725567-- 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])
740568
741569-- $only_param
742570--
743571-- 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?
572+-- value you want substituted into a query, what should you do?
746573--
747574-- The obvious approach would appear to be something like this:
748575--
749−-- > instance (ToField a) => ToRow a where
576+-- > instance (Param a) => QueryParam a where
750577-- > ...
751578--
752579-- Unfortunately, this wreaks havoc with type inference, so we take a
@@ -757,9 +584,6 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
757584--
758585-- > execute conn "insert into users (first_name) values (?)"
759586-- > ["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.
763587
764588-- $in
765589--
@@ -777,7 +601,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
777601-- Here's an example:
778602--
779603-- > query conn "select * from users where first_name in ?" $
780−-- > Only $ In ["Anna", "Boris", "Carla"]
604+-- > In ["Anna", "Boris", "Carla"]
781605--
782606-- If your 'In'-wrapped list is empty, the string @\"(null)\"@ will be
783607-- substituted instead, to ensure that your clause remains
@@ -805,7 +629,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
805629-- * There must be no other \"@?@\" characters anywhere in your
806630-- template.
807631--
808−-- * There must be one or more \"@?@\" in the parentheses.
632+-- * There must one or more \"@?@\" in the parentheses.
809633--
810634-- * Extra white space is fine.
811635--
@@ -826,26 +650,10 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
826650-- > insert into users (first_name,last_name) values
827651-- > ('Boris','Karloff'),('Ed','Wood')
828652
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−
845653-- $result
846654--
847655-- The 'query' and 'query_' functions return a list of values in the
848−-- 'FromRow' typeclass. This class performs automatic extraction
656+-- 'QueryResults' typeclass. This class performs automatic extraction
849657-- and type conversion of rows from a query result.
850658--
851659-- Here is a simple example of how to extract results:
@@ -876,7 +684,7 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
876684--
877685-- Although SQL can accommodate @NULL@ as a value for any of these
878686-- types, Haskell cannot. If your result contains columns that may be
879−-- @NULL@, be sure that you use 'Maybe' in those positions of your
687+-- @NULL@, be sure that you use 'Maybe' in those positions of of your
880688-- tuple.
881689--
882690-- > (Text, Maybe Int, Int)
@@ -899,15 +707,16 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
899707-- permissive. Here are the rules.
900708--
901709-- * For numeric types, any Haskell type that can accurately represent
902−-- all values of the given PostgreSQL type is considered \"compatible\".
903−-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
904−-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
905−-- represent a @SMALLINT@, so it is considered compatible with those types.
710+-- all values of the given MySQL type is considered \"compatible\".
711+-- For instance, you can always extract a MySQL @TINYINT@ column to
712+-- a Haskell 'Int'. The Haskell 'Float' type can accurately
713+-- represent MySQL integer types of size up to @INT24@, so it is
714+-- considered compatble with those types.
906715--
907716-- * A numeric compatibility check is based only on the type of a
908−-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
909−-- @BIGINT@ column will be considered incompatible with a Haskell
910−-- 'Int16', even if it contains the value @1@.
717+-- column, /not/ on its values. For instance, a MySQL @LONG_LONG@
718+-- column will be considered incompatible with a Haskell 'Int8',
719+-- even if it contains the value @1@.
911720--
912721-- * If a numeric incompatibility is found, 'query' will throw a
913722-- 'ResultError'.
@@ -916,3 +725,29 @@ forEachWith_ parser conn template = foldWith_ parser conn template () . const
916725-- UTF-8. If you use some other encoding, decoding may fail or give
917726-- wrong results. In such cases, write a @newtype@ wrapper and a
918727-- custom 'Result' instance to handle your encoding.
728+
729+getTypename :: Connection -> PQ.Oid -> IO ByteString
730+getTypename conn@Connection{..} oid =
731+ case oid2builtin oid of
732+ Just builtin -> return $! builtin2typname builtin
733+ Nothing -> do
734+ oidmap <- takeMVar connectionObjects
735+ case IntMap.lookup (oid2int oid) oidmap of
736+ Just name -> putMVar connectionObjects oidmap >> return name
737+ Nothing -> do
738+ names <- query conn "SELECT typname FROM pg_type WHERE oid=?"
739+ (Only oid)
740+ name <- case names of
741+ [] -> return $ throw SqlError {
742+ sqlNativeError = -1,
743+ sqlErrorMsg = "invalid type oid",
744+ sqlState = ""
745+ }
746+ [Only x] -> return x
747+ _ -> fail "typename query returned more than one result"
748+ -- oid is a primary key, so the query should
749+ -- never return more than one result
750+ putMVar connectionObjects (IntMap.insert (oid2int oid) name oidmap)
751+ return name
752+ `onException` do
753+ putMVar connectionObjects oidmap