{-# LANGUAGE CPP #-}

module Effectful.PostgreSQL
  ( -- * Effect
    WithConnection (..)
  , withConnection

    -- ** Interpreters
  , runWithConnection

#if POOL
  , runWithConnectionPool
#endif

    -- * Lifted versions of functions from Database.PostgreSQL.Simple

    -- ** Queries that return results
  , query
  , query_
  , queryWith
  , queryWith_

    -- ** Statements that do not return results
  , execute
  , execute_
  , executeMany

    -- ** Transaction handling
  , withTransaction
  , withSavepoint
  , begin
  , commit
  , rollback

    -- ** Queries that stream results
  , fold
  , foldWithOptions
  , fold_
  , foldWithOptions_
  , forEach
  , forEach_
  , returning
  , foldWith
  , foldWithOptionsAndParser
  , foldWith_
  , foldWithOptionsAndParser_
  , forEachWith
  , forEachWith_
  , returningWith
  )
where

import Data.Int (Int64)
import qualified Database.PostgreSQL.Simple as PSQL
import qualified Database.PostgreSQL.Simple.FromRow as PSQL
import Effectful
import Effectful.PostgreSQL.Connection as Conn
import GHC.Stack
#if POOL
import Effectful.PostgreSQL.Connection.Pool as Pool
#endif

-- | Lifted 'PSQL.query'.
query ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.ToRow q, PSQL.FromRow r) =>
  PSQL.Query ->
  q ->
  Eff es [r]
query :: forall (es :: [Effect]) q r.
(HasCallStack, WithConnection :> es, IOE :> es, ToRow q,
 FromRow r) =>
Query -> q -> Eff es [r]
query Query
q q
row = (Connection -> Eff es [r]) -> Eff es [r]
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es [r]) -> Eff es [r])
-> (Connection -> Eff es [r]) -> Eff es [r]
forall a b. (a -> b) -> a -> b
$ \Connection
conn -> IO [r] -> Eff es [r]
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Connection -> Query -> q -> IO [r]
forall q r.
(ToRow q, FromRow r) =>
Connection -> Query -> q -> IO [r]
PSQL.query Connection
conn Query
q q
row)

-- | Lifted 'PSQL.query_'.
query_ ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.FromRow r) =>
  PSQL.Query ->
  Eff es [r]
query_ :: forall (es :: [Effect]) r.
(HasCallStack, WithConnection :> es, IOE :> es, FromRow r) =>
Query -> Eff es [r]
query_ Query
row = (Connection -> Eff es [r]) -> Eff es [r]
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es [r]) -> Eff es [r])
-> (Connection -> Eff es [r]) -> Eff es [r]
forall a b. (a -> b) -> a -> b
$ \Connection
conn -> IO [r] -> Eff es [r]
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Connection -> Query -> IO [r]
forall r. FromRow r => Connection -> Query -> IO [r]
PSQL.query_ Connection
conn Query
row)

-- | Lifted 'PSQL.queryWith'.
queryWith ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.ToRow q) =>
  PSQL.RowParser r ->
  PSQL.Query ->
  q ->
  Eff es [r]
queryWith :: forall (es :: [Effect]) q r.
(HasCallStack, WithConnection :> es, IOE :> es, ToRow q) =>
RowParser r -> Query -> q -> Eff es [r]
queryWith RowParser r
parser Query
q q
row =
  (Connection -> Eff es [r]) -> Eff es [r]
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es [r]) -> Eff es [r])
-> (Connection -> Eff es [r]) -> Eff es [r]
forall a b. (a -> b) -> a -> b
$ \Connection
conn -> IO [r] -> Eff es [r]
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (RowParser r -> Connection -> Query -> q -> IO [r]
forall q r.
ToRow q =>
RowParser r -> Connection -> Query -> q -> IO [r]
PSQL.queryWith RowParser r
parser Connection
conn Query
q q
row)

-- | Lifted 'PSQL.queryWith_'.
queryWith_ ::
  (HasCallStack, WithConnection :> es, IOE :> es) =>
  PSQL.RowParser r ->
  PSQL.Query ->
  Eff es [r]
