@@ -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
@@ -246,7 +246,7 @@ class IndexedMonadTransPQ pq where
246246 --
247247 -- @define statement1 & pqThen (define statement2) = define (statement1 >>> statement2)@
248248 define
249− :: MonadBase IO io
249+ :: MonadIO io
250250 => Definition schemas0 schemas1
251251 -> pq schemas0 schemas1 io (K LibPQ.Result '[])
252252
@@ -262,7 +262,7 @@ instance IndexedMonadTransPQ PQ where
262262 unPQ (f x') (K (unK conn))
263263
264264 define (UnsafeDefinition q) = PQ $ \ (K conn) -> do
265− resultMaybe <- liftBase $ LibPQ.exec conn q
265+ resultMaybe <- liftIO $ LibPQ.exec conn q
266266 case resultMaybe of
267267 Nothing -> throw $ ResultException
268268 "define: LibPQ.exec returned no results"
@@ -381,7 +381,7 @@ class Monad pq => MonadPQ schemas pq | pq -> schemas where
381381 => (LibPQ.Connection -> IO a) -> pq a
382382 liftPQ = lift . liftPQ
383383
384−instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
384+instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
385385 => MonadPQ schemas (PQ schemas0 schemas1 io) where
386386
387387 manipulateParams
@@ -392,7 +392,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
392392 (LibPQ.invalidOid, encodingBytes encoding, LibPQ.Binary)
393393 params' = fmap (fmap toParam') (hcollapse (toParams @x @ps params))
394394 q' = q <> ";"
395− resultMaybe <- liftBase $ LibPQ.execParams conn q' params' LibPQ.Binary
395+ resultMaybe <- liftIO $ LibPQ.execParams conn q' params' LibPQ.Binary
396396 case resultMaybe of
397397 Nothing -> throw $ ResultException
398398 "manipulateParams: LibPQ.execParams returned no results"
@@ -402,7 +402,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
402402
403403 traversePrepared
404404 (UnsafeManipulation q :: Manipulation '[] schemas xs ys) (list :: list x) =
405− PQ $ \ (K conn) -> liftBase $ do
405+ PQ $ \ (K conn) -> liftIO $ do
406406 let temp = "temporary_statement"
407407 prepResultMaybe <- LibPQ.prepare conn temp q Nothing
408408 case prepResultMaybe of
@@ -429,7 +429,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
429429
430430 traversePrepared_
431431 (UnsafeManipulation q :: Manipulation '[] schemas xs '[]) (list :: list x) =
432− PQ $ \ (K conn) -> liftBase $ do
432+ PQ $ \ (K conn) -> liftIO $ do
433433 let temp = "temporary_statement"
434434 prepResultMaybe <- LibPQ.prepare conn temp q Nothing
435435 case prepResultMaybe of
@@ -453,7 +453,7 @@ instance (MonadBase IO io, schemas0 ~ schemas, schemas1 ~ schemas)
453453 return (K ())
454454
455455 liftPQ pq = PQ $ \ (K conn) -> do
456− y <- liftBase $ pq conn
456+ y <- liftIO $ pq conn
457457 return (K y)
458458
459459instance MonadPQ schemas m => MonadPQ schemas (IdentityT m)
@@ -494,14 +494,19 @@ instance schemas0 ~ schemas1 => MMonad (PQ schemas0 schemas1) where
494494 embed f (PQ pq) = PQ $ \ conn -> do
495495 evalPQ (f (pq conn)) conn
496496
497−instance (MonadBase b m, schemas0 ~ schemas1)
498− => MonadBase b (PQ schemas0 schemas1 m) where
499− liftBase = lift . liftBase
500−
501497instance (MonadIO m, schema0 ~ schema1)
502498 => MonadIO (PQ schema0 schema1 m) where
503499 liftIO = lift . liftIO
504500
501+instance (MonadUnliftIO m, schemas0 ~ schemas1)
502+ => MonadUnliftIO (PQ schemas0 schemas1 m) where
503+ withRunInIO
504+ :: ((forall a . PQ schemas0 schema1 m a -> IO a) -> IO b)
505+ -> PQ schemas0 schema1 m b
506+ withRunInIO inner = PQ $ \conn ->
507+ withRunInIO $ \(run :: (forall x . m x -> IO x)) ->
508+ K <$> inner (\pq -> run $ unK <$> unPQ pq conn)
509+
505510-- | A snapshot of the state of a `PQ` computation.
506511type PQRun schemas =
507512 forall m x. Monad m => PQ schemas schemas m x -> m (K x schemas)
@@ -511,23 +516,16 @@ pqliftWith :: Functor m => (PQRun schemas -> m a) -> PQ schemas schemas m a
511516pqliftWith f = PQ $ \ conn ->
512517 fmap K (f $ \ pq -> unPQ pq conn)
513518
514−instance (MonadBaseControl b m, schemas0 ~ schemas1)
515− => MonadBaseControl b (PQ schemas0 schemas1 m) where
516− type StM (PQ schemas0 schemas1 m) x = StM m (K x schemas0)
517− liftBaseWith f =
518− pqliftWith $ \ run -> liftBaseWith $ \ runInBase -> f $ runInBase . run
519− restoreM = PQ . const . restoreM
520−
521519-- | Get a row corresponding to a given row number from a `LibPQ.Result`,
522520-- throwing an exception if the row number is out of bounds.
523521getRow
524− :: (FromRow columns y, MonadBase IO io)
522+ :: (FromRow columns y, MonadIO io)
525523 => LibPQ.Row
526524 -- ^ row number
527525 -> K LibPQ.Result columns
528526 -- ^ result
529527 -> io y
530−getRow r (K result :: K LibPQ.Result columns) = liftBase $ do
528+getRow r (K result :: K LibPQ.Result columns) = liftIO $ do
531529 numRows <- LibPQ.ntuples result
532530 when (numRows < r) $ throw $ ResultException $
533531 "getRow: expected at least " <> pack (show r) <> "rows but only saw "
@@ -545,13 +543,13 @@ getRow r (K result :: K LibPQ.Result columns) = liftBase $ do
545543-- and a `LibPQ.Result` and given a row number if it's too large returns `Nothing`,
546544-- otherwise returning the row along with the next row number.
547545nextRow
548− :: (FromRow columns y, MonadBase IO io)
546+ :: (FromRow columns y, MonadIO io)
549547 => LibPQ.Row -- ^ total number of rows
550548 -> K LibPQ.Result columns -- ^ result
551549 -> LibPQ.Row -- ^ row number
552550 -> io (Maybe (LibPQ.Row,y))
553551nextRow total (K result :: K LibPQ.Result columns) r
554− = liftBase $ if r >= total then return Nothing else do
552+ = liftIO $ if r >= total then return Nothing else do
555553 let len = fromIntegral (lengthSList (Proxy @columns))
556554 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
557555 case fromList row' of
@@ -562,10 +560,10 @@ nextRow total (K result :: K LibPQ.Result columns) r
562560
563561-- | Get all rows from a `LibPQ.Result`.
564562getRows
565− :: (FromRow columns y, MonadBase IO io)
563+ :: (FromRow columns y, MonadIO io)
566564 => K LibPQ.Result columns -- ^ result
567565 -> io [y]
568−getRows (K result :: K LibPQ.Result columns) = liftBase $ do
566+getRows (K result :: K LibPQ.Result columns) = liftIO $ do
569567 let len = fromIntegral (lengthSList (Proxy @columns))
570568 numRows <- LibPQ.ntuples result
571569 for [0 .. numRows - 1] $ \ r -> do
@@ -578,10 +576,10 @@ getRows (K result :: K LibPQ.Result columns) = liftBase $ do
578576
579577-- | Get the first row if possible from a `LibPQ.Result`.
580578firstRow
581− :: (FromRow columns y, MonadBase IO io)
579+ :: (FromRow columns y, MonadIO io)
582580 => K LibPQ.Result columns -- ^ result
583581 -> io (Maybe y)
584−firstRow (K result :: K LibPQ.Result columns) = liftBase $ do
582+firstRow (K result :: K LibPQ.Result columns) = liftIO $ do
585583 numRows <- LibPQ.ntuples result
586584 if numRows <= 0 then return Nothing else do
587585 let len = fromIntegral (lengthSList (Proxy @columns))
@@ -594,23 +592,23 @@ firstRow (K result :: K LibPQ.Result columns) = liftBase $ do
594592
595593-- | Lifts actions on results from @LibPQ@.
596594liftResult
597− :: MonadBase IO io
595+ :: MonadIO io
598596 => (LibPQ.Result -> IO x)
599597 -> K LibPQ.Result results -> io x
600−liftResult f (K result) = liftBase $ f result
598+liftResult f (K result) = liftIO $ f result
601599
602600-- | Returns the number of rows (tuples) in the query result.
603−ntuples :: MonadBase IO io => K LibPQ.Result columns -> io LibPQ.Row
601+ntuples :: MonadIO io => K LibPQ.Result columns -> io LibPQ.Row
604602ntuples = liftResult LibPQ.ntuples
605603
606604-- | Returns the result status of the command.
607−resultStatus :: MonadBase IO io => K LibPQ.Result results -> io LibPQ.ExecStatus
605+resultStatus :: MonadIO io => K LibPQ.Result results -> io LibPQ.ExecStatus
608606resultStatus = liftResult LibPQ.resultStatus
609607
610608-- | Returns the error message most recently generated by an operation
611609-- on the connection.
612610resultErrorMessage
613− :: MonadBase IO io => K LibPQ.Result results -> io (Maybe ByteString)
611+ :: MonadIO io => K LibPQ.Result results -> io (Maybe ByteString)
614612resultErrorMessage = liftResult LibPQ.resultErrorMessage
615613
616614-- | Returns the error code most recently generated by an operation
@@ -618,7 +616,7 @@ resultErrorMessage = liftResult LibPQ.resultErrorMessage
618616--
619617-- https://www.postgresql.org/docs/current/static/errcodes-appendix.html
620618resultErrorCode
621− :: MonadBase IO io
619+ :: MonadIO io
622620 => K LibPQ.Result results
623621 -> io (Maybe ByteString)
624622resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
@@ -637,10 +635,10 @@ data SquealException
637635instance Exception SquealException
638636
639637tryResult
640− :: MonadBase IO io
638+ :: MonadIO io
641639 => LibPQ.Result
642640 -> io ()
643−tryResult result = liftBase $ do
641+tryResult result = liftIO $ do
644642 status <- LibPQ.resultStatus result
645643 case status of
646644 LibPQ.CommandOk -> return ()
@@ -652,7 +650,7 @@ tryResult result = liftBase $ do
652650
653651-- | Catch `SquealException`s.
654652catchSqueal
655− :: MonadBaseControl IO io
653+ :: MonadUnliftIO io
656654 => io a
657655 -> (SquealException -> io a) -- ^ handler
658656 -> io a
@@ -660,7 +658,7 @@ catchSqueal = catch
660658
661659-- | Handle `SquealException`s.
662660handleSqueal
663− :: MonadBaseControl IO io
661+ :: MonadUnliftIO io
664662 => (SquealException -> io a) -- ^ handler
665663 -> io a -> io a
666664handleSqueal = handle