packages feed

hasql-streams-core (empty) → 0.1.0.0

raw patch · 3 files changed

+91/−0 lines, 3 filesdep +basedep +hasqldep +hasql-transaction-io

Dependencies added: base, hasql, hasql-transaction-io

Files

+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2022 Andre Marianiello++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ hasql-streams-core.cabal view
@@ -0,0 +1,31 @@+cabal-version: 2.4+name:          hasql-streams-core+version:       0.1.0.0+synopsis:      Stream Hasql queries+homepage:      https://github.com/andremarianiello/hasql-streams+bug-reports:   https://github.com/andremarianiello/hasql-streams/issues+license:       MIT+license-file:  LICENSE+author:        Andre Marianiello+maintainer:    andremarianiello@users.noreply.github.com+copyright:     (c) 2022, Andre Marianiello+category:      Database, PostgreSQL, Hasql, Streaming++source-repository head+  type:     git+  location: https://github.com/andremarianiello/hasql-streams.git++library+  exposed-modules:    Hasql.Streams+  default-extensions:+    RankNTypes+    ScopedTypeVariables++  other-extensions:   OverloadedStrings+  build-depends:+    , base                  >=4.14  && <4.17+    , hasql                 ^>=1.5.0+    , hasql-transaction-io  ^>=0.1.0++  hs-source-dirs:     library+  default-language:   Haskell2010
+ library/Hasql/Streams.hs view
@@ -0,0 +1,40 @@+{-# LANGUAGE OverloadedStrings #-}++module Hasql.Streams (+  CursorStreamFold,+  cursorStreamQuery,+) where++-- hasql+import Hasql.Statement++-- hasql-transaction-io+import Hasql.CursorTransactionIO as CursorTransactionIO++-- | A fold over a stream of @a@ values to produce an @r@ using @CursorTransactionIO s@+type CursorStreamFold s a r = +  (forall x. +    (x -> CursorTransactionIO s (Maybe (a, x))) ->+    CursorTransactionIO s x ->+    r+  )++-- | Run a `Statement` using a cursor inside a `CursorTransactionIO` to produce a stream of @a@s, which are consumed by a `CursorStreamFold`. This function can produce any type of stream without depending on any particular stream library.+cursorStreamQuery :: forall params s a r.+  Statement params [a] -> params -> CursorStreamFold s a r -> r+cursorStreamQuery stmt params foldStream = foldStream step init+  where+    init :: CursorTransactionIO s (StreamState s a)+    init = do+      cursor <- declareCursorFor params stmt+      pure (cursor, [])+    step :: StreamState s a -> CursorTransactionIO s (Maybe (a, StreamState s a))+    step (cursor, batch) = case batch of+      (a:batch') -> pure $ Just (a, (cursor, batch'))+      [] -> do+        batch' <- fetchWithCursor cursor+        if Prelude.null batch'+        then pure Nothing+        else step (cursor, batch')++type StreamState s a = (Cursor s [a], [a])