git.iko.soy/typeable/postgresql-simple
on master help sign in
Add queryWith, a qeury taking RowParser as argument

Helps with writing higher level libraries using postgresql-simple

commit3333358d83e7a66ab62c253d347ece8b53018f87 authorLeonid Onokhov <sopvop@gmail.com> dateWed 13 Nov 2013, 11:17 (13 years ago) parentd984a4a
1 file changed+23−10
src/Database/PostgreSQL/Simple.hs+23−10
src/Database/PostgreSQL/Simple.hs+23−10
@@ -79,6 +79,9 @@ module Database.PostgreSQL.Simple
7979 -- * Queries that return results
8080 , query
8181 , query_
82+ -- ** Queries taking parser as argument
83+ , queryWith
84+ , queryWith_
8285 -- * Queries that stream results
8386 , FoldOptions(..)
8487 , FetchQuantity(..)
@@ -366,17 +369,24 @@ returning conn q qs = do
366369-- using 'execute' instead of 'query').
367370--
368371-- * 'ResultError': result conversion failed.
369−query :: (ToRow q, FromRow r)
370− => Connection -> Query -> q -> IO [r]
371−query conn template qs = do
372− result <- exec conn =<< formatQuery conn template qs
373− finishQuery conn template result
372+query :: (ToRow q, FromRow r) => Connection -> Query -> q -> IO [r]
373+query = queryWith fromRow
374374
375375-- | A version of 'query' that does not perform query substitution.
376376query_ :: (FromRow r) => Connection -> Query -> IO [r]
377−query_ conn q@(Query que) = do
377+query_ = queryWith_ fromRow
378+
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
384+
385+-- | A version of 'query_' taking parser as argument
386+queryWith_ :: RowParser r -> Connection -> Query -> IO [r]
387+queryWith_ parser conn q@(Query que) = do
378388 result <- exec conn que
379− finishQuery conn q result
389+ finishQueryWith parser conn q result
380390
381391-- | Perform a @SELECT@ or other SQL query that is expected to return
382392-- results. Results are streamed incrementally from the server, and
@@ -541,8 +551,11 @@ forM' lo hi m = loop hi []
541551 a <- m n
542552 loop (n-1) (a:as)
543553
544−finishQuery :: (FromRow r) => Connection -> Query -> PQ.Result -> IO [r]
545−finishQuery conn q result = do
554+finishQuery :: FromRow r => Connection -> Query -> PQ.Result -> IO [r]
555+finishQuery = finishQueryWith fromRow
556+
557+finishQueryWith :: RowParser r -> Connection -> Query -> PQ.Result -> IO [r]
558+finishQueryWith parser conn q result = do
546559 status <- PQ.resultStatus result
547560 case status of
548561 PQ.EmptyQuery ->
@@ -555,7 +568,7 @@ finishQuery conn q result = do
555568 ncols <- PQ.nfields result
556569 forM' 0 (nrows-1) $ \row -> do
557570 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
559572 case okvc of
560573 Ok (val,col) | col == ncols -> return val
561574 | otherwise -> do