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 @@ -1169,17 +1169,30 @@ } void SQLiteQueryExecutor::removeAllDrafts() const { - SQLiteQueryExecutor::getStorage().remove_all(); + static std::string removeAllDraftsSQL = "DELETE FROM drafts;"; + removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllDraftsSQL); } void SQLiteQueryExecutor::removeDrafts( const std::vector &ids) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(in(&Draft::key, ids))); + if (!ids.size()) { + return; + } + + std::stringstream removeDraftsByKeysSQLStream; + removeDraftsByKeysSQLStream << "DELETE FROM drafts " + "WHERE key IN " + << getSQLStatementArray(ids.size()) << ";"; + + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removeDraftsByKeysSQLStream.str(), + ids); } void SQLiteQueryExecutor::removeAllMessages() const { - SQLiteQueryExecutor::getStorage().remove_all(); + static std::string removeAllMessagesSQL = "DELETE FROM messages;"; + removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllMessagesSQL); } std::vector>> @@ -1217,14 +1230,36 @@ void SQLiteQueryExecutor::removeMessages( const std::vector &ids) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(in(&Message::id, ids))); + if (!ids.size()) { + return; + } + + std::stringstream removeMessagesByKeysSQLStream; + removeMessagesByKeysSQLStream << "DELETE FROM messages " + "WHERE id IN " + << getSQLStatementArray(ids.size()) << ";"; + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removeMessagesByKeysSQLStream.str(), + ids); } void SQLiteQueryExecutor::removeMessagesForThreads( const std::vector &threadIDs) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(in(&Message::thread, threadIDs))); + if (!threadIDs.size()) { + return; + } + + std::stringstream removeMessagesByKeysSQLStream; + removeMessagesByKeysSQLStream << "DELETE FROM messages " + "WHERE thread IN " + << getSQLStatementArray(threadIDs.size()) + << ";"; + + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removeMessagesByKeysSQLStream.str(), + threadIDs); } void SQLiteQueryExecutor::replaceMessage(const Message &message) const { @@ -1245,24 +1280,51 @@ } void SQLiteQueryExecutor::removeAllMedia() const { - SQLiteQueryExecutor::getStorage().remove_all(); + static std::string removeAllMediaSQL = "DELETE FROM media;"; + removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllMediaSQL); } void SQLiteQueryExecutor::removeMediaForMessages( const std::vector &msg_ids) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(in(&Media::container, msg_ids))); + if (!msg_ids.size()) { + return; + } + + std::stringstream removeMediaByKeysSQLStream; + removeMediaByKeysSQLStream << "DELETE FROM media " + "WHERE container IN " + << getSQLStatementArray(msg_ids.size()) << ";"; + + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removeMediaByKeysSQLStream.str(), + msg_ids); } void SQLiteQueryExecutor::removeMediaForMessage(std::string msg_id) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(c(&Media::container) == msg_id)); + static std::string removeMediaByKeySQL = + "DELETE FROM media " + "WHERE container IN (?);"; + std::vector keys = {msg_id}; + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), removeMediaByKeySQL, keys); } void SQLiteQueryExecutor::removeMediaForThreads( const std::vector &thread_ids) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(in(&Media::thread, thread_ids))); + if (!thread_ids.size()) { + return; + } + + std::stringstream removeMediaByKeysSQLStream; + removeMediaByKeysSQLStream << "DELETE FROM media " + "WHERE thread IN " + << getSQLStatementArray(thread_ids.size()) << ";"; + + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removeMediaByKeysSQLStream.str(), + thread_ids); } void SQLiteQueryExecutor::replaceMedia(const Media &media) const { @@ -1296,13 +1358,28 @@ } void SQLiteQueryExecutor::removeAllMessageStoreThreads() const { - SQLiteQueryExecutor::getStorage().remove_all(); + static std::string removeAllMessageStoreThreadsSQL = + "DELETE FROM message_store_threads;"; + removeAllEntities( + SQLiteQueryExecutor::getConnection(), removeAllMessageStoreThreadsSQL); } void SQLiteQueryExecutor::removeMessageStoreThreads( const std::vector &ids) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(in(&MessageStoreThread::id, ids))); + if (!ids.size()) { + return; + } + + std::stringstream removeMessageStoreThreadsByKeysSQLStream; + removeMessageStoreThreadsByKeysSQLStream + << "DELETE FROM message_store_threads " + "WHERE id IN " + << getSQLStatementArray(ids.size()) << ";"; + + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removeMessageStoreThreadsByKeysSQLStream.str(), + ids); } std::vector @@ -1323,8 +1400,19 @@ }; void SQLiteQueryExecutor::removeThreads(std::vector ids) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(in(&Thread::id, ids))); + if (!ids.size()) { + return; + } + + std::stringstream removeThreadsByKeysSQLStream; + removeThreadsByKeysSQLStream << "DELETE FROM threads " + "WHERE id IN " + << getSQLStatementArray(ids.size()) << ";"; + + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removeThreadsByKeysSQLStream.str(), + ids); }; void SQLiteQueryExecutor::replaceThread(const Thread &thread) const { @@ -1340,7 +1428,8 @@ }; void SQLiteQueryExecutor::removeAllThreads() const { - SQLiteQueryExecutor::getStorage().remove_all(); + static std::string removeAllThreadsSQL = "DELETE FROM threads;"; + removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllThreadsSQL); }; void SQLiteQueryExecutor::replaceReport(const Report &report) const { @@ -1353,13 +1442,24 @@ } void SQLiteQueryExecutor::removeAllReports() const { - SQLiteQueryExecutor::getStorage().remove_all(); + static std::string removeAllReportsSQL = "DELETE FROM reports;"; + removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllReportsSQL); } void SQLiteQueryExecutor::removeReports( const std::vector &ids) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(in(&Report::id, ids))); + if (!ids.size()) { + return; + } + + std::stringstream removeReportsByKeysSQLStream; + removeReportsByKeysSQLStream << "DELETE FROM reports " + "WHERE id IN " + << getSQLStatementArray(ids.size()) << ";"; + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removeReportsByKeysSQLStream.str(), + ids); } std::vector SQLiteQueryExecutor::getAllReports() const { @@ -1387,7 +1487,14 @@ } void SQLiteQueryExecutor::removePersistStorageItem(std::string key) const { - SQLiteQueryExecutor::getStorage().remove(key); + static std::string removePersistStorageItemByKeySQL = + "DELETE FROM persist_storage " + "WHERE key IN (?);"; + std::vector keys = {key}; + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removePersistStorageItemByKeySQL, + keys); } std::string SQLiteQueryExecutor::getPersistStorageItem(std::string key) const { @@ -1411,13 +1518,24 @@ } void SQLiteQueryExecutor::removeAllUsers() const { - SQLiteQueryExecutor::getStorage().remove_all(); + static std::string removeAllUsersSQL = "DELETE FROM users;"; + removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllUsersSQL); } void SQLiteQueryExecutor::removeUsers( const std::vector &ids) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(in(&UserInfo::id, ids))); + if (!ids.size()) { + return; + } + + std::stringstream removeUsersByKeysSQLStream; + removeUsersByKeysSQLStream << "DELETE FROM users " + "WHERE id IN " + << getSQLStatementArray(ids.size()) << ";"; + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removeUsersByKeysSQLStream.str(), + ids); } void SQLiteQueryExecutor::replaceKeyserver( @@ -1432,13 +1550,26 @@ } void SQLiteQueryExecutor::removeAllKeyservers() const { - SQLiteQueryExecutor::getStorage().remove_all(); + static std::string removeAllKeyserversSQL = "DELETE FROM keyservers;"; + removeAllEntities( + SQLiteQueryExecutor::getConnection(), removeAllKeyserversSQL); } void SQLiteQueryExecutor::removeKeyservers( const std::vector &ids) const { - SQLiteQueryExecutor::getStorage().remove_all( - where(in(&KeyserverInfo::id, ids))); + if (!ids.size()) { + return; + } + + std::stringstream removeKeyserversByKeysSQLStream; + removeKeyserversByKeysSQLStream << "DELETE FROM keyservers " + "WHERE id IN " + << getSQLStatementArray(ids.size()) << ";"; + + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), + removeKeyserversByKeysSQLStream.str(), + ids); } std::vector SQLiteQueryExecutor::getAllKeyservers() const { @@ -1551,7 +1682,12 @@ } void SQLiteQueryExecutor::clearMetadata(std::string entry_name) const { - SQLiteQueryExecutor::getStorage().remove(entry_name); + static std::string removeMetadataByKeySQL = + "DELETE FROM metadata " + "WHERE name IN (?);"; + std::vector keys = {entry_name}; + removeEntitiesByKeys( + SQLiteQueryExecutor::getConnection(), removeMetadataByKeySQL, keys); } std::string SQLiteQueryExecutor::getMetadata(std::string entry_name) const { 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 @@ -1,5 +1,6 @@ #pragma once +#include "SQLiteDataConverters.h" #include "SQLiteStatementWrapper.h" #include #include @@ -68,6 +69,42 @@ sqlite3_step(preparedSQL); } +void removeAllEntities(sqlite3 *db, std::string removeAllEntitiesSQL) { + SQLiteStatementWrapper preparedSQL( + db, removeAllEntitiesSQL, "Failed to remove all entities."); + sqlite3_step(preparedSQL); +} + +void removeEntitiesByKeys( + sqlite3 *db, + std::string removeEntitiesByKeysSQL, + const std::vector &keys) { + SQLiteStatementWrapper preparedSQL( + db, removeEntitiesByKeysSQL, "Failed to remove entities by keys."); + for (int i = 0; i < keys.size(); i++) { + int bindResult = bindStringToSQL(keys[i], preparedSQL, i + 1); + if (bindResult != SQLITE_OK) { + std::stringstream error_message; + error_message << "Failed to bind key to SQL statement. Details: " + << sqlite3_errstr(bindResult) << std::endl; + sqlite3_finalize(preparedSQL); + throw std::runtime_error(error_message.str()); + } + } + + sqlite3_step(preparedSQL); +} + +std::string getSQLStatementArray(int length) { + std::stringstream array; + array << "("; + for (int i = 0; i < length - 1; i++) { + array << "?, "; + } + array << "?)"; + return array.str(); +} + void executeQuery(sqlite3 *db, std::string querySQL) { char *err; sqlite3_exec(db, querySQL.c_str(), nullptr, nullptr, &err); 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$@