@@ -79,6 +79,9 @@ module Database.PostgreSQL.Simple
79 -- * Queries that return results
79 -- * Queries that return results
82+ -- ** Queries taking parser as argument
82 -- * Queries that stream results
85 -- * Queries that stream results
83 , FoldOptions(..)
86 , FoldOptions(..)
84 , FetchQuantity(..)
87 , FetchQuantity(..)
@@ -366,17 +369,24 @@ returning conn q qs = do
366-- using 'execute' instead of 'query').
369-- using 'execute' instead of 'query').
368-- * 'ResultError': result conversion failed.
371-- * 'ResultError': result conversion failed.
369−query :: (ToRow q, FromRow r)
372+query :: (ToRow q, FromRow r) => Connection -> Query -> q -> IO [r]
370− => Connection -> Query -> q -> IO [r]
373+query = queryWith fromRow
371−query conn template qs = do
372− result <- exec conn =<< formatQuery conn template qs
373− finishQuery conn template result
375-- | A version of 'query' that does not perform query substitution.
375-- | A version of 'query' that does not perform query substitution.
376query_ :: (FromRow r) => Connection -> Query -> IO [r]
376query_ :: (FromRow r) => Connection -> Query -> IO [r]
377+query_ = queryWith_ fromRow
379+-- | A version of 'query' taking parser as argument
380+queryWith :: ToRow q => RowParser r -> Connection -> Query -> q -> IO [r]
381+queryWith parser conn template qs = do
382+ result <- exec conn =<< formatQuery conn template qs
383+ finishQueryWith parser conn template result
385+-- | A version of 'query_' taking parser as argument
386+queryWith_ :: RowParser r -> Connection -> Query -> IO [r]
377−query_ conn q@(Query que) = do
387+queryWith_ parser conn q@(Query que) = do
378 result <- exec conn que
388 result <- exec conn que
379− finishQuery conn q result
389+ finishQueryWith parser conn q result
381-- | Perform a @SELECT@ or other SQL query that is expected to return
391-- | Perform a @SELECT@ or other SQL query that is expected to return
382-- results. Results are streamed incrementally from the server, and
392-- results. Results are streamed incrementally from the server, and
@@ -541,8 +551,11 @@ forM' lo hi m = loop hi []
542 loop (n-1) (a:as)
552 loop (n-1) (a:as)
544−finishQuery :: (FromRow r) => Connection -> Query -> PQ.Result -> IO [r]
554+finishQuery :: FromRow r => Connection -> Query -> PQ.Result -> IO [r]
555+finishQuery = finishQueryWith fromRow
557+finishQueryWith :: RowParser r -> Connection -> Query -> PQ.Result -> IO [r]
545−finishQuery conn q result = do
558+finishQueryWith parser conn q result = do
546 status <- PQ.resultStatus result
559 status <- PQ.resultStatus result
547 case status of
560 case status of
548 PQ.EmptyQuery ->
561 PQ.EmptyQuery ->
@@ -555,7 +568,7 @@ finishQuery conn q result = do
555 ncols <- PQ.nfields result
568 ncols <- PQ.nfields result
556 forM' 0 (nrows-1) $ \row -> do
569 forM' 0 (nrows-1) $ \row -> do
557 let rw = Row row result
570 let rw = Row row result
558− okvc <- runConversion (runStateT (runReaderT (unRP fromRow) rw) 0) conn
571+ okvc <- runConversion (runStateT (runReaderT (unRP parser) rw) 0) conn
559 case okvc of
572 case okvc of
560 Ok (val,col) | col == ncols -> return val
573 Ok (val,col) | col == ncols -> return val
561 | otherwise -> do
574 | otherwise -> do