diff options
author | George Hazan <ghazan@miranda.im> | 2021-01-16 19:30:20 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2021-01-16 19:30:20 +0300 |
commit | b2a65cb0e93f070ebb78dd69a9740527fb0e6261 (patch) | |
tree | e83aac634813a44991f212cbaab43dbab60f37a3 /plugins/Dbx_sqlite/src/dbintf.cpp | |
parent | 97f80dd9e5737bb65cdd2b745c864e9c3b7ab433 (diff) |
dbx_sqlite:
- major speed-up (we don't close each transaction immediately);
- assert replaced with correct error logging (to be able to debug problems using network logs);
- code cleaning;
- version bump
Diffstat (limited to 'plugins/Dbx_sqlite/src/dbintf.cpp')
-rwxr-xr-x | plugins/Dbx_sqlite/src/dbintf.cpp | 123 |
1 files changed, 57 insertions, 66 deletions
diff --git a/plugins/Dbx_sqlite/src/dbintf.cpp b/plugins/Dbx_sqlite/src/dbintf.cpp index b219cb11af..e0f45a7e18 100755 --- a/plugins/Dbx_sqlite/src/dbintf.cpp +++ b/plugins/Dbx_sqlite/src/dbintf.cpp @@ -1,7 +1,8 @@ #include "stdafx.h" -CDbxSQLite::CDbxSQLite(sqlite3 *database) - : m_db(database), +CDbxSQLite::CDbxSQLite(sqlite3 *database) : + m_db(database), + m_impl(*this), m_safetyMode(true), m_modules(1, strcmp) { @@ -9,16 +10,17 @@ CDbxSQLite::CDbxSQLite(sqlite3 *database) CDbxSQLite::~CDbxSQLite() { + int rc = sqlite3_exec(m_db, "commit;", nullptr, nullptr, nullptr); + logError(rc); + UninitEvents(); UninitContacts(); UninitSettings(); if (m_db) { - int rc = sqlite3_close(m_db); - if (rc != SQLITE_OK) - { - //TODO: - } + rc = sqlite3_close(m_db); + logError(rc); + m_db = nullptr; } } @@ -28,57 +30,41 @@ int CDbxSQLite::Create(const wchar_t *profile) sqlite3 *database = nullptr; ptrA path(mir_utf8encodeW(profile)); int rc = sqlite3_open_v2(path, &database, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, nullptr); + logError(rc); if (rc != SQLITE_OK) return 1; rc = sqlite3_exec(database, "CREATE TABLE contacts (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT);", nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) - { - //TODO: handle error - } + logError(rc); + rc = sqlite3_exec(database, "CREATE TABLE events (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, contact_id INTEGER NOT NULL, module TEXT NOT NULL," "timestamp INTEGER NOT NULL, type INTEGER NOT NULL, flags INTEGER NOT NULL, data BLOB, server_id TEXT);", nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) - { - //TODO: handle error - } + logError(rc); + rc = sqlite3_exec(database, "CREATE INDEX idx_events_contactid_timestamp ON events(contact_id, timestamp);", nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) - { - //TODO: handle error - } + logError(rc); + rc = sqlite3_exec(database, "CREATE INDEX idx_events_module_serverid ON events(module, server_id);", nullptr, nullptr, nullptr); if (rc != SQLITE_OK) - { - //TODO: handle error - } - rc = sqlite3_exec(database, "CREATE TABLE events_srt (id INTEGER NOT NULL, contact_id INTEGER NOT NULL, timestamp INTEGER, PRIMARY KEY(contact_id, timestamp, id));", - nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) - { - //TODO: handle error - } + logError(rc); + + rc = sqlite3_exec(database, "CREATE TABLE events_srt (id INTEGER NOT NULL, contact_id INTEGER NOT NULL, timestamp INTEGER, PRIMARY KEY(contact_id, timestamp, id));", nullptr, nullptr, nullptr); + logError(rc); + rc = sqlite3_exec(database, "CREATE TABLE settings (contact_id INTEGER NOT NULL, module TEXT NOT NULL, setting TEXT NOT NULL, type INTEGER NOT NULL, value ANY," "PRIMARY KEY(contact_id, module, setting)) WITHOUT ROWID;", nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) - { - //TODO: handle error - } + logError(rc); + rc = sqlite3_exec(database, "CREATE INDEX idx_settings_module ON settings(module);", nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) - { - //TODO: handle error - } + logError(rc); sqlite3_close(database); - return 0; } int CDbxSQLite::Check(const wchar_t *profile) { FILE *hFile = _wfopen(profile, L"rb"); - if (hFile == INVALID_HANDLE_VALUE) return EGROKPRF_CANTREAD; @@ -113,33 +99,21 @@ MDatabaseCommon* CDbxSQLite::Load(const wchar_t *profile, int readonly) int flags = SQLITE_OPEN_READWRITE; if (readonly) flags |= SQLITE_OPEN_READONLY; + int rc = sqlite3_open_v2(path, &database, flags, nullptr); if (rc != SQLITE_OK) return nullptr; rc = sqlite3_exec(database, "begin transaction;", nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) - { - //TODO: handle error - } - rc = sqlite3_exec(database, "pragma locking_mode = EXCLUSIVE;", nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) { - // TODO: handle error - } - rc = sqlite3_exec(database, "pragma synchronous = NORMAL;", nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) { - // TODO: handle error - } - sqlite3_exec(database, "pragma foreign_keys = OFF;", nullptr, nullptr, nullptr); - rc = sqlite3_exec(database, "pragma journal_mode = OFF;", nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) { - // TODO: handle error - } - rc = sqlite3_exec(database, "commit;", nullptr, nullptr, nullptr); - if (rc != SQLITE_OK) { - // TODO: handle error - } + logError(rc); + + sqlite3_exec(database, "pragma locking_mode = EXCLUSIVE;", nullptr, nullptr, nullptr); + sqlite3_exec(database, "pragma synchronous = NORMAL;", nullptr, nullptr, nullptr); + sqlite3_exec(database, "pragma foreign_keys = OFF;", nullptr, nullptr, nullptr); + sqlite3_exec(database, "pragma journal_mode = OFF;", nullptr, nullptr, nullptr); + rc = sqlite3_exec(database, "commit;", nullptr, nullptr, nullptr); + logError(rc); CDbxSQLite *db = new CDbxSQLite(database); db->InitContacts(); @@ -151,16 +125,11 @@ MDatabaseCommon* CDbxSQLite::Load(const wchar_t *profile, int readonly) return nullptr; } + rc = sqlite3_exec(database, "begin transaction;", nullptr, nullptr, nullptr); + logError(rc); return db; } -BOOL CDbxSQLite::Compact() -{ - sqlite3_exec(m_db, "pragma optimize;", nullptr, nullptr, nullptr); - sqlite3_exec(m_db, "vacuum;", nullptr, nullptr, nullptr); - return 0; -} - BOOL CDbxSQLite::Backup(LPCWSTR profile) { sqlite3 *database = nullptr; @@ -176,11 +145,33 @@ BOOL CDbxSQLite::Backup(LPCWSTR profile) sqlite3_backup_step(backup, -1); sqlite3_backup_finish(backup); } + sqlite3_close(database); + return 0; +} +BOOL CDbxSQLite::Compact() +{ + sqlite3_exec(m_db, "pragma optimize;", nullptr, nullptr, nullptr); + sqlite3_exec(m_db, "vacuum;", nullptr, nullptr, nullptr); return 0; } +void CDbxSQLite::DBFlush(bool bForce) +{ + if (bForce) { + mir_cslock lck(m_csDbAccess); + + int rc = sqlite3_exec(m_db, "commit;", nullptr, nullptr, nullptr); + logError(rc); + + rc = sqlite3_exec(m_db, "begin transaction;", nullptr, nullptr, nullptr); + logError(rc); + } + else if (m_safetyMode) + m_impl.m_timer.Start(50); +} + BOOL CDbxSQLite::IsRelational() { return TRUE; |