diff --git a/.gitignore b/.gitignore index 9722583..b618340 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,4 @@ -cabal-dev/ -dist/ -TAGS -/*.hs.html -/hpc_*.html -/test.tix -.stack-work/ -dist-newstyle/ +/dist-newstyle/ +/.stack-work/ +/stack.yaml.lock +/stack-*.yaml.lock diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ec5670b..0000000 --- a/.travis.yml +++ /dev/null @@ -1,3 +0,0 @@ -language: haskell -script: - - cabal configure --enable-tests && cabal build && cabal test diff --git a/Database/SQLite/Simple.hs b/Database/SQLite/Simple.hs index 6e3a509..b12a5ae 100644 --- a/Database/SQLite/Simple.hs +++ b/Database/SQLite/Simple.hs @@ -64,9 +64,10 @@ module Database.SQLite.Simple ( -- * Queries that return results , query , query_ + , queryNamed , queryWith , queryWith_ - , queryNamed + , queryNamedWith , lastInsertRowId , changes , totalChanges @@ -74,12 +75,16 @@ module Database.SQLite.Simple ( , fold , fold_ , foldNamed + , foldWith + , foldWith_ + , foldNamedWith -- * Statements that do not return results , execute - , execute_ , executeMany , executeNamed + , execute_ , field + , returnRowError -- * Transactions , withTransaction , withImmediateTransaction @@ -244,7 +249,7 @@ bindNamed (Statement stmt) params = do reset :: Statement -> IO () reset (Statement stmt) = Base.reset stmt --- | Return the name of a a particular column in the result set of a +-- | Return the name of a particular column in the result set of a -- 'Statement'. Throws an 'ArrayException' if the colum index is out -- of bounds. -- @@ -314,27 +319,6 @@ withStatementNamedParams :: Connection withStatementNamedParams conn template namedParams action = withStatement conn template $ \stmt -> bindNamed stmt namedParams >> action stmt --- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not --- expected to return results. --- --- Throws 'FormatError' if the query could not be formatted correctly. -execute :: (ToRow q) => Connection -> Query -> q -> IO () -execute conn template qs = - withStatementParams conn template qs $ \(Statement stmt) -> - void . Base.step $ stmt - --- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not --- expected to return results. --- --- Throws 'FormatError' if the query could not be formatted correctly. -executeMany :: ToRow q => Connection -> Query -> [q] -> IO () -executeMany conn template paramRows = withStatement conn template $ \stmt -> do - let Statement stmt' = stmt - forM_ paramRows $ \params -> - withBind stmt params - (void . Base.step $ stmt') - - doFoldToList :: RowParser row -> Statement -> IO [row] doFoldToList fromRow_ stmt = fmap reverse $ doFold fromRow_ stmt [] (\acc e -> return (e : acc)) @@ -359,19 +343,8 @@ query = queryWith fromRow query_ :: (FromRow r) => Connection -> Query -> IO [r] query_ = queryWith_ fromRow --- | A version of 'query' that takes an explicit 'RowParser'. -queryWith :: (ToRow q) => RowParser r -> Connection -> Query -> q -> IO [r] -queryWith fromRow_ conn templ qs = - withStatementParams conn templ qs $ \stmt -> doFoldToList fromRow_ stmt - --- | A version of 'query' that does not perform query substitution and --- takes an explicit 'RowParser'. -queryWith_ :: RowParser r -> Connection -> Query -> IO [r] -queryWith_ fromRow_ conn query = - withStatement conn query (doFoldToList fromRow_) - --- | A version of 'query' where the query parameters (placeholders) --- are named. +-- | A version of 'query' whose query parameters (placeholders) are +-- named. -- -- Example: -- @@ -379,21 +352,24 @@ queryWith_ fromRow_ conn query = -- r \<- 'queryNamed' c \"SELECT * FROM posts WHERE id=:id AND date>=:date\" [\":id\" ':=' postId, \":date\" ':=' afterDate] -- @ queryNamed :: (FromRow r) => Connection -> Query -> [NamedParam] -> IO [r] -queryNamed conn templ params = - withStatementNamedParams conn templ params $ \stmt -> doFoldToList fromRow stmt +queryNamed = queryNamedWith fromRow --- | A version of 'execute' that does not perform query substitution. -execute_ :: Connection -> Query -> IO () -execute_ conn template = - withStatement conn template $ \(Statement stmt) -> - void $ Base.step stmt +-- | A version of 'query' that takes an explicit 'RowParser'. +queryWith :: (ToRow q) => RowParser r -> Connection -> Query -> q -> IO [r] +queryWith fromRow_ conn templ qs = + withStatementParams conn templ qs $ \stmt -> doFoldToList fromRow_ stmt --- | A version of 'execute' where the query parameters (placeholders) --- are named. -executeNamed :: Connection -> Query -> [NamedParam] -> IO () -executeNamed conn template params = - withStatementNamedParams conn template params $ \(Statement stmt) -> - void $ Base.step stmt +-- | A version of 'query' that takes an explicit 'RowParser' and does +-- not perform query substitution. +queryWith_ :: RowParser r -> Connection -> Query -> IO [r] +queryWith_ fromRow_ conn query = + withStatement conn query (doFoldToList fromRow_) + +-- | A version of 'query' whose query parameters (placeholders) are +-- named that takes an explicit 'RowParser'. +queryNamedWith :: RowParser r -> Connection -> Query -> [NamedParam] -> IO [r] +queryNamedWith fromRow_ conn templ params = + withStatementNamedParams conn templ params $ \stmt -> doFoldToList fromRow_ stmt -- | Perform a @SELECT@ or other SQL query that is expected to return results. -- Results are converted and fed into the 'action' callback as they are being @@ -414,22 +390,18 @@ fold :: ( FromRow row, ToRow params ) -> a -> (a -> row -> IO a) -> IO a -fold conn query params initalState action = - withStatementParams conn query params $ \stmt -> - doFold fromRow stmt initalState action +fold = foldWith fromRow --- | A version of 'fold' which does not perform parameter substitution. +-- | A version of 'fold' that does not perform query substitution. fold_ :: ( FromRow row ) => Connection -> Query -> a -> (a -> row -> IO a) -> IO a -fold_ conn query initalState action = - withStatement conn query $ \stmt -> - doFold fromRow stmt initalState action +fold_ = foldWith_ fromRow --- | A version of 'fold' where the query parameters (placeholders) are +-- | A version of 'fold' whose query parameters (placeholders) are -- named. foldNamed :: ( FromRow row ) => Connection @@ -438,9 +410,45 @@ foldNamed :: ( FromRow row ) -> a -> (a -> row -> IO a) -> IO a -foldNamed conn query params initalState action = +foldNamed = foldNamedWith fromRow + +-- | A version of 'fold' that takes an explicit 'RowParser'. +foldWith :: ( ToRow params ) + => RowParser row + -> Connection + -> Query + -> params + -> a + -> (a -> row -> IO a) + -> IO a +foldWith fromRow_ conn query params initalState action = + withStatementParams conn query params $ \stmt -> + doFold fromRow_ stmt initalState action + +-- | A version of 'fold' that takes an explicit 'RowParser' and does +-- not perform query substitution. +foldWith_ :: RowParser row + -> Connection + -> Query + -> a + -> (a -> row -> IO a) + -> IO a +foldWith_ fromRow_ conn query initalState action = + withStatement conn query $ \stmt -> + doFold fromRow_ stmt initalState action + +-- | A version of 'fold' whose query parameters (placeholders) are +-- named that takes an explicit 'RowParser'. +foldNamedWith :: RowParser row + -> Connection + -> Query + -> [NamedParam] + -> a + -> (a -> row -> IO a) + -> IO a +foldNamedWith fromRow_ conn query params initalState action = withStatementNamedParams conn query params $ \stmt -> - doFold fromRow stmt initalState action + doFold fromRow_ stmt initalState action doFold :: RowParser row -> Statement -> a -> (a -> row -> IO a) -> IO a doFold fromRow_ stmt initState action = @@ -454,6 +462,39 @@ doFold fromRow_ stmt initState action = val' `seq` loop val' Nothing -> return val +-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not +-- expected to return results. +-- +-- Throws 'FormatError' if the query could not be formatted correctly. +execute :: (ToRow q) => Connection -> Query -> q -> IO () +execute conn template qs = + withStatementParams conn template qs $ \(Statement stmt) -> + void . Base.step $ stmt + +-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not +-- expected to return results. +-- +-- Throws 'FormatError' if the query could not be formatted correctly. +executeMany :: ToRow q => Connection -> Query -> [q] -> IO () +executeMany conn template paramRows = withStatement conn template $ \stmt -> do + let Statement stmt' = stmt + forM_ paramRows $ \params -> + withBind stmt params + (void . Base.step $ stmt') + +-- | A version of 'execute' where the query parameters (placeholders) +-- are named. +executeNamed :: Connection -> Query -> [NamedParam] -> IO () +executeNamed conn template params = + withStatementNamedParams conn template params $ \(Statement stmt) -> + void $ Base.step stmt + +-- | A version of 'execute' that does not perform query substitution. +execute_ :: Connection -> Query -> IO () +execute_ conn template = + withStatement conn template $ \(Statement stmt) -> + void $ Base.step stmt + -- | Extracts the next row from the prepared statement. nextRow :: (FromRow r) => Statement -> IO (Maybe r) nextRow = nextRowWith fromRow @@ -806,7 +847,7 @@ getQuery stmt = -- -- Although SQL can accommodate @NULL@ as a value for any of these -- types, Haskell cannot. If your result contains columns that may be --- @NULL@, be sure that you use 'Maybe' in those positions of of your +-- @NULL@, be sure that you use 'Maybe' in those positions of your -- tuple. -- -- > (Text, Maybe Int, Int) diff --git a/Database/SQLite/Simple/FromField.hs b/Database/SQLite/Simple/FromField.hs index 35240d2..ca9f0d8 100644 --- a/Database/SQLite/Simple/FromField.hs +++ b/Database/SQLite/Simple/FromField.hs @@ -1,7 +1,7 @@ - -{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-} -{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-} -{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE CPP #-} +{-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE ScopedTypeVariables #-} ------------------------------------------------------------------------------ -- | @@ -183,7 +183,7 @@ instance FromField UTCTime where fromField f@(Field (SQLText t) _) = case parseUTCTime t of Right t -> Ok t - Left e -> returnError ConversionFailed f ("couldn't parse UTCTime field: " ++ e) + Left e -> returnError ConversionFailed f ("couldn't parse UTCTime field: " ++ e ++ ", field contents: " ++ T.unpack t) fromField f = returnError ConversionFailed f "expecting SQLText column type" @@ -192,7 +192,7 @@ instance FromField Day where fromField f@(Field (SQLText t) _) = case parseDay t of Right t -> Ok t - Left e -> returnError ConversionFailed f ("couldn't parse Day field: " ++ e) + Left e -> returnError ConversionFailed f ("couldn't parse Day field: " ++ e ++ ", field contents: " ++ T.unpack t) fromField f = returnError ConversionFailed f "expecting SQLText column type" diff --git a/Database/SQLite/Simple/FromRow.hs b/Database/SQLite/Simple/FromRow.hs index fa4fdcd..57337ce 100644 --- a/Database/SQLite/Simple/FromRow.hs +++ b/Database/SQLite/Simple/FromRow.hs @@ -1,5 +1,4 @@ -{-# LANGUAGE RecordWildCards, DefaultSignatures, FlexibleContexts, - StandaloneDeriving #-} +{-# LANGUAGE DefaultSignatures, FlexibleContexts #-} ------------------------------------------------------------------------------ -- | @@ -24,9 +23,10 @@ module Database.SQLite.Simple.FromRow , field , fieldWith , numFieldsRemaining + , returnRowError ) where -import Control.Exception (SomeException(..)) +import Control.Exception (Exception, SomeException(..)) import Control.Monad (replicateM) import Control.Monad.Trans.State.Strict import Control.Monad.Trans.Reader @@ -135,6 +135,9 @@ numFieldsRemaining = RP $ do (columnIdx,_) <- lift get return $! ncols - columnIdx +returnRowError :: Exception err => err -> RowParser a +returnRowError err = RP . lift . lift $ Errors [SomeException err] + instance (FromField a) => FromRow (Only a) where fromRow = Only <$> field diff --git a/Database/SQLite/Simple/Ok.hs b/Database/SQLite/Simple/Ok.hs index 753acd5..15fe39a 100644 --- a/Database/SQLite/Simple/Ok.hs +++ b/Database/SQLite/Simple/Ok.hs @@ -19,7 +19,7 @@ -- commonly-used type and typeclass included in @base@. -- -- Extending the failure case to a list of 'SomeException's enables a --- more sensible 'Alternative' instance definitions: '<|>' concatinates +-- more sensible 'Alternative' instance definitions: '<|>' concatenates -- the list of exceptions when both cases fail, and 'empty' is defined as -- 'Errors []'. Though '<|>' one could pick one of two exceptions, and -- throw away the other, and have 'empty' provide a generic exception, @@ -73,18 +73,17 @@ instance MonadPlus Ok where mplus = (<|>) instance Monad Ok where - return = Ok - Errors es >>= _ = Errors es Ok a >>= f = f a #if MIN_VERSION_base(4,9,0) instance MonadFail Ok where fail str = Errors [SomeException (ErrorCall str)] +#endif instance MonadThrow Ok where - throwM = fail . show -#endif + throwM = Errors . pure . toException + -- | a way to reify a list of exceptions into a single exception diff --git a/Database/SQLite/Simple/QQ.hs b/Database/SQLite/Simple/QQ.hs index 53172bc..962f397 100644 --- a/Database/SQLite/Simple/QQ.hs +++ b/Database/SQLite/Simple/QQ.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE TemplateHaskellQuotes #-} ------------------------------------------------------------------------------ -- | @@ -27,7 +27,7 @@ import Language.Haskell.TH.Quote (QuasiQuoter (..)) One should consider turning on the @-XQuasiQuotes@ pragma in that module: @ -{-# LANGUAGE QuasiQuoter #-} +{-# LANGUAGE QuasiQuotes #-} myQuery = query conn [sql| SELECT diff --git a/Database/SQLite/Simple/ToField.hs b/Database/SQLite/Simple/ToField.hs index 85924e5..0ec2276 100644 --- a/Database/SQLite/Simple/ToField.hs +++ b/Database/SQLite/Simple/ToField.hs @@ -1,5 +1,4 @@ -{-# LANGUAGE DeriveDataTypeable, DeriveFunctor #-} -{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-} +{-# LANGUAGE FlexibleInstances #-} ------------------------------------------------------------------------------ -- | diff --git a/Database/SQLite/Simple/Types.hs b/Database/SQLite/Simple/Types.hs index 457c667..0eba8d7 100644 --- a/Database/SQLite/Simple/Types.hs +++ b/Database/SQLite/Simple/Types.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, GeneralizedNewtypeDeriving, CPP #-} +{-# LANGUAGE DeriveDataTypeable, CPP #-} ------------------------------------------------------------------------------ -- | diff --git a/README.markdown b/README.md similarity index 89% rename from README.markdown rename to README.md index 3ab4e20..28ab6c5 100644 --- a/README.markdown +++ b/README.md @@ -1,7 +1,7 @@ sqlite-simple: mid-level bindings to the sqlite database ======================================================== -[![Build Status](https://secure.travis-ci.org/nurpax/sqlite-simple.png)](http://travis-ci.org/nurpax/sqlite-simple) +[![Build Status](https://github.com/nurpax/sqlite-simple/actions/workflows/haskell-ci.yml/badge.svg)](https://github.com/nurpax/sqlite-simple/actions/workflows/haskell-ci.yml) [![Hackage](https://img.shields.io/hackage/v/sqlite-simple.svg)](https://hackage.haskell.org/package/sqlite-simple) This library is a mid-level Haskell binding to the SQLite database. @@ -95,7 +95,8 @@ tests for any such features. ## Credits - [Janne Hellsten](https://github.com/nurpax) author, long-term maintainer -- [Sergey Bushnyak](https://github.com/sigrlami) current maintainer +- [Sergey Bushnyak](https://github.com/sigrlami) long-term maintainer +- [Joshua Chia](https://github.com/jchia) current maintainer A lot of the code is directly borrowed from [mysql-simple](http://github.com/bos/mysql-simple) by Bryan O'Sullivan diff --git a/cabal.project b/cabal.project index dcdf91d..f8d4688 100644 --- a/cabal.project +++ b/cabal.project @@ -4,4 +4,4 @@ packages: . source-repository-package type: git location: https://github.com/simplex-chat/direct-sqlcipher.git - tag: 34309410eb2069b029b8fc1872deb1e0db123294 + tag: 2330df3dc8c4b660674d3c02d76bb86f66c2abee diff --git a/changelog b/changelog index 6fa04ce..412a09e 100644 --- a/changelog +++ b/changelog @@ -1,5 +1,14 @@ -0.4.18.1 - * Add generic implementation of 'FromRow' and 'ToRow'. +0.4.19.0 + * Generic derivation of `FromRow` and `ToRow` + * Add `withSavePoint` + * Add `MonadFail` instance of `Ok` + +0.4.18.0 + * Allow semigroups-0.19 + * Support GHC 8.8.1 + +0.4.17.0 + * Add `createFunction` and `deleteFunction` 0.4.16.0 * Add FromField instance for SQLData (thanks @LindaOrtega, @Shimuuar) diff --git a/sqlcipher-simple.cabal b/sqlcipher-simple.cabal index fd685b5..2a4641e 100644 --- a/sqlcipher-simple.cabal +++ b/sqlcipher-simple.cabal @@ -1,5 +1,5 @@ Name: sqlcipher-simple -Version: 0.4.18.1 +Version: 0.4.19.0 Synopsis: Mid-Level SQLite client library Description: Mid-level SQLCipher client library, forked from sqlite-simple. @@ -16,6 +16,7 @@ Maintainer: Evgeny Poberezkin Copyright: (c) 2011 MailRank, Inc., (c) 2011-2012 Leon P Smith, (c) 2012-2014 Janne Hellsten + (c) 2022-2026 Joshua Chia Homepage: https://github.com/simplex-chat/sqlcipher-simple bug-reports: https://github.com/simplex-chat/sqlcipher-simple/issues Stability: stable @@ -23,8 +24,8 @@ Category: Database Build-type: Simple Cabal-version: >= 1.10 - -extra-source-files: README.markdown +tested-with: GHC == 8.10.7 || == 9.0.2 || == 9.2.8 || == 9.4.8 || == 9.6.7 || == 9.8.4 || == 9.10.3 +extra-source-files: README.md changelog Library @@ -57,7 +58,7 @@ Library time, transformers, Only >= 0.1 && < 0.1.1 - + if impl(ghc < 8.0) Build-depends: semigroups >= 0.18 && < 0.20 @@ -94,6 +95,7 @@ test-suite test , TestImports , UserInstances , Utf8Strings + , With ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-unused-do-bind diff --git a/stack-9.0.yaml b/stack-9.0.yaml new file mode 100644 index 0000000..f9994e6 --- /dev/null +++ b/stack-9.0.yaml @@ -0,0 +1 @@ +resolver: lts-19.33 diff --git a/stack-9.10.yaml b/stack-9.10.yaml new file mode 100644 index 0000000..753e117 --- /dev/null +++ b/stack-9.10.yaml @@ -0,0 +1 @@ +resolver: lts-24.33 diff --git a/stack-9.2.yaml b/stack-9.2.yaml new file mode 100644 index 0000000..fc9172f --- /dev/null +++ b/stack-9.2.yaml @@ -0,0 +1 @@ +resolver: lts-20.26 diff --git a/stack-9.4.yaml b/stack-9.4.yaml new file mode 100644 index 0000000..377040a --- /dev/null +++ b/stack-9.4.yaml @@ -0,0 +1 @@ +resolver: lts-21.25 diff --git a/stack-9.6.yaml b/stack-9.6.yaml new file mode 100644 index 0000000..1fe2deb --- /dev/null +++ b/stack-9.6.yaml @@ -0,0 +1 @@ +resolver: lts-22.44 diff --git a/stack-9.8.yaml b/stack-9.8.yaml new file mode 100644 index 0000000..e54b316 --- /dev/null +++ b/stack-9.8.yaml @@ -0,0 +1 @@ +resolver: lts-23.28 diff --git a/stack.yaml b/stack.yaml index a482ea3..f05b0df 100644 --- a/stack.yaml +++ b/stack.yaml @@ -9,4 +9,4 @@ extra-deps: # - direct-sqlcipher-2.3.26 # - ../direct-sqlcipher - github: simplex-chat/direct-sqlcipher - commit: 34309410eb2069b029b8fc1872deb1e0db123294 + commit: 2330df3dc8c4b660674d3c02d76bb86f66c2abee diff --git a/test/Errors.hs b/test/Errors.hs index d1735d1..80821d6 100644 --- a/test/Errors.hs +++ b/test/Errors.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE ScopedTypeVariables, BangPatterns #-} +{-# LANGUAGE ScopedTypeVariables #-} module Errors ( testErrorsColumns @@ -23,15 +23,11 @@ import Data.Time (Day, UTCTime) import Common import Database.SQLite.Simple.Types (Null) --- The "length (show e) `seq` .." trickery below is to force evaluate --- the contents of error messages. Another option would be to log --- them (would be useful), but I don't know if HUnit has any logging --- mechanisms. Just printing them as is will look like the tests are --- hitting errors and would be confusing. -assertResultErrorCaught :: IO a -> Assertion -assertResultErrorCaught action = do - catch (action >> return False) (\(e :: ResultError) -> length (show e) `seq` return True) >>= - assertBool "assertResultError exc" +assertResultErrorThrown :: IO a -> ResultError -> Assertion +assertResultErrorThrown action expectedError = + catch + (action >> assertFailure ("Expected error: " ++ show expectedError ++ ", but nothing was thrown")) + (\(e :: ResultError) -> assertEqual "assertResultErrorThrown" expectedError e) assertFormatErrorCaught :: IO a -> Assertion assertFormatErrorCaught action = do @@ -56,54 +52,78 @@ testErrorsColumns TestEnv{..} = TestCase $ do assertEqual "row count" 1 (length rows) assertEqual "string" (Only "test string") (head rows) -- Mismatched number of output columns (selects two, dest type has 1 field) - assertResultErrorCaught (query_ conn "SELECT id,t FROM cols" :: IO [Only Int]) + assertResultErrorThrown (query_ conn "SELECT id,t FROM cols" :: IO [Only Int]) + ConversionFailed + { errSQLType = "2 values: [(\"INTEGER\",\"SQLInteger 1\"),(\"TEXT\",\"SQLText \\\"test s[...]\")]" + , errHaskellType = "at least 1 slots in target type" + , errMessage = "mismatch between number of columns to convert and number in target type" + } -- Same as above but the other way round (select 1, dst has two) - assertResultErrorCaught (query_ conn "SELECT id FROM cols" :: IO [(Int, String)]) - -- Mismatching types (source int,text doesn't match dst string,int - assertResultErrorCaught (query_ conn "SELECT id, t FROM cols" :: IO [(String, Int)]) - -- Mismatching types (source string doesn't match dst integer - assertResultErrorCaught (query_ conn "SELECT 'foo'" :: IO [Only Integer]) + assertResultErrorThrown (query_ conn "SELECT id FROM cols" :: IO [(Int, String)]) + ConversionFailed + { errSQLType = "1 values: [(\"INTEGER\",\"SQLInteger 1\")]" + , errHaskellType = "at least 2 slots in target type" + , errMessage = "mismatch between number of columns to convert and number in target type" + } + -- Mismatching types (source int,text doesn't match dst string,int) + assertResultErrorThrown (query_ conn "SELECT id, t FROM cols" :: IO [(String, Int)]) + ConversionFailed {errSQLType = "TEXT", errHaskellType = "Int", errMessage = "need an int"} + -- Mismatching types (source string doesn't match dst integer) + assertResultErrorThrown (query_ conn "SELECT 'foo'" :: IO [Only Integer]) + ConversionFailed {errSQLType = "TEXT", errHaskellType = "Integer", errMessage = "need an int"} -- Mismatching types (sources don't match destination float/double type) - assertResultErrorCaught (query_ conn "SELECT 1" :: IO [Only Double]) - assertResultErrorCaught (query_ conn "SELECT 'foo'" :: IO [Only Double]) - assertResultErrorCaught (query_ conn "SELECT 1" :: IO [Only Float]) - assertResultErrorCaught (query_ conn "SELECT 'foo'" :: IO [Only Float]) + assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only Double]) + ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Double", errMessage = "expecting an SQLFloat column type"} + assertResultErrorThrown (query_ conn "SELECT 'foo'" :: IO [Only Double]) + ConversionFailed {errSQLType = "TEXT", errHaskellType = "Double", errMessage = "expecting an SQLFloat column type"} + assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only Float]) + ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Float", errMessage = "expecting an SQLFloat column type"} + assertResultErrorThrown (query_ conn "SELECT 'foo'" :: IO [Only Float]) + ConversionFailed {errSQLType = "TEXT", errHaskellType = "Float", errMessage = "expecting an SQLFloat column type"} -- Mismatching types (sources don't match destination bool type, or is out of bounds) - assertResultErrorCaught (query_ conn "SELECT 'true'" :: IO [Only Bool]) - assertResultErrorCaught (query_ conn "SELECT 2" :: IO [Only Bool]) - -- Mismatching types (sources don't match destination string types (text, string) - assertResultErrorCaught (query_ conn "SELECT 1" :: IO [Only T.Text]) - assertResultErrorCaught (query_ conn "SELECT 1" :: IO [Only LT.Text]) - assertResultErrorCaught (query_ conn "SELECT 1.0" :: IO [Only T.Text]) - assertResultErrorCaught (query_ conn "SELECT 1.0" :: IO [Only LT.Text]) + assertResultErrorThrown (query_ conn "SELECT 'true'" :: IO [Only Bool]) + ConversionFailed {errSQLType = "TEXT", errHaskellType = "Bool", errMessage = "expecting an SQLInteger column type"} + assertResultErrorThrown (query_ conn "SELECT 2" :: IO [Only Bool]) + ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Bool", errMessage = "bool must be 0 or 1, got 2"} + numbers <- query_ conn "SELECT 1, 1.0" :: IO [(T.Text, LT.Text)] + [("1", "1.0")] @=? numbers -- Mismatching types (sources don't match destination string types (time/date) - assertResultErrorCaught (query_ conn "SELECT 1" :: IO [Only UTCTime]) - assertResultErrorCaught (query_ conn "SELECT 1" :: IO [Only Day]) + assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only UTCTime]) + ConversionFailed {errSQLType = "INTEGER", errHaskellType = "UTCTime", errMessage = "expecting SQLText column type"} + assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only Day]) + ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Day", errMessage = "expecting SQLText column type"} -- Mismatching types (sources don't match destination bytestring) [Only (_ :: B.ByteString)] <- query_ conn "SELECT X'3177'" - assertResultErrorCaught (query_ conn "SELECT 1" :: IO [Only B.ByteString]) - assertResultErrorCaught (query_ conn "SELECT 1" :: IO [Only LB.ByteString]) - assertResultErrorCaught (query_ conn "SELECT 'foo'" :: IO [Only B.ByteString]) - assertResultErrorCaught (query_ conn "SELECT 'foo'" :: IO [Only LB.ByteString]) + assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only B.ByteString]) + ConversionFailed {errSQLType = "INTEGER", errHaskellType = "ByteString", errMessage = "expecting SQLBlob column type"} + assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only LB.ByteString]) + ConversionFailed {errSQLType = "INTEGER", errHaskellType = "ByteString", errMessage = "expecting SQLBlob column type"} + assertResultErrorThrown (query_ conn "SELECT 'foo'" :: IO [Only B.ByteString]) + ConversionFailed {errSQLType = "TEXT", errHaskellType = "ByteString", errMessage = "expecting SQLBlob column type"} + assertResultErrorThrown (query_ conn "SELECT 'foo'" :: IO [Only LB.ByteString]) + ConversionFailed {errSQLType = "TEXT", errHaskellType = "ByteString", errMessage = "expecting SQLBlob column type"} -- Trying to get a blob into a string let d = B.pack ([0..127] :: [Word8]) execute_ conn "CREATE TABLE cols_blobs (id INTEGER, b BLOB)" execute conn "INSERT INTO cols_blobs (id, b) VALUES (?,?)" (1 :: Int, d) - assertResultErrorCaught + assertResultErrorThrown (do [Only _t1] <- query conn "SELECT b FROM cols_blobs WHERE id = ?" (Only (1 :: Int)) :: IO [Only String] return ()) + ConversionFailed {errSQLType = "BLOB", errHaskellType = "Text", errMessage = "need convertible to text"} execute_ conn "CREATE TABLE cols_bools (id INTEGER PRIMARY KEY, b BOOLEAN)" -- 3 = invalid value for bool, must be 0 or 1 execute_ conn "INSERT INTO cols_bools (b) VALUES (3)" - assertResultErrorCaught + assertResultErrorThrown (do [Only _t1] <- query_ conn "SELECT b FROM cols_bools" :: IO [Only Bool] return ()) + ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Bool", errMessage = "bool must be 0 or 1, got 3"} [Only (nullVal :: Null)] <- query_ conn "SELECT NULL" False @=? nullVal == nullVal False @=? nullVal /= nullVal - assertResultErrorCaught + assertResultErrorThrown (do [Only (_t1 :: Null)] <- query_ conn "SELECT 1" :: IO [Only Null] return ()) + ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Null", errMessage = "data is not null"} testErrorsInvalidParams :: TestEnv -> Test testErrorsInvalidParams TestEnv{..} = TestCase $ do diff --git a/test/Main.hs b/test/Main.hs index ff5fcc8..5a6188b 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -17,6 +17,7 @@ import TestImports() import TestImports import UserInstances import Utf8Strings +import With tests :: [TestEnv -> Test] tests = @@ -68,7 +69,7 @@ tests = , TestLabel "Imports" . testImports , TestLabel "QQ" . testSimpleQQ , TestLabel "QQ" . testMultiLinedQQ - ] + ] ++ testsWith -- | Action for connecting to the database that will be used for testing. -- diff --git a/test/Simple.hs b/test/Simple.hs index 78abaa8..c0c530d 100644 --- a/test/Simple.hs +++ b/test/Simple.hs @@ -174,7 +174,7 @@ testSimpleUTCTime TestEnv{..} = TestCase $ do [d] <- query conn "SELECT ?" (Only (T.append zulu "Z")) matchDates (zulu, d) where - matchDates (str,(Only date)) = do + matchDates (str, Only date) = do -- Remove 'T' when reading in to Haskell let t = read (makeReadable str) :: UTCTime t @=? date @@ -198,9 +198,9 @@ testSimpleUTCTimeTZ TestEnv{..} = TestCase $ do execute_ conn "CREATE TABLE utctimestz (t TIMESTAMP)" mapM_ (\t -> execute conn "INSERT INTO utctimestz (t) VALUES (?)" (Only t)) timestrs dates <- query_ conn "SELECT t from utctimestz" :: IO [Only UTCTime] - mapM_ matchDates (zip (timestrs) dates) + mapM_ matchDates (zip timestrs dates) where - matchDates (str,(Only date)) = do + matchDates (str, Only date) = do -- Remove 'T' when reading in to Haskell let t = read . T.unpack $ str :: UTCTime t @=? date @@ -221,7 +221,7 @@ testSimpleUTCTimeParams TestEnv{..} = TestCase $ do assertEqual "UTCTime" tstr t testSimpleQueryCov :: TestEnv -> Test -testSimpleQueryCov TestEnv{..} = TestCase $ do +testSimpleQueryCov _ = TestCase $ do let str = "SELECT 1+1" :: T.Text q = "SELECT 1+1" :: Query fromQuery q @=? str diff --git a/test/With.hs b/test/With.hs new file mode 100644 index 0000000..b704fd7 --- /dev/null +++ b/test/With.hs @@ -0,0 +1,367 @@ +{-# LANGUAGE DerivingVia, RankNTypes, ScopedTypeVariables, TypeApplications #-} + +module With ( + testsWith + ) where + +import Control.Exception (Exception, bracket_, catch) +import Control.Monad (replicateM, when) +import Data.List (intercalate) +import Data.Semigroup (Sum(..)) +import Data.String (fromString) +import qualified Data.Text as T +import Data.Typeable (Typeable) + +import Common +import Database.SQLite.Simple.FromRow (RowParser, numFieldsRemaining) + +newtype IntWrapper = IntWrapper Int + deriving (Eq, Show) + deriving (Semigroup, Monoid) via Sum Int + +data TestRowError + = WrongArity + | TooManyNullColumns + deriving (Eq, Show, Typeable) + +instance Exception TestRowError + +data AccessMode = QueryMode | FoldMode + deriving Show + +data BindingMode = Underscore | Positional | Named + deriving Show + +data Table + = ReversePairTable + | ConcatText1Table + | ConcatText2Table + | ConcatText3Table + | ConcatText4Table + deriving (Eq, Show) + +data PathKind = Happy | Unhappy + deriving (Eq, Show) + +-- TableDef is the declarative per-table metadata. It keeps the fixture rows, +-- parser, and binding-mode-specific expected happy results in one place. +data TableDef good bad r e = TableDef + { tableDefName :: String + , tableDefColumns :: [ColumnDef] + , tableDefParser :: RowParser r + , tableDefGoodRows :: [good] + , tableDefBadRows :: [bad] + , tableDefHappyRowsUnsuffixed :: [r] + , tableDefHappyRowsSuffixed :: [r] + , tableDefError :: e + } + +data ColumnDef = ColumnDef + { columnDefName :: String + , columnDefType :: String + } + +-- TableSpec is the fully compiled form used by the test matrix: +-- setup/teardown, bad-row injection, and the final assertion. +data TableSpec = TableSpec + { withTable :: forall a. Connection -> IO a -> IO a + , injectBadRow :: Connection -> IO () + , assertFor :: AccessMode -> BindingMode -> PathKind -> Connection -> Assertion + } + +-- Cover the product of access mode, binding mode, table shape, and path kind. +-- ConcatText4Table is only meaningful on the unhappy path. +testsWith :: [TestEnv -> Test] +testsWith = + [ mkTest accessMode bindingMode table pathKind + | accessMode <- [QueryMode, FoldMode] + , bindingMode <- [Underscore, Positional, Named] + , table <- [ReversePairTable, ConcatText1Table, ConcatText2Table, ConcatText3Table, ConcatText4Table] + , pathKind <- [Happy, Unhappy] + , table /= ConcatText4Table || pathKind == Unhappy + ] + +assertExceptionThrown :: forall e a. (Eq e, Show e, Exception e) => IO a -> e -> Assertion +assertExceptionThrown action expectedError = + catch + (action >> assertFailure ("Expected error: " ++ show expectedError ++ ", but nothing was thrown")) + (\(e :: e) -> assertEqual "assertExceptionThrown" expectedError e) + +-- A 2-column parser that deliberately swaps the SQL column order. +reversePairRowParser :: RowParser (IntWrapper, T.Text) +reversePairRowParser = (\txt i -> (IntWrapper i, txt)) <$> field <*> field + +-- A row-level parser with cross-column validation. It accepts only 2-3 +-- columns, rejects rows with too many NULLs, then reverses and concatenates the +-- rendered column payloads. +concatTextRowParser :: RowParser T.Text +concatTextRowParser = do + n <- numFieldsRemaining + cols <- replicateM n (field :: RowParser SQLData) + if n /= 2 && n /= 3 then + returnRowError WrongArity + else if length (filter isNull cols) > 1 then + returnRowError TooManyNullColumns + else + return . T.concat . reverse $ map payload cols + where + isNull SQLNull = True + isNull _ = False + + payload (SQLText t) = t + payload SQLNull = T.empty + payload (SQLInteger i) = T.pack (show i) + payload (SQLFloat d) = T.pack (show d) + payload (SQLBlob b) = T.pack (show b) + +expectedTypeError :: ResultError +expectedTypeError = + ConversionFailed + { errSQLType = "NULL" + , errHaskellType = "Text" + , errMessage = "need convertible to text" + } + +mkTest :: AccessMode -> BindingMode -> Table -> PathKind -> TestEnv -> Test +mkTest accessMode bindingMode table pathKind TestEnv{conn} = + TestLabel label . TestCase $ + withTable tableSpec conn $ do + when (pathKind == Unhappy) $ + injectBadRow tableSpec conn + assertFor tableSpec accessMode bindingMode pathKind conn + where + tableSpec = compileTable table + label = + "With/" + ++ show accessMode + ++ "/" + ++ functionLabel accessMode bindingMode + ++ "/" + ++ show table + ++ "/" + ++ show pathKind + +compileTable :: Table -> TableSpec +compileTable ReversePairTable = + compileTableDef TableDef + { tableDefName = "with_reverse_pair" + , tableDefColumns = [ColumnDef "t1" "TEXT", ColumnDef "i" "INTEGER"] + , tableDefParser = reversePairRowParser + , tableDefGoodRows = + ([ ("a", 1) + , ("b", 2) + ] :: [(T.Text, Int)]) + , tableDefBadRows = + ([(Nothing @T.Text, Just @Int 8)] :: [(Maybe T.Text, Maybe Int)]) + , tableDefHappyRowsUnsuffixed = + ([ (IntWrapper 1, "a") + , (IntWrapper 2, "b") + ] :: [(IntWrapper, T.Text)]) + , tableDefHappyRowsSuffixed = + ([ (IntWrapper 1, "az") + , (IntWrapper 2, "bz") + ] :: [(IntWrapper, T.Text)]) + , tableDefError = expectedTypeError + } +compileTable ConcatText1Table = + compileTableDef TableDef + { tableDefName = "with_concat_text1" + , tableDefColumns = [ColumnDef "t1" "TEXT", ColumnDef "i" "INTEGER"] + , tableDefParser = concatTextRowParser + , tableDefGoodRows = + ([ (Just "c", Just 3) + , (Nothing, Just 4) + , (Just "d", Nothing) + ] :: [(Maybe T.Text, Maybe Int)]) + , tableDefBadRows = + ([(Nothing, Nothing)] :: [(Maybe T.Text, Maybe Int)]) + , tableDefHappyRowsUnsuffixed = (["4", "3c", "d"] :: [T.Text]) + , tableDefHappyRowsSuffixed = (["4", "3cz", "dz"] :: [T.Text]) + , tableDefError = TooManyNullColumns + } +compileTable ConcatText2Table = + compileTableDef TableDef + { tableDefName = "with_concat_text2" + , tableDefColumns = [ColumnDef "t1" "TEXT", ColumnDef "t2" "TEXT"] + , tableDefParser = concatTextRowParser + , tableDefGoodRows = + ([ (Just "e", Just "f") + , (Nothing, Just "g") + , (Just "h", Nothing) + ] :: [(Maybe T.Text, Maybe T.Text)]) + , tableDefBadRows = + ([(Nothing, Nothing)] :: [(Maybe T.Text, Maybe T.Text)]) + , tableDefHappyRowsUnsuffixed = (["g", "fe", "h"] :: [T.Text]) + , tableDefHappyRowsSuffixed = (["g", "fez", "hz"] :: [T.Text]) + , tableDefError = TooManyNullColumns + } +compileTable ConcatText3Table = + compileTableDef TableDef + { tableDefName = "with_concat_text3" + , tableDefColumns = [ColumnDef "t1" "TEXT", ColumnDef "i" "INTEGER", ColumnDef "t2" "TEXT"] + , tableDefParser = concatTextRowParser + , tableDefGoodRows = + ([ (Just "i", Just 5, Just "j") + , (Nothing, Just 6, Just "k") + , (Just "l", Nothing, Just "m") + , (Just "n", Just 7, Nothing) + ] :: [(Maybe T.Text, Maybe Int, Maybe T.Text)]) + , tableDefBadRows = + ([(Nothing, Nothing, Just "o")] :: [(Maybe T.Text, Maybe Int, Maybe T.Text)]) + , tableDefHappyRowsUnsuffixed = (["k6", "j5i", "ml", "7n"] :: [T.Text]) + , tableDefHappyRowsSuffixed = (["k6", "j5iz", "mlz", "7nz"] :: [T.Text]) + , tableDefError = TooManyNullColumns + } +compileTable ConcatText4Table = + compileTableDef TableDef + { tableDefName = "with_concat_text4" + , tableDefColumns = + [ ColumnDef "t1" "TEXT" + , ColumnDef "t2" "TEXT" + , ColumnDef "t3" "TEXT" + , ColumnDef "t4" "TEXT" + ] + , tableDefParser = concatTextRowParser + , tableDefGoodRows = + ([("p", "q", "r", "s")] :: [(T.Text, T.Text, T.Text, T.Text)]) + , tableDefBadRows = ([] :: [(T.Text, T.Text, T.Text, T.Text)]) + , tableDefHappyRowsUnsuffixed = ([] :: [T.Text]) + , tableDefHappyRowsSuffixed = ([] :: [T.Text]) + , tableDefError = WrongArity + } + +-- Turn declarative table metadata into executable test behavior. +compileTableDef + :: (ToRow good, ToRow bad, Eq r, Show r, Monoid r, Eq e, Show e, Exception e) + => TableDef good bad r e + -> TableSpec +compileTableDef TableDef + { tableDefName + , tableDefColumns + , tableDefParser + , tableDefGoodRows + , tableDefBadRows + , tableDefHappyRowsUnsuffixed + , tableDefHappyRowsSuffixed + , tableDefError + } = + TableSpec + { withTable = \conn -> + bracket_ + (do + execute_ conn (createTableSql tableDefName tableDefColumns) + executeMany conn (insertSql tableDefName tableDefColumns) tableDefGoodRows) + (execute_ conn ("DROP TABLE " <> fromString tableDefName)) + , injectBadRow = \conn -> + executeMany conn (insertSql tableDefName tableDefColumns) tableDefBadRows + , assertFor = + mkAssertFor + tableDefParser + tableDefName + tableDefColumns + tableDefHappyRowsUnsuffixed + tableDefHappyRowsSuffixed + tableDefError + } + +-- The same table data is checked through both query-like and fold-like APIs. +-- Underscore uses the unsuffixed expectations; positional and named use the +-- suffixed expectations because they bind the same "z" suffix parameter. +mkAssertFor + :: (Eq r, Show r, Monoid r, Eq e, Show e, Exception e) + => RowParser r + -> String + -> [ColumnDef] + -> [r] + -> [r] + -> e + -> AccessMode -> BindingMode -> PathKind -> Connection -> Assertion +mkAssertFor parser tableName columns happyRowsUnsuffixed happyRowsSuffixed unhappyError accessMode bindingMode pathKind conn = + case (accessMode, pathKind) of + (QueryMode, Happy) -> do + actual <- runQuerySpec parser tableName columns bindingMode conn + expectedHappyRows @=? actual + (QueryMode, Unhappy) -> + assertExceptionThrown + (runQuerySpec parser tableName columns bindingMode conn) + unhappyError + (FoldMode, Happy) -> do + actual <- runFoldSpec parser tableName columns bindingMode conn + mconcat expectedHappyRows @=? actual + (FoldMode, Unhappy) -> + assertExceptionThrown + (runFoldSpec parser tableName columns bindingMode conn) + unhappyError + where + expectedHappyRows = + case bindingMode of + Underscore -> happyRowsUnsuffixed + Positional -> happyRowsSuffixed + Named -> happyRowsSuffixed + +-- BindingMode changes how the first selected column is formed and how the +-- suffix parameter is passed to the library call. +runQuerySpec :: RowParser r -> String -> [ColumnDef] -> BindingMode -> Connection -> IO [r] +runQuerySpec parser tableName columns bindingMode conn = + case bindingMode of + Underscore -> + queryWith_ parser conn (selectSql Underscore tableName columns) + Positional -> + queryWith parser conn (selectSql Positional tableName columns) (Only @T.Text "z") + Named -> + queryNamedWith parser conn (selectSql Named tableName columns) [":suffix" := ("z" :: T.Text)] + +runFoldSpec :: Monoid r => RowParser r -> String -> [ColumnDef] -> BindingMode -> Connection -> IO r +runFoldSpec parser tableName columns bindingMode conn = + case bindingMode of + Underscore -> + foldWith_ parser conn (selectSql Underscore tableName columns) mempty appendMonoid + Positional -> + foldWith parser conn (selectSql Positional tableName columns) (Only @T.Text "z") mempty appendMonoid + Named -> + foldNamedWith parser conn (selectSql Named tableName columns) [":suffix" := ("z" :: T.Text)] mempty appendMonoid + where + appendMonoid acc row = return (acc <> row) + +createTableSql :: String -> [ColumnDef] -> Query +createTableSql name columns = + fromString + ( "CREATE TABLE " + ++ name + ++ " (" + ++ intercalate ", " [columnDefName ++ " " ++ columnDefType | ColumnDef{columnDefName, columnDefType} <- columns] + ++ ")" + ) + +insertSql :: String -> [ColumnDef] -> Query +insertSql name columns = + fromString + ( "INSERT INTO " + ++ name + ++ " VALUES (" + ++ intercalate ", " (replicate (length columns) "?") + ++ ")" + ) + +selectSql :: BindingMode -> String -> [ColumnDef] -> Query +selectSql bindingMode tableName (firstCol:restCols) = + fromString + ( "SELECT " + ++ intercalate ", " (renderFirst bindingMode firstCol : map columnDefName restCols) + ++ " FROM " + ++ tableName + ++ " ORDER BY t1" + ) + where + renderFirst Underscore = columnDefName + renderFirst Positional = \col -> columnDefName col ++ " || ?" + renderFirst Named = \col -> columnDefName col ++ " || :suffix" +selectSql _ _ [] = error "selectSql requires at least one column" + +functionLabel :: AccessMode -> BindingMode -> String +functionLabel QueryMode Underscore = "queryWith_" +functionLabel QueryMode Positional = "queryWith" +functionLabel QueryMode Named = "queryNamedWith" +functionLabel FoldMode Underscore = "foldWith_" +functionLabel FoldMode Positional = "foldWith" +functionLabel FoldMode Named = "foldNamedWith"