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