on dev help sign in
Merge pull request #1 from morphismtech/with-conn
b6520e9Eitan Chatav
squeal-postgresql/exe/Example.hs+23−11
@@ -1,15 +1,17 @@
1{-# LANGUAGE
1{-# LANGUAGE
2 DataKinds
2 DataKinds
3 , DeriveGeneric
3 , DeriveGeneric
4+ , FlexibleContexts
4 , OverloadedLabels
5 , OverloadedLabels
5 , OverloadedStrings
6 , OverloadedStrings
6 , TypeApplications
7 , TypeApplications
7 , TypeOperators
8 , TypeOperators
8#-}
9#-}
9
10
10−module Main (main) where
11+module Main (main, main2) where
11
12
12−import Control.Monad.Base (liftBase)
13+import Control.Monad (void)
14+import Control.Monad.Base (liftBase, MonadBase)
13import Data.Int (Int32)
15import Data.Int (Int32)
14import Data.Monoid ((<>))
16import Data.Monoid ((<>))
15import Data.Text (Text)
17import Data.Text (Text)
@@ -86,6 +88,17 @@ users =
86 , User "Carole" (Just "carole@hotmail.com")
88 , User "Carole" (Just "carole@hotmail.com")
87 ]
89 ]
88
90
91+session :: (MonadBase IO pq, MonadPQ Schema pq) => pq ()
92+session = do
93+ liftBase $ Char8.putStrLn "manipulating"
94+ idResults <- traversePrepared insertUser (Only . userName <$> users)
95+ ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
96+ traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
97+ liftBase $ Char8.putStrLn "querying"
98+ usersResult <- runQuery getUsers
99+ usersRows <- getRows usersResult
100+ liftBase $ print (usersRows :: [User])
101+
89main :: IO ()
102main :: IO ()
90main = do
103main = do
91 Char8.putStrLn "squeal"
104 Char8.putStrLn "squeal"
@@ -95,15 +108,14 @@ main = do
95 connection0 <- connectdb connectionString
108 connection0 <- connectdb connectionString
96 Char8.putStrLn "setting up schema"
109 Char8.putStrLn "setting up schema"
97 connection1 <- execPQ (define setup) connection0
110 connection1 <- execPQ (define setup) connection0
98− connection2 <- flip execPQ connection1 $ do
111+ 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"
112 Char8.putStrLn "tearing down schema"
108 connection3 <- execPQ (define teardown) connection2
113 connection3 <- execPQ (define teardown) connection2
109 finish connection3
114 finish connection3
115+
116+main2 :: IO ()
117+main2 = void $
118+ withConnection "host=localhost port=5432 dbname=exampledb" . runPQ $
119+ define setup
120+ & pqThen session
121+ & thenDefine teardown
squeal-postgresql/squeal-postgresql.cabal+1−1
@@ -1,5 +1,5 @@
1name: squeal-postgresql
1name: squeal-postgresql
2−version: 0.1.0.0
2+version: 0.1.1.0
3synopsis: Squeal PostgreSQL Library
3synopsis: Squeal PostgreSQL Library
4description: Squeal is a type-safe embedding of PostgreSQL in Haskell
4description: Squeal is a type-safe embedding of PostgreSQL in Haskell
5homepage: https://github.com/morphismtech/squeal
5homepage: https://github.com/morphismtech/squeal
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/Definition.hs+1−1
@@ -569,5 +569,5 @@ dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
569-- in renderDefinition definition
569-- in renderDefinition definition
570-- :}
570-- :}
571-- "ALTER TABLE tab ALTER COLUMN col TYPE numeric NOT NULL;"
571-- "ALTER TABLE tab ALTER COLUMN col TYPE numeric NOT NULL;"
572−alterType :: TypeExpression ty -> AlterColumn ty0 ty1
572+alterType :: TypeExpression ty -> AlterColumn ty0 ty
573alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderTypeExpression ty
573alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderTypeExpression ty
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`.