2024-04-20 13:59:11 +03:00
|
|
|
module Hasql.TestingUtils.TestingDsl
|
2024-04-20 13:46:51 +03:00
|
|
|
( Session.Session,
|
|
|
|
SessionError (..),
|
|
|
|
Session.QueryError (..),
|
|
|
|
Session.CommandError (..),
|
2024-04-20 16:40:11 +03:00
|
|
|
Pipeline.Pipeline,
|
|
|
|
Statement.Statement (..),
|
2024-04-20 14:01:58 +03:00
|
|
|
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 SessionError
|
|
|
|
= ConnectionError (Connection.ConnectionError)
|
|
|
|
| SessionError (Session.QueryError)
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2024-04-20 14:01:58 +03:00
|
|
|
runSessionOnLocalDb :: Session.Session a -> IO (Either SessionError 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
|
|
|
|
|
2024-04-20 16:40:11 +03:00
|
|
|
runPipelineOnLocalDb :: Pipeline.Pipeline a -> IO (Either SessionError a)
|
|
|
|
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
|