on dev help sign in
Merge pull request #1 from morphismtech/with-conn

With conn

commitb6520e9f4c8b1c9c35c21b07ee330f93efa579de authorEitan Chatav <eitan.chatav@gmail.com> dateThu 31 Aug 2017, 15:37 (9 years ago) parent535e444f399510
5 files changed+47−32
squeal-postgresql/exe/Example.hs+23−11 squeal-postgresql/squeal-postgresql.cabal+1−1 squeal-postgresql/src/Squeal/PostgreSQL.hs+17−15 squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+1−1 squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+5−4
squeal-postgresql/exe/Example.hs+23−11
@@ -1,15 +1,17 @@
11{-# LANGUAGE
22 DataKinds
33 , DeriveGeneric
4+ , FlexibleContexts
45 , OverloadedLabels
56 , OverloadedStrings
67 , TypeApplications
78 , TypeOperators
89#-}
910
10−module Main (main) where
11+module Main (main, main2) where
1112
12−import Control.Monad.Base (liftBase)
13+import Control.Monad (void)
14+import Control.Monad.Base (liftBase, MonadBase)
1315import Data.Int (Int32)
1416import Data.Monoid ((<>))
1517import Data.Text (Text)
@@ -86,6 +88,17 @@ users =
8688 , User "Carole" (Just "carole@hotmail.com")
8789 ]
8890
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+
89102main :: IO ()
90103main = do
91104 Char8.putStrLn "squeal"
@@ -95,15 +108,14 @@ main = do
95108 connection0 <- connectdb connectionString
96109 Char8.putStrLn "setting up schema"
97110 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])
111+ connection2 <- execPQ session connection1
107112 Char8.putStrLn "tearing down schema"
108113 connection3 <- execPQ (define teardown) connection2
109114 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 @@
11name: squeal-postgresql
2−version: 0.1.0.0
2+version: 0.1.1.0
33synopsis: Squeal PostgreSQL Library
44description: Squeal is a type-safe embedding of PostgreSQL in Haskell
55homepage: https://github.com/morphismtech/squeal
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/Definition.hs+1−1
@@ -569,5 +569,5 @@ dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
569569-- in renderDefinition definition
570570-- :}
571571-- "ALTER TABLE tab ALTER COLUMN col TYPE numeric NOT NULL;"
572−alterType :: TypeExpression ty -> AlterColumn ty0 ty1
572+alterType :: TypeExpression ty -> AlterColumn ty0 ty
573573alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderTypeExpression ty
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`.