free-monad-example/src/DB/Interpreter.hs
Sanchayan Maity e4c13abefa src: Refactor DB and Logger out of lib
Signed-off-by: Sanchayan Maity <maitysanchayan@gmail.com>
2020-07-28 18:27:33 +05:30

14 lines
525 B
Haskell

module DB.Interpreter where
import Control.Exception (throwIO)
import Control.Monad.Free.Church (foldF)
import Database.PostgreSQL.Simple (Connection, Query)
import DB.Language
interpretSqlDBMethod :: Connection -> Query -> SqlDBMethodF a -> IO a
interpretSqlDBMethod conn squery (SqlDBMethod runner next) = next <$> runner conn squery
interpretSqlDBMethod _ _ (SqlThrowException exception _) = throwIO exception
runDB :: Connection -> Query -> SqlDb a -> IO a
runDB conn squery = foldF $ interpretSqlDBMethod conn squery