diff --git a/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h --- a/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h @@ -103,6 +103,10 @@ const std::vector &messages) const = 0; virtual std::vector getAllMessagesToDevice(const std::string &deviceID) const = 0; + virtual void removeMessagesToDeviceOlderThan( + const ClientMessageToDevice &lastConfirmedMessage) const = 0; + virtual void + removeAllMessagesForDevice(const std::string &deviceID) const = 0; #ifdef EMSCRIPTEN virtual std::vector getAllThreadsWeb() const = 0; diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h @@ -116,6 +116,9 @@ const std::vector &messages) const override; std::vector getAllMessagesToDevice(const std::string &deviceID) const override; + void removeMessagesToDeviceOlderThan( + const ClientMessageToDevice &lastConfirmedMessage) const override; + void removeAllMessagesForDevice(const std::string &deviceID) const override; #ifdef EMSCRIPTEN std::vector getAllThreadsWeb() const override; diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp @@ -1731,6 +1731,47 @@ return messages; } + +void SQLiteQueryExecutor::removeMessagesToDeviceOlderThan( + const ClientMessageToDevice &lastConfirmedMessageClient) const { + static std::string query = + "DELETE FROM messages_to_device " + "WHERE timestamp <= ? AND device_id IN (?);"; + + MessageToDevice lastConfirmedMessage = + lastConfirmedMessageClient.toMessageToDevice(); + + comm::SQLiteStatementWrapper preparedSQL( + SQLiteQueryExecutor::getConnection(), + query, + "Failed to remove messages to device"); + + sqlite3_bind_int64(preparedSQL, 1, lastConfirmedMessage.timestamp); + sqlite3_bind_text( + preparedSQL, + 2, + lastConfirmedMessage.device_id.c_str(), + -1, + SQLITE_TRANSIENT); + + int result = sqlite3_step(preparedSQL); + if (result != SQLITE_DONE) { + throw std::runtime_error( + "Failed to execute removeMessagesToDeviceOlderThan statement: " + + std::string(sqlite3_errmsg(SQLiteQueryExecutor::getConnection()))); + } +} + +void SQLiteQueryExecutor::removeAllMessagesForDevice( + const std::string &deviceID) const { + static std::string removeMessagesSQL = + "DELETE FROM messages_to_device " + "WHERE device_id IN (?);"; + std::vector keys = {deviceID}; + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), removeMessagesSQL, keys); +} + #ifdef EMSCRIPTEN std::vector SQLiteQueryExecutor::getAllThreadsWeb() const { auto threads = this->getAllThreads(); diff --git a/web/database/_generated/comm_query_executor.wasm b/web/database/_generated/comm_query_executor.wasm index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 GIT binary patch literal 0 Hc$@