Changeset View
Changeset View
Standalone View
Standalone View
native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp
Show First 20 Lines • Show All 369 Lines • ▼ Show 20 Lines | std::string query = | ||||
" id TEXT UNIQUE PRIMARY KEY NOT NULL," | " id TEXT UNIQUE PRIMARY KEY NOT NULL," | ||||
" start_reached INTEGER NOT NULL," | " start_reached INTEGER NOT NULL," | ||||
" last_navigated_to BIGINT NOT NULL," | " last_navigated_to BIGINT NOT NULL," | ||||
" last_pruned BIGINT NOT NULL" | " last_pruned BIGINT NOT NULL" | ||||
");"; | ");"; | ||||
return create_table(db, query, "message_store_threads"); | return create_table(db, query, "message_store_threads"); | ||||
} | } | ||||
bool create_reports_table(sqlite3 *db) { | |||||
std::string query = | |||||
"CREATE TABLE IF NOT EXISTS reports (" | |||||
" id TEXT UNIQUE PRIMARY KEY NOT NULL," | |||||
" report TEXT NOT NULL" | |||||
");"; | |||||
return create_table(db, query, "reports"); | |||||
} | |||||
bool create_schema(sqlite3 *db) { | bool create_schema(sqlite3 *db) { | ||||
char *error; | char *error; | ||||
sqlite3_exec( | sqlite3_exec( | ||||
db, | db, | ||||
"CREATE TABLE IF NOT EXISTS drafts (" | "CREATE TABLE IF NOT EXISTS drafts (" | ||||
" key TEXT UNIQUE PRIMARY KEY NOT NULL," | " key TEXT UNIQUE PRIMARY KEY NOT NULL," | ||||
" text TEXT NOT NULL" | " text TEXT NOT NULL" | ||||
");" | ");" | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | sqlite3_exec( | ||||
"CREATE TABLE IF NOT EXISTS message_store_threads (" | "CREATE TABLE IF NOT EXISTS message_store_threads (" | ||||
" id TEXT UNIQUE PRIMARY KEY NOT NULL," | " id TEXT UNIQUE PRIMARY KEY NOT NULL," | ||||
" start_reached INTEGER NOT NULL," | " start_reached INTEGER NOT NULL," | ||||
" last_navigated_to BIGINT NOT NULL," | " last_navigated_to BIGINT NOT NULL," | ||||
" last_pruned BIGINT NOT NULL" | " last_pruned BIGINT NOT NULL" | ||||
");" | ");" | ||||
"CREATE TABLE IF NOT EXISTS reports (" | |||||
" id TEXT UNIQUE PRIMARY KEY NOT NULL," | |||||
" report TEXT NOT NULL" | |||||
");" | |||||
"CREATE INDEX IF NOT EXISTS media_idx_container" | "CREATE INDEX IF NOT EXISTS media_idx_container" | ||||
" ON media (container);" | " ON media (container);" | ||||
"CREATE INDEX IF NOT EXISTS messages_idx_thread_time" | "CREATE INDEX IF NOT EXISTS messages_idx_thread_time" | ||||
" ON messages (thread, time);", | " ON messages (thread, time);", | ||||
nullptr, | nullptr, | ||||
nullptr, | nullptr, | ||||
&error); | &error); | ||||
▲ Show 20 Lines • Show All 221 Lines • ▼ Show 20 Lines | std::vector<std::pair<uint, SQLiteMigration>> migrations{ | ||||
{20, {create_threads_table, true}}, | {20, {create_threads_table, true}}, | ||||
{21, {update_threadID_for_pending_threads_in_drafts, true}}, | {21, {update_threadID_for_pending_threads_in_drafts, true}}, | ||||
{22, {enable_write_ahead_logging_mode, false}}, | {22, {enable_write_ahead_logging_mode, false}}, | ||||
{23, {create_metadata_table, true}}, | {23, {create_metadata_table, true}}, | ||||
{24, {add_not_null_constraint_to_drafts, true}}, | {24, {add_not_null_constraint_to_drafts, true}}, | ||||
{25, {add_not_null_constraint_to_metadata, true}}, | {25, {add_not_null_constraint_to_metadata, true}}, | ||||
{26, {add_avatar_column_to_threads_table, true}}, | {26, {add_avatar_column_to_threads_table, true}}, | ||||
{27, {add_pinned_count_column_to_threads, true}}, | {27, {add_pinned_count_column_to_threads, true}}, | ||||
{28, {create_message_store_threads_table, true}}}}; | {28, {create_message_store_threads_table, true}}, | ||||
{29, {create_reports_table, true}}}}; | |||||
enum class MigrationResult { SUCCESS, FAILURE, NOT_APPLIED }; | enum class MigrationResult { SUCCESS, FAILURE, NOT_APPLIED }; | ||||
MigrationResult applyMigrationWithTransaction( | MigrationResult applyMigrationWithTransaction( | ||||
sqlite3 *db, | sqlite3 *db, | ||||
const MigrateFunction &migrate, | const MigrateFunction &migrate, | ||||
int index) { | int index) { | ||||
sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, nullptr); | sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, nullptr); | ||||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | static auto storage = make_storage( | ||||
make_column("name", &Metadata::name, unique(), primary_key()), | make_column("name", &Metadata::name, unique(), primary_key()), | ||||
make_column("data", &Metadata::data)), | make_column("data", &Metadata::data)), | ||||
make_table( | make_table( | ||||
"message_store_threads", | "message_store_threads", | ||||
make_column("id", &MessageStoreThread::id, unique(), primary_key()), | make_column("id", &MessageStoreThread::id, unique(), primary_key()), | ||||
make_column("start_reached", &MessageStoreThread::start_reached), | make_column("start_reached", &MessageStoreThread::start_reached), | ||||
make_column( | make_column( | ||||
"last_navigated_to", &MessageStoreThread::last_navigated_to), | "last_navigated_to", &MessageStoreThread::last_navigated_to), | ||||
make_column("last_pruned", &MessageStoreThread::last_pruned))); | make_column("last_pruned", &MessageStoreThread::last_pruned)), | ||||
make_table( | |||||
"reports", | |||||
make_column("id", &Report::id, unique(), primary_key()), | |||||
make_column("report", &Report::report))); | |||||
storage.on_open = on_database_open; | storage.on_open = on_database_open; | ||||
return storage; | return storage; | ||||
} | } | ||||
void SQLiteQueryExecutor::initialize(std::string &databasePath) { | void SQLiteQueryExecutor::initialize(std::string &databasePath) { | ||||
std::call_once(SQLiteQueryExecutor::initialized, [&databasePath]() { | std::call_once(SQLiteQueryExecutor::initialized, [&databasePath]() { | ||||
SQLiteQueryExecutor::sqliteFilePath = databasePath; | SQLiteQueryExecutor::sqliteFilePath = databasePath; | ||||
CommSecureStore commSecureStore{}; | CommSecureStore commSecureStore{}; | ||||
▲ Show 20 Lines • Show All 209 Lines • ▼ Show 20 Lines | |||||
void SQLiteQueryExecutor::replaceThread(const Thread &thread) const { | void SQLiteQueryExecutor::replaceThread(const Thread &thread) const { | ||||
SQLiteQueryExecutor::getStorage().replace(thread); | SQLiteQueryExecutor::getStorage().replace(thread); | ||||
}; | }; | ||||
void SQLiteQueryExecutor::removeAllThreads() const { | void SQLiteQueryExecutor::removeAllThreads() const { | ||||
SQLiteQueryExecutor::getStorage().remove_all<Thread>(); | SQLiteQueryExecutor::getStorage().remove_all<Thread>(); | ||||
}; | }; | ||||
void SQLiteQueryExecutor::replaceReport(const Report &report) const { | |||||
SQLiteQueryExecutor::getStorage().replace(report); | |||||
} | |||||
void SQLiteQueryExecutor::removeAllReports() const { | |||||
SQLiteQueryExecutor::getStorage().remove_all<Report>(); | |||||
} | |||||
void SQLiteQueryExecutor::removeReports( | |||||
const std::vector<std::string> &ids) const { | |||||
SQLiteQueryExecutor::getStorage().remove_all<Report>( | |||||
where(in(&Report::id, ids))); | |||||
} | |||||
std::vector<Report> SQLiteQueryExecutor::getAllReports() const { | |||||
return SQLiteQueryExecutor::getStorage().get_all<Report>(); | |||||
} | |||||
void SQLiteQueryExecutor::beginTransaction() const { | void SQLiteQueryExecutor::beginTransaction() const { | ||||
SQLiteQueryExecutor::getStorage().begin_transaction(); | SQLiteQueryExecutor::getStorage().begin_transaction(); | ||||
} | } | ||||
void SQLiteQueryExecutor::commitTransaction() const { | void SQLiteQueryExecutor::commitTransaction() const { | ||||
SQLiteQueryExecutor::getStorage().commit(); | SQLiteQueryExecutor::getStorage().commit(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 91 Lines • Show Last 20 Lines |