diff --git a/.github/workflows/haskell-ci.yml b/.github/workflows/haskell-ci.yml index a2b5e3b..d17436d 100644 --- a/.github/workflows/haskell-ci.yml +++ b/.github/workflows/haskell-ci.yml @@ -1,6 +1,6 @@ # This GitHub workflow config has been generated by a script via # -# haskell-ci '--distribution' 'noble' 'github' 'sqlite-simple.cabal' +# haskell-ci '--distribution' 'noble' 'github' 'sqlcipher-simple.cabal' # # To regenerate the script (for example after adjusting tested-with) run # @@ -10,7 +10,7 @@ # # version: 0.19.20250506 # -# REGENDATA ("0.19.20250506",["--distribution","noble","github","sqlite-simple.cabal"]) +# REGENDATA ("0.19.20250506",["--distribution","noble","github","sqlcipher-simple.cabal"]) # name: Haskell-CI on: @@ -171,17 +171,21 @@ jobs: find sdist -maxdepth 1 -type f -name '*.tar.gz' -exec tar -C $GITHUB_WORKSPACE/unpacked -xzvf {} \; - name: generate cabal.project run: | - PKGDIR_sqlite_simple="$(find "$GITHUB_WORKSPACE/unpacked" -maxdepth 1 -type d -regex '.*/sqlite-simple-[0-9.]*')" - echo "PKGDIR_sqlite_simple=${PKGDIR_sqlite_simple}" >> "$GITHUB_ENV" + PKGDIR_sqlcipher_simple="$(find "$GITHUB_WORKSPACE/unpacked" -maxdepth 1 -type d -regex '.*/sqlcipher-simple-[0-9.]*')" + echo "PKGDIR_sqlcipher_simple=${PKGDIR_sqlcipher_simple}" >> "$GITHUB_ENV" rm -f cabal.project cabal.project.local touch cabal.project touch cabal.project.local - echo "packages: ${PKGDIR_sqlite_simple}" >> cabal.project - echo "package sqlite-simple" >> cabal.project + echo "packages: ${PKGDIR_sqlcipher_simple}" >> cabal.project + echo "package sqlcipher-simple" >> cabal.project echo " ghc-options: -Werror=missing-methods" >> cabal.project cat >> cabal.project <> cabal.project.local + $HCPKG list --simple-output --names-only | perl -ne 'for (split /\s+/) { print "constraints: any.$_ installed\n" unless /^(sqlcipher-simple)$/; }' >> cabal.project.local cat cabal.project cat cabal.project.local - name: dump install plan @@ -209,7 +213,7 @@ jobs: $CABAL v2-test $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --test-show-details=direct - name: cabal check run: | - cd ${PKGDIR_sqlite_simple} || false + cd ${PKGDIR_sqlcipher_simple} || false ${CABAL} -vnormal check - name: haddock run: | diff --git a/Database/SQLite/Simple.hs b/Database/SQLite/Simple.hs index b12a5ae..669eb18 100644 --- a/Database/SQLite/Simple.hs +++ b/Database/SQLite/Simple.hs @@ -109,7 +109,7 @@ module Database.SQLite.Simple ( ) where import Control.Exception -import Control.Monad (void, when, forM_) +import Control.Monad (void, when, unless, forM_) import Control.Monad.Trans.Reader import Control.Monad.Trans.State.Strict import Data.Int (Int64) @@ -542,7 +542,7 @@ withTransactionPrivate conn action ttype = mask $ \restore -> do begin r <- restore action `onException` rollback - commit + commit `onException` rollback return r where begin = execute_ conn $ case ttype of @@ -553,9 +553,11 @@ withTransactionPrivate conn action ttype = commit = execute_ conn $ case ttype of Savepoint name -> Query $ "RELEASE '" <> name <> "'" _ -> "COMMIT TRANSACTION" - rollback = execute_ conn $ case ttype of - Savepoint name -> Query $ "ROLLBACK TO '" <> name <> "'" - _ -> "ROLLBACK TRANSACTION" + rollback = do + autocommit <- BaseD.getAutoCommit $ connectionHandle conn + unless autocommit $ execute_ conn $ case ttype of + Savepoint name -> Query $ "ROLLBACK TO '" <> name <> "'" + _ -> "ROLLBACK TRANSACTION" -- | Run an IO action inside a SQL transaction started with @BEGIN IMMEDIATE @@ -611,15 +613,17 @@ withTransaction conn action = -- | Run an IO action inside an SQLite @SAVEPOINT@. If the action throws any -- kind of an exception, the transaction will be rolled back to the savepoint -- with @ROLLBACK TO@. Otherwise the results are released to the outer --- transaction if any with @RELEASE@. +-- transaction with @RELEASE@. Outside of a transaction, the action is run +-- with 'withTransaction'. -- -- See for a full description of -- savepoint semantics. withSavepoint :: Connection -> IO a -> IO a withSavepoint conn action = do n <- atomicModifyIORef' (connectionTempNameCounter conn) $ \n -> (n + 1, n) + autocommit <- BaseD.getAutoCommit $ connectionHandle conn withTransactionPrivate conn action $ - Savepoint $ "sqlite_simple_savepoint_" <> T.pack (show n) + if autocommit then Deferred else Savepoint $ "sqlite_simple_savepoint_" <> T.pack (show n) fmtError :: Show v => String -> Query -> [v] -> a fmtError msg q xs = diff --git a/Database/SQLite/Simple/FromField.hs b/Database/SQLite/Simple/FromField.hs index 5ade933..ca9f0d8 100644 --- a/Database/SQLite/Simple/FromField.hs +++ b/Database/SQLite/Simple/FromField.hs @@ -159,16 +159,17 @@ instance FromField Bool where fromField f = returnError ConversionFailed f "expecting an SQLInteger column type" instance FromField T.Text where - fromField (Field (SQLText txt) _) = Ok txt - fromField f = returnError ConversionFailed f "need a text" + fromField f@(Field sqlData _) = case sqlData of + SQLText v -> Ok v + SQLInteger v -> Ok $ T.pack $ show v + SQLFloat v -> Ok $ T.pack $ show v + _ -> returnError ConversionFailed f "need convertible to text" instance FromField LT.Text where - fromField (Field (SQLText txt) _) = Ok . LT.fromStrict $ txt - fromField f = returnError ConversionFailed f "need a text" + fromField f = LT.fromStrict <$> fromField f instance FromField [Char] where - fromField (Field (SQLText t) _) = Ok $ T.unpack t - fromField f = returnError ConversionFailed f "expecting SQLText column type" + fromField f = T.unpack <$> fromField f instance FromField ByteString where fromField (Field (SQLBlob blb) _) = Ok blb diff --git a/cabal.project b/cabal.project new file mode 100644 index 0000000..f8d4688 --- /dev/null +++ b/cabal.project @@ -0,0 +1,7 @@ +packages: . +-- packages: . ../direct-sqlcipher + +source-repository-package + type: git + location: https://github.com/simplex-chat/direct-sqlcipher.git + tag: 2330df3dc8c4b660674d3c02d76bb86f66c2abee diff --git a/sqlite-simple.cabal b/sqlcipher-simple.cabal similarity index 82% rename from sqlite-simple.cabal rename to sqlcipher-simple.cabal index e581d59..d010878 100644 --- a/sqlite-simple.cabal +++ b/sqlcipher-simple.cabal @@ -1,24 +1,24 @@ -Name: sqlite-simple +Name: sqlcipher-simple Version: 0.4.19.0 Synopsis: Mid-Level SQLite client library Description: - Mid-level SQLite client library, based on postgresql-simple. + Mid-level SQLCipher client library, forked from sqlite-simple. . Main documentation (with examples): . - You can view the project page at + You can view the project page at for more information. License: BSD3 License-file: LICENSE Author: Bryan O'Sullivan, Leon P Smith, Janne Hellsten -Maintainer: Janne Hellsten +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: http://github.com/nurpax/sqlite-simple -bug-reports: http://github.com/nurpax/sqlite-simple/issues +Homepage: https://github.com/simplex-chat/sqlcipher-simple +bug-reports: https://github.com/simplex-chat/sqlcipher-simple/issues Stability: stable Category: Database Build-type: Simple @@ -51,7 +51,7 @@ Library blaze-textual, bytestring >= 0.9, containers, - direct-sqlite >= 2.3.13 && < 2.4, + direct-sqlcipher >= 2.3.13 && < 2.4, exceptions >= 0.4, template-haskell, text >= 0.11, @@ -73,7 +73,7 @@ Library source-repository head type: git - location: http://github.com/nurpax/sqlite-simple + location: http://github.com/simplex-chat/sqlcipher-simple test-suite test @@ -108,8 +108,9 @@ test-suite test build-depends: base , base16-bytestring , bytestring >= 0.9 + , directory , HUnit - , sqlite-simple - , direct-sqlite + , sqlcipher-simple + , direct-sqlcipher , text , time diff --git a/stack.yaml b/stack.yaml deleted file mode 120000 index 3610612..0000000 --- a/stack.yaml +++ /dev/null @@ -1 +0,0 @@ -stack-9.10.yaml \ No newline at end of file diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 0000000..f05b0df --- /dev/null +++ b/stack.yaml @@ -0,0 +1,12 @@ +resolver: lts-15.1 # 8.8.2 - base-4.13 +# resolver: lts-14.27 # 8.6.5 - base-4.12 +# resolver: lts-12.26 # 8.4.4 - base-4.11 +# resolver: lts-11.22 # 8.2.2 - base-4.10 +# resolver: lts-9.21 # 8.0.2 - base-4.9 +# resolver: lts-6.35 # 7.10.3 - base-4.8 + +extra-deps: + # - direct-sqlcipher-2.3.26 + # - ../direct-sqlcipher + - github: simplex-chat/direct-sqlcipher + commit: 2330df3dc8c4b660674d3c02d76bb86f66c2abee diff --git a/test/Errors.hs b/test/Errors.hs index 40ab7a4..3fb74b1 100644 --- a/test/Errors.hs +++ b/test/Errors.hs @@ -10,6 +10,8 @@ module Errors ( , testErrorsImmediateTransaction , testErrorsExclusiveTransaction , testErrorsSavepoint + , testErrorsBusyCommit + , testErrorsEndedTransaction ) where import Control.Exception @@ -19,6 +21,8 @@ import qualified Data.Text as T import qualified Data.Text.Lazy as LT import Data.Word import Data.Time (Day, UTCTime) +import System.Directory (getTemporaryDirectory, removeFile) +import System.IO (hClose, openTempFile) import Common import Database.SQLite.Simple.Types (Null) @@ -29,6 +33,12 @@ assertResultErrorThrown action expectedError = (action >> assertFailure ("Expected error: " ++ show expectedError ++ ", but nothing was thrown")) (\(e :: ResultError) -> assertEqual "assertResultErrorThrown" expectedError e) +assertBusyThrown :: IO a -> Assertion +assertBusyThrown action = + catch + (action >> assertFailure "Expected ErrorBusy, but nothing was thrown") + (\(e :: SQLError) -> assertEqual "assertBusyThrown" ErrorBusy (sqlError e)) + assertFormatErrorCaught :: IO a -> Assertion assertFormatErrorCaught action = do catch (action >> return False) (\(e :: FormatError) -> length (show e) `seq` return True) >>= @@ -67,7 +77,7 @@ testErrorsColumns TestEnv{..} = TestCase $ do } -- Mismatching types (source int,text doesn't match dst string,int) assertResultErrorThrown (query_ conn "SELECT id, t FROM cols" :: IO [(String, Int)]) - ConversionFailed {errSQLType = "INTEGER", errHaskellType = "[Char]", errMessage = "expecting SQLText column type"} + 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"} @@ -85,15 +95,8 @@ testErrorsColumns TestEnv{..} = TestCase $ do 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"} - -- Mismatching types (sources don't match destination string types (text, string) - assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only T.Text]) - ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Text", errMessage = "need a text"} - assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only LT.Text]) - ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Text", errMessage = "need a text"} - assertResultErrorThrown (query_ conn "SELECT 1.0" :: IO [Only T.Text]) - ConversionFailed {errSQLType = "FLOAT", errHaskellType = "Text", errMessage = "need a text"} - assertResultErrorThrown (query_ conn "SELECT 1.0" :: IO [Only LT.Text]) - ConversionFailed {errSQLType = "FLOAT", errHaskellType = "Text", errMessage = "need a text"} + 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) assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only UTCTime]) ConversionFailed {errSQLType = "INTEGER", errHaskellType = "UTCTime", errMessage = "expecting SQLText column type"} @@ -116,7 +119,7 @@ testErrorsColumns TestEnv{..} = TestCase $ do assertResultErrorThrown (do [Only _t1] <- query conn "SELECT b FROM cols_blobs WHERE id = ?" (Only (1 :: Int)) :: IO [Only String] return ()) - ConversionFailed {errSQLType = "BLOB", errHaskellType = "[Char]", errMessage = "expecting SQLText column type"} + 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)" @@ -297,6 +300,7 @@ testErrorsSavepoint TestEnv{..} = TestCase $ do "INSERT INTO strans (t) VALUES (:txt)" [":missing" := ("foo" :: String)]) e <- rowExists False @=? e + withTransaction conn $ return () where rowExists = do rows <- query_ conn "SELECT t FROM strans" :: IO [Only String] @@ -307,3 +311,24 @@ testErrorsSavepoint TestEnv{..} = TestCase $ do [] -> return False _ -> error "should have only one row" + +testErrorsBusyCommit :: (Connection -> IO () -> IO ()) -> TestEnv -> Test +testErrorsBusyCommit transaction _ = TestCase $ do + dir <- getTemporaryDirectory + bracket (openTempFile dir "busy.db") (removeFile . fst) $ \(path, h) -> do + hClose h + withConnection path $ \writer -> withConnection path $ \reader -> do + let insert = transaction writer $ execute_ writer "INSERT INTO busy DEFAULT VALUES" + execute_ writer "CREATE TABLE busy (id INTEGER PRIMARY KEY)" + withTransaction reader $ do + _ <- query_ reader "SELECT id FROM busy" :: IO [Only Int] + assertBusyThrown insert + insert + [Only n] <- query_ reader "SELECT count(*) FROM busy" + (1 :: Int) @=? n + +testErrorsEndedTransaction :: TestEnv -> Test +testErrorsEndedTransaction TestEnv{..} = TestCase $ + catch + (withTransaction conn (execute_ conn "ROLLBACK") >> assertFailure "Expected SQLError, but nothing was thrown") + (\(e :: SQLError) -> "cannot commit - no transaction is active" @=? sqlErrorDetails e) diff --git a/test/Main.hs b/test/Main.hs index 5a6188b..88d063b 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -54,6 +54,9 @@ tests = , TestLabel "Errors" . testErrorsImmediateTransaction , TestLabel "Errors" . testErrorsExclusiveTransaction , TestLabel "Errors" . testErrorsSavepoint + , TestLabel "Errors" . testErrorsBusyCommit withImmediateTransaction + , TestLabel "Errors" . testErrorsBusyCommit withSavepoint + , TestLabel "Errors" . testErrorsEndedTransaction , TestLabel "Utf8" . testUtf8Simplest , TestLabel "Utf8" . testBlobs , TestLabel "Instances" . testUserFromField diff --git a/test/With.hs b/test/With.hs index 43a8cc3..b704fd7 100644 --- a/test/With.hs +++ b/test/With.hs @@ -119,7 +119,7 @@ expectedTypeError = ConversionFailed { errSQLType = "NULL" , errHaskellType = "Text" - , errMessage = "need a text" + , errMessage = "need convertible to text" } mkTest :: AccessMode -> BindingMode -> Table -> PathKind -> TestEnv -> Test