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 14:01:58 +03:00
|
|
|
runSessionOnLocalDb,
|
2024-04-20 13:46:51 +03:00
|
|
|
runStatementInSession,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Hasql.Connection qualified as Connection
|
|
|
|
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
|
|
|
|
|
|
|
|
runStatementInSession :: Statement.Statement a b -> a -> Session.Session b
|
|
|
|
runStatementInSession statement params =
|
|
|
|
Session.statement params statement
|