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