on dev help sign in
use indexed monad
commit2ed537b0d20d7abb17fc3549970af3a5c5e71013 authorEitan Chatav <eitan.chatav@gmail.com> dateThu 31 Aug 2017, 13:43 (9 years ago) parent535e444
3 files changed+43−29
squeal-postgresql/exe/Example.hs+21−10 squeal-postgresql/src/Squeal/PostgreSQL.hs+17−15 squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+5−4
squeal-postgresql/exe/Example.hs+21−10
@@ -7,8 +7,9 @@
77 , TypeOperators
88#-}
99
10−module Main (main) where
10+module Main (main, main2) where
1111
12+import Control.Monad (void)
1213import Control.Monad.Base (liftBase)
1314import Data.Int (Int32)
1415import Data.Monoid ((<>))
@@ -86,6 +87,17 @@ users =
8687 , User "Carole" (Just "carole@hotmail.com")
8788 ]
8889
90+session :: PQ Schema Schema IO ()
91+session = do
92+ liftBase $ Char8.putStrLn "manipulating"
93+ idResults <- traversePrepared insertUser (Only . userName <$> users)
94+ ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
95+ traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
96+ liftBase $ Char8.putStrLn "querying"
97+ usersResult <- runQuery getUsers
98+ usersRows <- getRows usersResult
99+ liftBase $ print (usersRows :: [User])
100+
89101main :: IO ()
90102main = do
91103 Char8.putStrLn "squeal"
@@ -95,15 +107,14 @@ main = do
95107 connection0 <- connectdb connectionString
96108 Char8.putStrLn "setting up schema"
97109 connection1 <- execPQ (define setup) connection0
98− connection2 <- flip execPQ connection1 $ do
99− liftBase $ Char8.putStrLn "manipulating"
100− idResults <- traversePrepared insertUser (Only . userName <$> users)
101− ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
102− traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
103− liftBase $ Char8.putStrLn "querying"
104− usersResult <- runQuery getUsers
105− usersRows <- getRows usersResult
106− liftBase $ print (usersRows :: [User])
110+ connection2 <- execPQ session connection1
107111 Char8.putStrLn "tearing down schema"
108112 connection3 <- execPQ (define teardown) connection2
109113 finish connection3
114+
115+main2 :: IO ()
116+main2 = void $
117+ withConnection "host=localhost port=5432 dbname=exampledb" . runPQ $
118+ define setup
119+ & pqThen session
120+ & thenDefine teardown
squeal-postgresql/src/Squeal/PostgreSQL.hs+17−15
@@ -147,23 +147,25 @@
147147-- Now we can put together all the pieces into a program. The program
148148-- connects to the database, sets up the schema, inserts the user data
149149-- (using prepared statements as an optimization), queries the user
150−-- data and prints it out and finally closes the connection. Since the
151−-- `Connection` tracks the schema of the database, we get a new one
152−-- when we execute our statements.
150+-- data and prints it out and finally closes the connection. We can thread
151+-- the changing schema information through by using the indexed `PQ` monad
152+-- transformer and when the schema doesn't change we can use `Monad` and
153+-- `MonadPQ` functionality.
153154--
154155-- > main :: IO ()
155−-- > main = do
156−-- > connection0 <- connectdb "host=localhost port=5432 dbname=exampledb"
157−-- > connection1 <- execPQ (define setup) connection0
158−-- > connection2 <- flip execPQ connection1 $ do
159−-- > idResults <- traversePrepared insertUser (Only . userName <$> users)
160−-- > ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
161−-- > traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
162−-- > usersResult <- runQuery getUsers
163−-- > usersRows <- getRows usersResult
164−-- > liftBase $ print (usersRows :: [User])
165−-- > connection3 <- execPQ (define teardown) connection2
166−-- > finish connection3
156+-- > main = void $
157+-- > withConnection "host=localhost port=5432 dbname=exampledb" . runPQ $
158+-- > define setup
159+-- > & pqThen session
160+-- > & thenDefine teardown
161+-- > where
162+-- > session = do
163+-- > idResults <- traversePrepared insertUser (Only . userName <$> users)
164+-- > ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
165+-- > traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
166+-- > usersResult <- runQuery getUsers
167+-- > usersRows <- getRows usersResult
168+-- > liftBase $ print (usersRows :: [User])
167169
168170module Squeal.PostgreSQL
169171 ( module Squeal.PostgreSQL.Binary
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+5−4
@@ -141,13 +141,14 @@ finish = liftBase . LibPQ.finish . unConnection
141141
142142-- | Do `connectdb` and `finish` before and after a computation.
143143withConnection
144− :: forall schema io x
144+ :: forall schema0 schema1 io x
145145 . MonadBaseControl IO io
146146 => ByteString
147− -> (Connection schema -> io x)
147+ -> (Connection schema0 -> io (x, Connection schema1))
148148 -> io x
149−withConnection connString = bracket (connectdb connString) finish
150−
149+withConnection connString action = do
150+ (x, _conn) <- bracket (connectdb connString) finish action
151+ return x
151152
152153-- | We keep track of the schema via an Atkey indexed state monad transformer,
153154-- `PQ`.