pipes-sqlite-simple (empty) → 0.2
raw patch · 4 files changed
+116/−0 lines, 4 filesdep +basedep +pipesdep +pipes-safesetup-changed
Dependencies added: base, pipes, pipes-safe, sqlite-simple, text
Files
- LICENSE +12/−0
- Pipes/SQLite.hs +70/−0
- Setup.hs +2/−0
- pipes-sqlite-simple.cabal +32/−0
+ LICENSE view
@@ -0,0 +1,12 @@+Copyright (c) 2016, Nick Partridge+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Pipes/SQLite.hs view
@@ -0,0 +1,70 @@+{-# LANGUAGE RankNTypes #-}+module Pipes.SQLite (+ -- * Producers - turn a query into a stream of results+ query,+ query_,+ queryNamed,+ eachRow,+ -- * Consumers - execute a statement using upstream values as bound parameters+ execute,+ executeNamed+ ) where++import Pipes as P+import Pipes.Safe as PS+import qualified Database.SQLite.Simple as SQL+import Control.Monad (forever)++-- | Run a parameterless query and yield each parsed row in the result.+query_ :: (MonadSafe m, SQL.FromRow a)+ => SQL.Connection+ -> SQL.Query+ -> Producer' a m ()+query_ conn q = bracketIO (SQL.openStatement conn q) SQL.closeStatement eachRow++-- | Run a query with parameters and yield each parsed row in the result.+query :: (MonadSafe m, SQL.FromRow a, SQL.ToRow params)+ => SQL.Connection+ -> SQL.Query+ -> params+ -> Producer' a m ()+query conn q p = bracketIO open' SQL.closeStatement eachRow+ where open' = do s <- SQL.openStatement conn q+ SQL.bind s p+ return s++-- | Run a query with named parameters and yield each parsed row in the result.+queryNamed :: (MonadSafe m, SQL.FromRow a)+ => SQL.Connection+ -> SQL.Query+ -> [SQL.NamedParam]+ -> Producer' a m ()+queryNamed conn q ns = bracketIO open' SQL.closeStatement eachRow+ where open' = do s <- SQL.openStatement conn q+ SQL.bindNamed s ns+ return s++-- | Executes the SQL statment by binding each value provided by each upstream yield.+execute :: (MonadIO m, SQL.ToRow q) => SQL.Connection -> SQL.Query -> Consumer' q m b+execute conn q = writeEach+ where writeEach = forever $ do+ v <- await+ liftIO $ SQL.execute conn q v++-- | Executes the SQL statement with the set of bound parameters provided by each upstream yield.+executeNamed :: MonadIO m => SQL.Connection -> SQL.Query -> Consumer' [SQL.NamedParam] m b+executeNamed conn q = forever $ do+ v <- await+ liftIO $ SQL.executeNamed conn q v++-- | Yields each row in the given Statement. It is assumed that the statement has already been opened.+eachRow :: (MonadIO m, SQL.FromRow a) => SQL.Statement -> Producer' a m ()+eachRow s = do+ v <- liftIO (SQL.nextRow s)+ case v of+ Just z -> yield z >> eachRow s+ Nothing -> return ()++-- | Bracket a MonadSafe action with IO.+bracketIO :: MonadSafe m => IO a -> (a -> IO a1) -> (a -> m c) -> m c+bracketIO a b = PS.bracket (liftIO a) (liftIO . b)
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ pipes-sqlite-simple.cabal view
@@ -0,0 +1,32 @@+-- Initial pipes-sqlite-simple.cabal generated by cabal init. For further+-- documentation, see http://haskell.org/cabal/users-guide/++name: pipes-sqlite-simple+version: 0.2+synopsis: Functions that smash Pipes and sqlite-simple together+description: Functions that smash Pipes and sqlite-simple together+license: BSD3+license-file: LICENSE+author: Nick Partridge+maintainer: nkpart@gmail.com+-- copyright:+category: Database+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10+source-repository head+ type: git+ location: https://github.com/nkpart/pipes-sqlite-simple++library+ exposed-modules: Pipes.SQLite+ -- other-modules:+ -- other-extensions:+ ghc-options: -Wall+ build-depends: base >=4.7 && <4.10,+ text >= 1.1.1.2 && <1.3+ , pipes >= 4.1.2 && <4.2+ , sqlite-simple >= 0.4.8.0 && <0.5+ , pipes-safe >= 2.2.0 && <2.3+ -- hs-source-dirs:+ default-language: Haskell2010