git.iko.soy/typeable/postgresql-simple
on edcb018 help sign in
1000 lines 38 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE NamedFieldPuns #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE PatternGuards #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE ViewPatterns #-}
7{-# LANGUAGE QuasiQuotes #-}
9------------------------------------------------------------------------------
10-- |
11-- Module: Database.PostgreSQL.Simple
12-- Copyright: (c) 2011 MailRank, Inc.
13-- (c) 2011-2012 Leon P Smith
14-- License: BSD3
15-- Maintainer: Leon P Smith <leon@melding-monads.com>
16-- Stability: experimental
17-- Portability: portable
18--
19-- A mid-level client library for the PostgreSQL database, aimed at ease of
20-- use and high performance.
21--
22------------------------------------------------------------------------------
24module Database.PostgreSQL.Simple
25 (
26 -- * Writing queries
27 -- $use
29 -- ** The Query type
30 -- $querytype
32 -- ** Parameter substitution
33 -- $subst
35 -- *** Type inference
36 -- $inference
38 -- ** Substituting a single parameter
39 -- $only_param
41 -- ** Representing a list of values
42 -- $in
44 -- ** Modifying multiple rows at once
45 -- $many
47 -- ** @RETURNING@: modifications that returns results
48 -- $returning
50 -- * Extracting results
51 -- $result
53 -- ** Handling null values
54 -- $null
56 -- ** Type conversions
57 -- $types
59 -- * Types
60 Base.ConnectInfo(..)
61 , Connection
62 , Query
63 , ToRow
64 , FromRow
65 , In(..)
66 , Binary(..)
67 , Only(..)
68 , (:.)(..)
69 -- ** Exceptions
70 , SqlError(..)
71 , FormatError(fmtMessage, fmtQuery, fmtParams)
72 , QueryError(qeMessage, qeQuery)
73 , ResultError(errSQLType, errHaskellType, errMessage)
74 -- * Connection management
75 , Base.connect
76 , Base.connectPostgreSQL
77 , Base.postgreSQLConnectionString
78 , Base.defaultConnectInfo
79 , Base.close
80 -- * Queries that return results
81 , query
82 , query_
83 -- * Queries that stream results
84 , FoldOptions(..)
85 , FetchQuantity(..)
86 , defaultFoldOptions
87 , fold
88 , foldWithOptions
89 , fold_
90 , foldWithOptions_
91 , forEach
92 , forEach_
93 , returning
94 -- * Statements that do not return results
95 , execute
96 , execute_
97 , executeMany
98-- , Base.insertID
99 -- * Transaction handling
100 , withTransaction
101 , TransactionMode(..)
102 , IsolationLevel(..)
103 , ReadWriteMode(..)
104 , defaultTransactionMode
105 , defaultIsolationLevel
106 , defaultReadWriteMode
107 , withTransactionLevel
108 , withTransactionMode
109-- , Base.autocommit
110 , begin
111 , beginLevel
112 , beginMode
113 , commit
114 , rollback
115 -- * Helper functions
116 , formatMany
117 , formatQuery
118 ) where
120import Blaze.ByteString.Builder
121 ( Builder, fromByteString, toByteString )
122import Blaze.ByteString.Builder.Char8 (fromChar)
123import Control.Applicative ((<$>), pure)
124import Control.Concurrent.MVar
125import Control.Exception
126 ( Exception, onException, throw, throwIO, finally )
127import Control.Monad (foldM)
128import Data.ByteString (ByteString)
129import Data.Int (Int64)
130import qualified Data.IntMap as IntMap
131import Data.List (intersperse)
132import Data.Monoid (mappend, mconcat)
133import Data.Typeable (Typeable)
134import Database.PostgreSQL.Simple.BuiltinTypes ( oid2typname )
135import Database.PostgreSQL.Simple.Compat ( mask )
136import Database.PostgreSQL.Simple.FromField (ResultError(..))
137import Database.PostgreSQL.Simple.FromRow (FromRow(..))
138import Database.PostgreSQL.Simple.Ok
139import Database.PostgreSQL.Simple.ToField (Action(..), inQuotes)
140import Database.PostgreSQL.Simple.ToRow (ToRow(..))
141import Database.PostgreSQL.Simple.Types
142 ( Binary(..), In(..), Only(..), Query(..), (:.)(..) )
143import Database.PostgreSQL.Simple.Internal as Base
144import Database.PostgreSQL.Simple.SqlQQ (sql)
145import qualified Database.PostgreSQL.LibPQ as PQ
146import qualified Data.ByteString.Char8 as B
147import qualified Data.Text as T
148import qualified Data.Text.Encoding as TE
149import qualified Data.Vector as V
150import Control.Monad.Trans.Reader
151import Control.Monad.Trans.State.Strict
153-- | Exception thrown if a 'Query' could not be formatted correctly.
154-- This may occur if the number of \'@?@\' characters in the query
155-- string does not match the number of parameters provided.
156data FormatError = FormatError {
157 fmtMessage :: String
158 , fmtQuery :: Query
159 , fmtParams :: [ByteString]
160 } deriving (Eq, Show, Typeable)
162instance Exception FormatError
164-- | Format a query string.
165--
166-- This function is exposed to help with debugging and logging. Do not
167-- use it to prepare queries for execution.
168--
169-- String parameters are escaped according to the character set in use
170-- on the 'Connection'.
171--
172-- Throws 'FormatError' if the query string could not be formatted
173-- correctly.
174formatQuery :: ToRow q => Connection -> Query -> q -> IO ByteString
175formatQuery conn q@(Query template) qs
176 | null xs && '?' `B.notElem` template = return template
177 | otherwise = toByteString <$> buildQuery conn q template xs
178 where xs = toRow qs
180-- | Format a query string with a variable number of rows.
181--
182-- This function is exposed to help with debugging and logging. Do not
183-- use it to prepare queries for execution.
184--
185-- The query string must contain exactly one substitution group,
186-- identified by the SQL keyword \"@VALUES@\" (case insensitive)
187-- followed by an \"@(@\" character, a series of one or more \"@?@\"
188-- characters separated by commas, and a \"@)@\" character. White
189-- space in a substitution group is permitted.
190--
191-- Throws 'FormatError' if the query string could not be formatted
192-- correctly.
193formatMany :: (ToRow q) => Connection -> Query -> [q] -> IO ByteString
194formatMany _ q [] = fmtError "no rows supplied" q []
195formatMany conn q@(Query template) qs = do
196 case parseTemplate template of
197 Just (before, qbits, after) -> do
198 bs <- mapM (buildQuery conn q qbits . toRow) qs
199 return . toByteString . mconcat $ fromByteString before :
200 intersperse (fromChar ',') bs ++
201 [fromByteString after]
202 Nothing -> fmtError "syntax error in multi-row template" q []
204-- Split the input string into three pieces, @before@, @qbits@, and @after@,
205-- following this grammar:
206--
207-- start: ^ before qbits after $
208-- before: ([^?]* [^?\w])? 'VALUES' \s*
209-- qbits: '(' \s* '?' \s* (',' \s* '?' \s*)* ')'
210-- after: [^?]*
211--
212-- \s: [ \t\n\r\f]
213-- \w: [A-Z] | [a-z] | [\x80-\xFF] | '_' | '$' | [0-9]
214--
215-- This would be much more concise with some sort of regex engine.
216-- 'formatMany' used to use pcre-light instead of this hand-written parser,
217-- but pcre is a hassle to install on Windows.
218parseTemplate :: ByteString -> Maybe (ByteString, ByteString, ByteString)
219parseTemplate template =
220 -- Convert input string to uppercase, to facilitate searching.
221 search $ B.map toUpper_ascii template
222 where
223 -- Search for the next occurrence of "VALUES"
224 search bs =
225 case B.breakSubstring "VALUES" bs of
226 (x, y)
227 -- If "VALUES" is not present in the string, or any '?' characters
228 -- were encountered prior to it, fail.
229 | B.null y || ('?' `B.elem` x)
230 -> Nothing
232 -- If "VALUES" is preceded by an identifier character (a.k.a. \w),
233 -- try the next occurrence.
234 | not (B.null x) && isIdent (B.last x)
235 -> search $ B.drop 6 y
237 -- Otherwise, we have a legitimate "VALUES" token.
238 | otherwise
239 -> parseQueryBits $ skipSpace $ B.drop 6 y
241 -- Parse '(' \s* '?' \s* . If this doesn't match
242 -- (and we don't consume a '?'), look for another "VALUES".
243 --
244 -- qb points to the open paren (if present), meaning it points to the
245 -- beginning of the "qbits" production described above. This is why we
246 -- pass it down to finishQueryBits.
247 parseQueryBits qb
248 | Just ('(', skipSpace -> bs1) <- B.uncons qb
249 , Just ('?', skipSpace -> bs2) <- B.uncons bs1
250 = finishQueryBits qb bs2
251 | otherwise
252 = search qb
254 -- Parse (',' \s* '?' \s*)* ')' [^?]* .
255 --
256 -- Since we've already consumed at least one '?', there's no turning back.
257 -- The parse has to succeed here, or the whole thing fails
258 -- (because we don't allow '?' to appear outside of the VALUES list).
259 finishQueryBits qb bs0
260 | Just (')', bs1) <- B.uncons bs0
261 = if '?' `B.elem` bs1
262 then Nothing
263 else Just $ slice3 template qb bs1
264 | Just (',', skipSpace -> bs1) <- B.uncons bs0
265 , Just ('?', skipSpace -> bs2) <- B.uncons bs1
266 = finishQueryBits qb bs2
267 | otherwise
268 = Nothing
270 -- Slice a string into three pieces, given the start offset of the second
271 -- and third pieces. Each "offset" is actually a tail of the uppercase
272 -- version of the template string. Its length is used to infer the offset.
273 --
274 -- It is important to note that we only slice the original template.
275 -- We don't want our all-caps trick messing up the actual query string.
276 slice3 source p1 p2 =
277 (s1, s2, source'')
278 where
279 (s1, source') = B.splitAt (B.length source - B.length p1) source
280 (s2, source'') = B.splitAt (B.length p1 - B.length p2) source'
282 toUpper_ascii c | c >= 'a' && c <= 'z' = toEnum (fromEnum c - 32)
283 | otherwise = c
285 -- Based on the definition of {ident_cont} in src/backend/parser/scan.l
286 -- in the PostgreSQL source. No need to check [a-z], since we converted
287 -- the whole string to uppercase.
288 isIdent c = (c >= '0' && c <= '9')
289 || (c >= 'A' && c <= 'Z')
290 || (c >= '\x80' && c <= '\xFF')
291 || c == '_'
292 || c == '$'
294 -- Based on {space} in scan.l
295 isSpace_ascii c = (c == ' ') || (c >= '\t' && c <= '\r')
297 skipSpace = B.dropWhile isSpace_ascii
299escapeStringConn :: Connection -> ByteString -> IO (Either ByteString ByteString)
300escapeStringConn conn s =
301 withConnection conn $ \c ->
302 PQ.escapeStringConn c s >>= checkError c
304escapeByteaConn :: Connection -> ByteString -> IO (Either ByteString ByteString)
305escapeByteaConn conn s =
306 withConnection conn $ \c ->
307 PQ.escapeByteaConn c s >>= checkError c
309checkError :: PQ.Connection -> Maybe a -> IO (Either ByteString a)
310checkError _ (Just x) = return $ Right x
311checkError c Nothing = Left . maybe "" id <$> PQ.errorMessage c
313buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
314buildQuery conn q template xs = zipParams (split template) <$> mapM sub xs
315 where quote = either (\msg -> fmtError (utf8ToString msg) q xs)
316 (inQuotes . fromByteString)
317 utf8ToString = T.unpack . TE.decodeUtf8
318 sub (Plain b) = pure b
319 sub (Escape s) = quote <$> escapeStringConn conn s
320 sub (EscapeByteA s) = quote <$> escapeByteaConn conn s
321 sub (Many ys) = mconcat <$> mapM sub ys
322 split s = fromByteString h : if B.null t then [] else split (B.tail t)
323 where (h,t) = B.break (=='?') s
324 zipParams (t:ts) (p:ps) = t `mappend` p `mappend` zipParams ts ps
325 zipParams [t] [] = t
326 zipParams _ _ = fmtError (show (B.count '?' template) ++
327 " '?' characters, but " ++
328 show (length xs) ++ " parameters") q xs
330-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
331-- expected to return results.
332--
333-- Returns the number of rows affected.
334--
335-- Throws 'FormatError' if the query could not be formatted correctly.
336execute :: (ToRow q) => Connection -> Query -> q -> IO Int64
337execute conn template qs = do
338 result <- exec conn =<< formatQuery conn template qs
339 finishExecute conn template result
341-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not
342-- expected to return results.
343--
344-- Returns the number of rows affected.
345--
346-- Throws 'FormatError' if the query could not be formatted correctly.
347executeMany :: (ToRow q) => Connection -> Query -> [q] -> IO Int64
348executeMany _ _ [] = return 0
349executeMany conn q qs = do
350 result <- exec conn =<< formatMany conn q qs
351 finishExecute conn q result
353-- | Execute @INSERT ... RETURNING@, @UPDATE ... RETURNING@, or other SQL
354-- query that accepts multi-row input and is expected to return results.
355-- Note that it is possible to write
356-- @'query' conn "INSERT ... RETURNING ..." ...@
357-- in cases where you are only inserting a single row, and do not need
358-- functionality analogous to 'executeMany'.
359--
360-- Throws 'FormatError' if the query could not be formatted correctly.
361returning :: (ToRow q, FromRow r) => Connection -> Query -> [q] -> IO [r]
362returning _ _ [] = return []
363returning conn q qs = do
364 result <- exec conn =<< formatMany conn q qs
365 finishQuery conn q result
367-- | Perform a @SELECT@ or other SQL query that is expected to return
368-- results. All results are retrieved and converted before this
369-- function returns.
370--
371-- When processing large results, this function will consume a lot of
372-- client-side memory. Consider using 'fold' instead.
373--
374-- Exceptions that may be thrown:
375--
376-- * 'FormatError': the query string could not be formatted correctly.
377--
378-- * 'QueryError': the result contains no columns (i.e. you should be
379-- using 'execute' instead of 'query').
380--
381-- * 'ResultError': result conversion failed.
382query :: (ToRow q, FromRow r)
383 => Connection -> Query -> q -> IO [r]
384query conn template qs = do
385 result <- exec conn =<< formatQuery conn template qs
386 finishQuery conn template result
388-- | A version of 'query' that does not perform query substitution.
389query_ :: (FromRow r) => Connection -> Query -> IO [r]
390query_ conn q@(Query que) = do
391 result <- exec conn que
392 finishQuery conn q result
394-- | Perform a @SELECT@ or other SQL query that is expected to return
395-- results. Results are streamed incrementally from the server, and
396-- consumed via a left fold.
397--
398-- When dealing with small results, it may be simpler (and perhaps
399-- faster) to use 'query' instead.
400--
401-- This fold is /not/ strict. The stream consumer is responsible for
402-- forcing the evaluation of its result to avoid space leaks.
403--
404-- Exceptions that may be thrown:
405--
406-- * 'FormatError': the query string could not be formatted correctly.
407--
408-- * 'QueryError': the result contains no columns (i.e. you should be
409-- using 'execute' instead of 'query').
410--
411-- * 'ResultError': result conversion failed.
413fold :: ( FromRow row, ToRow params )
414 => Connection
415 -> Query
416 -> params
417 -> a
418 -> (a -> row -> IO a)
419 -> IO a
420fold = foldWithOptions defaultFoldOptions
422data FetchQuantity
423 = Automatic
424 | Fixed !Int
426data FoldOptions
427 = FoldOptions {
428 fetchQuantity :: !FetchQuantity,
429 transactionMode :: !TransactionMode
430 }
432defaultFoldOptions :: FoldOptions
433defaultFoldOptions = FoldOptions {
434 fetchQuantity = Automatic,
435 transactionMode = TransactionMode ReadCommitted ReadOnly
436 }
438foldWithOptions :: ( FromRow row, ToRow params )
439 => FoldOptions
440 -> Connection
441 -> Query
442 -> params
443 -> a
444 -> (a -> row -> IO a)
445 -> IO a
446foldWithOptions opts conn template qs a f = do
447 q <- formatQuery conn template qs
448 doFold opts conn template (Query q) a f
450-- | A version of 'fold' that does not perform query substitution.
451fold_ :: (FromRow r) =>
452 Connection
453 -> Query -- ^ Query.
454 -> a -- ^ Initial state for result consumer.
455 -> (a -> r -> IO a) -- ^ Result consumer.
456 -> IO a
457fold_ = foldWithOptions_ defaultFoldOptions
460foldWithOptions_ :: (FromRow r) =>
461 FoldOptions
462 -> Connection
463 -> Query -- ^ Query.
464 -> a -- ^ Initial state for result consumer.
465 -> (a -> r -> IO a) -- ^ Result consumer.
466 -> IO a
467foldWithOptions_ opts conn query a f = doFold opts conn query query a f
470doFold :: ( FromRow row )
471 => FoldOptions
472 -> Connection
473 -> Query
474 -> Query
475 -> a
476 -> (a -> row -> IO a)
477 -> IO a
478doFold FoldOptions{..} conn _template q a f = do
479 stat <- withConnection conn PQ.transactionStatus
480 case stat of
481 PQ.TransIdle -> withTransactionMode transactionMode conn go
482 PQ.TransInTrans -> go
483 PQ.TransActive -> fail "foldWithOpts FIXME: PQ.TransActive"
484 -- This _shouldn't_ occur in the current incarnation of
485 -- the library, as we aren't using libpq asynchronously.
486 -- However, it could occur in future incarnations of
487 -- this library or if client code uses the Internal module
488 -- to use raw libpq commands on postgresql-simple connections.
489 PQ.TransInError -> fail "foldWithOpts FIXME: PQ.TransInError"
490 -- This should be turned into a better error message.
491 -- It is probably a bad idea to automatically roll
492 -- back the transaction and start another.
493 PQ.TransUnknown -> fail "foldWithOpts FIXME: PQ.TransUnknown"
494 -- Not sure what this means.
495 where
496 go = do
497 -- FIXME: what about name clashes with already-declared cursors?
498 _ <- execute_ conn ("DECLARE fold NO SCROLL CURSOR FOR "
499 `mappend` q)
500 loop a `finally` execute_ conn "CLOSE fold"
502-- FIXME: choose the Automatic chunkSize more intelligently
503-- One possibility is to use the type of the results, although this
504-- still isn't a perfect solution, given that common types (e.g. text)
505-- are of highly variable size.
506-- A refinement of this technique is to pick this number adaptively
507-- as results are read in from the database.
508 chunkSize = case fetchQuantity of
509 Automatic -> 256
510 Fixed n -> n
511 loop a = do
512 rs <- query conn "FETCH FORWARD ? FROM fold" (Only chunkSize)
513 if null rs then return a else foldM f a rs >>= loop
515-- | A version of 'fold' that does not transform a state value.
516forEach :: (ToRow q, FromRow r) =>
517 Connection
518 -> Query -- ^ Query template.
519 -> q -- ^ Query parameters.
520 -> (r -> IO ()) -- ^ Result consumer.
521 -> IO ()
522forEach conn template qs = fold conn template qs () . const
523{-# INLINE forEach #-}
525-- | A version of 'forEach' that does not perform query substitution.
526forEach_ :: (FromRow r) =>
527 Connection
528 -> Query -- ^ Query template.
529 -> (r -> IO ()) -- ^ Result consumer.
530 -> IO ()
531forEach_ conn template = fold_ conn template () . const
532{-# INLINE forEach_ #-}
534forM' :: (Ord n, Num n) => n -> n -> (n -> IO a) -> IO [a]
535forM' lo hi m = loop hi []
536 where
537 loop !n !as
538 | n < lo = return as
539 | otherwise = do
540 a <- m n
541 loop (n-1) (a:as)
543finishQuery :: (FromRow r) => Connection -> Query -> PQ.Result -> IO [r]
544finishQuery conn q result = do
545 status <- PQ.resultStatus result
546 case status of
547 PQ.EmptyQuery ->
548 throwIO $ QueryError "query: Empty query" q
549 PQ.CommandOk -> do
550 throwIO $ QueryError "query resulted in a command response" q
551 PQ.TuplesOk -> do
552 ncols <- PQ.nfields result
553 let unCol (PQ.Col x) = fromIntegral x :: Int
554 typeinfos <- V.generateM (unCol ncols)
555 (\(PQ.Col . fromIntegral -> col) -> do
556 getTypeInfo conn =<< PQ.ftype result col)
557 nrows <- PQ.ntuples result
558 ncols <- PQ.nfields result
559 forM' 0 (nrows-1) $ \row -> do
560 let rw = Row row typeinfos result
561 case runStateT (runReaderT (unRP fromRow) rw) 0 of
562 Ok (val,col) | col == ncols -> return val
563 | otherwise -> do
564 vals <- forM' 0 (ncols-1) $ \c -> do
565 v <- PQ.getvalue result row c
566 return ( typeinfos V.! unCol c
567 , fmap ellipsis v )
568 throw (ConversionFailed
569 (show (unCol ncols) ++ " values: " ++ show vals)
570 (show (unCol col) ++ " slots in target type")
571 "mismatch between number of columns to \
572 \convert and number in target type")
573 Errors [] -> throwIO $ ConversionFailed "" "" "unknown error"
574 Errors [x] -> throwIO x
575 Errors xs -> throwIO $ ManyErrors xs
576 PQ.CopyOut ->
577 throwIO $ QueryError "query: COPY TO is not supported" q
578 PQ.CopyIn ->
579 throwIO $ QueryError "query: COPY FROM is not supported" q
580 PQ.BadResponse -> throwResultError "query" result status
581 PQ.NonfatalError -> throwResultError "query" result status
582 PQ.FatalError -> throwResultError "query" result status
584ellipsis :: ByteString -> ByteString
585ellipsis bs
586 | B.length bs > 15 = B.take 10 bs `B.append` "[...]"
587 | otherwise = bs
590-- | Of the four isolation levels defined by the SQL standard,
591-- these are the three levels distinguished by PostgreSQL as of version 9.0.
592-- See <http://www.postgresql.org/docs/9.1/static/transaction-iso.html>
593-- for more information. Note that prior to PostgreSQL 9.0, 'RepeatableRead'
594-- was equivalent to 'Serializable'.
596data IsolationLevel
597 = DefaultIsolationLevel -- ^ the isolation level will be taken from
598 -- PostgreSQL's per-connection
599 -- @default_transaction_isolation@ variable,
600 -- which is initialized according to the
601 -- server's config. The default configuration
602 -- is 'ReadCommitted'.
603 | ReadCommitted
604 | RepeatableRead
605 | Serializable
606 deriving (Show, Eq, Ord, Enum, Bounded)
608data ReadWriteMode
609 = DefaultReadWriteMode -- ^ the read-write mode will be taken from
610 -- PostgreSQL's per-connection
611 -- @default_transaction_read_only@ variable,
612 -- which is initialized according to the
613 -- server's config. The default configuration
614 -- is 'ReadWrite'.
615 | ReadWrite
616 | ReadOnly
617 deriving (Show, Eq, Ord, Enum, Bounded)
619data TransactionMode = TransactionMode {
620 isolationLevel :: !IsolationLevel,
621 readWriteMode :: !ReadWriteMode
622 } deriving (Show, Eq)
624defaultTransactionMode :: TransactionMode
625defaultTransactionMode = TransactionMode
626 defaultIsolationLevel
627 defaultReadWriteMode
629defaultIsolationLevel :: IsolationLevel
630defaultIsolationLevel = DefaultIsolationLevel
632defaultReadWriteMode :: ReadWriteMode
633defaultReadWriteMode = DefaultReadWriteMode
635-- | Execute an action inside a SQL transaction.
636--
637-- This function initiates a transaction with a \"@begin
638-- transaction@\" statement, then executes the supplied action. If
639-- the action succeeds, the transaction will be completed with
640-- 'Base.commit' before this function returns.
641--
642-- If the action throws /any/ kind of exception (not just a
643-- PostgreSQL-related exception), the transaction will be rolled back using
644-- 'rollback', then the exception will be rethrown.
645withTransaction :: Connection -> IO a -> IO a
646withTransaction = withTransactionMode defaultTransactionMode
648-- | Execute an action inside a SQL transaction with a given isolation level.
649withTransactionLevel :: IsolationLevel -> Connection -> IO a -> IO a
650withTransactionLevel lvl
651 = withTransactionMode defaultTransactionMode { isolationLevel = lvl }
653-- | Execute an action inside a SQL transaction with a given transaction mode.
654withTransactionMode :: TransactionMode -> Connection -> IO a -> IO a
655withTransactionMode mode conn act =
656 mask $ \restore -> do
657 beginMode mode conn
658 r <- restore act `onException` rollback conn
659 commit conn
660 return r
662-- | Rollback a transaction.
663rollback :: Connection -> IO ()
664rollback conn = execute_ conn "ABORT" >> return ()
666-- | Commit a transaction.
667commit :: Connection -> IO ()
668commit conn = execute_ conn "COMMIT" >> return ()
670-- | Begin a transaction.
671begin :: Connection -> IO ()
672begin = beginMode defaultTransactionMode
674-- | Begin a transaction with a given isolation level
675beginLevel :: IsolationLevel -> Connection -> IO ()
676beginLevel lvl = beginMode defaultTransactionMode { isolationLevel = lvl }
678-- | Begin a transaction with a given transaction mode
679beginMode :: TransactionMode -> Connection -> IO ()
680beginMode mode conn = do
681 _ <- execute_ conn $! Query (B.concat ["BEGIN", isolevel, readmode])
682 return ()
683 where
684 isolevel = case isolationLevel mode of
685 DefaultIsolationLevel -> ""
686 ReadCommitted -> " ISOLATION LEVEL READ COMMITTED"
687 RepeatableRead -> " ISOLATION LEVEL REPEATABLE READ"
688 Serializable -> " ISOLATION LEVEL SERIALIZABLE"
689 readmode = case readWriteMode mode of
690 DefaultReadWriteMode -> ""
691 ReadWrite -> " READ WRITE"
692 ReadOnly -> " READ ONLY"
694fmtError :: String -> Query -> [Action] -> a
695fmtError msg q xs = throw FormatError {
696 fmtMessage = msg
697 , fmtQuery = q
698 , fmtParams = map twiddle xs
699 }
700 where twiddle (Plain b) = toByteString b
701 twiddle (Escape s) = s
702 twiddle (EscapeByteA s) = s
703 twiddle (Many ys) = B.concat (map twiddle ys)
705-- $use
706--
707-- SQL-based applications are somewhat notorious for their
708-- susceptibility to attacks through the injection of maliciously
709-- crafted data. The primary reason for widespread vulnerability to
710-- SQL injections is that many applications are sloppy in handling
711-- user data when constructing SQL queries.
712--
713-- This library provides a 'Query' type and a parameter substitution
714-- facility to address both ease of use and security.
716-- $querytype
717--
718-- A 'Query' is a @newtype@-wrapped 'ByteString'. It intentionally
719-- exposes a tiny API that is not compatible with the 'ByteString'
720-- API; this makes it difficult to construct queries from fragments of
721-- strings. The 'query' and 'execute' functions require queries to be
722-- of type 'Query'.
723--
724-- To most easily construct a query, enable GHC's @OverloadedStrings@
725-- language extension and write your query as a normal literal string.
726--
727-- > {-# LANGUAGE OverloadedStrings #-}
728-- >
729-- > import Database.PostgreSQL.Simple
730-- >
731-- > hello = do
732-- > conn <- connect defaultConnectInfo
733-- > query conn "select 2 + 2"
734--
735-- A 'Query' value does not represent the actual query that will be
736-- executed, but is a template for constructing the final query.
738-- $subst
739--
740-- Since applications need to be able to construct queries with
741-- parameters that change, this library provides a query substitution
742-- capability.
743--
744-- The 'Query' template accepted by 'query' and 'execute' can contain
745-- any number of \"@?@\" characters. Both 'query' and 'execute'
746-- accept a third argument, typically a tuple. When constructing the
747-- real query to execute, these functions replace the first \"@?@\" in
748-- the template with the first element of the tuple, the second
749-- \"@?@\" with the second element, and so on. If necessary, each
750-- tuple element will be quoted and escaped prior to substitution;
751-- this defeats the single most common injection vector for malicious
752-- data.
753--
754-- For example, given the following 'Query' template:
755--
756-- > select * from user where first_name = ? and age > ?
757--
758-- And a tuple of this form:
759--
760-- > ("Boris" :: String, 37 :: Int)
761--
762-- The query to be executed will look like this after substitution:
763--
764-- > select * from user where first_name = 'Boris' and age > 37
765--
766-- If there is a mismatch between the number of \"@?@\" characters in
767-- your template and the number of elements in your tuple, a
768-- 'FormatError' will be thrown.
769--
770-- Note that the substitution functions do not attempt to parse or
771-- validate your query. It's up to you to write syntactically valid
772-- SQL, and to ensure that each \"@?@\" in your query template is
773-- matched with the right tuple element.
775-- $inference
776--
777-- Automated type inference means that you will often be able to avoid
778-- supplying explicit type signatures for the elements of a tuple.
779-- However, sometimes the compiler will not be able to infer your
780-- types. Consider a case where you write a numeric literal in a
781-- parameter tuple:
782--
783-- > query conn "select ? + ?" (40,2)
784--
785-- The above query will be rejected by the compiler, because it does
786-- not know the specific numeric types of the literals @40@ and @2@.
787-- This is easily fixed:
788--
789-- > query conn "select ? + ?" (40 :: Double, 2 :: Double)
790--
791-- The same kind of problem can arise with string literals if you have
792-- the @OverloadedStrings@ language extension enabled. Again, just
793-- use an explicit type signature if this happens.
795-- $only_param
796--
797-- Haskell lacks a single-element tuple type, so if you have just one
798-- value you want substituted into a query, what should you do?
799--
800-- The obvious approach would appear to be something like this:
801--
802-- > instance (Param a) => QueryParam a where
803-- > ...
804--
805-- Unfortunately, this wreaks havoc with type inference, so we take a
806-- different tack. To represent a single value @val@ as a parameter, write
807-- a singleton list @[val]@, use 'Just' @val@, or use 'Only' @val@.
808--
809-- Here's an example using a singleton list:
810--
811-- > execute conn "insert into users (first_name) values (?)"
812-- > ["Nuala"]
814-- $in
815--
816-- Suppose you want to write a query using an @IN@ clause:
817--
818-- > select * from users where first_name in ('Anna', 'Boris', 'Carla')
819--
820-- In such cases, it's common for both the elements and length of the
821-- list after the @IN@ keyword to vary from query to query.
822--
823-- To address this case, use the 'In' type wrapper, and use a single
824-- \"@?@\" character to represent the list. Omit the parentheses
825-- around the list; these will be added for you.
826--
827-- Here's an example:
828--
829-- > query conn "select * from users where first_name in ?" $
830-- > In ["Anna", "Boris", "Carla"]
831--
832-- If your 'In'-wrapped list is empty, the string @\"(null)\"@ will be
833-- substituted instead, to ensure that your clause remains
834-- syntactically valid.
836-- $many
837--
838-- If you know that you have many rows of data to insert into a table,
839-- it is much more efficient to perform all the insertions in a single
840-- multi-row @INSERT@ statement than individually.
841--
842-- The 'executeMany' function is intended specifically for helping
843-- with multi-row @INSERT@ and @UPDATE@ statements. Its rules for
844-- query substitution are different than those for 'execute'.
845--
846-- What 'executeMany' searches for in your 'Query' template is a
847-- single substring of the form:
848--
849-- > values (?,?,?)
850--
851-- The rules are as follows:
852--
853-- * The keyword @VALUES@ is matched case insensitively.
854--
855-- * There must be no other \"@?@\" characters anywhere in your
856-- template.
857--
858-- * There must one or more \"@?@\" in the parentheses.
859--
860-- * Extra white space is fine.
861--
862-- The last argument to 'executeMany' is a list of parameter
863-- tuples. These will be substituted into the query where the @(?,?)@
864-- string appears, in a form suitable for use in a multi-row @INSERT@
865-- or @UPDATE@.
866--
867-- Here is an example:
868--
869-- > executeMany conn
870-- > "insert into users (first_name,last_name) values (?,?)"
871-- > [("Boris","Karloff"),("Ed","Wood")]
872--
873-- The query that will be executed here will look like this
874-- (reformatted for tidiness):
875--
876-- > insert into users (first_name,last_name) values
877-- > ('Boris','Karloff'),('Ed','Wood')
879-- $returning
880--
881-- The 'returning' function is similar to 'executeMany' but is
882-- intended for use with multi-tuple @INSERT@ or @UPDATE@ statements
883-- that make use of @RETURNING@.
884--
885-- For example, were there an auto-incrementing @id@ column and
886-- timestamp column @t@ that defaulted to the present time for the
887-- @sales@ table, then the following query would insert two new
888-- sales records and also return their new @id@s and timestamps.
889--
890-- > let q = "insert into sales (amount, label) values (?,?) returning id, t"
891-- > xs :: [(Int, UTCTime)] <- returning conn q [(20,"Chips"),(300,"Wood")]
893-- $result
894--
895-- The 'query' and 'query_' functions return a list of values in the
896-- 'FromRow' typeclass. This class performs automatic extraction
897-- and type conversion of rows from a query result.
898--
899-- Here is a simple example of how to extract results:
900--
901-- > import qualified Data.Text as Text
902-- >
903-- > xs <- query_ conn "select name,age from users"
904-- > forM_ xs $ \(name,age) ->
905-- > putStrLn $ Text.unpack name ++ " is " ++ show (age :: Int)
906--
907-- Notice two important details about this code:
908--
909-- * The number of columns we ask for in the query template must
910-- exactly match the number of elements we specify in a row of the
911-- result tuple. If they do not match, a 'ResultError' exception
912-- will be thrown.
913--
914-- * Sometimes, the compiler needs our help in specifying types. It
915-- can infer that @name@ must be a 'Text', due to our use of the
916-- @unpack@ function. However, we have to tell it the type of @age@,
917-- as it has no other information to determine the exact type.
919-- $null
920--
921-- The type of a result tuple will look something like this:
922--
923-- > (Text, Int, Int)
924--
925-- Although SQL can accommodate @NULL@ as a value for any of these
926-- types, Haskell cannot. If your result contains columns that may be
927-- @NULL@, be sure that you use 'Maybe' in those positions of of your
928-- tuple.
929--
930-- > (Text, Maybe Int, Int)
931--
932-- If 'query' encounters a @NULL@ in a row where the corresponding
933-- Haskell type is not 'Maybe', it will throw a 'ResultError'
934-- exception.
936-- $only_result
937--
938-- To specify that a query returns a single-column result, use the
939-- 'Only' type.
940--
941-- > xs <- query_ conn "select id from users"
942-- > forM_ xs $ \(Only dbid) -> {- ... -}
944-- $types
945--
946-- Conversion of SQL values to Haskell values is somewhat
947-- permissive. Here are the rules.
948--
949-- * For numeric types, any Haskell type that can accurately represent
950-- all values of the given PostgreSQL type is considered \"compatible\".
951-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
952-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
953-- represent a @SMALLINT@, so it is considered compatble with those types.
954--
955-- * A numeric compatibility check is based only on the type of a
956-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
957-- @BIGINT@ column will be considered incompatible with a Haskell
958-- 'Int16', even if it contains the value @1@.
959--
960-- * If a numeric incompatibility is found, 'query' will throw a
961-- 'ResultError'.
962--
963-- * The 'String' and 'Text' types are assumed to be encoded as
964-- UTF-8. If you use some other encoding, decoding may fail or give
965-- wrong results. In such cases, write a @newtype@ wrapper and a
966-- custom 'Result' instance to handle your encoding.
968getTypeInfo :: Connection -> PQ.Oid -> IO TypeInfo
969getTypeInfo conn@Connection{..} oid =
970 case oid2typname oid of
971 Just name -> return $! TypeInfo { typ = NamedOid oid name
972 , typelem = Nothing
973 }
974 Nothing -> modifyMVar connectionObjects $ \oidmap -> do
975 case IntMap.lookup (oid2int oid) oidmap of
976 Just typeinfo -> return (oidmap, typeinfo)
977 Nothing -> do
978 names <- query conn
979 [sql| SELECT p.oid, p.typname, c.oid, c.typname
980 FROM pg_type AS p LEFT OUTER JOIN pg_type AS c
981 ON c.oid = p.typelem
982 WHERE p.oid = ?
983 |] (Only oid)
984 typinf <- case names of
985 [] -> return $ throw SqlError {
986 sqlNativeError = -1,
987 sqlErrorMsg = "invalid type oid",
988 sqlState = ""
989 }
990 [(pOid, pTypName, mbCOid, mbCTypName)] ->
991 return $! TypeInfo { typ = NamedOid pOid pTypName
992 , typelem = do
993 cOid <- mbCOid
994 cTypName <- mbCTypName
995 return $ NamedOid cOid cTypName
996 }
997 _ -> fail "typename query returned more than one result"
998 -- oid is a primary key, so the query should
999 -- never return more than one result
1000 return (IntMap.insert (oid2int oid) typinf oidmap, typinf)