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