on dev help sign in
pools
d1181e4Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs−11
@@ -49,8 +49,6 @@ module Squeal.PostgreSQL.PQ
49 , evalPQ
49 , evalPQ
50 , IndexedMonadTransPQ (..)
50 , IndexedMonadTransPQ (..)
51 , MonadPQ (..)
51 , MonadPQ (..)
52− , PQRun
53− , pqliftWith
54 -- * Results
52 -- * Results
55 , LibPQ.Result
53 , LibPQ.Result
56 , LibPQ.Row
54 , LibPQ.Row
@@ -507,15 +505,6 @@ instance (MonadUnliftIO m, schemas0 ~ schemas1)
507 withRunInIO $ \(run :: (forall x . m x -> IO x)) ->
505 withRunInIO $ \(run :: (forall x . m x -> IO x)) ->
508 K <$> inner (\pq -> run $ unK <$> unPQ pq conn)
506 K <$> inner (\pq -> run $ unK <$> unPQ pq conn)
509
507
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−
519-- | Get a row corresponding to a given row number from a `LibPQ.Result`,
508-- | Get a row corresponding to a given row number from a `LibPQ.Result`,
520-- throwing an exception if the row number is out of bounds.
509-- throwing an exception if the row number is out of bounds.
521getRow
510getRow
squeal-postgresql/src/Squeal/PostgreSQL/Pool.hs+17−26
@@ -27,8 +27,6 @@ module Squeal.PostgreSQL.Pool
27 , createConnectionPool
27 , createConnectionPool
28 , Pool
28 , Pool
29 , destroyAllResources
29 , destroyAllResources
30− , PoolPQRun
31− , poolpqliftWith
32 ) where
30 ) where
33
31
34import Control.Monad.Trans
32import Control.Monad.Trans
@@ -49,21 +47,23 @@ Typical use case would be to create your pool using `createConnectionPool` and r
49
47
50Here's a simplified example:
48Here's a simplified example:
51
49
52−> type Schema = '[ "tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint2])]
50+>>> import Squeal.PostgreSQL
53−>
51+>>> :{
54−> someQuery :: Manipulation Schema '[ 'NotNull 'PGint2] '[]
52+do
55−> someQuery = insertRow #tab
53+ let
56−> (Set (param @1) `As` #col :* Nil)
54+ query :: Query_ (Public '[]) () (Only Char)
57−> OnConflictDoNothing (Returning Nil)
55+ query = values_ $ literal 'a' `as` #fromOnly
58−>
56+ session :: PoolPQ (Public '[]) IO Char
59−> insertOne :: (MonadBaseControl IO m, MonadPQ Schema m) => m ()
57+ session = do
60−> insertOne = void $ manipulateParams someQuery . Only $ (1 :: Int16)
58+ result <- runQuery query
61−>
59+ Just (Only chr) <- firstRow result
62−> insertOneInPool :: ByteString -> IO ()
60+ return chr
63−> insertOneInPool connectionString = do
64−> pool <- createConnectionPool connectionString 1 0.5 10
61+ pool <- createConnectionPool "host=localhost port=5432 dbname=exampledb" 1 0.5 10
65−> liftIO $ runPoolPQ (insertOne) pool
62+ chr <- runPoolPQ session pool
66−
63+ destroyAllResources pool
64+ putChar chr
65+:}
66+a
67-}
67-}
68newtype PoolPQ (schemas :: SchemasType) m x =
68newtype PoolPQ (schemas :: SchemasType) m x =
69 PoolPQ { runPoolPQ :: Pool (K Connection schemas) -> m x }
69 PoolPQ { runPoolPQ :: Pool (K Connection schemas) -> m x }
@@ -152,12 +152,3 @@ instance (MonadUnliftIO m)
152 withRunInIO inner = PoolPQ $ \pool ->
152 withRunInIO inner = PoolPQ $ \pool ->
153 withRunInIO $ \(run :: (forall x . m x -> IO x)) ->
153 withRunInIO $ \(run :: (forall x . m x -> IO x)) ->
154 inner (\poolpq -> run $ runPoolPQ poolpq pool)
154 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)