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