@@ -24,6 +24,7 @@ of executing Squeal commands.
24 , FunctionalDependencies
24 , FunctionalDependencies
25 , FlexibleContexts
25 , FlexibleContexts
26 , FlexibleInstances
26 , FlexibleInstances
27 , OverloadedStrings
28 , OverloadedStrings
28 , RankNTypes
29 , RankNTypes
29 , ScopedTypeVariables
30 , ScopedTypeVariables
@@ -69,11 +70,10 @@ module Squeal.PostgreSQL.PQ
69 , handleSqueal
70 , handleSqueal
72−import Control.Exception.Lifted
73+import Control.Exception (Exception, throw)
73−import Control.Monad.Base
74import Control.Monad.Except
74import Control.Monad.Except
75import Control.Monad.Morph
75import Control.Monad.Morph
76−import Control.Monad.Trans.Control
76+import UnliftIO (MonadUnliftIO (..), bracket, catch, handle)
77import Data.ByteString (ByteString)
77import Data.ByteString (ByteString)
78import Data.Foldable
78import Data.Foldable
79import Data.Function ((&))
79import Data.Function ((&))
@@ -132,19 +132,19 @@ with the wrong schema!
134 :: forall schemas io
134 :: forall schemas io
135− . MonadBase IO io
135+ . MonadIO io
136 => ByteString -- ^ conninfo
136 => ByteString -- ^ conninfo
137 -> io (K LibPQ.Connection schemas)
137 -> io (K LibPQ.Connection schemas)
138−connectdb = fmap K . liftBase . LibPQ.connectdb
138+connectdb = fmap K . liftIO . LibPQ.connectdb
140-- | Closes the connection to the server.
140-- | Closes the connection to the server.
141−finish :: MonadBase IO io => K LibPQ.Connection schemas -> io ()
141+finish :: MonadIO io => K LibPQ.Connection schemas -> io ()
142−finish = liftBase . LibPQ.finish . unK
142+finish = liftIO . LibPQ.finish . unK
144-- | Do `connectdb` and `finish` before and after a computation.
144-- | Do `connectdb` and `finish` before and after a computation.
145withConnection
145withConnection
146 :: forall schemas0 schemas1 io x
146 :: forall schemas0 schemas1 io x
147− . MonadBaseControl IO io
147+ . MonadUnliftIO io
148 => ByteString
148 => ByteString
149 -> PQ schemas0 schemas1 io x
149 -> PQ schemas0 schemas1 io x
@@ -253,7 +253,7 @@ class IndexedMonadTransPQ pq where
254 -- @define statement1 & pqThen (define statement2) = define (statement1 >>> statement2)@
254 -- @define statement1 & pqThen (define statement2) = define (statement1 >>> statement2)@
256− :: MonadBase IO io
256+ :: MonadIO io
257 => Definition schemas0 schemas1
257 => Definition schemas0 schemas1
258 -> pq schemas0 schemas1 io (K LibPQ.Result '[])
258 -> pq schemas0 schemas1 io (K LibPQ.Result '[])
@@ -273,7 +273,7 @@ instance IndexedMonadTransPQ PQ where
273 return $ K x
273 return $ K x
275 define (UnsafeDefinition q) = PQ $ \ (K conn) -> do
275 define (UnsafeDefinition q) = PQ $ \ (K conn) -> do
276− resultMaybe <- liftBase $ LibPQ.exec conn q
276+ resultMaybe <- liftIO $ LibPQ.exec conn q
277 case resultMaybe of
277 case resultMaybe of
278 Nothing -> throw $ ResultException
278 Nothing -> throw $ ResultException
279 "define: LibPQ.exec returned no results"
279 "define: LibPQ.exec returned no results"
@@ -392,7 +392,7 @@ class Monad pq => MonadPQ schemas pq | pq -> schemas where
392 => (LibPQ.Connection -> IO a) -> pq a
392 => (LibPQ.Connection -> IO a) -> pq a
393 liftPQ = lift . liftPQ
393 liftPQ = lift . liftPQ
395−instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
395+instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
396 => MonadPQ schemas (PQ schemas0 schemas1 io) where
396 => MonadPQ schemas (PQ schemas0 schemas1 io) where
398 manipulateParams
398 manipulateParams
@@ -403,7 +403,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
403 (LibPQ.invalidOid, encodingBytes encoding, LibPQ.Binary)
403 (LibPQ.invalidOid, encodingBytes encoding, LibPQ.Binary)
404 params' = fmap (fmap toParam') (hcollapse (toParams @x @ps params))
404 params' = fmap (fmap toParam') (hcollapse (toParams @x @ps params))
405 q' = q <> ";"
405 q' = q <> ";"
406− resultMaybe <- liftBase $ LibPQ.execParams conn q' params' LibPQ.Binary
406+ resultMaybe <- liftIO $ LibPQ.execParams conn q' params' LibPQ.Binary
407 case resultMaybe of
407 case resultMaybe of
408 Nothing -> throw $ ResultException
408 Nothing -> throw $ ResultException
409 "manipulateParams: LibPQ.execParams returned no results"
409 "manipulateParams: LibPQ.execParams returned no results"
@@ -413,7 +413,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
414 traversePrepared
414 traversePrepared
415 (UnsafeManipulation q :: Manipulation '[] schemas xs ys) (list :: list x) =
415 (UnsafeManipulation q :: Manipulation '[] schemas xs ys) (list :: list x) =
416− PQ $ \ (K conn) -> liftBase $ do
416+ PQ $ \ (K conn) -> liftIO $ do
417 let temp = "temporary_statement"
417 let temp = "temporary_statement"
418 prepResultMaybe <- LibPQ.prepare conn temp q Nothing
418 prepResultMaybe <- LibPQ.prepare conn temp q Nothing
419 case prepResultMaybe of
419 case prepResultMaybe of
@@ -440,7 +440,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
441 traversePrepared_
441 traversePrepared_
442 (UnsafeManipulation q :: Manipulation '[] schemas xs '[]) (list :: list x) =
442 (UnsafeManipulation q :: Manipulation '[] schemas xs '[]) (list :: list x) =
443− PQ $ \ (K conn) -> liftBase $ do
443+ PQ $ \ (K conn) -> liftIO $ do
444 let temp = "temporary_statement"
444 let temp = "temporary_statement"
445 prepResultMaybe <- LibPQ.prepare conn temp q Nothing
445 prepResultMaybe <- LibPQ.prepare conn temp q Nothing
446 case prepResultMaybe of
446 case prepResultMaybe of
@@ -464,7 +464,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
464 return (K ())
464 return (K ())
466 liftPQ pq = PQ $ \ (K conn) -> do
466 liftPQ pq = PQ $ \ (K conn) -> do
467− y <- liftBase $ pq conn
467+ y <- liftIO $ pq conn
468 return (K y)
468 return (K y)
470instance MonadPQ schemas m => MonadPQ schemas (IdentityT m)
470instance MonadPQ schemas m => MonadPQ schemas (IdentityT m)
@@ -505,14 +505,19 @@ instance schemas0 ~ schemas1 => MMonad (PQ schemas0 schemas1) where
505 embed f (PQ pq) = PQ $ \ conn -> do
505 embed f (PQ pq) = PQ $ \ conn -> do
506 evalPQ (f (pq conn)) conn
506 evalPQ (f (pq conn)) conn
508−instance (MonadBase b m, schemas0 ~ schemas1)
509− => MonadBase b (PQ schemas0 schemas1 m) where
510− liftBase = lift . liftBase
512instance (MonadIO m, schema0 ~ schema1)
508instance (MonadIO m, schema0 ~ schema1)
513 => MonadIO (PQ schema0 schema1 m) where
509 => MonadIO (PQ schema0 schema1 m) where
514 liftIO = lift . liftIO
510 liftIO = lift . liftIO
512+instance (MonadUnliftIO m, schemas0 ~ schemas1)
513+ => MonadUnliftIO (PQ schemas0 schemas1 m) where
515+ :: ((forall a . PQ schemas0 schema1 m a -> IO a) -> IO b)
516+ -> PQ schemas0 schema1 m b
517+ withRunInIO inner = PQ $ \conn ->
518+ withRunInIO $ \(run :: (forall x . m x -> IO x)) ->
519+ K <$> inner (\pq -> run $ unK <$> unPQ pq conn)
516-- | A snapshot of the state of a `PQ` computation.
521-- | A snapshot of the state of a `PQ` computation.
517type PQRun schemas =
522type PQRun schemas =
518 forall m x. Monad m => PQ schemas schemas m x -> m (K x schemas)
523 forall m x. Monad m => PQ schemas schemas m x -> m (K x schemas)
@@ -522,23 +527,16 @@ pqliftWith :: Functor m => (PQRun schemas -> m a) -> PQ schemas schemas m a
522pqliftWith f = PQ $ \ conn ->
527pqliftWith f = PQ $ \ conn ->
523 fmap K (f $ \ pq -> unPQ pq conn)
528 fmap K (f $ \ pq -> unPQ pq conn)
525−instance (MonadBaseControl b m, schemas0 ~ schemas1)
526− => MonadBaseControl b (PQ schemas0 schemas1 m) where
527− type StM (PQ schemas0 schemas1 m) x = StM m (K x schemas0)
529− pqliftWith $ \ run -> liftBaseWith $ \ runInBase -> f $ runInBase . run
530− restoreM = PQ . const . restoreM
532-- | Get a row corresponding to a given row number from a `LibPQ.Result`,
530-- | Get a row corresponding to a given row number from a `LibPQ.Result`,
533-- throwing an exception if the row number is out of bounds.
531-- throwing an exception if the row number is out of bounds.
535− :: (FromRow columns y, MonadBase IO io)
533+ :: (FromRow columns y, MonadIO io)
536 => LibPQ.Row
534 => LibPQ.Row
537 -- ^ row number
535 -- ^ row number
538 -> K LibPQ.Result columns
536 -> K LibPQ.Result columns
539 -- ^ result
537 -- ^ result
541−getRow r (K result :: K LibPQ.Result columns) = liftBase $ do
539+getRow r (K result :: K LibPQ.Result columns) = liftIO $ do
542 numRows <- LibPQ.ntuples result
540 numRows <- LibPQ.ntuples result
543 when (numRows < r) $ throw $ ResultException $
541 when (numRows < r) $ throw $ ResultException $
544 "getRow: expected at least " <> pack (show r) <> "rows but only saw "
542 "getRow: expected at least " <> pack (show r) <> "rows but only saw "
@@ -556,13 +554,13 @@ getRow r (K result :: K LibPQ.Result columns) = liftBase $ do
556-- and a `LibPQ.Result` and given a row number if it's too large returns `Nothing`,
554-- and a `LibPQ.Result` and given a row number if it's too large returns `Nothing`,
557-- otherwise returning the row along with the next row number.
555-- otherwise returning the row along with the next row number.
559− :: (FromRow columns y, MonadBase IO io)
557+ :: (FromRow columns y, MonadIO io)
560 => LibPQ.Row -- ^ total number of rows
558 => LibPQ.Row -- ^ total number of rows
561 -> K LibPQ.Result columns -- ^ result
559 -> K LibPQ.Result columns -- ^ result
562 -> LibPQ.Row -- ^ row number
560 -> LibPQ.Row -- ^ row number
563 -> io (Maybe (LibPQ.Row,y))
561 -> io (Maybe (LibPQ.Row,y))
564nextRow total (K result :: K LibPQ.Result columns) r
562nextRow total (K result :: K LibPQ.Result columns) r
565− = liftBase $ if r >= total then return Nothing else do
563+ = liftIO $ if r >= total then return Nothing else do
566 let len = fromIntegral (lengthSList (Proxy @columns))
564 let len = fromIntegral (lengthSList (Proxy @columns))
567 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
565 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
568 case fromList row' of
566 case fromList row' of
@@ -573,10 +571,10 @@ nextRow total (K result :: K LibPQ.Result columns) r
574-- | Get all rows from a `LibPQ.Result`.
572-- | Get all rows from a `LibPQ.Result`.
576− :: (FromRow columns y, MonadBase IO io)
574+ :: (FromRow columns y, MonadIO io)
577 => K LibPQ.Result columns -- ^ result
575 => K LibPQ.Result columns -- ^ result
578 -> io [y]
576 -> io [y]
579−getRows (K result :: K LibPQ.Result columns) = liftBase $ do
577+getRows (K result :: K LibPQ.Result columns) = liftIO $ do
580 let len = fromIntegral (lengthSList (Proxy @columns))
578 let len = fromIntegral (lengthSList (Proxy @columns))
581 numRows <- LibPQ.ntuples result
579 numRows <- LibPQ.ntuples result
582 for [0 .. numRows - 1] $ \ r -> do
580 for [0 .. numRows - 1] $ \ r -> do
@@ -589,10 +587,10 @@ getRows (K result :: K LibPQ.Result columns) = liftBase $ do
590-- | Get the first row if possible from a `LibPQ.Result`.
588-- | Get the first row if possible from a `LibPQ.Result`.
592− :: (FromRow columns y, MonadBase IO io)
590+ :: (FromRow columns y, MonadIO io)
593 => K LibPQ.Result columns -- ^ result
591 => K LibPQ.Result columns -- ^ result
594 -> io (Maybe y)
592 -> io (Maybe y)
595−firstRow (K result :: K LibPQ.Result columns) = liftBase $ do
593+firstRow (K result :: K LibPQ.Result columns) = liftIO $ do
596 numRows <- LibPQ.ntuples result
594 numRows <- LibPQ.ntuples result
597 if numRows <= 0 then return Nothing else do
595 if numRows <= 0 then return Nothing else do
598 let len = fromIntegral (lengthSList (Proxy @columns))
596 let len = fromIntegral (lengthSList (Proxy @columns))
@@ -605,23 +603,23 @@ firstRow (K result :: K LibPQ.Result columns) = liftBase $ do
606-- | Lifts actions on results from @LibPQ@.
604-- | Lifts actions on results from @LibPQ@.
607liftResult
605liftResult
608− :: MonadBase IO io
606+ :: MonadIO io
609 => (LibPQ.Result -> IO x)
607 => (LibPQ.Result -> IO x)
610 -> K LibPQ.Result results -> io x
608 -> K LibPQ.Result results -> io x
611−liftResult f (K result) = liftBase $ f result
609+liftResult f (K result) = liftIO $ f result
613-- | Returns the number of rows (tuples) in the query result.
611-- | Returns the number of rows (tuples) in the query result.
614−ntuples :: MonadBase IO io => K LibPQ.Result columns -> io LibPQ.Row
612+ntuples :: MonadIO io => K LibPQ.Result columns -> io LibPQ.Row
615ntuples = liftResult LibPQ.ntuples
613ntuples = liftResult LibPQ.ntuples
617-- | Returns the result status of the command.
615-- | Returns the result status of the command.
618−resultStatus :: MonadBase IO io => K LibPQ.Result results -> io LibPQ.ExecStatus
616+resultStatus :: MonadIO io => K LibPQ.Result results -> io LibPQ.ExecStatus
619resultStatus = liftResult LibPQ.resultStatus
617resultStatus = liftResult LibPQ.resultStatus
621-- | Returns the error message most recently generated by an operation
619-- | Returns the error message most recently generated by an operation
622-- on the connection.
620-- on the connection.
623resultErrorMessage
621resultErrorMessage
624− :: MonadBase IO io => K LibPQ.Result results -> io (Maybe ByteString)
622+ :: MonadIO io => K LibPQ.Result results -> io (Maybe ByteString)
625resultErrorMessage = liftResult LibPQ.resultErrorMessage
623resultErrorMessage = liftResult LibPQ.resultErrorMessage
627-- | Returns the error code most recently generated by an operation
625-- | Returns the error code most recently generated by an operation
@@ -629,7 +627,7 @@ resultErrorMessage = liftResult LibPQ.resultErrorMessage
630-- https://www.postgresql.org/docs/current/static/errcodes-appendix.html
628-- https://www.postgresql.org/docs/current/static/errcodes-appendix.html
631resultErrorCode
629resultErrorCode
632− :: MonadBase IO io
630+ :: MonadIO io
633 => K LibPQ.Result results
631 => K LibPQ.Result results
634 -> io (Maybe ByteString)
632 -> io (Maybe ByteString)
635resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
633resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
@@ -648,10 +646,10 @@ data SquealException
648instance Exception SquealException
646instance Exception SquealException
651− :: MonadBase IO io
649+ :: MonadIO io
652 => LibPQ.Result
650 => LibPQ.Result
654−tryResult result = liftBase $ do
652+tryResult result = liftIO $ do
655 status <- LibPQ.resultStatus result
653 status <- LibPQ.resultStatus result
656 case status of
654 case status of
657 LibPQ.CommandOk -> return ()
655 LibPQ.CommandOk -> return ()
@@ -663,7 +661,7 @@ tryResult result = liftBase $ do
664-- | Catch `SquealException`s.
662-- | Catch `SquealException`s.
665catchSqueal
663catchSqueal
666− :: MonadBaseControl IO io
664+ :: MonadUnliftIO io
668 -> (SquealException -> io a) -- ^ handler
666 -> (SquealException -> io a) -- ^ handler
@@ -671,7 +669,7 @@ catchSqueal = catch
672-- | Handle `SquealException`s.
670-- | Handle `SquealException`s.
673handleSqueal
671handleSqueal
674− :: MonadBaseControl IO io
672+ :: MonadUnliftIO io
675 => (SquealException -> io a) -- ^ handler
673 => (SquealException -> io a) -- ^ handler
676 -> io a -> io a
674 -> io a -> io a
677handleSqueal = handle
675handleSqueal = handle