hasql/testing-utils/Hasql/TestingUtils/TestingDsl.hs

51 lines
1.5 KiB
Haskell
Raw Normal View History

2024-04-20 13:59:11 +03:00
module Hasql.TestingUtils.TestingDsl
2024-04-20 13:46:51 +03:00
( Session.Session,
Error (..),
Session.SessionError (..),
2024-04-21 16:15:51 +03:00
Session.QueryError (..),
2024-04-20 16:40:11 +03:00
Pipeline.Pipeline,
Statement.Statement (..),
runSessionOnLocalDb,
2024-04-20 16:40:11 +03:00
runPipelineOnLocalDb,
2024-04-20 13:46:51 +03:00
runStatementInSession,
2024-04-20 16:40:11 +03:00
runPipelineInSession,
2024-04-20 13:46:51 +03:00
)
where
import Hasql.Connection qualified as Connection
2024-04-20 16:40:11 +03:00
import Hasql.Pipeline qualified as Pipeline
2024-04-20 13:46:51 +03:00
import Hasql.Session qualified as Session
import Hasql.Statement qualified as Statement
2024-04-20 13:59:11 +03:00
import Hasql.TestingUtils.Constants qualified as Constants
2024-04-20 13:46:51 +03:00
import Prelude
data Error
2024-04-20 13:46:51 +03:00
= ConnectionError (Connection.ConnectionError)
| SessionError (Session.SessionError)
2024-04-20 13:46:51 +03:00
deriving (Show, Eq)
runSessionOnLocalDb :: Session.Session a -> IO (Either Error a)
runSessionOnLocalDb session =
2024-04-20 13:46:51 +03:00
runExceptT $ acquire >>= \connection -> use connection <* release connection
where
acquire =
2024-04-20 13:59:11 +03:00
ExceptT $ fmap (mapLeft ConnectionError) $ Connection.acquire Constants.localConnectionSettings
2024-04-20 13:46:51 +03:00
use connection =
ExceptT
$ fmap (mapLeft SessionError)
$ Session.run session connection
release connection =
lift $ Connection.release connection
runPipelineOnLocalDb :: Pipeline.Pipeline a -> IO (Either Error a)
2024-04-20 16:40:11 +03:00
runPipelineOnLocalDb =
runSessionOnLocalDb . Session.pipeline
2024-04-20 13:46:51 +03:00
runStatementInSession :: Statement.Statement a b -> a -> Session.Session b
runStatementInSession statement params =
Session.statement params statement
2024-04-20 16:40:11 +03:00
runPipelineInSession :: Pipeline.Pipeline a -> Session.Session a
runPipelineInSession =
Session.pipeline