mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-08-28 05:04:40 +00:00
api to execute any sql query (#529)
* api to execute any sql query * agent api
This commit is contained in:
@@ -71,6 +71,7 @@ module Simplex.Messaging.Agent
|
||||
toggleConnectionNtfs,
|
||||
activateAgent,
|
||||
suspendAgent,
|
||||
execAgentStoreSQL,
|
||||
logConnection,
|
||||
)
|
||||
where
|
||||
@@ -91,6 +92,7 @@ import qualified Data.List.NonEmpty as L
|
||||
import Data.Map.Strict (Map)
|
||||
import qualified Data.Map.Strict as M
|
||||
import Data.Maybe (isJust)
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text as T
|
||||
import Data.Time.Clock
|
||||
import Data.Time.Clock.System (systemToUTCTime)
|
||||
@@ -273,6 +275,9 @@ activateAgent c = withAgentEnv c $ activateAgent' c
|
||||
suspendAgent :: AgentErrorMonad m => AgentClient -> Int -> m ()
|
||||
suspendAgent c = withAgentEnv c . suspendAgent' c
|
||||
|
||||
execAgentStoreSQL :: AgentErrorMonad m => AgentClient -> Text -> m [Text]
|
||||
execAgentStoreSQL c = withAgentEnv c . execAgentStoreSQL' c
|
||||
|
||||
withAgentEnv :: AgentClient -> ReaderT Env m a -> m a
|
||||
withAgentEnv c = (`runReaderT` agentEnv c)
|
||||
|
||||
@@ -1180,6 +1185,9 @@ suspendAgent' c@AgentClient {agentState = as} maxDelay = do
|
||||
-- unsafeIOToSTM $ putStrLn $ "in timeout: suspendSendingAndDatabase"
|
||||
suspendSendingAndDatabase c
|
||||
|
||||
execAgentStoreSQL' :: AgentMonad m => AgentClient -> Text -> m [Text]
|
||||
execAgentStoreSQL' c sql = withStore' c (`exexSQL` sql)
|
||||
|
||||
getSMPServer :: AgentMonad m => AgentClient -> m SMPServer
|
||||
getSMPServer c = readTVarIO (smpServers c) >>= pickServer
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ module Simplex.Messaging.Agent.Store.SQLite
|
||||
connectSQLiteStore,
|
||||
closeSQLiteStore,
|
||||
sqlString,
|
||||
exexSQL,
|
||||
|
||||
-- * Queues and connections
|
||||
createNewConn,
|
||||
@@ -240,6 +241,17 @@ sqlString s = quote <> T.replace quote "''" (T.pack s) <> quote
|
||||
-- auto_vacuum <- DB.query_ db "PRAGMA auto_vacuum;" :: IO [[Int]]
|
||||
-- print $ path <> " auto_vacuum: " <> show auto_vacuum
|
||||
|
||||
exexSQL :: DB.Connection -> Text -> IO [Text]
|
||||
exexSQL db query = do
|
||||
rs <- newTVarIO []
|
||||
SQLite3.execWithCallback (DB.connectionHandle db) query (addRow rs)
|
||||
reverse <$> readTVarIO rs
|
||||
where
|
||||
addRow rs _count names values = atomically . modifyTVar' rs $ \case
|
||||
[] -> [showValues values, T.intercalate "|" names]
|
||||
rs' -> showValues values : rs'
|
||||
showValues = T.intercalate "|" . map (fromMaybe "")
|
||||
|
||||
checkConstraint :: StoreError -> IO (Either StoreError a) -> IO (Either StoreError a)
|
||||
checkConstraint err action = action `E.catch` (pure . Left . handleSQLError err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user