sqlite-simple-errors 0.3.0.0 → 0.4.0.0
raw patch · 5 files changed
+38/−9 lines, 5 filesdep ~basedep ~mtldep ~parsec
Dependency ranges changed: base, mtl, parsec, text
Files
- sqlite-simple-errors.cabal +8/−7
- src/Database/SQLite/SimpleErrors.hs +6/−0
- src/Database/SQLite/SimpleErrors/Parser.hs +12/−1
- src/Database/SQLite/SimpleErrors/Types.hs +6/−0
- test/Utils.hs +6/−1
sqlite-simple-errors.cabal view
@@ -1,7 +1,8 @@ name: sqlite-simple-errors-version: 0.3.0.0+version: 0.4.0.0 synopsis: Wrapper around errors from sqlite-simple-description: Wrapper around errors from sqlite-simple+description: Wrapper around errors from sqlite-simple. Get easy-to-pattern-match+ data types for constraint errors. homepage: https://github.com/caneroj1/sqlite-simple-errors license: BSD3 license-file: LICENSE@@ -18,10 +19,10 @@ exposed-modules: Database.SQLite.SimpleErrors , Database.SQLite.SimpleErrors.Parser , Database.SQLite.SimpleErrors.Types- build-depends: base >= 4.7 && < 5+ build-depends: base >= 4.6 && < 5 , sqlite-simple >= 0.4.9 && < 0.5.0- , text >= 1.2.2.1 && < 1.2.3- , parsec >= 3.1.11 && < 3.2+ , text >= 1.2 && < 1.2.3+ , parsec >= 3.1.9 && < 3.2 default-language: Haskell2010 test-suite sqlite-simple-errors-test@@ -33,8 +34,8 @@ build-depends: base , sqlite-simple-errors , sqlite-simple >= 0.4.9 && < 0.5.0- , text >= 1.2.2.1 && < 1.2.3- , mtl >= 2.2.1 && < 2.3+ , text >= 1.2 && < 1.2.3+ , mtl >= 2.1 && < 2.3 ghc-options: -threaded -rtsopts -with-rtsopts=-N default-language: Haskell2010
src/Database/SQLite/SimpleErrors.hs view
@@ -9,8 +9,14 @@ import Database.SQLite.SimpleErrors.Types import Database.SQLite.SimpleErrors.Parser +-- | Type synonym for what is returned by runDBAction. Either a SQLiteResponse+-- or another type. type DatabaseResponse a = Either SQLiteResponse a +-- | runDBAction accepts an IO action to perform some database logic using+-- sqlite-simple. We capture any errors that are returned and wrap them+-- in our SQLiteResponse type. If any other type of exception is raised, it is+-- rethrown. runDBAction :: IO a -> IO (DatabaseResponse a) runDBAction sqlAction = do res <- try sqlAction
src/Database/SQLite/SimpleErrors/Parser.hs view
@@ -1,5 +1,10 @@-module Database.SQLite.SimpleErrors.Parser where+module Database.SQLite.SimpleErrors.Parser+(+ receiveSQLError+)+where +import Control.Applicative ((<$>)) import Control.Monad import Data.Text (Text) import qualified Data.Text as Text hiding (Text)@@ -37,6 +42,12 @@ parseError e@SQLError{sqlErrorDetails = details} = either (\_ -> SQLOtherError e) id $ parse constraintParser "" details +-- | Given a SQL error, converts it into a SQLiteResponse.+-- If the error is not an ErrorConstraint, it is essentially just wrapped in+-- SQLOtherError.+-- If the error is an ErrorConstraint error, try to parse the error as one+-- of the following kinds of constraint violations: Foreign Key, Not Null,+-- Unique, or Check. receiveSQLError :: SQLError -> SQLiteResponse receiveSQLError e@SQLError{sqlError = ErrorConstraint} = parseError e receiveSQLError e = SQLOtherError e
src/Database/SQLite/SimpleErrors/Types.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE DeriveDataTypeable #-}+ module Database.SQLite.SimpleErrors.Types where import Control.Exception@@ -5,12 +7,16 @@ import Data.Typeable import Database.SQLite.Simple (FormatError, ResultError, SQLError) +-- | Constraint represents the kind of constraint violation returned by SQLite. data Constraint = NotNull | ForeignKey | Unique | Check deriving (Show, Eq) +-- | SQLiteResponse is a wrapper around the different kinds of errors that can+-- be returned frm sqlite-simple. If there is a constraint error, then we will+-- construnct a SQLConstraintError instance. data SQLiteResponse = SQLConstraintError Constraint Text | SQLFormatError FormatError | SQLResultError ResultError
test/Utils.hs view
@@ -11,6 +11,7 @@ import Control.Monad.State import Database.SQLite.Simple (open, close, execute_, Connection) import SQLUtils+import System.Exit data Test a = Test { testName :: String@@ -43,7 +44,11 @@ } deriving (Monad, Functor, Applicative, MonadState Results, MonadIO) executeTestRunner :: (Eq a, Show a) => [Test a] -> IO ()-executeTestRunner tests = print =<< execStateT (runSuite $ runTests tests) empty+executeTestRunner tests = do+ results <- execStateT (runSuite $ runTests tests) empty+ print results+ when (failed results > 0) exitFailure+ exitSuccess setupDB :: Connection -> IO () setupDB conn = execute_ conn createTableSQL >>