queryWith_ :: forall (es :: [Effect]) r.
(HasCallStack, WithConnection :> es, IOE :> es) =>
RowParser r -> Query -> Eff es [r]
queryWith_ RowParser r
parser Query
row =
  (Connection -> Eff es [r]) -> Eff es [r]
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es [r]) -> Eff es [r])
-> (Connection -> Eff es [r]) -> Eff es [r]
forall a b. (a -> b) -> a -> b
$ \Connection
conn -> IO [r] -> Eff es [r]
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (RowParser r -> Connection -> Query -> IO [r]
forall r. RowParser r -> Connection -> Query -> IO [r]
PSQL.queryWith_ RowParser r
parser Connection
conn Query
row)

-- | Lifted 'PSQL.execute'.
execute ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.ToRow q) =>
  PSQL.Query ->
  q ->
  Eff es Int64
execute :: forall (es :: [Effect]) q.
(HasCallStack, WithConnection :> es, IOE :> es, ToRow q) =>
Query -> q -> Eff es Int64
execute Query
q q
row = (Connection -> Eff es Int64) -> Eff es Int64
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es Int64) -> Eff es Int64)
-> (Connection -> Eff es Int64) -> Eff es Int64
forall a b. (a -> b) -> a -> b
$ \Connection
conn -> IO Int64 -> Eff es Int64
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Connection -> Query -> q -> IO Int64
forall q. ToRow q => Connection -> Query -> q -> IO Int64
PSQL.execute Connection
conn Query
q q
row)

-- | Lifted 'PSQL.execute_'.
execute_ ::
  (HasCallStack, WithConnection :> es, IOE :> es) =>
  PSQL.Query ->
  Eff es Int64
execute_ :: forall (es :: [Effect]).
(HasCallStack, WithConnection :> es, IOE :> es) =>
Query -> Eff es Int64
execute_ Query
row = (Connection -> Eff es Int64) -> Eff es Int64
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es Int64) -> Eff es Int64)
-> (Connection -> Eff es Int64) -> Eff es Int64
forall a b. (a -> b) -> a -> b
$ \Connection
conn -> IO Int64 -> Eff es Int64
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Connection -> Query -> IO Int64
PSQL.execute_ Connection
conn Query
row)

-- | Lifted 'PSQL.executeMany'.
executeMany ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.ToRow q) =>
  PSQL.Query ->
  [q] ->
  Eff es Int64
executeMany :: forall (es :: [Effect]) q.
(HasCallStack, WithConnection :> es, IOE :> es, ToRow q) =>
Query -> [q] -> Eff es Int64
executeMany Query
q [q]
rows = (Connection -> Eff es Int64) -> Eff es Int64
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es Int64) -> Eff es Int64)
-> (Connection -> Eff es Int64) -> Eff es Int64
forall a b. (a -> b) -> a -> b
$ \Connection
conn -> IO Int64 -> Eff es Int64
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Connection -> Query -> [q] -> IO Int64
forall q. ToRow q => Connection -> Query -> [q] -> IO Int64
PSQL.executeMany Connection
conn Query
q [q]
rows)

-- | Lifted 'PSQL.withTransaction'.
withTransaction ::
  (HasCallStack, WithConnection :> es, IOE :> es) => Eff es a -> Eff es a
withTransaction :: forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
Eff es a -> Eff es a
withTransaction Eff es a
f =
  (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    Connection -> IO a -> IO a
forall a. Connection -> IO a -> IO a
PSQL.withTransaction Connection
conn (Eff es a -> IO a
forall b. Eff es b -> IO b
unlift Eff es a
f)

-- | Lifted 'PSQL.withSavepoint'.
withSavepoint :: (HasCallStack, WithConnection :> es, IOE :> es) => Eff es a -> Eff es a
withSavepoint :: forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
Eff es a -> Eff es a
withSavepoint Eff es a
f =
  (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    Connection -> IO a -> IO a
forall a. Connection -> IO a -> IO a
PSQL.withSavepoint Connection
conn (Eff es a -> IO a
forall b. Eff es b -> IO b
unlift Eff es a
f)

-- | Lifted 'PSQL.begin'.
begin :: (HasCallStack, WithConnection :> es, IOE :> es) => Eff es ()
begin :: forall (es :: [Effect]).
(HasCallStack, WithConnection :> es, IOE :> es) =>
Eff es ()
begin = (Connection -> Eff es ()) -> Eff es ()
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es ()) -> Eff es ())
-> (Connection -> Eff es ()) -> Eff es ()
forall a b. (a -> b) -> a -> b
$ IO () -> Eff es ()
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Eff es ())
-> (Connection -> IO ()) -> Connection -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Connection -> IO ()
PSQL.begin

