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 @@ -168,6 +168,10 @@ virtual std::vector getAllInboundP2PMessage() const = 0; virtual void removeInboundP2PMessages(const std::vector &ids) const = 0; + virtual void updateMessageSearchResult( + std::string original_message_id, + std::string message_id, + std::string processed_content) 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 @@ -177,6 +177,10 @@ std::vector getAllInboundP2PMessage() const override; void removeInboundP2PMessages(const std::vector &ids) const override; + void updateMessageSearchResult( + std::string original_message_id, + std::string message_id, + std::string processed_content) 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 @@ -1433,6 +1433,54 @@ SQLiteQueryExecutor::getConnection(), replaceMessageSQL, message); } +void SQLiteQueryExecutor::updateMessageSearchResult( + std::string original_message_id, + std::string message_id, + std::string processed_content) const { + + sqlite3 *db = SQLiteQueryExecutor::getConnection(); + int bindResult = 0; + std::unique_ptr preparedSQL; + + static std::string insertMessageSearchResultSQL = + "INSERT INTO message_search(" + " original_message_id, message_id, processed_content) " + "VALUES (?, ?, ?);"; + static std::string updateMessageSearchResultSQL = + "UPDATE message_search " + "SET message_id=?, processed_content=? " + "WHERE original_message_id=?;"; + + if (original_message_id == message_id) { + preparedSQL = std::make_unique( + db, + insertMessageSearchResultSQL, + "Failed to update message search entry."); + + bindStringToSQL(original_message_id, *preparedSQL, 1); + bindStringToSQL(message_id, *preparedSQL, 2); + bindResult = bindStringToSQL(processed_content, *preparedSQL, 3); + } else { + preparedSQL = std::make_unique( + db, + updateMessageSearchResultSQL, + "Failed to update message search entry."); + + bindStringToSQL(message_id, *preparedSQL, 1); + bindStringToSQL(processed_content, *preparedSQL, 2); + bindResult = bindStringToSQL(original_message_id, *preparedSQL, 3); + } + + if (bindResult != SQLITE_OK) { + std::stringstream error_message; + error_message << "Failed to bind key to SQL statement. Details: " + << sqlite3_errstr(bindResult) << std::endl; + throw std::runtime_error(error_message.str()); + } + + sqlite3_step(*preparedSQL); +} + void SQLiteQueryExecutor::rekeyMessage(std::string from, std::string to) const { static std::string rekeyMessageSQL = "UPDATE OR REPLACE messages "