Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit 0c733612dfb8bcaa1b0f793b224adf866f4b9881 @norm2782 committed Aug 29, 2011
Showing with 193 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +27 −0 LICENSE
  3. 0 README
  4. +3 −0 Setup.hs
  5. +37 −0 snaplet-hdbc.cabal
  6. +124 −0 src/Snap/Snaplet/Hdbc.hs
@@ -0,0 +1,2 @@
+.DS_Store
+dist/
27 LICENSE
@@ -0,0 +1,27 @@
+Copyright (c) 2009, Snap Framework authors (see CONTRIBUTORS)
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this
+list of conditions and the following disclaimer.
+
+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.
+
+Neither the name of the Snap Framework authors 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.
0 README
No changes.
@@ -0,0 +1,3 @@
+import Distribution.Simple
+
+main = defaultMain
@@ -0,0 +1,37 @@
+name: snaplet-hdbc
+version: 0.1.0
+synopsis: HDBC snaplet
+description: HDBC snaplet
+license: BSD3
+license-file: LICENSE
+author: Jurriën Stutterheim
+maintainer: j.stutterheim@me.com
+build-type: Simple
+cabal-version: >= 1.6
+homepage: http://norm2782.com/
+category: Web
+
+extra-source-files: LICENSE
+
+Library
+ hs-source-dirs: src
+
+ exposed-modules:
+ Snap.Snaplet.Hdbc
+
+ build-depends:
+ base >= 4 && < 5,
+ clientsession >= 0.4,
+ containers >= 0.3,
+ haskell98,
+ HDBC >= 2.2,
+ mtl > 2.0 && < 2.1,
+ snap >= 0.6 && < 0.7,
+ text >= 0.11
+
+ if impl(ghc >= 6.12.0)
+ ghc-options: -Wall -fwarn-tabs -funbox-strict-fields -O2
+ -fno-warn-orphans -fno-warn-unused-do-bind
+ else
+ ghc-options: -Wall -fwarn-tabs -funbox-strict-fields -O2
+ -fno-warn-orphans
@@ -0,0 +1,124 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE ExistentialQuantification #-}
+
+{-|
+
+ This module provides a very thin wrapper around HDBC
+-}
+module Snap.Snaplet.Hdbc where
+
+import Database.HDBC (IConnection(), SqlValue, Statement, SqlColDesc)
+import qualified Database.HDBC as HDBC
+import Snap.Snaplet
+import Control.Monad.State
+
+data HdbcSnaplet conn = IConnection conn => HdbcSnaplet {
+ hdbcConn :: conn
+ }
+
+hdbcInit :: IConnection conn => conn -> SnapletInit b (HdbcSnaplet conn)
+hdbcInit conn = makeSnaplet "hdbc" "HDBC abstraction" Nothing $ do
+ onUnload $ HDBC.disconnect conn
+ return $ HdbcSnaplet conn
+
+getConn :: IConnection conn => Handler b (HdbcSnaplet conn) conn
+getConn = gets hdbcConn
+
+runUnit :: IConnection conn => (conn -> IO ())
+ -> Handler b (HdbcSnaplet conn) ()
+runUnit f = do
+ conn <- gets hdbcConn
+ liftIO $ f conn
+
+disconnect :: IConnection conn => Handler b (HdbcSnaplet conn) ()
+disconnect = runUnit HDBC.disconnect
+
+commit :: IConnection conn => Handler b (HdbcSnaplet conn) ()
+commit = runUnit HDBC.commit
+
+rollback :: IConnection conn => Handler b (HdbcSnaplet conn) ()
+rollback = runUnit HDBC.rollback
+
+runRaw :: IConnection conn => String -> Handler b (HdbcSnaplet conn) ()
+runRaw str = do
+ conn <- gets hdbcConn
+ liftIO $ HDBC.runRaw conn str
+
+run :: IConnection conn => String -> [SqlValue]
+ -> Handler b (HdbcSnaplet conn) Integer
+run str vs = do
+ conn <- gets hdbcConn
+ liftIO $ HDBC.run conn str vs
+
+prepare :: IConnection conn => String -> Handler b (HdbcSnaplet conn) Statement
+prepare str = do
+ conn <- gets hdbcConn
+ liftIO $ HDBC.prepare conn str
+
+clone :: IConnection conn => Handler b (HdbcSnaplet conn) conn
+clone = do
+ conn <- gets hdbcConn
+ liftIO $ HDBC.clone conn
+
+pureStr :: IConnection conn => (conn -> String)
+ -> Handler b (HdbcSnaplet conn) String
+pureStr f = do
+ conn <- gets hdbcConn
+ return $ f conn
+
+hdbcDriverName :: IConnection conn => Handler b (HdbcSnaplet conn) String
+hdbcDriverName = pureStr HDBC.hdbcDriverName
+
+hdbcClientVer :: IConnection conn => Handler b (HdbcSnaplet conn) String
+hdbcClientVer = pureStr HDBC.hdbcClientVer
+
+proxiedClientName :: IConnection conn => Handler b (HdbcSnaplet conn) String
+proxiedClientName = pureStr HDBC.proxiedClientName
+
+proxiedClientVer :: IConnection conn => Handler b (HdbcSnaplet conn) String
+proxiedClientVer = pureStr HDBC.proxiedClientVer
+
+dbServerVer :: IConnection conn => Handler b (HdbcSnaplet conn) String
+dbServerVer = pureStr HDBC.dbServerVer
+
+dbTransactionSupport :: IConnection conn => Handler b (HdbcSnaplet conn) Bool
+dbTransactionSupport = do
+ conn <- gets hdbcConn
+ return $ HDBC.dbTransactionSupport conn
+
+getTables :: IConnection conn => Handler b (HdbcSnaplet conn) [String]
+getTables = do
+ conn <- gets hdbcConn
+ liftIO $ HDBC.getTables conn
+
+describeTable :: IConnection conn => String
+ -> Handler b (HdbcSnaplet conn) [(String, SqlColDesc)]
+describeTable str = do
+ conn <- gets hdbcConn
+ liftIO $ HDBC.describeTable conn str
+
+quickQuery' :: IConnection conn => String -> [SqlValue]
+ -> Handler b (HdbcSnaplet conn) [[SqlValue]]
+quickQuery' str vs = do
+ conn <- gets hdbcConn
+ liftIO $ HDBC.quickQuery' conn str vs
+
+quickQuery :: IConnection conn => String -> [SqlValue]
+ -> Handler b (HdbcSnaplet conn) [[SqlValue]]
+quickQuery str vs = do
+ conn <- gets hdbcConn
+ liftIO $ HDBC.quickQuery conn str vs
+
+sRun :: IConnection conn => String -> [Maybe String]
+ -> Handler b (HdbcSnaplet conn) Integer
+sRun str mstrs = do
+ conn <- gets hdbcConn
+ liftIO $ HDBC.sRun conn str mstrs
+
+withTransaction :: IConnection conn => (conn -> IO a) -> Handler b (HdbcSnaplet conn) a
+withTransaction f = do
+ conn <- gets hdbcConn
+ liftIO $ HDBC.withTransaction conn f
+

0 comments on commit 0c73361

Please sign in to comment.