Skip to content

Instantly share code, notes, and snippets.

@NicolasT
Created April 23, 2010 16:06
Show Gist options
  • Save NicolasT/376738 to your computer and use it in GitHub Desktop.
Save NicolasT/376738 to your computer and use it in GitHub Desktop.
-- 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