-- | Lifted 'PSQL.commit'.
commit :: (HasCallStack, WithConnection :> es, IOE :> es) => Eff es ()
commit :: forall (es :: [Effect]).
(HasCallStack, WithConnection :> es, IOE :> es) =>
Eff es ()
commit = (Connection -> Eff es ()) -> Eff es ()
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es ()) -> Eff es ())
-> (Connection -> Eff es ()) -> Eff es ()
forall a b. (a -> b) -> a -> b
$ IO () -> Eff es ()
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Eff es ())
-> (Connection -> IO ()) -> Connection -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Connection -> IO ()
PSQL.commit

-- | Lifted 'PSQL.rollback'.
rollback :: (HasCallStack, WithConnection :> es, IOE :> es) => Eff es ()
rollback :: forall (es :: [Effect]).
(HasCallStack, WithConnection :> es, IOE :> es) =>
Eff es ()
rollback = (Connection -> Eff es ()) -> Eff es ()
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es ()) -> Eff es ())
-> (Connection -> Eff es ()) -> Eff es ()
forall a b. (a -> b) -> a -> b
$ IO () -> Eff es ()
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Eff es ())
-> (Connection -> IO ()) -> Connection -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Connection -> IO ()
PSQL.rollback

(...) :: (a -> b) -> (t1 -> t2 -> a) -> t1 -> t2 -> b
a -> b
unlift ... :: forall a b t1 t2. (a -> b) -> (t1 -> t2 -> a) -> t1 -> t2 -> b
... t1 -> t2 -> a
f = \t1
a' t2
row -> a -> b
unlift (a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$ t1 -> t2 -> a
f t1
a' t2
row

unliftWithConn ::
  (HasCallStack, WithConnection :> es, IOE :> es) =>
  (PSQL.Connection -> (forall b. Eff es b -> IO b) -> IO a) ->
  Eff es a
unliftWithConn :: forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn Connection -> (forall b. Eff es b -> IO b) -> IO a
f =
  (Connection -> Eff es a) -> Eff es a
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es a) -> Eff es a)
-> (Connection -> Eff es a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn ->
    ((forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall b. ((forall b. Eff es b -> IO b) -> IO b) -> Eff es b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> ((forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \forall b. Eff es b -> IO b
unlift ->
      IO a -> IO a
forall a. IO a -> IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ Connection -> (forall b. Eff es b -> IO b) -> IO a
f Connection
conn Eff es b -> IO b
forall b. Eff es b -> IO b
unlift
{-# INLINE unliftWithConn #-}

-- | Lifted 'PSQL.fold'.
fold ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.FromRow row, PSQL.ToRow params) =>
  PSQL.Query ->
  params ->
  a ->
  (a -> row -> Eff es a) ->
  Eff es a
fold :: forall (es :: [Effect]) row params a.
(HasCallStack, WithConnection :> es, IOE :> es, FromRow row,
 ToRow params) =>
Query -> params -> a -> (a -> row -> Eff es a) -> Eff es a
fold Query
q params
params a
a a -> row -> Eff es a
f =
  (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    Connection -> Query -> params -> a -> (a -> row -> IO a) -> IO a
forall row params a.
(FromRow row, ToRow params) =>
Connection -> Query -> params -> a -> (a -> row -> IO a) -> IO a
PSQL.fold Connection
conn Query
q params
params a
a (Eff es a -> IO a
forall b. Eff es b -> IO b
unlift (Eff es a -> IO a) -> (a -> row -> Eff es a) -> a -> row -> IO a
forall a b t1 t2. (a -> b) -> (t1 -> t2 -> a) -> t1 -> t2 -> b
... a -> row -> Eff es a
f)

-- | Lifted 'PSQL.foldWithOptions'.
foldWithOptions ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.FromRow row, PSQL.ToRow params) =>
  PSQL.FoldOptions ->
  PSQL.Query ->
  params ->
  a ->
  (a -> row -> Eff es a) ->
  Eff es a
foldWithOptions :: forall (es :: [Effect]) row params a.
(HasCallStack, WithConnection :> es, IOE :> es, FromRow row,
 ToRow params) =>
FoldOptions
-> Query -> params -> a -> (a -> row -> Eff es a) -> Eff es a
foldWithOptions FoldOptions
opts Query
q params
params a
a a -> row -> Eff es a
f =
  (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    FoldOptions
-> Connection -> Query -> params -> a -> (a -> row -> IO a) -> IO a
forall row params a.
(FromRow row, ToRow params) =>
FoldOptions
-> Connection -> Query -> params -> a -> (a -> row -> IO a) -> IO a
PSQL.foldWithOptions FoldOptions
opts Connection
conn Query
q params
params a
a (Eff es a -> IO a
forall b. Eff es b -> IO b
unlift (Eff es a -> IO a) -> (a -> row -> Eff es a) -> a -> row -> IO a
forall a b t1 t2. (a -> b) -> (t1 -> t2 -> a) -> t1 -> t2 -> b
... a -> row -> Eff es a
f)

-- | Lifted 'PSQL.fold_'.
fold_ ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.FromRow row) =>
  PSQL.Query ->
  a ->
  (a -> row -> Eff es a) ->
  Eff es a
fold_ :: forall (es :: [Effect]) row a.
(HasCallStack, WithConnection :> es, IOE :> es, FromRow row) =>
Query -> a -> (a -> row -> Eff es a) -> Eff es a
fold_ Query
q a
a a -> row -> Eff es a
f =
  (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    Connection -> Query -> a -> (a -> row -> IO a) -> IO a
forall r a.
FromRow r =>
Connection -> Query -> a -> (a -> r -> IO a) -> IO a
PSQL.fold_ Connection
conn Query
q a
a (Eff es a -> IO a
forall b. Eff es b -> IO b
unlift (Eff es a -> IO a) -> (a -> row -> Eff es a) -> a -> row -> IO a
forall a b t1 t2. (a -> b) -> (t1 -> t2 -> a) -> t1 -> t2 -> b
... a -> row -> Eff es a
f)

-- | Lifted 'PSQL.foldWithOptions_'.
foldWithOptions_ ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.FromRow row) =>
  PSQL.FoldOptions ->
  PSQL.Query ->
  a ->
  (a -> row -> Eff es a) ->
  Eff es a
foldWithOptions_ :: forall (es :: [Effect]) row a.
(HasCallStack, WithConnection :> es, IOE :> es, FromRow row) =>
FoldOptions -> Query -> a -> (a -> row -> Eff es a) -> Eff es a
foldWithOptions_ FoldOptions
opts Query
q a
a a -> row -> Eff es a
f =
  (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    FoldOptions
-> Connection -> Query -> a -> (a -> row -> IO a) -> IO a
forall r a.
FromRow r =>
FoldOptions -> Connection -> Query -> a -> (a -> r -> IO a) -> IO a
PSQL.foldWithOptions_ FoldOptions
opts Connection
conn Query
q a
a (Eff es a -> IO a
forall b. Eff es b -> IO b
unlift (Eff es a -> IO a) -> (a -> row -> Eff es a) -> a -> row -> IO a
forall a b t1 t2. (a -> b) -> (t1 -> t2 -> a) -> t1 -> t2 -> b
... a -> row -> Eff es a
f)

-- | Lifted 'PSQL.forEach'.
forEach ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.FromRow r, PSQL.ToRow q) =>
  PSQL.Query ->
  q ->
  (r -> Eff es ()) ->
  Eff es ()
forEach :: forall (es :: [Effect]) r q.
(HasCallStack, WithConnection :> es, IOE :> es, FromRow r,
 ToRow q) =>
Query -> q -> (r -> Eff es ()) -> Eff es ()
forEach Query
q q
row r -> Eff es ()
forR =
  (Connection -> (forall b. Eff es b -> IO b) -> IO ()) -> Eff es ()
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO ())
 -> Eff es ())
-> (Connection -> (forall b. Eff es b -> IO b) -> IO ())
-> Eff es ()
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    Connection -> Query -> q -> (r -> IO ()) -> IO ()
forall q r.
(ToRow q, FromRow r) =>
Connection -> Query -> q -> (r -> IO ()) -> IO ()
PSQL.forEach Connection
conn Query
q q
row (Eff es () -> IO ()
forall b. Eff es b -> IO b
unlift (Eff es () -> IO ()) -> (r -> Eff es ()) -> r -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> Eff es ()
forR)

-- | Lifted 'PSQL.forEach_'.
forEach_ ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.FromRow r) =>
  PSQL.Query ->
  (r -> Eff es ()) ->
  Eff es ()
forEach_ :: forall (es :: [Effect]) r.
(HasCallStack, WithConnection :> es, IOE :> es, FromRow r) =>
Query -> (r -> Eff es ()) -> Eff es ()
forEach_ Query
q r -> Eff es ()
forR =
  (Connection -> (forall b. Eff es b -> IO b) -> IO ()) -> Eff es ()
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO ())
 -> Eff es ())
