@@ -67,8 +67,8 @@ import Simplex.Messaging.Agent.Store.Migrations (DBMigrate (..), sharedMigrateSc
6767import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations
6868import Simplex.Messaging.Agent.Store.SQLite.Common
6969import qualified Simplex.Messaging.Agent.Store.SQLite.DB as DB
70- import Simplex.Messaging.Agent.Store.Shared (Migration (.. ), MigrationConfig (.. ), MigrationError (.. ))
7170import Simplex.Messaging.Agent.Store.SQLite.Util (SQLiteFunc , createStaticFunction , mkSQLiteFunc )
71+ import Simplex.Messaging.Agent.Store.Shared (Migration (.. ), MigrationConfig (.. ), MigrationError (.. ))
7272import qualified Simplex.Messaging.Crypto as C
7373import Simplex.Messaging.Util (ifM , safeDecodeUtf8 )
7474import System.Directory (copyFile , createDirectoryIfMissing , doesFileExist )
@@ -77,10 +77,10 @@ import System.FilePath (takeDirectory, takeFileName, (</>))
7777-- * SQLite Store implementation
7878
7979createDBStore :: DBOpts -> [Migration ] -> MigrationConfig -> IO (Either MigrationError DBStore )
80- createDBStore opts@ DBOpts {dbFilePath, dbKey, keepKey, track } migrations migrationConfig = do
80+ createDBStore opts@ DBOpts {dbFilePath} migrations migrationConfig = do
8181 let dbDir = takeDirectory dbFilePath
8282 createDirectoryIfMissing True dbDir
83- st <- connectSQLiteStore dbFilePath dbKey keepKey track
83+ st <- connectSQLiteStore opts
8484 r <- migrateDBSchema st opts Nothing migrations migrationConfig `onException` closeDBStore st
8585 case r of
8686 Right () -> pure $ Right st
@@ -99,23 +99,24 @@ migrateDBSchema st DBOpts {dbFilePath, vacuum} migrationsTable migrations Migrat
9999 dbm = DBMigrate {initialize, getCurrent, run, backup}
100100 in sharedMigrateSchema dbm (dbNew st) migrations confirm
101101
102- connectSQLiteStore :: FilePath -> ScrubbedBytes -> Bool -> DB. TrackQueries -> IO DBStore
103- connectSQLiteStore dbFilePath key keepKey track = do
102+ connectSQLiteStore :: DBOpts -> IO DBStore
103+ connectSQLiteStore DBOpts { dbFilePath, dbFunctions, dbKey = key, keepKey, track} = do
104104 dbNew <- not <$> doesFileExist dbFilePath
105- dbConn <- dbBusyLoop ( connectDB dbFilePath key track)
105+ dbConn <- dbBusyLoop $ connectDB dbFilePath dbFunctions key track
106106 dbConnection <- newMVar dbConn
107107 dbKey <- newTVarIO $! storeKey key keepKey
108108 dbClosed <- newTVarIO False
109109 dbSem <- newTVarIO 0
110- pure DBStore {dbFilePath, dbKey, dbSem, dbConnection, dbNew, dbClosed}
110+ pure DBStore {dbFilePath, dbFunctions, dbKey, dbSem, dbConnection, dbNew, dbClosed}
111111
112- connectDB :: FilePath -> ScrubbedBytes -> DB. TrackQueries -> IO DB. Connection
113- connectDB path key track = do
112+ connectDB :: FilePath -> [ SQLiteFuncDef ] -> ScrubbedBytes -> DB. TrackQueries -> IO DB. Connection
113+ connectDB path functions key track = do
114114 db <- DB. open path track
115115 prepare db `onException` DB. close db
116116 -- _printPragmas db path
117117 pure db
118118 where
119+ functions' = SQLiteFuncDef " simplex_xor_md5_combine" 2 True sqliteXorMd5CombinePtr : functions
119120 prepare db = do
120121 let db' = SQL. connectionHandle $ DB. conn db
121122 unless (BA. null key) . SQLite3. exec db' $ " PRAGMA key = " <> keyString key <> " ;"
@@ -127,8 +128,9 @@ connectDB path key track = do
127128 PRAGMA secure_delete = ON;
128129 PRAGMA auto_vacuum = FULL;
129130 |]
130- createStaticFunction db' " simplex_xor_md5_combine" 2 True sqliteXorMd5CombinePtr
131- >>= either (throwIO . userError . show ) pure
131+ forM_ functions' $ \ SQLiteFuncDef {funcName, argCount, deterministic, funcPtr} ->
132+ createStaticFunction db' funcName argCount deterministic funcPtr
133+ >>= either (throwIO . userError . show ) pure
132134
133135foreign export ccall " simplex_xor_md5_combine" sqliteXorMd5Combine :: SQLiteFunc
134136
@@ -155,12 +157,12 @@ openSQLiteStore st@DBStore {dbClosed} key keepKey =
155157 ifM (readTVarIO dbClosed) (openSQLiteStore_ st key keepKey) (putStrLn " openSQLiteStore: already opened" )
156158
157159openSQLiteStore_ :: DBStore -> ScrubbedBytes -> Bool -> IO ()
158- openSQLiteStore_ DBStore {dbConnection, dbFilePath, dbKey, dbClosed} key keepKey =
160+ openSQLiteStore_ DBStore {dbConnection, dbFilePath, dbFunctions, dbKey, dbClosed} key keepKey =
159161 bracketOnError
160162 (takeMVar dbConnection)
161163 (tryPutMVar dbConnection)
162164 $ \ DB. Connection {slow, track} -> do
163- DB. Connection {conn} <- connectDB dbFilePath key track
165+ DB. Connection {conn} <- connectDB dbFilePath dbFunctions key track
164166 atomically $ do
165167 writeTVar dbClosed False
166168 writeTVar dbKey $! storeKey key keepKey
0 commit comments