Make Connection parameter go last in query functions

This commit is contained in:
2024-07-03 10:09:38 +02:00
parent ef52bd9235
commit 2b2a048197
4 changed files with 32 additions and 29 deletions
+13 -10
View File
@@ -6,6 +6,9 @@
module Database.PostgreSQL.Opium
-- * Queries
--
-- Functions for performing queries. @fetch@ retrieves rows, @execute@ doesn't.
-- The 'Connection' parameter comes last to facilitate currying for implicitly passing in the connection, e.g. from some framework's connection pool.
--
-- | TODO: Add @newtype Query = Query Text@ with @IsString@ instance to make constructing query strings at run time harder.
( fetch
, fetch_
@@ -45,29 +48,29 @@ import Database.PostgreSQL.Opium.ToParamList (ToParamList (..))
-- The order of the type parameters is important, because it is more common to use type applications for providing the row type.
fetch
:: forall a b. (ToParamList b, FromRow a)
=> Connection
-> Text
=> Text
-> b
-> Connection
-> IO (Either Error [a])
fetch conn query params = runExceptT $ do
fetch query params conn = runExceptT $ do
result <- execParams conn query params
columnTable <- ExceptT $ getColumnTable @a Proxy result
nRows <- liftIO $ LibPQ.ntuples result
mapM (ExceptT . fromRow result columnTable) [0..nRows - 1]
fetch_ :: forall a. FromRow a => Connection -> Text -> IO (Either Error [a])
fetch_ conn query = fetch conn query ()
fetch_ :: forall a. FromRow a => Text -> Connection -> IO (Either Error [a])
fetch_ query = fetch query ()
execute
:: forall a. ToParamList a
=> Connection
-> Text
=> Text
-> a
-> Connection
-> IO (Either Error ())
execute conn query params = runExceptT $ void $ execParams conn query params
execute query params conn = runExceptT $ void $ execParams conn query params
execute_ :: Connection -> Text -> IO (Either Error ())
execute_ conn query = execute conn query ()
execute_ :: Text -> Connection -> IO (Either Error ())
execute_ query = execute query ()
execParams
:: ToParamList a