git.iko.soy/typeable/postgresql-simple
on bf134aa help sign in
1001 lines 37 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 (unless)
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 'fold_' 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 close name =
571 (execute_ conn ("CLOSE " <> name) >> return ()) `E.catch` \ex ->
572 -- Don't throw exception if CLOSE failed because the transaction is
573 -- aborted. Otherwise, it will throw away the original error.
574 unless (isFailedTransactionError ex) $ throwIO ex
576 go = bracket declare close $ \(Query name) ->
577 let q = toByteString (byteString "FETCH FORWARD "
578 <> intDec chunkSize
579 <> byteString " FROM "
580 <> byteString name
581 )
582 loop a = do
583 result <- exec conn q
584 status <- PQ.resultStatus result
585 case status of
586 PQ.TuplesOk -> do
587 nrows <- PQ.ntuples result
588 ncols <- PQ.nfields result
589 if nrows > 0
590 then do
591 let inner a row = do
592 x <- getRowWith parser row ncols conn result
593 f a x
594 foldM' inner a 0 (nrows - 1) >>= loop
595 else return a
596 _ -> throwResultError "fold" result status
597 in loop a0
599-- FIXME: choose the Automatic chunkSize more intelligently
600-- One possibility is to use the type of the results, although this
601-- still isn't a perfect solution, given that common types (e.g. text)
602-- are of highly variable size.
603-- A refinement of this technique is to pick this number adaptively
604-- as results are read in from the database.
605 chunkSize = case fetchQuantity of
606 Automatic -> 256
607 Fixed n -> n
609-- | A version of 'fold' that does not transform a state value.
610forEach :: (ToRow q, FromRow r) =>
611 Connection
612 -> Query -- ^ Query template.
613 -> q -- ^ Query parameters.
614 -> (r -> IO ()) -- ^ Result consumer.
615 -> IO ()
616forEach = forEachWith fromRow
617{-# INLINE forEach #-}
619-- | A version of 'forEach' taking a parser as an argument
620forEachWith :: ( ToRow q )
621 => RowParser r
622 -> Connection
623 -> Query
624 -> q
625 -> (r -> IO ())
626 -> IO ()
627forEachWith parser conn template qs = foldWith parser conn template qs () . const
628{-# INLINE forEachWith #-}
630-- | A version of 'forEach' that does not perform query substitution.
631forEach_ :: (FromRow r) =>
632 Connection
633 -> Query -- ^ Query template.
634 -> (r -> IO ()) -- ^ Result consumer.
635 -> IO ()
636forEach_ = forEachWith_ fromRow
637{-# INLINE forEach_ #-}
639forEachWith_ :: RowParser r
640 -> Connection
641 -> Query
642 -> (r -> IO ())
643 -> IO ()
644forEachWith_ parser conn template = foldWith_ parser conn template () . const
645{-# INLINE forEachWith_ #-}
647forM' :: (Ord n, Num n) => n -> n -> (n -> IO a) -> IO [a]
648forM' lo hi m = loop hi []
649 where
650 loop !n !as
651 | n < lo = return as
652 | otherwise = do
653 a <- m n
654 loop (n-1) (a:as)
655{-# INLINE forM' #-}
657foldM' :: (Ord n, Num n) => (a -> n -> IO a) -> a -> n -> n -> IO a
658foldM' f a lo hi = loop a lo
659 where
660 loop a !n
661 | n > hi = return a
662 | otherwise = do
663 a' <- f a n
664 loop a' (n+1)
665{-# INLINE foldM' #-}
667finishQueryWith :: RowParser r -> Connection -> Query -> PQ.Result -> IO [r]
668finishQueryWith parser conn q result = do
669 status <- PQ.resultStatus result
670 case status of
671 PQ.EmptyQuery ->
672 throwIO $ QueryError "query: Empty query" q
673 PQ.CommandOk ->
674 throwIO $ QueryError "query resulted in a command response" q
675 PQ.TuplesOk -> do
676 nrows <- PQ.ntuples result
677 ncols <- PQ.nfields result
678 forM' 0 (nrows-1) $ \row ->
679 getRowWith parser row ncols conn result
680 PQ.CopyOut ->
681 throwIO $ QueryError "query: COPY TO is not supported" q
682 PQ.CopyIn ->
683 throwIO $ QueryError "query: COPY FROM is not supported" q
684 PQ.BadResponse -> throwResultError "query" result status
685 PQ.NonfatalError -> throwResultError "query" result status
686 PQ.FatalError -> throwResultError "query" result status
688getRowWith :: RowParser r -> PQ.Row -> PQ.Column -> Connection -> PQ.Result -> IO r
689getRowWith parser row ncols conn result = do
690 let rw = Row row result
691 let unCol (PQ.Col x) = fromIntegral x :: Int
692 okvc <- runConversion (runStateT (runReaderT (unRP parser) rw) 0) conn
693 case okvc of
694 Ok (val,col) | col == ncols -> return val
695 | otherwise -> do
696 vals <- forM' 0 (ncols-1) $ \c -> do
697 tinfo <- getTypeInfo conn =<< PQ.ftype result c
698 v <- PQ.getvalue result row c
699 return ( tinfo
700 , fmap ellipsis v )
701 throw (ConversionFailed
702 (show (unCol ncols) ++ " values: " ++ show vals)
703 Nothing
704 ""
705 (show (unCol col) ++ " slots in target type")
706 "mismatch between number of columns to \
707 \convert and number in target type")
708 Errors [] -> throwIO $ ConversionFailed "" Nothing "" "" "unknown error"
709 Errors [x] -> throwIO x
710 Errors xs -> throwIO $ ManyErrors xs
712ellipsis :: ByteString -> ByteString
713ellipsis bs
714 | B.length bs > 15 = B.take 10 bs `B.append` "[...]"
715 | otherwise = bs
718-- $use
719--
720-- SQL-based applications are somewhat notorious for their
721-- susceptibility to attacks through the injection of maliciously
722-- crafted data. The primary reason for widespread vulnerability to
723-- SQL injections is that many applications are sloppy in handling
724-- user data when constructing SQL queries.
725--
726-- This library provides a 'Query' type and a parameter substitution
727-- facility to address both ease of use and security.
729-- $querytype
730--
731-- A 'Query' is a @newtype@-wrapped 'ByteString'. It intentionally
732-- exposes a tiny API that is not compatible with the 'ByteString'
733-- API; this makes it difficult to construct queries from fragments of
734-- strings. The 'query' and 'execute' functions require queries to be
735-- of type 'Query'.
736--
737-- To most easily construct a query, enable GHC's @OverloadedStrings@
738-- language extension and write your query as a normal literal string.
739--
740-- > {-# LANGUAGE OverloadedStrings #-}
741-- >
742-- > import Database.PostgreSQL.Simple
743-- >
744-- > hello :: IO Int
745-- > hello = do
746-- > conn <- connectPostgreSQL ""
747-- > [Only i] <- query_ conn "select 2 + 2"
748-- > return i
749--
750-- A 'Query' value does not represent the actual query that will be
751-- executed, but is a template for constructing the final query.
753-- $subst
754--
755-- Since applications need to be able to construct queries with
756-- parameters that change, this library provides a query substitution
757-- capability.
758--
759-- The 'Query' template accepted by 'query' and 'execute' can contain
760-- any number of \"@?@\" characters. Both 'query' and 'execute'
761-- accept a third argument, typically a tuple. When constructing the
762-- real query to execute, these functions replace the first \"@?@\" in
763-- the template with the first element of the tuple, the second
764-- \"@?@\" with the second element, and so on. If necessary, each
765-- tuple element will be quoted and escaped prior to substitution;
766-- this defeats the single most common injection vector for malicious
767-- data.
768--
769-- For example, given the following 'Query' template:
770--
771-- > select * from user where first_name = ? and age > ?
772--
773-- And a tuple of this form:
774--
775-- > ("Boris" :: String, 37 :: Int)
776--
777-- The query to be executed will look like this after substitution:
778--
779-- > select * from user where first_name = 'Boris' and age > 37
780--
781-- If there is a mismatch between the number of \"@?@\" characters in
782-- your template and the number of elements in your tuple, a
783-- 'FormatError' will be thrown.
784--
785-- Note that the substitution functions do not attempt to parse or
786-- validate your query. It's up to you to write syntactically valid
787-- SQL, and to ensure that each \"@?@\" in your query template is
788-- matched with the right tuple element.
790-- $inference
791--
792-- Automated type inference means that you will often be able to avoid
793-- supplying explicit type signatures for the elements of a tuple.
794-- However, sometimes the compiler will not be able to infer your
795-- types. Consider a case where you write a numeric literal in a
796-- parameter tuple:
797--
798-- > query conn "select ? + ?" (40,2)
799--
800-- The above query will be rejected by the compiler, because it does
801-- not know the specific numeric types of the literals @40@ and @2@.
802-- This is easily fixed:
803--
804-- > query conn "select ? + ?" (40 :: Double, 2 :: Double)
805--
806-- The same kind of problem can arise with string literals if you have
807-- the @OverloadedStrings@ language extension enabled. Again, just
808-- use an explicit type signature if this happens.
809--
810-- Finally, remember that the compiler must be able to infer the type
811-- of a query's /results/ as well as its parameters. We might like
812-- the following example to work:
813--
814-- > print =<< query_ conn "select 2 + 2"
815--
816-- Unfortunately, while a quick glance tells us that the result type
817-- should be a single row containing a single numeric column, the
818-- compiler has no way to infer what the types are. We can easily fix
819-- this by providing an explicit type annotation:
820--
821-- > xs <- query_ conn "select 2 + 2"
822-- > print (xs :: [Only Int])
824-- $only_param
825--
826-- Haskell lacks a single-element tuple type, so if you have just one
827-- value you want substituted into a query or a single-column result,
828-- what should you do?
829--
830-- The obvious approach would appear to be something like this:
831--
832-- > instance (ToField a) => ToRow a where
833-- > ...
834--
835-- Unfortunately, this wreaks havoc with type inference, so we take a
836-- different tack. To represent a single value @val@ as a parameter, write
837-- a singleton list @[val]@, use 'Just' @val@, or use 'Only' @val@.
838--
839-- Here's an example using a singleton list:
840--
841-- > execute conn "insert into users (first_name) values (?)"
842-- > ["Nuala"]
843--
844-- A row of /n/ query results is represented using an /n/-tuple, so
845-- you should use 'Only' to represent a single-column result.
847-- $in
848--
849-- Suppose you want to write a query using an @IN@ clause:
850--
851-- > select * from users where first_name in ('Anna', 'Boris', 'Carla')
852--
853-- In such cases, it's common for both the elements and length of the
854-- list after the @IN@ keyword to vary from query to query.
855--
856-- To address this case, use the 'In' type wrapper, and use a single
857-- \"@?@\" character to represent the list. Omit the parentheses
858-- around the list; these will be added for you.
859--
860-- Here's an example:
861--
862-- > query conn "select * from users where first_name in ?" $
863-- > Only $ In ["Anna", "Boris", "Carla"]
864--
865-- If your 'In'-wrapped list is empty, the string @\"(null)\"@ will be
866-- substituted instead, to ensure that your clause remains
867-- syntactically valid.
869-- $many
870--
871-- If you know that you have many rows of data to insert into a table,
872-- it is much more efficient to perform all the insertions in a single
873-- multi-row @INSERT@ statement than individually.
874--
875-- The 'executeMany' function is intended specifically for helping
876-- with multi-row @INSERT@ and @UPDATE@ statements. Its rules for
877-- query substitution are different than those for 'execute'.
878--
879-- What 'executeMany' searches for in your 'Query' template is a
880-- single substring of the form:
881--
882-- > values (?,?,?)
883--
884-- The rules are as follows:
885--
886-- * The keyword @VALUES@ is matched case insensitively.
887--
888-- * There must be no other \"@?@\" characters anywhere in your
889-- template.
890--
891-- * There must be one or more \"@?@\" in the parentheses.
892--
893-- * Extra white space is fine.
894--
895-- The last argument to 'executeMany' is a list of parameter
896-- tuples. These will be substituted into the query where the @(?,?)@
897-- string appears, in a form suitable for use in a multi-row @INSERT@
898-- or @UPDATE@.
899--
900-- Here is an example:
901--
902-- > executeMany conn
903-- > "insert into users (first_name,last_name) values (?,?)"
904-- > [("Boris","Karloff"),("Ed","Wood")]
905--
906-- The query that will be executed here will look like this
907-- (reformatted for tidiness):
908--
909-- > insert into users (first_name,last_name) values
910-- > ('Boris','Karloff'),('Ed','Wood')
912-- $returning
913--
914-- PostgreSQL supports returning values from data manipulation statements
915-- such as @INSERT@ and @UPDATE@. You can use these statements by
916-- using 'query' instead of 'execute'. For multi-tuple inserts,
917-- use 'returning' instead of 'executeMany'.
918--
919-- For example, were there an auto-incrementing @id@ column and
920-- timestamp column @t@ that defaulted to the present time for the
921-- @sales@ table, then the following query would insert two new
922-- sales records and also return their new @id@s and timestamps.
923--
924-- > let q = "insert into sales (amount, label) values (?,?) returning id, t"
925-- > xs :: [(Int, UTCTime)] <- query conn q (15,"Sawdust")
926-- > ys :: [(Int, UTCTime)] <- returning conn q [(20,"Chips"),(300,"Wood")]
928-- $result
929--
930-- The 'query' and 'query_' functions return a list of values in the
931-- 'FromRow' typeclass. This class performs automatic extraction
932-- and type conversion of rows from a query result.
933--
934-- Here is a simple example of how to extract results:
935--
936-- > import qualified Data.Text as Text
937-- >
938-- > xs <- query_ conn "select name,age from users"
939-- > forM_ xs $ \(name,age) ->
940-- > putStrLn $ Text.unpack name ++ " is " ++ show (age :: Int)
941--
942-- Notice two important details about this code:
943--
944-- * The number of columns we ask for in the query template must
945-- exactly match the number of elements we specify in a row of the
946-- result tuple. If they do not match, a 'ResultError' exception
947-- will be thrown.
948--
949-- * Sometimes, the compiler needs our help in specifying types. It
950-- can infer that @name@ must be a 'Text', due to our use of the
951-- @unpack@ function. However, we have to tell it the type of @age@,
952-- as it has no other information to determine the exact type.
954-- $null
955--
956-- The type of a result tuple will look something like this:
957--
958-- > (Text, Int, Int)
959--
960-- Although SQL can accommodate @NULL@ as a value for any of these
961-- types, Haskell cannot. If your result contains columns that may be
962-- @NULL@, be sure that you use 'Maybe' in those positions of your
963-- tuple.
964--
965-- > (Text, Maybe Int, Int)
966--
967-- If 'query' encounters a @NULL@ in a row where the corresponding
968-- Haskell type is not 'Maybe', it will throw a 'ResultError'
969-- exception.
971-- $only_result
972--
973-- To specify that a query returns a single-column result, use the
974-- 'Only' type.
975--
976-- > xs <- query_ conn "select id from users"
977-- > forM_ xs $ \(Only dbid) -> {- ... -}
979-- $types
980--
981-- Conversion of SQL values to Haskell values is somewhat
982-- permissive. Here are the rules.
983--
984-- * For numeric types, any Haskell type that can accurately represent
985-- all values of the given PostgreSQL type is considered \"compatible\".
986-- For instance, you can always extract a PostgreSQL 16-bit @SMALLINT@
987-- column to a Haskell 'Int'. The Haskell 'Float' type can accurately
988-- represent a @SMALLINT@, so it is considered compatble with those types.
989--
990-- * A numeric compatibility check is based only on the type of a
991-- column, /not/ on its values. For instance, a PostgreSQL 64-bit
992-- @BIGINT@ column will be considered incompatible with a Haskell
993-- 'Int16', even if it contains the value @1@.
994--
995-- * If a numeric incompatibility is found, 'query' will throw a
996-- 'ResultError'.
997--
998-- * The 'String' and 'Text' types are assumed to be encoded as
999-- UTF-8. If you use some other encoding, decoding may fail or give
1000-- wrong results. In such cases, write a @newtype@ wrapper and a
1001-- custom 'Result' instance to handle your encoding.