@@ -24,6 +24,7 @@ of executing Squeal commands.
2424 , FunctionalDependencies
2525 , FlexibleContexts
2626 , FlexibleInstances
27+ , InstanceSigs
2728 , OverloadedStrings
2829 , RankNTypes
2930 , ScopedTypeVariables
@@ -69,11 +70,10 @@ module Squeal.PostgreSQL.PQ
6970 , handleSqueal
7071 ) where
7172
72−import Control.Exception.Lifted
73−import Control.Monad.Base
73+import Control.Exception (Exception, throw)
7474import Control.Monad.Except
7575import Control.Monad.Morph
76−import Control.Monad.Trans.Control
76+import UnliftIO (MonadUnliftIO (..), bracket, catch, handle)
7777import Data.ByteString (ByteString)
7878import Data.Foldable
7979import Data.Function ((&))
@@ -132,19 +132,19 @@ with the wrong schema!
132132-}
133133connectdb
134134 :: forall schemas io
135− . MonadBase IO io
135+ . MonadIO io
136136 => ByteString -- ^ conninfo
137137 -> io (K LibPQ.Connection schemas)
138−connectdb = fmap K . liftBase . LibPQ.connectdb
138+connectdb = fmap K . liftIO . LibPQ.connectdb
139139
140140-- | Closes the connection to the server.
141−finish :: MonadBase IO io => K LibPQ.Connection schemas -> io ()
142−finish = liftBase . LibPQ.finish . unK
141+finish :: MonadIO io => K LibPQ.Connection schemas -> io ()
142+finish = liftIO . LibPQ.finish . unK
143143
144144-- | Do `connectdb` and `finish` before and after a computation.
145145withConnection
146146 :: forall schemas0 schemas1 io x
147− . MonadBaseControl IO io
147+ . MonadUnliftIO io
148148 => ByteString
149149 -> PQ schemas0 schemas1 io x
150150 -> io x
@@ -253,7 +253,7 @@ class IndexedMonadTransPQ pq where
253253 --
254254 -- @define statement1 & pqThen (define statement2) = define (statement1 >>> statement2)@
255255 define
256− :: MonadBase IO io
256+ :: MonadIO io
257257 => Definition schemas0 schemas1
258258 -> pq schemas0 schemas1 io (K LibPQ.Result '[])
259259
@@ -273,7 +273,7 @@ instance IndexedMonadTransPQ PQ where
273273 return $ K x
274274
275275 define (UnsafeDefinition q) = PQ $ \ (K conn) -> do
276− resultMaybe <- liftBase $ LibPQ.exec conn q
276+ resultMaybe <- liftIO $ LibPQ.exec conn q
277277 case resultMaybe of
278278 Nothing -> throw $ ResultException
279279 "define: LibPQ.exec returned no results"
@@ -392,7 +392,7 @@ class Monad pq => MonadPQ schemas pq | pq -> schemas where
392392 => (LibPQ.Connection -> IO a) -> pq a
393393 liftPQ = lift . liftPQ
394394
395−instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
395+instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
396396 => MonadPQ schemas (PQ schemas0 schemas1 io) where
397397
398398 manipulateParams
@@ -403,7 +403,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
403403 (LibPQ.invalidOid, encodingBytes encoding, LibPQ.Binary)
404404 params' = fmap (fmap toParam') (hcollapse (toParams @x @ps params))
405405 q' = q <> ";"
406− resultMaybe <- liftBase $ LibPQ.execParams conn q' params' LibPQ.Binary
406+ resultMaybe <- liftIO $ LibPQ.execParams conn q' params' LibPQ.Binary
407407 case resultMaybe of
408408 Nothing -> throw $ ResultException
409409 "manipulateParams: LibPQ.execParams returned no results"
@@ -413,7 +413,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
413413
414414 traversePrepared
415415 (UnsafeManipulation q :: Manipulation '[] schemas xs ys) (list :: list x) =
416− PQ $ \ (K conn) -> liftBase $ do
416+ PQ $ \ (K conn) -> liftIO $ do
417417 let temp = "temporary_statement"
418418 prepResultMaybe <- LibPQ.prepare conn temp q Nothing
419419 case prepResultMaybe of
@@ -440,7 +440,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
440440
441441 traversePrepared_
442442 (UnsafeManipulation q :: Manipulation '[] schemas xs '[]) (list :: list x) =
443− PQ $ \ (K conn) -> liftBase $ do
443+ PQ $ \ (K conn) -> liftIO $ do
444444 let temp = "temporary_statement"
445445 prepResultMaybe <- LibPQ.prepare conn temp q Nothing
446446 case prepResultMaybe of
@@ -464,7 +464,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
464464 return (K ())
465465
466466 liftPQ pq = PQ $ \ (K conn) -> do
467− y <- liftBase $ pq conn
467+ y <- liftIO $ pq conn
468468 return (K y)
469469
470470instance MonadPQ schemas m => MonadPQ schemas (IdentityT m)
@@ -505,14 +505,19 @@ instance schemas0 ~ schemas1 => MMonad (PQ schemas0 schemas1) where
505505 embed f (PQ pq) = PQ $ \ conn -> do
506506 evalPQ (f (pq conn)) conn
507507
508−instance (MonadBase b m, schemas0 ~ schemas1)
509− => MonadBase b (PQ schemas0 schemas1 m) where
510− liftBase = lift . liftBase
511−
512508instance (MonadIO m, schema0 ~ schema1)
513509 => MonadIO (PQ schema0 schema1 m) where
514510 liftIO = lift . liftIO
515511
512+instance (MonadUnliftIO m, schemas0 ~ schemas1)
513+ => MonadUnliftIO (PQ schemas0 schemas1 m) where
514+ withRunInIO
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)
520+
516521-- | A snapshot of the state of a `PQ` computation.
517522type PQRun schemas =
518523 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
522527pqliftWith f = PQ $ \ conn ->
523528 fmap K (f $ \ pq -> unPQ pq conn)
524529
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)
528− liftBaseWith f =
529− pqliftWith $ \ run -> liftBaseWith $ \ runInBase -> f $ runInBase . run
530− restoreM = PQ . const . restoreM
531−
532530-- | Get a row corresponding to a given row number from a `LibPQ.Result`,
533531-- throwing an exception if the row number is out of bounds.
534532getRow
535− :: (FromRow columns y, MonadBase IO io)
533+ :: (FromRow columns y, MonadIO io)
536534 => LibPQ.Row
537535 -- ^ row number
538536 -> K LibPQ.Result columns
539537 -- ^ result
540538 -> io y
541−getRow r (K result :: K LibPQ.Result columns) = liftBase $ do
539+getRow r (K result :: K LibPQ.Result columns) = liftIO $ do
542540 numRows <- LibPQ.ntuples result
543541 when (numRows < r) $ throw $ ResultException $
544542 "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
556554-- and a `LibPQ.Result` and given a row number if it's too large returns `Nothing`,
557555-- otherwise returning the row along with the next row number.
558556nextRow
559− :: (FromRow columns y, MonadBase IO io)
557+ :: (FromRow columns y, MonadIO io)
560558 => LibPQ.Row -- ^ total number of rows
561559 -> K LibPQ.Result columns -- ^ result
562560 -> LibPQ.Row -- ^ row number
563561 -> io (Maybe (LibPQ.Row,y))
564562nextRow 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
566564 let len = fromIntegral (lengthSList (Proxy @columns))
567565 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
568566 case fromList row' of
@@ -573,10 +571,10 @@ nextRow total (K result :: K LibPQ.Result columns) r
573571
574572-- | Get all rows from a `LibPQ.Result`.
575573getRows
576− :: (FromRow columns y, MonadBase IO io)
574+ :: (FromRow columns y, MonadIO io)
577575 => K LibPQ.Result columns -- ^ result
578576 -> io [y]
579−getRows (K result :: K LibPQ.Result columns) = liftBase $ do
577+getRows (K result :: K LibPQ.Result columns) = liftIO $ do
580578 let len = fromIntegral (lengthSList (Proxy @columns))
581579 numRows <- LibPQ.ntuples result
582580 for [0 .. numRows - 1] $ \ r -> do
@@ -589,10 +587,10 @@ getRows (K result :: K LibPQ.Result columns) = liftBase $ do
589587
590588-- | Get the first row if possible from a `LibPQ.Result`.
591589firstRow
592− :: (FromRow columns y, MonadBase IO io)
590+ :: (FromRow columns y, MonadIO io)
593591 => K LibPQ.Result columns -- ^ result
594592 -> io (Maybe y)
595−firstRow (K result :: K LibPQ.Result columns) = liftBase $ do
593+firstRow (K result :: K LibPQ.Result columns) = liftIO $ do
596594 numRows <- LibPQ.ntuples result
597595 if numRows <= 0 then return Nothing else do
598596 let len = fromIntegral (lengthSList (Proxy @columns))
@@ -605,23 +603,23 @@ firstRow (K result :: K LibPQ.Result columns) = liftBase $ do
605603
606604-- | Lifts actions on results from @LibPQ@.
607605liftResult
608− :: MonadBase IO io
606+ :: MonadIO io
609607 => (LibPQ.Result -> IO x)
610608 -> K LibPQ.Result results -> io x
611−liftResult f (K result) = liftBase $ f result
609+liftResult f (K result) = liftIO $ f result
612610
613611-- | 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
615613ntuples = liftResult LibPQ.ntuples
616614
617615-- | 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
619617resultStatus = liftResult LibPQ.resultStatus
620618
621619-- | Returns the error message most recently generated by an operation
622620-- on the connection.
623621resultErrorMessage
624− :: MonadBase IO io => K LibPQ.Result results -> io (Maybe ByteString)
622+ :: MonadIO io => K LibPQ.Result results -> io (Maybe ByteString)
625623resultErrorMessage = liftResult LibPQ.resultErrorMessage
626624
627625-- | Returns the error code most recently generated by an operation
@@ -629,7 +627,7 @@ resultErrorMessage = liftResult LibPQ.resultErrorMessage
629627--
630628-- https://www.postgresql.org/docs/current/static/errcodes-appendix.html
631629resultErrorCode
632− :: MonadBase IO io
630+ :: MonadIO io
633631 => K LibPQ.Result results
634632 -> io (Maybe ByteString)
635633resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
@@ -648,10 +646,10 @@ data SquealException
648646instance Exception SquealException
649647
650648tryResult
651− :: MonadBase IO io
649+ :: MonadIO io
652650 => LibPQ.Result
653651 -> io ()
654−tryResult result = liftBase $ do
652+tryResult result = liftIO $ do
655653 status <- LibPQ.resultStatus result
656654 case status of
657655 LibPQ.CommandOk -> return ()
@@ -663,7 +661,7 @@ tryResult result = liftBase $ do
663661
664662-- | Catch `SquealException`s.
665663catchSqueal
666− :: MonadBaseControl IO io
664+ :: MonadUnliftIO io
667665 => io a
668666 -> (SquealException -> io a) -- ^ handler
669667 -> io a
@@ -671,7 +669,7 @@ catchSqueal = catch
671669
672670-- | Handle `SquealException`s.
673671handleSqueal
674− :: MonadBaseControl IO io
672+ :: MonadUnliftIO io
675673 => (SquealException -> io a) -- ^ handler
676674 -> io a -> io a
677675handleSqueal = handle