git.iko.soy/typeable/postgresql-simple
on dfbf5d9 help sign in
905 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 return results
46 -- $returning
48 -- * Extracting results
49 -- $result
51 -- ** Handling null values
52 -- $null
54 -- ** Type conversions
55 -- $types
57 -- * Types
58 Connection
59 , Query
60 , ToRow
61 , FromRow
62 , In(..)
63 , Binary(..)
64 , Only(..)
65 , (:.)(..)
66 -- ** Exceptions
67 , SqlError(..)
68 , PQ.ExecStatus(..)
69 , FormatError(..)
70 , QueryError(..)
71 , ResultError(..)
72 -- * Connection management
73 , Base.connectPostgreSQL
74 , Base.close
75 , Base.connect
76 , Base.ConnectInfo(..)
77 , Base.defaultConnectInfo
78 , Base.postgreSQLConnectionString
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 -- ** Queries that stream results taking a parser as an argument
97 , foldWith
98 , foldWithOptionsAndParser
99 , foldWith_
100 , foldWithOptionsAndParser_
101 , forEachWith
102 , forEachWith_
103 , returningWith
104 -- * Statements that do not return results
105 , execute
106 , execute_
107 , executeMany
108-- , Base.insertID
109 -- * Transaction handling
110 , withTransaction
111 , withSavepoint
112-- , Base.autocommit
113 , begin
114 , commit
115 , rollback
116 -- * Helper functions
117 , formatMany
118 , formatQuery
119 ) where
121import Data.ByteString.Builder (Builder, byteString, char8)
122import Control.Applicative ((<$>))
123import Control.Exception as E
124import Data.ByteString (ByteString)
125import Data.Int (Int64)
126import Data.List (intersperse)
127import Data.Monoid (mconcat)
128import Database.PostgreSQL.Simple.Compat ((<>), toByteString)
129import Database.PostgreSQL.Simple.Cursor
130import Database.PostgreSQL.Simple.FromField (ResultError(..))
131import Database.PostgreSQL.Simple.FromRow (FromRow(..))
132import Database.PostgreSQL.Simple.ToField (Action(..))
133import Database.PostgreSQL.Simple.ToRow (ToRow(..))
134import Database.PostgreSQL.Simple.Types
135 ( Binary(..), In(..), Only(..), Query(..), (:.)(..) )
136import Database.PostgreSQL.Simple.Internal as Base
137import Database.PostgreSQL.Simple.Internal.PQResultUtils
138import Database.PostgreSQL.Simple.Transaction
139import qualified Database.PostgreSQL.LibPQ as PQ
140import qualified Data.ByteString.Char8 as B
143-- | Format a query string.
144--
145-- This function is exposed to help with debugging and logging. Do not
146-- use it to prepare queries for execution.
147--
148-- String parameters are escaped according to the character set in use
149-- on the 'Connection'.
150--
151-- Throws 'FormatError' if the query string could not be formatted
152-- correctly.
153formatQuery :: ToRow q => Connection -> Query -> q -> IO ByteString
154formatQuery conn q@(Query template) qs
155 | null xs && '?' `B.notElem` template = return template
156 | otherwise = toByteString <$> buildQuery conn q template xs
157 where xs = toRow qs
159-- | Format a query string with a variable number of rows.
160--
161-- This function is exposed to help with debugging and logging. Do not
162-- use it to prepare queries for execution.
163--
164-- The query string must contain exactly one substitution group,
165-- identified by the SQL keyword \"@VALUES@\" (case insensitive)
166-- followed by an \"@(@\" character, a series of one or more \"@?@\"
167-- characters separated by commas, and a \"@)@\" character. White
168-- space in a substitution group is permitted.
169--
170-- Throws 'FormatError' if the query string could not be formatted
171-- correctly.
172formatMany :: (ToRow q) => Connection -> Query -> [q] -> IO ByteString
173formatMany _ q [] = fmtError "no rows supplied" q []
174formatMany conn q@(Query template) qs = do
175 case parseTemplate template of
176 Just (before, qbits, after) -> do
177 bs <- mapM (buildQuery conn q qbits . toRow) qs
178 return . toByteString . mconcat $ byteString before :
179 intersperse (char8 ',') bs ++
180 [byteString after]
181 Nothing -> fmtError "syntax error in multi-row template" q []
183-- Split the input string into three pieces, @before@, @qbits@, and @after@,
184-- following this grammar:
185--
186-- start: ^ before qbits after $
187-- before: ([^?]* [^?\w])? 'VALUES' \s*
188-- qbits: '(' \s* '?' \s* (',' \s* '?' \s*)* ')'
189-- after: [^?]*
190--
191-- \s: [ \t\n\r\f]
192-- \w: [A-Z] | [a-z] | [\x80-\xFF] | '_' | '$' | [0-9]
193--
194-- This would be much more concise with some sort of regex engine.
195-- 'formatMany' used to use pcre-light instead of this hand-written parser,
196-- but pcre is a hassle to install on Windows.
197parseTemplate :: ByteString -> Maybe (ByteString, ByteString, ByteString)
198parseTemplate template =
199 -- Convert input string to uppercase, to facilitate searching.
200 search $ B.map toUpper_ascii template
201 where
202 -- Search for the next occurrence of "VALUES"
203 search bs =
204 case B.breakSubstring "VALUES" bs of
205 (x, y)
206 -- If "VALUES" is not present in the string, or any '?' characters
207 -- were encountered prior to it, fail.
208 | B.null y || ('?' `B.elem` x)
209 -> Nothing
211 -- If "VALUES" is preceded by an identifier character (a.k.a. \w),
212 -- try the next occurrence.
213 | not (B.null x) && isIdent (B.last x)
214 -> search $ B.drop 6 y
216 -- Otherwise, we have a legitimate "VALUES" token.
217 | otherwise
218 -> parseQueryBits $ skipSpace $ B.drop 6 y
220 -- Parse '(' \s* '?' \s* . If this doesn't match
221 -- (and we don't consume a '?'), look for another "VALUES".
222 --
223 -- qb points to the open paren (if present), meaning it points to the
224 -- beginning of the "qbits" production described above. This is why we
225 -- pass it down to finishQueryBits.
226 parseQueryBits qb
227 | Just ('(', skipSpace -> bs1) <- B.uncons qb
228 , Just ('?', skipSpace -> bs2) <- B.uncons bs1
229 = finishQueryBits qb bs2
230 | otherwise
231 = search qb
233 -- Parse (',' \s* '?' \s*)* ')' [^?]* .
234 --
235 -- Since we've already consumed at least one '?', there's no turning back.
236 -- The parse has to succeed here, or the whole thing fails
237 -- (because we don't allow '?' to appear outside of the VALUES list).
238 finishQueryBits qb bs0
239 | Just (')', bs1) <- B.uncons bs0
240 = if '?' `B.elem` bs1
241 then Nothing
242 else Just $ slice3 template qb bs1
243 | Just (',', skipSpace -> bs1) <- B.uncons bs0
244 , Just ('?', skipSpace -> bs2) <- B.uncons bs1
245 = finishQueryBits qb bs2
246 | otherwise
247 = Nothing
249 -- Slice a string into three pieces, given the start offset of the second
250 -- and third pieces. Each "offset" is actually a tail of the uppercase
251 -- version of the template string. Its length is used to infer the offset.
252 --
253 -- It is important to note that we only slice the original template.
254 -- We don't want our all-caps trick messing up the actual query string.
255 slice3 source p1 p2 =
256 (s1, s2, source'')
257 where
258 (s1, source') = B.splitAt (B.length source - B.length p1) source
259 (s2, source'') = B.splitAt (B.length p1 - B.length p2) source'
261 toUpper_ascii c | c >= 'a' && c <= 'z' = toEnum (fromEnum c - 32)
262 | otherwise = c
264 -- Based on the definition of {ident_cont} in src/backend/parser/scan.l
265 -- in the PostgreSQL source. No need to check [a-z], since we converted
266 -- the whole string to uppercase.
267 isIdent c = (c >= '0' && c <= '9')
268 || (c >= 'A' && c <= 'Z')
269 || (c >= '\x80' && c <= '\xFF')
270 || c == '_'
271 || c == '$'
273 -- Based on {space} in scan.l
274 isSpace_ascii c = (c == ' ') || (c >= '\t' && c <= '\r')
276 skipSpace = B.dropWhile isSpace_ascii
280buildQuery :: Connection -> Query -> ByteString -> [Action] -> IO Builder
281buildQuery conn q template xs =
282 zipParams (split template) <$> mapM (buildAction conn q xs) xs
283 where split s =
284 let (h,t) = B.break (=='?') s
285 in byteString h
286 : if B.null t
287 then []
288 else split (B.tail t)
289 zipParams (t:ts) (p:ps) = t <> p <> zipParams ts ps
290 zipParams [t] [] = t
291 zipParams _ _ = fmtError (show (B.count '?' template) ++
292 " '?' characters, but " ++
293 show (length xs) ++ " parameters") q xs
295-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
296-- expected to return results.
297--
298-- Returns the number of rows affected.
299--
300-- Throws 'FormatError' if the query could not be formatted correctly, or
301-- a 'SqlError' exception if the backend returns an error.
302execute :: (ToRow q) => Connection -> Query -> q -> IO Int64
303execute conn template qs = do
304 result <- exec conn =<< formatQuery conn template qs
305 finishExecute conn template result
307-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not
308-- expected to return results.
309--
310-- Returns the number of rows affected. If the list of parameters is empty,
311-- this function will simply return 0 without issuing the query to the backend.
312-- If this is not desired, consider using the 'Values' constructor instead.
313--
314-- Throws 'FormatError' if the query could not be formatted correctly, or
315-- a 'SqlError' exception if the backend returns an error.
316--
317-- For example, here's a command that inserts two rows into a table
318-- with two columns:
319--
320-- @
321-- executeMany c [sql|
322-- INSERT INTO sometable VALUES (?,?)
323-- |] [(1, \"hello\"),(2, \"world\")]
324-- @
325--
326-- Here's an canonical example of a multi-row update command:
327--
328-- @
329-- executeMany c [sql|
330-- UPDATE sometable
331-- SET sometable.y = upd.y
332-- FROM (VALUES (?,?)) as upd(x,y)
333-- WHERE sometable.x = upd.x
334-- |] [(1, \"hello\"),(2, \"world\")]
335-- @
337executeMany :: (ToRow q) => Connection -> Query -> [q] -> IO Int64
338executeMany _ _ [] = return 0
339executeMany conn q qs = do
340 result <- exec conn =<< formatMany conn q qs
341 finishExecute conn q result
343-- | Execute @INSERT ... RETURNING@, @UPDATE ... RETURNING@, or other SQL
344-- query that accepts multi-row input and is expected to return results.
345-- Note that it is possible to write
346-- @'query' conn "INSERT ... RETURNING ..." ...@
347-- in cases where you are only inserting a single row, and do not need
348-- functionality analogous to 'executeMany'.
349--
350-- If the list of parameters is empty, this function will simply return @[]@
351-- without issuing the query to the backend. If this is not desired,
352-- consider using the 'Values' constructor instead.
353--
354-- Throws 'FormatError' if the query could not be formatted correctly.
355returning :: (ToRow q, FromRow r) => Connection -> Query -> [q] -> IO [r]
356returning = returningWith fromRow
358returningWith :: (ToRow q) => RowParser r -> Connection -> Query -> [q] -> IO [r]
359returningWith _ _ _ [] = return []
360returningWith parser conn q qs = do
361 result <- exec conn =<< formatMany conn q qs
362 finishQueryWith parser conn q result
364-- | Perform a @SELECT@ or other SQL query that is expected to return
365-- results. All results are retrieved and converted before this
366-- function returns.
367--
368-- When processing large results, this function will consume a lot of
369-- client-side memory. Consider using 'fold' instead.
370--
371-- Exceptions that may be thrown:
372--
373-- * 'FormatError': the query string could not be formatted correctly.
374--
375-- * 'QueryError': the result contains no columns (i.e. you should be
376-- using 'execute' instead of 'query').
377--
378-- * 'ResultError': result conversion failed.
379--
380-- * 'SqlError': the postgresql backend returned an error, e.g.
381-- a syntax or type error, or an incorrect table or column name.
382query :: (ToRow q, FromRow r) => Connection -> Query -> q -> IO [r]
383query = queryWith fromRow
385-- | A version of 'query' that does not perform query substitution.
386query_ :: (FromRow r) => Connection -> Query -> IO [r]
387query_ = queryWith_ fromRow
389-- | A version of 'query' taking parser as argument
390queryWith :: ToRow q => RowParser r -> Connection -> Query -> q -> IO [r]
391queryWith parser conn template qs = do
392 result <- exec conn =<< formatQuery conn template qs
393 finishQueryWith parser conn template result
395-- | A version of 'query_' taking parser as argument
396queryWith_ :: RowParser r -> Connection -> Query -> IO [r]
397queryWith_ parser conn q@(Query que) = do
398 result <- exec conn que
399 finishQueryWith parser conn q result
401-- | Perform a @SELECT@ or other SQL query that is expected to return
402-- results. Results are streamed incrementally from the server, and
403-- consumed via a left fold.
404--
405-- When dealing with small results, it may be simpler (and perhaps
406-- faster) to use 'query' instead.
407--
408-- This fold is /not/ strict. The stream consumer is responsible for
409-- forcing the evaluation of its result to avoid space leaks.
410--
411-- This is implemented using a database cursor. As such, this requires
412-- a transaction. This function will detect whether or not there is a
413-- transaction in progress, and will create a 'ReadCommitted' 'ReadOnly'
414-- transaction if needed. The cursor is given a unique temporary name,
415-- so the consumer may itself call fold.
416--
417-- Exceptions that may be thrown:
418--
419-- * 'FormatError': the query string could not be formatted correctly.
420--
421-- * 'QueryError': the result contains no columns (i.e. you should be
422-- using 'execute' instead of 'query').
423--
424-- * 'ResultError': result conversion failed.
425--
426-- * 'SqlError': the postgresql backend returned an error, e.g.
427-- a syntax or type error, or an incorrect table or column name.
428fold :: ( FromRow row, ToRow params )
429 => Connection
430 -> Query
431 -> params
432 -> a
433 -> (a -> row -> IO a)
434 -> IO a
435fold = foldWithOptions defaultFoldOptions
437-- | A version of 'fold' taking a parser as an argument
438foldWith :: ( ToRow params )
439 => RowParser row
440 -> Connection
441 -> Query
442 -> params
443 -> a
444 -> (a -> row -> IO a)
445 -> IO a
446foldWith = foldWithOptionsAndParser defaultFoldOptions
448-- | Number of rows to fetch at a time. 'Automatic' currently defaults
449-- to 256 rows, although it might be nice to make this more intelligent
450-- based on e.g. the average size of the rows.
451data FetchQuantity
452 = Automatic
453 | Fixed !Int
455data FoldOptions
456 = FoldOptions {
457 fetchQuantity :: !FetchQuantity,
458 transactionMode :: !TransactionMode
459 }
461-- | defaults to 'Automatic', and 'TransactionMode' 'ReadCommitted' 'ReadOnly'
462defaultFoldOptions :: FoldOptions
463defaultFoldOptions = FoldOptions {
464 fetchQuantity = Automatic,
465 transactionMode = TransactionMode ReadCommitted ReadOnly
466 }
468-- | The same as 'fold', but this provides a bit more control over
469-- lower-level details. Currently, the number of rows fetched per
470-- round-trip to the server and the transaction mode may be adjusted
471-- accordingly. If the connection is already in a transaction,
472-- then the existing transaction is used and thus the 'transactionMode'
473-- option is ignored.
474foldWithOptions :: ( FromRow row, ToRow params )
475 => FoldOptions
476 -> Connection
477 -> Query
478 -> params
479 -> a
480 -> (a -> row -> IO a)
481 -> IO a
482foldWithOptions opts = foldWithOptionsAndParser opts fromRow
484-- | A version of 'foldWithOptions' taking a parser as an argument
485foldWithOptionsAndParser :: (ToRow params)
486 => FoldOptions
487 -> RowParser row
488 -> Connection
489 -> Query
490 -> params
491 -> a
492 -> (a -> row -> IO a)
493 -> IO a
494foldWithOptionsAndParser opts parser conn template qs a f = do
495 q <- formatQuery conn template qs
496 doFold opts parser conn template (Query q) a f
498-- | A version of 'fold' that does not perform query substitution.
499fold_ :: (FromRow r) =>
500 Connection
501 -> Query -- ^ Query.
502 -> a -- ^ Initial state for result consumer.
503 -> (a -> r -> IO a) -- ^ Result consumer.
504 -> IO a
505fold_ = foldWithOptions_ defaultFoldOptions
507-- | A version of 'fold_' taking a parser as an argument
508foldWith_ :: RowParser r
509 -> Connection
510 -> Query
511 -> a
512 -> (a -> r -> IO a)
513 -> IO a
514foldWith_ = foldWithOptionsAndParser_ defaultFoldOptions
516foldWithOptions_ :: (FromRow r) =>
517 FoldOptions
518 -> Connection
519 -> Query -- ^ Query.
520 -> a -- ^ Initial state for result consumer.
521 -> (a -> r -> IO a) -- ^ Result consumer.
522 -> IO a
523foldWithOptions_ opts conn query a f = doFold opts fromRow conn query query a f
525-- | A version of 'foldWithOptions_' taking a parser as an argument
526foldWithOptionsAndParser_ :: FoldOptions
527 -> RowParser r
528 -> Connection
529 -> Query -- ^ Query.
530 -> a -- ^ Initial state for result consumer.
531 -> (a -> r -> IO a) -- ^ Result consumer.
532 -> IO a
533foldWithOptionsAndParser_ opts parser conn query a f = doFold opts parser conn query query a f
535doFold :: FoldOptions
536 -> RowParser row
537 -> Connection
538 -> Query
539 -> Query
540 -> a
541 -> (a -> row -> IO a)
542 -> IO a
543doFold FoldOptions{..} parser conn _template q a0 f = do
544 stat <- withConnection conn PQ.transactionStatus
545 case stat of
546 PQ.TransIdle -> withTransactionMode transactionMode conn go
547 PQ.TransInTrans -> go
548 PQ.TransActive -> fail "foldWithOpts FIXME: PQ.TransActive"
549 -- This _shouldn't_ occur in the current incarnation of
550 -- the library, as we aren't using libpq asynchronously.
551 -- However, it could occur in future incarnations of
552 -- this library or if client code uses the Internal module
553 -- to use raw libpq commands on postgresql-simple connections.
554 PQ.TransInError -> fail "foldWithOpts FIXME: PQ.TransInError"
555 -- This should be turned into a better error message.
556 -- It is probably a bad idea to automatically roll
557 -- back the transaction and start another.
558 PQ.TransUnknown -> fail "foldWithOpts FIXME: PQ.TransUnknown"
559 -- Not sure what this means.
560 where
561 declare =
562 declareCursor conn q
563 fetch cursor a =
564 foldForwardWithParser cursor parser chunkSize f a
566 go = bracket declare closeCursor $ \cursor ->
567 let loop a = fetch cursor a >>=
568 \r -> case r of
569 Left a -> return a
570 Right a -> loop a
571 in loop a0
573-- FIXME: choose the Automatic chunkSize more intelligently
574-- One possibility is to use the type of the results, although this
575-- still isn't a perfect solution, given that common types (e.g. text)
576-- are of highly variable size.
577-- A refinement of this technique is to pick this number adaptively
578-- as results are read in from the database.
579 chunkSize = case fetchQuantity of
580 Automatic -> 256
581 Fixed n -> n
583-- | A version of 'fold' that does not transform a state value.
584forEach :: (ToRow q, FromRow r) =>
585 Connection
586 -> Query -- ^ Query template.
587 -> q -- ^ Query parameters.
588 -> (r -> IO ()) -- ^ Result consumer.
589 -> IO ()
590forEach = forEachWith fromRow
591{-# INLINE forEach #-}
593-- | A version of 'forEach' taking a parser as an argument
594forEachWith :: ( ToRow q )
595 => RowParser r
596 -> Connection
597 -> Query
598 -> q
599 -> (r -> IO ())
600 -> IO ()
601forEachWith parser conn template qs = foldWith parser conn template qs () . const
602{-# INLINE forEachWith #-}
604-- | A version of 'forEach' that does not perform query substitution.
605forEach_ :: (FromRow r) =>
606 Connection
607 -> Query -- ^ Query template.
608 -> (r -> IO ()) -- ^ Result consumer.
609 -> IO ()
610forEach_ = forEachWith_ fromRow
611{-# INLINE forEach_ #-}
613forEachWith_ :: RowParser r
614 -> Connection
615 -> Query
616 -> (r -> IO ())
617 -> IO ()
618forEachWith_ parser conn template = foldWith_ parser conn template () . const
619{-# INLINE forEachWith_ #-}
622-- $use
623--
624-- SQL-based applications are somewhat notorious for their
625-- susceptibility to attacks through the injection of maliciously
626-- crafted data. The primary reason for widespread vulnerability to
627-- SQL injections is that many applications are sloppy in handling
628-- user data when constructing SQL queries.
629--
630-- This library provides a 'Query' type and a parameter substitution
631-- facility to address both ease of use and security.
633-- $querytype
634--
635-- A 'Query' is a @newtype@-wrapped 'ByteString'. It intentionally
636-- exposes a tiny API that is not compatible with the 'ByteString'
637-- API; this makes it difficult to construct queries from fragments of
638-- strings. The 'query' and 'execute' functions require queries to be
639-- of type 'Query'.
640--
641-- To most easily construct a query, enable GHC's @OverloadedStrings@
642-- language extension and write your query as a normal literal string.
643--
644-- > {-# LANGUAGE OverloadedStrings #-}
645-- >
646-- > import Database.PostgreSQL.Simple
647-- >
648-- > hello :: IO Int
649-- > hello = do
650-- > conn <- connectPostgreSQL ""
651-- > [Only i] <- query_ conn "select 2 + 2"
652-- > return i
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.
713--
714-- Finally, remember that the compiler must be able to infer the type
715-- of a query's /results/ as well as its parameters. We might like
716-- the following example to work:
717--
718-- > print =<< query_ conn "select 2 + 2"
719--
720-- Unfortunately, while a quick glance tells us that the result type
721-- should be a single row containing a single numeric column, the
722-- compiler has no way to infer what the types are. We can easily fix
723-- this by providing an explicit type annotation:
724--
725-- > xs <- query_ conn "select 2 + 2"
726-- > print (xs :: [Only Int])
728-- $only_param
729--
730-- Haskell lacks a single-element tuple type, so if you have just one
731-- value you want substituted into a query or a single-column result,
732-- what should you do?
733--
734-- The obvious approach would appear to be something like this:
735--
736-- > instance (ToField a) => ToRow a where
737-- > ...
738--
739-- Unfortunately, this wreaks havoc with type inference, so we take a
740-- different tack. To represent a single value @val@ as a parameter, write
741-- a singleton list @[val]@, use 'Just' @val@, or use 'Only' @val@.
742--
743-- Here's an example using a singleton list:
744--
745-- > execute conn "insert into users (first_name) values (?)"
746-- > ["Nuala"]
747--
748-- A row of /n/ query results is represented using an /n/-tuple, so
749-- you should use 'Only' to represent a single-column result.
751-- $in
752--
753-- Suppose you want to write a query using an @IN@ clause:
754--
755-- > select * from users where first_name in ('Anna', 'Boris', 'Carla')
756--
757-- In such cases, it's common for both the elements and length of the
758-- list after the @IN@ keyword to vary from query to query.
759--
760-- To address this case, use the 'In' type wrapper, and use a single
761-- \"@?@\" character to represent the list. Omit the parentheses
762-- around the list; these will be added for you.
763--
764-- Here's an example:
765--
766-- > query conn "select * from users where first_name in ?" $
767-- > Only $ In ["Anna", "Boris", "Carla"]
768--
769-- If your 'In'-wrapped list is empty, the string @\"(null)\"@ will be
770-- substituted instead, to ensure that your clause remains
771-- syntactically valid.
773-- $many
774--
775-- If you know that you have many rows of data to insert into a table,
776-- it is much more efficient to perform all the insertions in a single
777-- multi-row @INSERT@ statement than individually.
778--
779-- The 'executeMany' function is intended specifically for helping
780-- with multi-row @INSERT@ and @UPDATE@ statements. Its rules for
781-- query substitution are different than those for 'execute'.
782--
783-- What 'executeMany' searches for in your 'Query' template is a
784-- single substring of the form:
785--
786-- > values (?,?,?)
787--
788-- The rules are as follows:
789--
790-- * The keyword @VALUES@ is matched case insensitively.
791--
792-- * There must be no other \"@?@\" characters anywhere in your
793-- template.
794--
795-- * There must be one or more \"@?@\" in the parentheses.
796--
797-- * Extra white space is fine.
798--
799-- The last argument to 'executeMany' is a list of parameter
800-- tuples. These will be substituted into the query where the @(?,?)@
801-- string appears, in a form suitable for use in a multi-row @INSERT@
802-- or @UPDATE@.
803--
804-- Here is an example:
805--
806-- > executeMany conn
807-- > "insert into users (first_name,last_name) values (?,?)"
808-- > [("Boris","Karloff"),("Ed","Wood")]
809--
810-- The query that will be executed here will look like this
811-- (reformatted for tidiness):
812--
813-- > insert into users (first_name,last_name) values
814-- > ('Boris','Karloff'),('Ed','Wood')
816-- $returning
817--
818-- PostgreSQL supports returning values from data manipulation statements
819-- such as @INSERT@ and @UPDATE@. You can use these statements by
820-- using 'query' instead of 'execute'. For multi-tuple inserts,
821-- use 'returning' instead of 'executeMany'.
822--
823-- For example, were there an auto-incrementing @id@ column and
824-- timestamp column @t@ that defaulted to the present time for the
825-- @sales@ table, then the following query would insert two new
826-- sales records and also return their new @id@s and timestamps.
827--
828-- > let q = "insert into sales (amount, label) values (?,?) returning id, t"
829-- > xs :: [(Int, UTCTime)] <- query conn q (15,"Sawdust")
830-- > ys :: [(Int, UTCTime)] <- returning conn q [(20,"Chips"),(300,"Wood")]
832-- $result
833--
834-- The 'query' and 'query_' functions return a list of values in the
835-- 'FromRow' typeclass. This class performs automatic extraction
836-- and type conversion of rows from a query result.
837--
838-- Here is a simple example of how to extract results:
839--
840-- > import qualified Data.Text as Text
841-- >
842-- > xs <- query_ conn "select name,age from users"
843-- > forM_ xs $ \(name,age) ->
844-- > putStrLn $ Text.unpack name ++ " is " ++ show (age :: Int)
845--
846-- Notice two important details about this code:
847--
848-- * The number of columns we ask for in the query template must
849-- exactly match the number of elements we specify in a row of the
850-- result tuple. If they do not match, a 'ResultError' exception
851-- will be thrown.
852--
853-- * Sometimes, the compiler needs our help in specifying types. It
854-- can infer that @name@ must be a 'Text', due to our use of the
855-- @unpack@ function. However, we have to tell it the type of @age@,
856-- as it has no other information to determine the exact type.
858-- $null
859--
860-- The type of a result tuple will look something like this:
861--
862-- > (Text, Int, Int)
863--
864-- Although SQL can accommodate @NULL@ as a value for any of these
865-- types, Haskell cannot. If your result contains columns that may be
866-- @NULL@, be sure that you use 'Maybe' in those positions of your
867-- tuple.
868--
869-- > (Text, Maybe Int, Int)
870--
871-- If 'query' encounters a @NULL@ in a row where the corresponding
872-- Haskell type is not 'Maybe', it will throw a 'ResultError'
873-- exception.
875-- $only_result
876--
877-- To specify that a query returns a single-column result, use the
878-- 'Only' type.
879--
880-- > xs <- query_ conn "select id from users"
881-- > forM_ xs $ \(Only dbid) -> {- ... -}
883-- $types
884--
885-- Conversion of SQL values to Haskell values is somewhat
886-- permissive. Here are the rules.
887--
888-- * For numeric types, any Haskell type that can accurately represent
889-- all values of the given PostgreSQL type is considered \"compatible\".
890-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
891-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
892-- represent a @SMALLINT@, so it is considered compatble with those types.
893--
894-- * A numeric compatibility check is based only on the type of a
895-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
896-- @BIGINT@ column will be considered incompatible with a Haskell
897-- 'Int16', even if it contains the value @1@.
898--
899-- * If a numeric incompatibility is found, 'query' will throw a
900-- 'ResultError'.
901--
902-- * The 'String' and 'Text' types are assumed to be encoded as
903-- UTF-8. If you use some other encoding, decoding may fail or give
904-- wrong results. In such cases, write a @newtype@ wrapper and a
905-- custom 'Result' instance to handle your encoding.