on dev help sign in
pools
commitd1181e45e73f5cba250269230d8617e66654fb7e authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateFri 29 Mar 2019, 15:55 (8 years ago) parentdf3162b
2 files changed+17−37
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs−11 squeal-postgresql/src/Squeal/PostgreSQL/Pool.hs+17−26
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs−11
@@ -49,8 +49,6 @@ module Squeal.PostgreSQL.PQ
4949 , evalPQ
5050 , IndexedMonadTransPQ (..)
5151 , MonadPQ (..)
52− , PQRun
53− , pqliftWith
5452 -- * Results
5553 , LibPQ.Result
5654 , LibPQ.Row
@@ -507,15 +505,6 @@ instance (MonadUnliftIO m, schemas0 ~ schemas1)
507505 withRunInIO $ \(run :: (forall x . m x -> IO x)) ->
508506 K <$> inner (\pq -> run $ unK <$> unPQ pq conn)
509507
510−-- | A snapshot of the state of a `PQ` computation.
511−type PQRun schemas =
512− forall m x. Monad m => PQ schemas schemas m x -> m (K x schemas)
513−
514−-- | Helper function in defining `MonadBaseControl` instance for `PQ`.
515−pqliftWith :: Functor m => (PQRun schemas -> m a) -> PQ schemas schemas m a
516−pqliftWith f = PQ $ \ conn ->
517− fmap K (f $ \ pq -> unPQ pq conn)
518−
519508-- | Get a row corresponding to a given row number from a `LibPQ.Result`,
520509-- throwing an exception if the row number is out of bounds.
521510getRow
squeal-postgresql/src/Squeal/PostgreSQL/Pool.hs+17−26
@@ -27,8 +27,6 @@ module Squeal.PostgreSQL.Pool
2727 , createConnectionPool
2828 , Pool
2929 , destroyAllResources
30− , PoolPQRun
31− , poolpqliftWith
3230 ) where
3331
3432import Control.Monad.Trans
@@ -49,21 +47,23 @@ Typical use case would be to create your pool using `createConnectionPool` and r
4947
5048Here's a simplified example:
5149
52−> type Schema = '[ "tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint2])]
53−>
54−> someQuery :: Manipulation Schema '[ 'NotNull 'PGint2] '[]
55−> someQuery = insertRow #tab
56−> (Set (param @1) `As` #col :* Nil)
57−> OnConflictDoNothing (Returning Nil)
58−>
59−> insertOne :: (MonadBaseControl IO m, MonadPQ Schema m) => m ()
60−> insertOne = void $ manipulateParams someQuery . Only $ (1 :: Int16)
61−>
62−> insertOneInPool :: ByteString -> IO ()
63−> insertOneInPool connectionString = do
64−> pool <- createConnectionPool connectionString 1 0.5 10
65−> liftIO $ runPoolPQ (insertOne) pool
66−
50+>>> import Squeal.PostgreSQL
51+>>> :{
52+do
53+ let
54+ query :: Query_ (Public '[]) () (Only Char)
55+ query = values_ $ literal 'a' `as` #fromOnly
56+ session :: PoolPQ (Public '[]) IO Char
57+ session = do
58+ result <- runQuery query
59+ Just (Only chr) <- firstRow result
60+ return chr
61+ pool <- createConnectionPool "host=localhost port=5432 dbname=exampledb" 1 0.5 10
62+ chr <- runPoolPQ session pool
63+ destroyAllResources pool
64+ putChar chr
65+:}
66+a
6767-}
6868newtype PoolPQ (schemas :: SchemasType) m x =
6969 PoolPQ { runPoolPQ :: Pool (K Connection schemas) -> m x }
@@ -152,12 +152,3 @@ instance (MonadUnliftIO m)
152152 withRunInIO inner = PoolPQ $ \pool ->
153153 withRunInIO $ \(run :: (forall x . m x -> IO x)) ->
154154 inner (\poolpq -> run $ runPoolPQ poolpq pool)
155−
156−-- | A snapshot of the state of a `PoolPQ` computation.
157−type PoolPQRun schemas =
158− forall m x. Monad m => PoolPQ schemas m x -> m x
159−
160−-- | Helper function in defining `MonadBaseControl` instance for `PoolPQ`.
161−poolpqliftWith :: Functor m => (PoolPQRun schemas -> m a) -> PoolPQ schemas m a
162−poolpqliftWith f = PoolPQ $ \ pool ->
163− (f $ \ pq -> runPoolPQ pq pool)