on dev help sign in
use indexed monad
2ed537bEitan Chatav
squeal-postgresql/exe/Example.hs+21−10
@@ -7,8 +7,9 @@
7 , TypeOperators
7 , TypeOperators
8#-}
8#-}
9
9
10−module Main (main) where
10+module Main (main, main2) where
11
11
12+import Control.Monad (void)
12import Control.Monad.Base (liftBase)
13import Control.Monad.Base (liftBase)
13import Data.Int (Int32)
14import Data.Int (Int32)
14import Data.Monoid ((<>))
15import Data.Monoid ((<>))
@@ -86,6 +87,17 @@ users =
86 , User "Carole" (Just "carole@hotmail.com")
87 , User "Carole" (Just "carole@hotmail.com")
87 ]
88 ]
88
89
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+
89main :: IO ()
101main :: IO ()
90main = do
102main = do
91 Char8.putStrLn "squeal"
103 Char8.putStrLn "squeal"
@@ -95,15 +107,14 @@ main = do
95 connection0 <- connectdb connectionString
107 connection0 <- connectdb connectionString
96 Char8.putStrLn "setting up schema"
108 Char8.putStrLn "setting up schema"
97 connection1 <- execPQ (define setup) connection0
109 connection1 <- execPQ (define setup) connection0
98− connection2 <- flip execPQ connection1 $ do
110+ connection2 <- execPQ session connection1
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])
107 Char8.putStrLn "tearing down schema"
111 Char8.putStrLn "tearing down schema"
108 connection3 <- execPQ (define teardown) connection2
112 connection3 <- execPQ (define teardown) connection2
109 finish connection3
113 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 @@
147-- Now we can put together all the pieces into a program. The program
147-- Now we can put together all the pieces into a program. The program
148-- connects to the database, sets up the schema, inserts the user data
148-- connects to the database, sets up the schema, inserts the user data
149-- (using prepared statements as an optimization), queries the user
149-- (using prepared statements as an optimization), queries the user
150−-- data and prints it out and finally closes the connection. Since the
150+-- data and prints it out and finally closes the connection. We can thread
151−-- `Connection` tracks the schema of the database, we get a new one
151+-- the changing schema information through by using the indexed `PQ` monad
152−-- when we execute our statements.
152+-- transformer and when the schema doesn't change we can use `Monad` and
153+-- `MonadPQ` functionality.
153--
154--
154-- > main :: IO ()
155-- > main :: IO ()
155−-- > main = do
156+-- > main = void $
156−-- > connection0 <- connectdb "host=localhost port=5432 dbname=exampledb"
157+-- > withConnection "host=localhost port=5432 dbname=exampledb" . runPQ $
157−-- > connection1 <- execPQ (define setup) connection0
158+-- > define setup
158−-- > connection2 <- flip execPQ connection1 $ do
159+-- > & pqThen session
160+-- > & thenDefine teardown
161+-- > where
162+-- > session = do
159−-- > idResults <- traversePrepared insertUser (Only . userName <$> users)
163+-- > idResults <- traversePrepared insertUser (Only . userName <$> users)
160−-- > ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
164+-- > ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
161−-- > traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
165+-- > traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
162−-- > usersResult <- runQuery getUsers
166+-- > usersResult <- runQuery getUsers
163−-- > usersRows <- getRows usersResult
167+-- > usersRows <- getRows usersResult
164−-- > liftBase $ print (usersRows :: [User])
168+-- > liftBase $ print (usersRows :: [User])
165−-- > connection3 <- execPQ (define teardown) connection2
166−-- > finish connection3
167
169
168module Squeal.PostgreSQL
170module Squeal.PostgreSQL
169 ( module Squeal.PostgreSQL.Binary
171 ( module Squeal.PostgreSQL.Binary
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+5−4
@@ -141,13 +141,14 @@ finish = liftBase . LibPQ.finish . unConnection
141
141
142-- | Do `connectdb` and `finish` before and after a computation.
142-- | Do `connectdb` and `finish` before and after a computation.
143withConnection
143withConnection
144− :: forall schema io x
144+ :: forall schema0 schema1 io x
145 . MonadBaseControl IO io
145 . MonadBaseControl IO io
146 => ByteString
146 => ByteString
147− -> (Connection schema -> io x)
147+ -> (Connection schema0 -> io (x, Connection schema1))
148 -> io x
148 -> io x
149+withConnection connString action = do
149−withConnection connString = bracket (connectdb connString) finish
150+ (x, _conn) <- bracket (connectdb connString) finish action
150−
151+ return x
151
152
152-- | We keep track of the schema via an Atkey indexed state monad transformer,
153-- | We keep track of the schema via an Atkey indexed state monad transformer,
153-- `PQ`.
154-- `PQ`.