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 @@ -1149,14 +1149,16 @@ bool SQLiteQueryExecutor::moveDraft(std::string oldKey, std::string newKey) const { - std::unique_ptr draft = - SQLiteQueryExecutor::getStorage().get_pointer(oldKey); - if (draft == nullptr) { + std::string draftText = this->getDraft(oldKey); + if (!draftText.size()) { return false; } - draft->key = newKey; - SQLiteQueryExecutor::getStorage().replace(*draft); - SQLiteQueryExecutor::getStorage().remove(oldKey); + static std::string rekeyDraftSQL = + "UPDATE OR REPLACE drafts " + "SET key = ? " + "WHERE key = ?;"; + rekeyAllEntities( + SQLiteQueryExecutor::getConnection(), rekeyDraftSQL, oldKey, newKey); return true; } @@ -1273,10 +1275,12 @@ } void SQLiteQueryExecutor::rekeyMessage(std::string from, std::string to) const { - auto msg = SQLiteQueryExecutor::getStorage().get(from); - msg.id = to; - SQLiteQueryExecutor::getStorage().replace(msg); - SQLiteQueryExecutor::getStorage().remove(from); + static std::string rekeyMessageSQL = + "UPDATE OR REPLACE messages " + "SET id = ? " + "WHERE id = ?"; + rekeyAllEntities( + SQLiteQueryExecutor::getConnection(), rekeyMessageSQL, from, to); } void SQLiteQueryExecutor::removeAllMedia() const { @@ -1338,8 +1342,10 @@ void SQLiteQueryExecutor::rekeyMediaContainers(std::string from, std::string to) const { - SQLiteQueryExecutor::getStorage().update_all( - set(c(&Media::container) = to), where(c(&Media::container) == from)); + static std::string rekeyMediaContainersSQL = + "UPDATE media SET container = ? WHERE container = ?;"; + rekeyAllEntities( + SQLiteQueryExecutor::getConnection(), rekeyMediaContainersSQL, from, to); } void SQLiteQueryExecutor::replaceMessageStoreThreads( diff --git a/native/cpp/CommonCpp/DatabaseManagers/entities/EntityQueryHelpers.h b/native/cpp/CommonCpp/DatabaseManagers/entities/EntityQueryHelpers.h --- a/native/cpp/CommonCpp/DatabaseManagers/entities/EntityQueryHelpers.h +++ b/native/cpp/CommonCpp/DatabaseManagers/entities/EntityQueryHelpers.h @@ -95,6 +95,27 @@ sqlite3_step(preparedSQL); } +void rekeyAllEntities( + sqlite3 *db, + std::string rekeyAllEntitiesSQL, + std::string from, + std::string to) { + SQLiteStatementWrapper preparedSQL( + db, rekeyAllEntitiesSQL, "Failed to rekey all entities."); + + bindStringToSQL(to, preparedSQL, 1); + int bindResult = bindStringToSQL(from, preparedSQL, 2); + + 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); +} + std::string getSQLStatementArray(int length) { std::stringstream array; array << "("; 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$@