module Hmssql.Tds.Reader
( CompiledRow (..), Prepared (..), rowRecipe, rowReader, ReadFailure (..), RowRead (..), newRowRead, wholeRows, discardRows, drainingRows, skippedAs
) where
import Data.IORef
import Hmssql.Tds.Parser (Parser, ReadFailure (..))
import qualified Hmssql.Tds.Parser as P
import Hmssql.Tds.Value (Row)
data CompiledRow a = CompiledRow
{ forall a. CompiledRow a -> Int -> Prepared (Parser a)
rowPrepare :: Int -> Prepared (Parser a)
}
data Prepared a = Prepared a
rowRecipe :: CompiledRow a -> Int -> Parser (Either ReadFailure a)
rowRecipe :: forall a. CompiledRow a -> Int -> Parser (Either ReadFailure a)
rowRecipe CompiledRow a
compiled Int
ahead = Recipe () a -> Recipe () (Either ReadFailure a)
forall env a. Recipe env a -> Recipe env (Either ReadFailure a)
P.attempt (CompiledRow a -> Int -> Recipe () a
forall a. CompiledRow a -> Int -> Parser a
rowReader CompiledRow a
compiled Int
ahead)
rowReader :: CompiledRow a -> Int -> Parser a
rowReader :: forall a. CompiledRow a -> Int -> Parser a
rowReader CompiledRow a
compiled Int
ahead = case CompiledRow a -> Int -> Prepared (Parser a)
forall a. CompiledRow a -> Int -> Prepared (Parser a)
rowPrepare CompiledRow a
compiled Int
ahead of Prepared Parser a
recipe -> Parser a
recipe
data RowRead a
= ReadRows !(Parser a) !(IORef (Maybe (Either ReadFailure a)))
| WholeRows (Row -> a)
| DiscardRows a
| DrainRows a
| StopAtRow
newRowRead :: Int -> CompiledRow a -> IO (RowRead a)
newRowRead :: forall a. Int -> CompiledRow a -> IO (RowRead a)
newRowRead Int
ahead CompiledRow a
compiled =
Parser a -> IORef (Maybe (Either ReadFailure a)) -> RowRead a
forall a.
Parser a -> IORef (Maybe (Either ReadFailure a)) -> RowRead a
ReadRows (CompiledRow a -> Int -> Parser a
forall a. CompiledRow a -> Int -> Parser a
rowReader CompiledRow a
compiled Int
ahead) (IORef (Maybe (Either ReadFailure a)) -> RowRead a)
-> IO (IORef (Maybe (Either ReadFailure a))) -> IO (RowRead a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Either ReadFailure a)
-> IO (IORef (Maybe (Either ReadFailure a)))
forall a. a -> IO (IORef a)
newIORef Maybe (Either ReadFailure a)
forall a. Maybe a
Nothing
wholeRows :: RowRead Row
wholeRows :: RowRead Row
wholeRows = (Row -> Row) -> RowRead Row
forall a. (Row -> a) -> RowRead a
WholeRows Row -> Row
forall a. a -> a
id
discardRows :: RowRead ()
discardRows :: RowRead ()
discardRows = () -> RowRead ()
forall a. a -> RowRead a
DiscardRows ()
drainingRows :: RowRead ()
drainingRows :: RowRead ()
drainingRows = () -> RowRead ()
forall a. a -> RowRead a
DrainRows ()
skippedAs :: RowRead a -> Maybe a
skippedAs :: forall a. RowRead a -> Maybe a
skippedAs (DiscardRows a
a) = a -> Maybe a
forall a. a -> Maybe a
Just a
a
skippedAs (DrainRows a
a) = a -> Maybe a
forall a. a -> Maybe a
Just a
a
skippedAs RowRead a
_ = Maybe a
forall a. Maybe a
Nothing