hasql/tasty/Main/DSL.hs

42 lines
1.2 KiB
Haskell
Raw Normal View History

2015-11-15 12:13:45 +03:00
module Main.DSL where
import Main.Prelude
import qualified Hasql as H
import qualified Hasql.Serialization as HS
import qualified Hasql.Deserialization as HD
newtype Session a =
Session (ReaderT H.Connection (EitherT H.ResultsError IO) a)
deriving (Functor, Applicative, Monad, MonadIO)
data SessionError =
2015-11-16 20:03:33 +03:00
ConnectionError (H.ConnectionError) |
2015-11-15 12:13:45 +03:00
ResultsError (H.ResultsError)
deriving (Show, Eq)
session :: Session a -> IO (Either SessionError a)
session (Session impl) =
runEitherT $ acquire >>= \connection -> use connection <* release connection
where
acquire =
2015-11-16 20:03:33 +03:00
EitherT $ fmap (mapLeft ConnectionError) $ H.connect settings
2015-11-15 12:13:45 +03:00
where
settings =
H.ParametricSettings host port user password database
where
host = "localhost"
port = 5432
user = "postgres"
password = ""
database = "postgres"
use connection =
bimapEitherT ResultsError id $
runReaderT impl connection
release connection =
2015-11-16 20:03:33 +03:00
lift $ H.disconnect connection
2015-11-15 12:13:45 +03:00
query :: a -> H.Query a b -> Session b
query params query =
Session $ ReaderT $ \connection -> EitherT $ H.query connection query params