-> (Connection -> (forall b. Eff es b -> IO b) -> IO ())
-> Eff es ()
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    Connection -> Query -> (r -> IO ()) -> IO ()
forall r. FromRow r => Connection -> Query -> (r -> IO ()) -> IO ()
PSQL.forEach_ Connection
conn Query
q (Eff es () -> IO ()
forall b. Eff es b -> IO b
unlift (Eff es () -> IO ()) -> (r -> Eff es ()) -> r -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> Eff es ()
forR)

-- | Lifted 'PSQL.returning'.
returning ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.ToRow q, PSQL.FromRow r) =>
  PSQL.Query ->
  [q] ->
  Eff es [r]
returning :: forall (es :: [Effect]) q r.
(HasCallStack, WithConnection :> es, IOE :> es, ToRow q,
 FromRow r) =>
Query -> [q] -> Eff es [r]
returning Query
q [q]
rows = (Connection -> Eff es [r]) -> Eff es [r]
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es [r]) -> Eff es [r])
-> (Connection -> Eff es [r]) -> Eff es [r]
forall a b. (a -> b) -> a -> b
$ \Connection
conn -> IO [r] -> Eff es [r]
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [r] -> Eff es [r]) -> IO [r] -> Eff es [r]
forall a b. (a -> b) -> a -> b
$ Connection -> Query -> [q] -> IO [r]
forall q r.
(ToRow q, FromRow r) =>
Connection -> Query -> [q] -> IO [r]
PSQL.returning Connection
conn Query
q [q]
rows

