Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make stream more general #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
nix_direnv_watch_file odbc.cabal
nix_direnv_watch_file default.nix
use nix

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.stack-work
_release
result
cabal.project.local
4 changes: 2 additions & 2 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ repl c = do
hSetBuffering stdout LineBuffering
catch
(catch
(do (_, count) <- ODBC.stream c input output (False, 0 :: Int)
(do (_, count) <- ODBC.stream c input output (\_ -> pure (False, 0 :: Int))
putStrLn ("Rows: " ++ show count))
(\case
UserInterrupt -> pure ()
Expand All @@ -54,7 +54,7 @@ piped c = do
hSetBuffering stdout LineBuffering
catch
(catch
(do (_, count) <- ODBC.stream c input output (False, 0 :: Int)
(do (_, count) <- ODBC.stream c input output (\_ -> pure (False, 0 :: Int))
putStrLn ("Rows: " ++ show count))
(\case
UserInterrupt -> pure ()
Expand Down
20 changes: 20 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- compile-command: "nix-shell --run 'cabal exec -- ghc-pkg list'"; -*-
{ pkgs ? import <nixpkgs> {} }:
# https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/haskell-modules/make-package-set.nix
let this = pkgs.haskellPackages.developPackage {
root = ./.;
withHoogle = false;
returnShellEnv = false;
modifier = with pkgs.haskell.lib; drv:
disableLibraryProfiling
(addExtraLibraries
(dontHaddock
(addBuildTools drv
(with pkgs.haskellPackages; [ cabal-install ghcid pkgs.unixODBC])))
[ pkgs.unixODBC ]);

};
in this
// { env = this.env.overrideAttrs(_: prev: { shellHook = prev.shellHook + ''
export LD_LIBRARY_PATH=${pkgs.unixODBC}/lib
''; });}
2 changes: 2 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- compile-command: "nix-shell --run 'cabal exec -- ghc-pkg list'"; -*-
(import ./. {}).env
11 changes: 5 additions & 6 deletions src/Database/ODBC/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ stream ::
-> (state -> [(Column, Value)] -> m (Step state))
-- ^ A stepping function that gets as input the current @state@ and
-- a row, returning either a new @state@ or a final @result@.
-> state
-> ([Column] -> m state)
-- ^ A state that you can use for the computation. Strictly
-- evaluated each iteration.
-> m state
Expand All @@ -360,7 +360,7 @@ streamWithParams ::
-> (state -> [(Column, Value)] -> m (Step state))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-> (state -> [(Column, Value)] -> m (Step state))
-> (state -> [Value] -> m (Step state))

Given that state is now derived from our columns, does it make sense to remove Column here?

-- ^ A stepping function that gets as input the current @state@ and
-- a row, returning either a new @state@ or a final @result@.
-> state
-> ([Column] -> m state)
-- ^ A state that you can use for the computation. Strictly
-- evaluated each iteration.
-> m state
Expand Down Expand Up @@ -504,7 +504,7 @@ fetchIterator ::
Ptr EnvAndDbc
-> UnliftIO m
-> (state -> [(Column, Value)] -> m (Step state))
-> state
-> ([Column] -> m state)
-> SQLHSTMT s
-> IO state
fetchIterator dbc (UnliftIO runInIO) step state0 stmt = do
Expand Down Expand Up @@ -541,9 +541,8 @@ fetchIterator dbc (UnliftIO runInIO) step state0 stmt = do
(coerce retcode0)
"Unexpected return code"
sqlState)
if cols > 0
then loop state0
else pure state0

(if cols > 0 then loop else pure) =<< runInIO (state0 types)

-- | Fetch all results from possible multiple statements.
fetchAllResults :: Ptr EnvAndDbc -> SQLHSTMT s -> IO ()
Expand Down
3 changes: 2 additions & 1 deletion src/Database/ODBC/SQLServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module Database.ODBC.SQLServer

, stream
, Internal.Step(..)
, Internal.Column(..)

-- * Exceptions
-- $exceptions
Expand Down Expand Up @@ -455,7 +456,7 @@ stream ::
-> (state -> row -> m (Internal.Step state))
-- ^ A stepping function that gets as input the current @state@ and
-- a row, returning either a new @state@ or a final @result@.
-> state
-> ([Internal.Column] -> m state)
-- ^ A state that you can use for the computation. Strictly
-- evaluated each iteration.
-> m state
Expand Down
2 changes: 1 addition & 1 deletion test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ dataRetrieval = do
c
"DROP TABLE IF EXISTS no_such_table"
(\s _ -> pure (Stop s))
[]
(\_ -> pure [])
shouldBe (map (map snd) (rows1 ++ rows2)) [])
quickCheckInternalRoundtrip
"Int"
Expand Down