1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE NamedFieldPuns #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE PatternGuards #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE ViewPatterns #-}
8------------------------------------------------------------------------------
10-- Module: Database.PostgreSQL.Simple
11-- Copyright: (c) 2011 MailRank, Inc.
12-- (c) 2011-2012 Leon P Smith
14-- Maintainer: Leon P Smith <leon@melding-monads.com>
15-- Stability: experimental
17-- A mid-level client library for the PostgreSQL database, aimed at ease of
18-- use and high performance.
20------------------------------------------------------------------------------
22module Database.PostgreSQL.Simple
30 -- ** Parameter substitution
36 -- ** Substituting a single parameter
39 -- ** Representing a list of values
42 -- ** Modifying multiple rows at once
45 -- ** @RETURNING@: modifications that return results
48 -- * Extracting results
51 -- ** Handling null values
54 -- ** Type conversions
72 -- * Connection management
73 , Base.connectPostgreSQL
77 , Base.ConnectInfo(..)
78 , Base.defaultConnectInfo
79 , Base.postgreSQLConnectionString
80 -- * Queries that return results
83 -- ** Queries taking parser as argument
86 -- * Queries that stream results
97 -- ** Queries that stream results taking a parser as an argument
99 , foldWithOptionsAndParser
101 , foldWithOptionsAndParser_
105 -- * Statements that do not return results
110 -- * Transaction handling
117 -- * Helper functions
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.
146-- This function is exposed to help with debugging and logging. Do not
147-- use it to prepare queries for execution.
149-- String parameters are escaped according to the character set in use
150-- on the 'Connection'.
152-- Throws 'FormatError' if the query string could not be formatted
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
160-- | Format a query string with a variable number of rows.
162-- This function is exposed to help with debugging and logging. Do not
163-- use it to prepare queries for execution.
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.
171-- Throws 'FormatError' if the query string could not be formatted
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 ++
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:
187-- start: ^ before qbits after $
188-- before: ([^?]* [^?\w])? 'VALUES' \s*
189-- qbits: '(' \s* '?' \s* (',' \s* '?' \s*)* ')'
193-- \w: [A-Z] | [a-z] | [\x80-\xFF] | '_' | '$' | [0-9]
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
203 -- Search for the next occurrence of "VALUES"
205 case B.breakSubstring "VALUES" bs of
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)
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.
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".
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.
228 | Just ('(', skipSpace -> bs1) <- B.uncons qb
229 , Just ('?', skipSpace -> bs2) <- B.uncons bs1
230 = finishQueryBits qb bs2
234 -- Parse (',' \s* '?' \s*)* ')' [^?]* .
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
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
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.
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 =
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)
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')
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
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
290 else split (B.tail t)
291 zipParams (t:ts) (p:ps) = t <> p <> zipParams ts ps
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
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.
309-- Returns the number of rows affected.
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.
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.
325-- Throws 'FormatError' if the query could not be formatted correctly, or
326-- a 'SqlError' exception if the backend returns an error.
328-- For example, here's a command that inserts two rows into a table
332-- executeMany c [sql|
333-- INSERT INTO sometable VALUES (?,?)
334-- |] [(1, \"hello\"),(2, \"world\")]
337-- Here's an canonical example of a multi-row update command:
340-- executeMany c [sql|
343-- FROM (VALUES (?,?)) as upd(x,y)
344-- WHERE sometable.x = upd.x
345-- |] [(1, \"hello\"),(2, \"world\")]
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'.
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.
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
380-- When processing large results, this function will consume a lot of
381-- client-side memory. Consider using 'fold' instead.
383-- Exceptions that may be thrown:
385-- * 'FormatError': the query string could not be formatted correctly.
387-- * 'QueryError': the result contains no columns (i.e. you should be
388-- using 'execute' instead of 'query').
390-- * 'ResultError': result conversion failed.
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.
417-- When dealing with small results, it may be simpler (and perhaps
418-- faster) to use 'query' instead.
420-- This fold is /not/ strict. The stream consumer is responsible for
421-- forcing the evaluation of its result to avoid space leaks.
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.
429-- Exceptions that may be thrown:
431-- * 'FormatError': the query string could not be formatted correctly.
433-- * 'QueryError': the result contains no columns (i.e. you should be
434-- using 'execute' instead of 'query').
436-- * 'ResultError': result conversion failed.
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 )
445 -> (a -> row -> IO a)
447fold = foldWithOptions defaultFoldOptions
449-- | A version of 'fold' taking a parser as an argument
450foldWith :: ( ToRow params )
456 -> (a -> row -> 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.
469 fetchQuantity :: !FetchQuantity,
470 transactionMode :: !TransactionMode
473-- | defaults to 'Automatic', and 'TransactionMode' 'ReadCommitted' 'ReadOnly'
474defaultFoldOptions :: FoldOptions
475defaultFoldOptions = FoldOptions {
476 fetchQuantity = Automatic,
477 transactionMode = TransactionMode ReadCommitted ReadOnly
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'
486foldWithOptions :: ( FromRow row, ToRow params )
492 -> (a -> row -> IO a)
494foldWithOptions opts = foldWithOptionsAndParser opts fromRow
496-- | A version of 'foldWithOptions' taking a parser as an argument
497foldWithOptionsAndParser :: (ToRow params)
504 -> (a -> row -> 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) =>
514 -> a -- ^ Initial state for result consumer.
515 -> (a -> r -> IO a) -- ^ Result consumer.
517fold_ = foldWithOptions_ defaultFoldOptions
519-- | A version of 'fold_' taking a parser as an argument
520foldWith_ :: RowParser r
526foldWith_ = foldWithOptionsAndParser_ defaultFoldOptions
528foldWithOptions_ :: (FromRow r) =>
532 -> a -- ^ Initial state for result consumer.
533 -> (a -> r -> IO a) -- ^ Result consumer.
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
542 -> a -- ^ Initial state for result consumer.
543 -> (a -> r -> IO a) -- ^ Result consumer.
545foldWithOptionsAndParser_ opts parser conn query' a f = doFold opts parser conn query' query' a f
553 -> (a -> row -> IO a)
555doFold FoldOptions{..} parser conn _template q a0 f = do
556 stat <- withConnection conn PQ.transactionStatus
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.
576 foldForwardWithParser cursor parser chunkSize f a
578 go = bracket declare closeCursor $ \cursor ->
579 let loop a = fetch cursor a >>=
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
595-- | A version of 'fold' that does not transform a state value.
596forEach :: (ToRow q, FromRow r) =>
598 -> Query -- ^ Query template.
599 -> q -- ^ Query parameters.
600 -> (r -> IO ()) -- ^ Result consumer.
602forEach = forEachWith fromRow
603{-# INLINE forEach #-}
605-- | A version of 'forEach' taking a parser as an argument
606forEachWith :: ( ToRow q )
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) =>
619 -> Query -- ^ Query template.
620 -> (r -> IO ()) -- ^ Result consumer.
622forEach_ = forEachWith_ fromRow
623{-# INLINE forEach_ #-}
625forEachWith_ :: RowParser r
630forEachWith_ parser conn template = foldWith_ parser conn template () . const
631{-# INLINE forEachWith_ #-}
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.
642-- This library provides a 'Query' type and a parameter substitution
643-- facility to address both ease of use and security.
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
653-- To most easily construct a query, enable GHC's @OverloadedStrings@
654-- language extension and write your query as a normal literal string.
656-- > {-# LANGUAGE OverloadedStrings #-}
658-- > import Database.PostgreSQL.Simple
662-- > conn <- connectPostgreSQL ""
663-- > [Only i] <- query_ conn "select 2 + 2"
666-- A 'Query' value does not represent the actual query that will be
667-- executed, but is a template for constructing the final query.
671-- Since applications need to be able to construct queries with
672-- parameters that change, this library provides a query substitution
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
685-- For example, given the following 'Query' template:
687-- > select * from user where first_name = ? and age > ?
689-- And a tuple of this form:
691-- > ("Boris" :: String, 37 :: Int)
693-- The query to be executed will look like this after substitution:
695-- > select * from user where first_name = 'Boris' and age > 37
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.
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.
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
714-- > query conn "select ? + ?" (40,2)
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:
720-- > query conn "select ? + ?" (40 :: Double, 2 :: Double)
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.
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:
730-- > print =<< query_ conn "select 2 + 2"
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:
737-- > xs <- query_ conn "select 2 + 2"
738-- > print (xs :: [Only Int])
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?
746-- The obvious approach would appear to be something like this:
748-- > instance (ToField a) => ToRow a where
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@.
755-- Here's an example using a singleton list:
757-- > execute conn "insert into users (first_name) values (?)"
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.
765-- Suppose you want to write a query using an @IN@ clause:
767-- > select * from users where first_name in ('Anna', 'Boris', 'Carla')
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.
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.
778-- > query conn "select * from users where first_name in ?" $
779-- > Only $ In ["Anna", "Boris", "Carla"]
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.
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.
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'.
795-- What 'executeMany' searches for in your 'Query' template is a
796-- single substring of the form:
800-- The rules are as follows:
802-- * The keyword @VALUES@ is matched case insensitively.
804-- * There must be no other \"@?@\" characters anywhere in your
807-- * There must be one or more \"@?@\" in the parentheses.
809-- * Extra white space is fine.
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@
816-- Here is an example:
819-- > "insert into users (first_name,last_name) values (?,?)"
820-- > [("Boris","Karloff"),("Ed","Wood")]
822-- The query that will be executed here will look like this
823-- (reformatted for tidiness):
825-- > insert into users (first_name,last_name) values
826-- > ('Boris','Karloff'),('Ed','Wood')
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'.
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.
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")]
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.
850-- Here is a simple example of how to extract results:
852-- > import qualified Data.Text as Text
854-- > xs <- query_ conn "select name,age from users"
855-- > forM_ xs $ \(name,age) ->
856-- > putStrLn $ Text.unpack name ++ " is " ++ show (age :: Int)
858-- Notice two important details about this code:
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
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.
872-- The type of a result tuple will look something like this:
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
881-- > (Text, Maybe Int, Int)
883-- If 'query' encounters a @NULL@ in a row where the corresponding
884-- Haskell type is not 'Maybe', it will throw a 'ResultError'
889-- To specify that a query returns a single-column result, use the
892-- > xs <- query_ conn "select id from users"
893-- > forM_ xs $ \(Only dbid) -> {- ... -}
897-- Conversion of SQL values to Haskell values is somewhat
898-- permissive. Here are the rules.
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.
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@.
911-- * If a numeric incompatibility is found, 'query' will throw a
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.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list