-- | Lifted 'PSQL.foldWith'.
foldWith ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.ToRow params) =>
  PSQL.RowParser row ->
  PSQL.Query ->
  params ->
  a ->
  (a -> row -> Eff es a) ->
  Eff es a
foldWith :: forall (es :: [Effect]) params row a.
(HasCallStack, WithConnection :> es, IOE :> es, ToRow params) =>
RowParser row
-> Query -> params -> a -> (a -> row -> Eff es a) -> Eff es a
foldWith RowParser row
parser Query
q params
params a
a a -> row -> Eff es a
f =
  (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    RowParser row
-> Connection -> Query -> params -> a -> (a -> row -> IO a) -> IO a
forall params row a.
ToRow params =>
RowParser row
-> Connection -> Query -> params -> a -> (a -> row -> IO a) -> IO a
PSQL.foldWith RowParser row
parser Connection
conn Query
q params
params a
a (Eff es a -> IO a
forall b. Eff es b -> IO b
unlift (Eff es a -> IO a) -> (a -> row -> Eff es a) -> a -> row -> IO a
forall a b t1 t2. (a -> b) -> (t1 -> t2 -> a) -> t1 -> t2 -> b
... a -> row -> Eff es a
f)

-- | Lifted 'PSQL.foldWithOptionsAndParser'.
foldWithOptionsAndParser ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.ToRow params) =>
  PSQL.FoldOptions ->
  PSQL.RowParser row ->
  PSQL.Query ->
  params ->
  a ->
  (a -> row -> Eff es a) ->
  Eff es a
foldWithOptionsAndParser :: forall (es :: [Effect]) params row a.
(HasCallStack, WithConnection :> es, IOE :> es, ToRow params) =>
FoldOptions
-> RowParser row
-> Query
-> params
-> a
-> (a -> row -> Eff es a)
-> Eff es a
foldWithOptionsAndParser FoldOptions
opts RowParser row
parser Query
q params
params a
a a -> row -> Eff es a
f =
  (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    FoldOptions
-> RowParser row
-> Connection
-> Query
-> params
-> a
-> (a -> row -> IO a)
-> IO a
forall params row a.
ToRow params =>
FoldOptions
-> RowParser row
-> Connection
-> Query
-> params
-> a
-> (a -> row -> IO a)
-> IO a
PSQL.foldWithOptionsAndParser FoldOptions
opts RowParser row
parser Connection
conn Query
q params
params a
a (Eff es a -> IO a
forall b. Eff es b -> IO b
unlift (Eff es a -> IO a) -> (a -> row -> Eff es a) -> a -> row -> IO a
forall a b t1 t2. (a -> b) -> (t1 -> t2 -> a) -> t1 -> t2 -> b
... a -> row -> Eff es a
f)

-- | Lifted 'PSQL.foldWith_'.
foldWith_ ::
  (HasCallStack, WithConnection :> es, IOE :> es) =>
  PSQL.RowParser row ->
  PSQL.Query ->
  a ->
  (a -> row -> Eff es a) ->
  Eff es a
foldWith_ :: forall (es :: [Effect]) row a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
RowParser row -> Query -> a -> (a -> row -> Eff es a) -> Eff es a
foldWith_ RowParser row
parser Query
q a
a a -> row -> Eff es a
f =
  (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    RowParser row
-> Connection -> Query -> a -> (a -> row -> IO a) -> IO a
forall r a.
RowParser r -> Connection -> Query -> a -> (a -> r -> IO a) -> IO a
PSQL.foldWith_ RowParser row
parser Connection
conn Query
q a
a (Eff es a -> IO a
forall b. Eff es b -> IO b
unlift (Eff es a -> IO a) -> (a -> row -> Eff es a) -> a -> row -> IO a
forall a b t1 t2. (a -> b) -> (t1 -> t2 -> a) -> t1 -> t2 -> b
... a -> row -> Eff es a
f)

-- | Lifted 'PSQL.foldWithOptionsAndParser_'.
foldWithOptionsAndParser_ ::
  (HasCallStack, WithConnection :> es, IOE :> es) =>
  PSQL.FoldOptions ->
  PSQL.RowParser row ->
  PSQL.Query ->
  a ->
  (a -> row -> Eff es a) ->
  Eff es a
foldWithOptionsAndParser_ :: forall (es :: [Effect]) row a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
FoldOptions
-> RowParser row
-> Query
-> a
-> (a -> row -> Eff es a)
-> Eff es a
foldWithOptionsAndParser_ FoldOptions
opts RowParser row
parser Query
q a
a a -> row -> Eff es a
f =
  (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a)
-> (Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    FoldOptions
-> RowParser row
-> Connection
-> Query
-> a
-> (a -> row -> IO a)
-> IO a
forall r a.
FoldOptions
-> RowParser r
-> Connection
-> Query
-> a
-> (a -> r -> IO a)
-> IO a
PSQL.foldWithOptionsAndParser_ FoldOptions
opts RowParser row
parser Connection
conn Query
q a
a (Eff es a -> IO a
forall b. Eff es b -> IO b
unlift (Eff es a -> IO a) -> (a -> row -> Eff es a) -> a -> row -> IO a
forall a b t1 t2. (a -> b) -> (t1 -> t2 -> a) -> t1 -> t2 -> b
... a -> row -> Eff es a
f)

-- | Lifted 'PSQL.forEachWith'.
forEachWith ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.ToRow q) =>
  PSQL.RowParser r ->
  PSQL.Query ->
  q ->
  (r -> Eff es ()) ->
  Eff es ()
forEachWith :: forall (es :: [Effect]) q r.
(HasCallStack, WithConnection :> es, IOE :> es, ToRow q) =>
RowParser r -> Query -> q -> (r -> Eff es ()) -> Eff es ()
forEachWith RowParser r
parser Query
q q
row r -> Eff es ()
forR =
  (Connection -> (forall b. Eff es b -> IO b) -> IO ()) -> Eff es ()
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO ())
 -> Eff es ())
-> (Connection -> (forall b. Eff es b -> IO b) -> IO ())
-> Eff es ()
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    RowParser r -> Connection -> Query -> q -> (r -> IO ()) -> IO ()
forall q r.
ToRow q =>
RowParser r -> Connection -> Query -> q -> (r -> IO ()) -> IO ()
PSQL.forEachWith RowParser r
parser Connection
conn Query
q q
row (Eff es () -> IO ()
forall b. Eff es b -> IO b
unlift (Eff es () -> IO ()) -> (r -> Eff es ()) -> r -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> Eff es ()
forR)

-- | Lifted 'PSQL.forEachWith_'.
forEachWith_ ::
  (HasCallStack, WithConnection :> es, IOE :> es) =>
  PSQL.RowParser r ->
  PSQL.Query ->
  (r -> Eff es ()) ->
  Eff es ()
forEachWith_ :: forall (es :: [Effect]) r.
(HasCallStack, WithConnection :> es, IOE :> es) =>
RowParser r -> Query -> (r -> Eff es ()) -> Eff es ()
forEachWith_ RowParser r
parser Query
row r -> Eff es ()
forR =
  (Connection -> (forall b. Eff es b -> IO b) -> IO ()) -> Eff es ()
forall (es :: [Effect]) a.
(HasCallStack, WithConnection :> es, IOE :> es) =>
(Connection -> (forall b. Eff es b -> IO b) -> IO a) -> Eff es a
unliftWithConn ((Connection -> (forall b. Eff es b -> IO b) -> IO ())
 -> Eff es ())
-> (Connection -> (forall b. Eff es b -> IO b) -> IO ())
-> Eff es ()
forall a b. (a -> b) -> a -> b
$ \Connection
conn forall b. Eff es b -> IO b
unlift ->
    RowParser r -> Connection -> Query -> (r -> IO ()) -> IO ()
forall r.
RowParser r -> Connection -> Query -> (r -> IO ()) -> IO ()
PSQL.forEachWith_ RowParser r
parser Connection
conn Query
row (Eff es () -> IO ()
forall b. Eff es b -> IO b
unlift (Eff es () -> IO ()) -> (r -> Eff es ()) -> r -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> Eff es ()
forR)

-- | Lifted 'PSQL.returningWith'.
returningWith ::
  (HasCallStack, WithConnection :> es, IOE :> es, PSQL.ToRow q) =>
  PSQL.RowParser r ->
  PSQL.Query ->
  [q] ->
  Eff es [r]
returningWith :: forall (es :: [Effect]) q r.
(HasCallStack, WithConnection :> es, IOE :> es, ToRow q) =>
RowParser r -> Query -> [q] -> Eff es [r]
returningWith RowParser r
parser Query
q [q]
rows =
  (Connection -> Eff es [r]) -> Eff es [r]
forall a (es :: [Effect]).
(HasCallStack, WithConnection :> es) =>
(Connection -> Eff es a) -> Eff es a
withConnection ((Connection -> Eff es [r]) -> Eff es [r])
-> (Connection -> Eff es [r]) -> Eff es [r]
forall a b. (a -> b) -> a -> b
$ \Connection
conn -> IO [r] -> Eff es [r]
forall a. IO a -> Eff es a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [r] -> Eff es [r]) -> IO [r] -> Eff es [r]
forall a b. (a -> b) -> a -> b
$ RowParser r -> Connection -> Query -> [q] -> IO [r]
forall q r.
ToRow q =>
RowParser r -> Connection -> Query -> [q] -> IO [r]
PSQL.returningWith RowParser r
parser Connection
conn Query
q [q]
rows