module Hasql.Pipes (
  pipesQuery,
) where

-- transformers
import Control.Monad.Trans.Class

-- pipes
import Pipes
import qualified Pipes.Prelude as Pipes

-- hasql
import Hasql.Statement

-- hasql-transaction-io
import Hasql.CursorTransactionIO

-- hasql-streams-core
import Hasql.Streams

-- | Run a `Statement` using a cursor to return a `Producer` instead of a list
pipesQuery :: Statement params [a] -> params -> Producer a (CursorTransactionIO s) ()
pipesQuery :: Statement params [a]
-> params -> Producer a (CursorTransactionIO s) ()
pipesQuery Statement params [a]
stmt params
params = Statement params [a]
-> params
-> CursorStreamFold s a (Producer a (CursorTransactionIO s) ())
-> Producer a (CursorTransactionIO s) ()
forall params s a r.
Statement params [a] -> params -> CursorStreamFold s a r -> r
cursorStreamQuery Statement params [a]
stmt params
params CursorStreamFold s a (Producer a (CursorTransactionIO s) ())
forall s a.
CursorStreamFold s a (Producer a (CursorTransactionIO s) ())
foldPipesStream

foldPipesStream :: CursorStreamFold s a (Producer a (CursorTransactionIO s) ())
foldPipesStream :: (x -> CursorTransactionIO s (Maybe (a, x)))
-> CursorTransactionIO s x -> Producer a (CursorTransactionIO s) ()
foldPipesStream x -> CursorTransactionIO s (Maybe (a, x))
step CursorTransactionIO s x
init = CursorTransactionIO s x
-> Proxy X () () a (CursorTransactionIO s) x
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift CursorTransactionIO s x
init Proxy X () () a (CursorTransactionIO s) x
-> (x -> Producer a (CursorTransactionIO s) ())
-> Producer a (CursorTransactionIO s) ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (x -> CursorTransactionIO s (Either () (a, x)))
-> x -> Producer a (CursorTransactionIO s) ()
forall (m :: * -> *) s r a.
Monad m =>
(s -> m (Either r (a, s))) -> s -> Producer a m r
Pipes.unfoldr ((Maybe (a, x) -> Either () (a, x))
-> CursorTransactionIO s (Maybe (a, x))
-> CursorTransactionIO s (Either () (a, x))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe (a, x) -> Either () (a, x)
forall a. Maybe a -> Either () a
maybeToEither (CursorTransactionIO s (Maybe (a, x))
 -> CursorTransactionIO s (Either () (a, x)))
-> (x -> CursorTransactionIO s (Maybe (a, x)))
-> x
-> CursorTransactionIO s (Either () (a, x))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> CursorTransactionIO s (Maybe (a, x))
step)

maybeToEither :: Maybe a -> Either () a
maybeToEither :: Maybe a -> Either () a
maybeToEither = Either () a -> (a -> Either () a) -> Maybe a -> Either () a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> Either () a
forall a b. a -> Either a b
Left ()) a -> Either () a
forall a b. b -> Either a b
Right