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