Created
April 23, 2010 16:06
-
-
Save NicolasT/376738 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Assumptions: | |
-- Every query returns only one integer | |
-- The DB connection is somewhere global (should be hidden using a State | |
-- transformer in a real implementation) | |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
module Transaction | |
( | |
Transaction | |
, runTransaction | |
, query | |
) where | |
newtype Transaction a = Transaction { runTransaction :: IO a } | |
deriving (Monad) | |
-- Demonstrate we can do IO inside a transaction, but only from functions | |
-- we defined | |
query :: String -> Transaction Int | |
query q = Transaction ( putStrLn "Performing Query" >> return 123 ) | |
main = runTransaction(do | |
i <- query "SELECT * FROM foo" | |
-- The following line is not allowed: compilation error | |
-- putStrLn "Hello" | |
query $ "UPDATE foo SET a = " ++ (show $ i + 1) | |
return $ i + 2 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment