diff options
Diffstat (limited to 'plugins/Dbx_sqlite')
-rwxr-xr-x | plugins/Dbx_sqlite/src/dbcontacts.cpp | 38 | ||||
-rwxr-xr-x | plugins/Dbx_sqlite/src/dbevents.cpp | 97 | ||||
-rwxr-xr-x | plugins/Dbx_sqlite/src/dbsettings.cpp | 102 | ||||
-rw-r--r-- | plugins/Dbx_sqlite/src/stdafx.h | 8 |
4 files changed, 123 insertions, 122 deletions
diff --git a/plugins/Dbx_sqlite/src/dbcontacts.cpp b/plugins/Dbx_sqlite/src/dbcontacts.cpp index 628c479860..87ebd448d3 100755 --- a/plugins/Dbx_sqlite/src/dbcontacts.cpp +++ b/plugins/Dbx_sqlite/src/dbcontacts.cpp @@ -7,24 +7,22 @@ enum { SQL_CTC_STMT_DELETESETTINGS, SQL_CTC_STMT_DELETEEVENTS, SQL_CTC_STMT_DELETEEVENTS_SRT, - SQL_CTC_STMT_NUM }; -static char *ctc_stmts[SQL_CTC_STMT_NUM] = { - "SELECT COUNT(1) FROM contacts LIMIT 1;", - "INSERT INTO contacts VALUES (null);", - "DELETE FROM contacts WHERE id = ?;", - "DELETE FROM settings WHERE contact_id = ?;", - "DELETE FROM events WHERE contact_id = ?;", - "DELETE FROM events_srt WHERE contact_id = ?;" +static CQuery ctc_stmts[] = +{ + { "SELECT COUNT(1) FROM contacts LIMIT 1;" }, + { "INSERT INTO contacts VALUES (null);" }, + { "DELETE FROM contacts WHERE id = ?;" }, + { "DELETE FROM settings WHERE contact_id = ?;" }, + { "DELETE FROM events WHERE contact_id = ?;" }, + { "DELETE FROM events_srt WHERE contact_id = ?;" }, }; -static sqlite3_stmt *ctc_stmts_prep[SQL_CTC_STMT_NUM] = { 0 }; - void CDbxSQLite::InitContacts() { - for (size_t i = 0; i < SQL_CTC_STMT_NUM; i++) - sqlite3_prepare_v3(m_db, ctc_stmts[i], -1, SQLITE_PREPARE_PERSISTENT, &ctc_stmts_prep[i], nullptr); + for (auto &it : ctc_stmts) + sqlite3_prepare_v3(m_db, it.szQuery, -1, SQLITE_PREPARE_PERSISTENT, &it.pQuery, nullptr); sqlite3_stmt *stmt = nullptr; sqlite3_prepare_v2(m_db, "SELECT contacts.id, COUNT(es.id) FROM contacts LEFT JOIN events_srt es ON es.contact_id = contacts.id GROUP BY contacts.id;", -1, &stmt, nullptr); @@ -55,14 +53,14 @@ void CDbxSQLite::InitContacts() void CDbxSQLite::UninitContacts() { - for (size_t i = 0; i < SQL_CTC_STMT_NUM; i++) - sqlite3_finalize(ctc_stmts_prep[i]); + for (auto &it : ctc_stmts) + sqlite3_finalize(it.pQuery); } LONG CDbxSQLite::GetContactCount() { mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = ctc_stmts_prep[SQL_CTC_STMT_COUNT]; + sqlite3_stmt *stmt = ctc_stmts[SQL_CTC_STMT_COUNT].pQuery; int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); int count = sqlite3_column_int(stmt, 0); @@ -75,7 +73,7 @@ MCONTACT CDbxSQLite::AddContact() MCONTACT hContact = INVALID_CONTACT_ID; { mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = ctc_stmts_prep[SQL_CTC_STMT_ADD]; + sqlite3_stmt *stmt = ctc_stmts[SQL_CTC_STMT_ADD].pQuery; int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); sqlite3_reset(stmt); @@ -100,7 +98,7 @@ LONG CDbxSQLite::DeleteContact(MCONTACT hContact) mir_cslockfull lock(m_csDbAccess); - sqlite3_stmt *stmt = ctc_stmts_prep[SQL_CTC_STMT_DELETEEVENTS]; + sqlite3_stmt *stmt = ctc_stmts[SQL_CTC_STMT_DELETEEVENTS].pQuery; sqlite3_bind_int64(stmt, 1, hContact); int rc = sqlite3_step(stmt); assert(rc == SQLITE_DONE); @@ -108,7 +106,7 @@ LONG CDbxSQLite::DeleteContact(MCONTACT hContact) if (rc != SQLITE_DONE) return 1; - stmt = ctc_stmts_prep[SQL_CTC_STMT_DELETEEVENTS_SRT]; + stmt = ctc_stmts[SQL_CTC_STMT_DELETEEVENTS_SRT].pQuery; sqlite3_bind_int64(stmt, 1, hContact); rc = sqlite3_step(stmt); assert(rc == SQLITE_DONE); @@ -116,7 +114,7 @@ LONG CDbxSQLite::DeleteContact(MCONTACT hContact) if (rc != SQLITE_DONE) return 1; - stmt = ctc_stmts_prep[SQL_CTC_STMT_DELETESETTINGS]; + stmt = ctc_stmts[SQL_CTC_STMT_DELETESETTINGS].pQuery; sqlite3_bind_int64(stmt, 1, hContact); rc = sqlite3_step(stmt); assert(rc == SQLITE_DONE); @@ -124,7 +122,7 @@ LONG CDbxSQLite::DeleteContact(MCONTACT hContact) if (rc != SQLITE_DONE) return 1; - stmt = ctc_stmts_prep[SQL_CTC_STMT_DELETE]; + stmt = ctc_stmts[SQL_CTC_STMT_DELETE].pQuery; sqlite3_bind_int64(stmt, 1, hContact); rc = sqlite3_step(stmt); assert(rc == SQLITE_DONE); diff --git a/plugins/Dbx_sqlite/src/dbevents.cpp b/plugins/Dbx_sqlite/src/dbevents.cpp index c23ff06e20..170f5ab15f 100755 --- a/plugins/Dbx_sqlite/src/dbevents.cpp +++ b/plugins/Dbx_sqlite/src/dbevents.cpp @@ -18,7 +18,6 @@ enum { SQL_EVT_STMT_DELETE_SRT, SQL_EVT_STMT_META_SPLIT, SQL_EVT_STMT_META_MERGE_SELECT, - SQL_EVT_STMT_NUM }; //TODO: hide it inside cursor class @@ -32,32 +31,31 @@ static const char* reverse_order_query = static const char* reverse_order_pos_query = "SELECT id FROM events_srt WHERE contact_id = ? AND id <= ? ORDER BY timestamp desc, id DESC;"; -static const char *evt_stmts[SQL_EVT_STMT_NUM] = { - "SELECT COUNT(1) FROM events_srt WHERE contact_id = ? LIMIT 1;", - "INSERT INTO events(contact_id, module, timestamp, type, flags, data, server_id) VALUES (?, ?, ?, ?, ?, ?, ?);", - "DELETE FROM events WHERE id = ?;", - "UPDATE events SET module = ?, timestamp = ?, type = ?, flags = ?, blob = ? WHERE id = ?;", - "SELECT LENGTH(data) FROM events WHERE id = ? LIMIT 1;", - "SELECT module, timestamp, type, flags, length(data), data FROM events WHERE id = ? LIMIT 1;", - "SELECT flags FROM events WHERE id = ? LIMIT 1;", - "UPDATE events SET flags = ? WHERE id = ?;", - "SELECT contact_id FROM events WHERE id = ? LIMIT 1;", - normal_order_query, - "SELECT id, timestamp FROM events WHERE contact_id = ? AND (flags & ?) = 0 ORDER BY timestamp, id LIMIT 1;", - reverse_order_query, - "SELECT id, timestamp FROM events WHERE module = ? AND server_id = ? LIMIT 1;", - "INSERT INTO events_srt(id, contact_id, timestamp) VALUES (?, ?, ?);", - "DELETE FROM events_srt WHERE id = ?;", - "DELETE FROM events_srt WHERE contact_id = ?;", - "SELECT id, timestamp FROM events WHERE contact_id = ?;", +static CQuery evt_stmts[] = +{ + { "SELECT COUNT(1) FROM events_srt WHERE contact_id = ? LIMIT 1;" }, + { "INSERT INTO events(contact_id, module, timestamp, type, flags, data, server_id) VALUES (?, ?, ?, ?, ?, ?, ?);" }, + { "DELETE FROM events WHERE id = ?;" }, + { "UPDATE events SET module = ?, timestamp = ?, type = ?, flags = ?, blob = ? WHERE id = ?;" }, + { "SELECT LENGTH(data) FROM events WHERE id = ? LIMIT 1;" }, + { "SELECT module, timestamp, type, flags, length(data), data FROM events WHERE id = ? LIMIT 1;" }, + { "SELECT flags FROM events WHERE id = ? LIMIT 1;" }, + { "UPDATE events SET flags = ? WHERE id = ?;" }, + { "SELECT contact_id FROM events WHERE id = ? LIMIT 1;" }, + { normal_order_query }, + { "SELECT id, timestamp FROM events WHERE contact_id = ? AND (flags & ?) = 0 ORDER BY timestamp, id LIMIT 1;" }, + { reverse_order_query }, + { "SELECT id, timestamp FROM events WHERE module = ? AND server_id = ? LIMIT 1;" }, + { "INSERT INTO events_srt(id, contact_id, timestamp) VALUES (?, ?, ?);" }, + { "DELETE FROM events_srt WHERE id = ?;" }, + { "DELETE FROM events_srt WHERE contact_id = ?;" }, + { "SELECT id, timestamp FROM events WHERE contact_id = ?;" }, }; -static sqlite3_stmt *evt_stmts_prep[SQL_EVT_STMT_NUM] = { 0 }; - void CDbxSQLite::InitEvents() { - for (size_t i = 0; i < SQL_EVT_STMT_NUM; i++) - sqlite3_prepare_v3(m_db, evt_stmts[i], -1, SQLITE_PREPARE_PERSISTENT, &evt_stmts_prep[i], nullptr); + for (auto &it : evt_stmts) + sqlite3_prepare_v3(m_db, it.szQuery, -1, SQLITE_PREPARE_PERSISTENT, &it.pQuery, nullptr); sqlite3_stmt *stmt = nullptr; sqlite3_prepare_v2(m_db, "SELECT DISTINCT module FROM events;", -1, &stmt, nullptr); @@ -78,8 +76,8 @@ void CDbxSQLite::UninitEvents() mir_free(module); } - for (size_t i = 0; i < SQL_EVT_STMT_NUM; i++) - sqlite3_finalize(evt_stmts_prep[i]); + for (auto &it : evt_stmts) + sqlite3_finalize(it.pQuery); } LONG CDbxSQLite::GetEventCount(MCONTACT hContact) @@ -89,7 +87,7 @@ LONG CDbxSQLite::GetEventCount(MCONTACT hContact) return cc->m_count; mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_COUNT]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_COUNT].pQuery; sqlite3_bind_int64(stmt, 1, hContact); int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); @@ -142,7 +140,7 @@ MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, const DBEVENTINFO *dbei) else szEventId = ""; mir_cslockfull lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_ADDEVENT]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_ADDEVENT].pQuery; sqlite3_bind_int64(stmt, 1, hContact); sqlite3_bind_text(stmt, 2, dbei->szModule, (int)mir_strlen(dbei->szModule), nullptr); sqlite3_bind_int64(stmt, 3, dbei->timestamp); @@ -156,7 +154,7 @@ MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, const DBEVENTINFO *dbei) MEVENT hDbEvent = sqlite3_last_insert_rowid(m_db); - stmt = evt_stmts_prep[SQL_EVT_STMT_ADDEVENT_SRT]; + stmt = evt_stmts[SQL_EVT_STMT_ADDEVENT_SRT].pQuery; sqlite3_bind_int64(stmt, 1, hDbEvent); sqlite3_bind_int64(stmt, 2, cc->contactID); sqlite3_bind_int64(stmt, 3, dbei->timestamp); @@ -166,7 +164,7 @@ MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, const DBEVENTINFO *dbei) cc->AddEvent(hDbEvent, dbei->timestamp, !dbei->markedRead()); if (ccSub != nullptr) { - stmt = evt_stmts_prep[SQL_EVT_STMT_ADDEVENT_SRT]; + stmt = evt_stmts[SQL_EVT_STMT_ADDEVENT_SRT].pQuery; sqlite3_bind_int64(stmt, 1, hDbEvent); sqlite3_bind_int64(stmt, 2, ccSub->contactID); sqlite3_bind_int64(stmt, 3, dbei->timestamp); @@ -199,7 +197,7 @@ BOOL CDbxSQLite::DeleteEvent(MEVENT hDbEvent) return 1; mir_cslockfull lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_DELETE]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_DELETE].pQuery; sqlite3_bind_int64(stmt, 1, hDbEvent); int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); @@ -207,7 +205,7 @@ BOOL CDbxSQLite::DeleteEvent(MEVENT hDbEvent) if (rc != SQLITE_DONE) return 1; - stmt = evt_stmts_prep[SQL_EVT_STMT_DELETE_SRT]; + stmt = evt_stmts[SQL_EVT_STMT_DELETE_SRT].pQuery; sqlite3_bind_int64(stmt, 1, hDbEvent); rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); @@ -239,7 +237,7 @@ BOOL CDbxSQLite::EditEvent(MCONTACT hContact, MEVENT hDbEvent, const DBEVENTINFO return 1; mir_cslockfull lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_EDIT]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_EDIT].pQuery; sqlite3_bind_text(stmt, 1, dbei->szModule, (int)mir_strlen(dbei->szModule), nullptr); sqlite3_bind_int64(stmt, 2, dbei->timestamp); sqlite3_bind_int(stmt, 3, dbei->eventType); @@ -269,7 +267,7 @@ LONG CDbxSQLite::GetBlobSize(MEVENT hDbEvent) return -1; mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_BLOBSIZE]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_BLOBSIZE].pQuery; sqlite3_bind_int(stmt, 1, hDbEvent); int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); @@ -296,7 +294,7 @@ BOOL CDbxSQLite::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei) } mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_GET]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_GET].pQuery; sqlite3_bind_int64(stmt, 1, hDbEvent); int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); @@ -339,7 +337,7 @@ BOOL CDbxSQLite::MarkEventRead(MCONTACT hContact, MEVENT hDbEvent) DWORD flags = 0; { mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_GETFLAGS]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_GETFLAGS].pQuery; sqlite3_bind_int64(stmt, 1, hDbEvent); int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); @@ -357,7 +355,7 @@ BOOL CDbxSQLite::MarkEventRead(MCONTACT hContact, MEVENT hDbEvent) flags |= DBEF_READ; { mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_SETFLAGS]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_SETFLAGS].pQuery; sqlite3_bind_int(stmt, 1, flags); sqlite3_bind_int64(stmt, 2, hDbEvent); int rc = sqlite3_step(stmt); @@ -382,7 +380,7 @@ MCONTACT CDbxSQLite::GetEventContact(MEVENT hDbEvent) return INVALID_CONTACT_ID; mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_GETCONTACT]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_GETCONTACT].pQuery; sqlite3_bind_int64(stmt, 1, hDbEvent); int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); @@ -411,7 +409,7 @@ MEVENT CDbxSQLite::FindFirstEvent(MCONTACT hContact) if (evt_cur_fwd) sqlite3_reset(evt_cur_fwd); - evt_cur_fwd = evt_stmts_prep[SQL_EVT_STMT_FINDFIRST]; + evt_cur_fwd = evt_stmts[SQL_EVT_STMT_FINDFIRST].pQuery; sqlite3_bind_int64(evt_cur_fwd, 1, hContact); int rc = sqlite3_step(evt_cur_fwd); @@ -468,7 +466,7 @@ MEVENT CDbxSQLite::FindFirstUnreadEvent(MCONTACT hContact) return cc->m_unread; } - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_FINDFIRSTUNREAD]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_FINDFIRSTUNREAD].pQuery; sqlite3_bind_int64(stmt, 1, hContact); sqlite3_bind_int(stmt, 2, DBEF_READ | DBEF_SENT); int rc = sqlite3_step(stmt); @@ -495,11 +493,10 @@ MEVENT CDbxSQLite::FindLastEvent(MCONTACT hContact) mir_cslock lock(m_csDbAccess); - if (evt_cur_backwd) { + if (evt_cur_backwd) sqlite3_reset(evt_cur_backwd); - } - evt_cur_backwd = evt_stmts_prep[SQL_EVT_STMT_FINDLAST]; + evt_cur_backwd = evt_stmts[SQL_EVT_STMT_FINDLAST].pQuery; sqlite3_bind_int64(evt_cur_backwd, 1, hContact); int rc = sqlite3_step(evt_cur_backwd); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); @@ -525,13 +522,13 @@ MEVENT CDbxSQLite::FindNextEvent(MCONTACT hContact, MEVENT hDbEvent) return 0; if (!evt_cur_fwd) { - evt_cur_fwd = evt_stmts_prep[SQL_EVT_STMT_FINDFIRST]; + evt_cur_fwd = evt_stmts[SQL_EVT_STMT_FINDFIRST].pQuery; sqlite3_bind_int64(evt_cur_fwd, 1, hContact); evt_cnt_fwd = hContact; } else if (hContact != evt_cnt_fwd) { sqlite3_reset(evt_cur_fwd); - evt_cur_fwd = evt_stmts_prep[SQL_EVT_STMT_FINDFIRST]; + evt_cur_fwd = evt_stmts[SQL_EVT_STMT_FINDFIRST].pQuery; sqlite3_bind_int64(evt_cur_fwd, 1, hContact); evt_cnt_fwd = hContact; } @@ -574,13 +571,13 @@ MEVENT CDbxSQLite::FindPrevEvent(MCONTACT hContact, MEVENT hDbEvent) return 0; if (!evt_cur_backwd) { - evt_cur_backwd = evt_stmts_prep[SQL_EVT_STMT_FINDLAST]; + evt_cur_backwd = evt_stmts[SQL_EVT_STMT_FINDLAST].pQuery; sqlite3_bind_int64(evt_cur_backwd, 1, hContact); evt_cnt_backwd = hContact; } else if (hContact != evt_cnt_backwd) { sqlite3_reset(evt_cur_fwd); - evt_cur_backwd = evt_stmts_prep[SQL_EVT_STMT_FINDLAST]; + evt_cur_backwd = evt_stmts[SQL_EVT_STMT_FINDLAST].pQuery; sqlite3_bind_int64(evt_cur_backwd, 1, hContact); evt_cnt_backwd = hContact; } @@ -619,7 +616,7 @@ MEVENT CDbxSQLite::GetEventById(LPCSTR szModule, LPCSTR szId) return 0; mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_GETIDBYSRVID]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_GETIDBYSRVID].pQuery; sqlite3_bind_text(stmt, 1, szModule, (int)mir_strlen(szModule), nullptr); sqlite3_bind_text(stmt, 2, szId, (int)mir_strlen(szId), nullptr); int rc = sqlite3_step(stmt); @@ -637,12 +634,12 @@ BOOL CDbxSQLite::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSu { //TODO: test this mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_META_MERGE_SELECT]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_META_MERGE_SELECT].pQuery; sqlite3_bind_int64(stmt, 1, ccSub->contactID); int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); while (rc == SQLITE_ROW) { - sqlite3_stmt *stmt2 = evt_stmts_prep[SQL_EVT_STMT_ADDEVENT_SRT]; + sqlite3_stmt *stmt2 = evt_stmts[SQL_EVT_STMT_ADDEVENT_SRT].pQuery; sqlite3_bind_int64(stmt2, 1, sqlite3_column_int64(stmt, 0)); sqlite3_bind_int64(stmt2, 2, ccMeta->contactID); sqlite3_bind_int64(stmt2, 3, sqlite3_column_int64(stmt, 1)); @@ -661,7 +658,7 @@ BOOL CDbxSQLite::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSu BOOL CDbxSQLite::MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *) { mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_META_SPLIT]; + sqlite3_stmt *stmt = evt_stmts[SQL_EVT_STMT_META_SPLIT].pQuery; sqlite3_bind_int64(stmt, 1, ccMeta->contactID); int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); diff --git a/plugins/Dbx_sqlite/src/dbsettings.cpp b/plugins/Dbx_sqlite/src/dbsettings.cpp index f3d234bb71..690bd7cb44 100755 --- a/plugins/Dbx_sqlite/src/dbsettings.cpp +++ b/plugins/Dbx_sqlite/src/dbsettings.cpp @@ -6,41 +6,39 @@ enum { SQL_SET_STMT_REPLACE, SQL_SET_STMT_DELETE, SQL_SET_STMT_ENUMMODULE, - SQL_SET_STMT_CHANGES, - SQL_SET_STMT_NUM + SQL_SET_STMT_CHANGES }; -static char *settings_stmts[SQL_SET_STMT_NUM] = { - "SELECT DISTINCT module FROM settings;", - "SELECT type, value FROM settings WHERE contact_id = ? AND module = ? AND setting = ? LIMIT 1;", - "REPLACE INTO settings(contact_id, module, setting, type, value) VALUES (?, ?, ?, ?, ?);", - "DELETE FROM settings WHERE contact_id = ? AND module = ? AND setting = ?;", - "SELECT setting FROM settings WHERE contact_id = ? AND module = ?;", - "SELECT changes() FROM settings;" +static CQuery settings_stmts[] = +{ + { "SELECT DISTINCT module FROM settings;" }, + { "SELECT type, value FROM settings WHERE contact_id = ? AND module = ? AND setting = ? LIMIT 1;" }, + { "REPLACE INTO settings(contact_id, module, setting, type, value) VALUES (?, ?, ?, ?, ?);" }, + { "DELETE FROM settings WHERE contact_id = ? AND module = ? AND setting = ?;" }, + { "SELECT setting FROM settings WHERE contact_id = ? AND module = ?;" }, + { "SELECT CHANGES() FROM settings;" }, }; -static sqlite3_stmt *settings_stmts_prep[SQL_SET_STMT_NUM] = { 0 }; - void CDbxSQLite::InitSettings() { - for (size_t i = 0; i < SQL_SET_STMT_NUM; i++) - sqlite3_prepare_v3(m_db, settings_stmts[i], -1, SQLITE_PREPARE_PERSISTENT, &settings_stmts_prep[i], nullptr); + for (auto &it : settings_stmts) + sqlite3_prepare_v3(m_db, it.szQuery, -1, SQLITE_PREPARE_PERSISTENT, &it.pQuery, nullptr); } void CDbxSQLite::UninitSettings() { - for (size_t i = 0; i < SQL_SET_STMT_NUM; i++) - sqlite3_finalize(settings_stmts_prep[i]); + for (auto &it : settings_stmts) + sqlite3_finalize(it.pQuery); } BOOL CDbxSQLite::EnumModuleNames(DBMODULEENUMPROC pFunc, void *param) { LIST<char> modules(100); { - sqlite3_stmt *stmt = settings_stmts_prep[SQL_SET_STMT_ENUM]; + sqlite3_stmt *stmt = settings_stmts[SQL_SET_STMT_ENUM].pQuery; int rc = 0; while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) { - const char *value = (const char*)sqlite3_column_text(stmt, 0); + const char *value = (const char *)sqlite3_column_text(stmt, 0); modules.insert(mir_strdup(value)); } assert(rc == SQLITE_ROW || rc == SQLITE_DONE); @@ -102,7 +100,7 @@ LBL_Seek: dbv->cchVal = cbLen; } else { - dbv->pszVal = (char*)mir_alloc(mir_strlen(pCachedValue->pszVal) + 1); + dbv->pszVal = (char *)mir_alloc(mir_strlen(pCachedValue->pszVal) + 1); mir_strcpy(dbv->pszVal, pCachedValue->pszVal); } } @@ -115,7 +113,7 @@ LBL_Seek: if (cachedSettingName[-1] != 0) return 1; - sqlite3_stmt *stmt = settings_stmts_prep[SQL_SET_STMT_GET]; + sqlite3_stmt *stmt = settings_stmts[SQL_SET_STMT_GET].pQuery; sqlite3_bind_int64(stmt, 1, hContact); sqlite3_bind_text(stmt, 2, szModule, (int)mir_strlen(szModule), nullptr); sqlite3_bind_text(stmt, 3, szSetting, (int)mir_strlen(szSetting), nullptr); @@ -147,25 +145,25 @@ LBL_Seek: dbv->dVal = sqlite3_column_int64(stmt, 1); break; case DBVT_UTF8: - { dbv->cchVal = sqlite3_column_bytes(stmt, 1); - const char *value = (const char*)sqlite3_column_text(stmt, 1); - if (!isStatic) - dbv->pszVal = (char*)mir_alloc(dbv->cchVal + 1); - memcpy(dbv->pszVal, value, dbv->cchVal); - dbv->pszVal[dbv->cchVal] = 0; + { + const char *value = (const char *)sqlite3_column_text(stmt, 1); + if (!isStatic) + dbv->pszVal = (char *)mir_alloc(dbv->cchVal + 1); + memcpy(dbv->pszVal, value, dbv->cchVal); + dbv->pszVal[dbv->cchVal] = 0; + } break; - } case DBVT_BLOB: - { dbv->cpbVal = sqlite3_column_bytes(stmt, 1); - const char *data = (const char*)sqlite3_column_blob(stmt, 1); - if (!isStatic) - dbv->pbVal = (BYTE*)mir_alloc(dbv->cpbVal + 1); - memcpy(dbv->pbVal, data, dbv->cpbVal); + { + const char *data = (const char *)sqlite3_column_blob(stmt, 1); + if (!isStatic) + dbv->pbVal = (BYTE *)mir_alloc(dbv->cpbVal + 1); + memcpy(dbv->pbVal, data, dbv->cpbVal); + } break; } - } sqlite3_reset(stmt); // add to cache @@ -196,19 +194,19 @@ BOOL CDbxSQLite::WriteContactSetting(MCONTACT hContact, DBCONTACTWRITESETTING *d dbcwNotif.value.pszVal = mir_strdup(dbcws->value.pszVal); break; case DBVT_ASCIIZ: - { - ptrA value(mir_utf8encode(dbcws->value.pszVal)); - dbcwNotif.value.pszVal = NEWSTR_ALLOCA(value); - dbcwNotif.value.type = DBVT_UTF8; - break; - } + { + ptrA value(mir_utf8encode(dbcws->value.pszVal)); + dbcwNotif.value.pszVal = NEWSTR_ALLOCA(value); + dbcwNotif.value.type = DBVT_UTF8; + break; + } case DBVT_WCHAR: - { - T2Utf value(dbcwNotif.value.pwszVal); - dbcwNotif.value.pszVal = NEWSTR_ALLOCA(value); - dbcwNotif.value.type = DBVT_UTF8; - break; - } + { + T2Utf value(dbcwNotif.value.pwszVal); + dbcwNotif.value.pszVal = NEWSTR_ALLOCA(value); + dbcwNotif.value.type = DBVT_UTF8; + break; + } } DBCONTACTWRITESETTING dbcwWork = dbcwNotif; if (dbcwWork.value.type == DBVT_UTF8) @@ -252,7 +250,7 @@ BOOL CDbxSQLite::WriteContactSetting(MCONTACT hContact, DBCONTACTWRITESETTING *d } else m_cache->GetCachedValuePtr(hContact, cachedSettingName, -1); - sqlite3_stmt *stmt = settings_stmts_prep[SQL_SET_STMT_REPLACE]; + sqlite3_stmt *stmt = settings_stmts[SQL_SET_STMT_REPLACE].pQuery; sqlite3_bind_int64(stmt, 1, hContact); sqlite3_bind_text(stmt, 2, dbcwWork.szModule, (int)mir_strlen(dbcwWork.szModule), nullptr); sqlite3_bind_text(stmt, 3, dbcwWork.szSetting, (int)mir_strlen(dbcwWork.szSetting), nullptr); @@ -274,6 +272,7 @@ BOOL CDbxSQLite::WriteContactSetting(MCONTACT hContact, DBCONTACTWRITESETTING *d sqlite3_bind_blob(stmt, 5, dbcwWork.value.pbVal, dbcwWork.value.cpbVal, nullptr); break; } + int rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW || rc == SQLITE_DONE); sqlite3_reset(stmt); @@ -297,19 +296,18 @@ BOOL CDbxSQLite::DeleteContactSetting(MCONTACT hContact, LPCSTR szModule, LPCSTR if (cc == nullptr) return 1; } - + char *szCachedSettingName = m_cache->GetCachedSetting(szModule, szSetting, mir_strlen(szModule), mir_strlen(szSetting)); - if (szCachedSettingName[-1] == 0) // it's not a resident variable - { + if (szCachedSettingName[-1] == 0) { // it's not a resident variable mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = settings_stmts_prep[SQL_SET_STMT_DELETE]; + sqlite3_stmt *stmt = settings_stmts[SQL_SET_STMT_DELETE].pQuery; sqlite3_bind_int64(stmt, 1, hContact); sqlite3_bind_text(stmt, 2, szModule, (int)mir_strlen(szModule), nullptr); sqlite3_bind_text(stmt, 3, szSetting, (int)mir_strlen(szSetting), nullptr); int rc = sqlite3_step(stmt); assert(rc == SQLITE_DONE); sqlite3_reset(stmt); - stmt = settings_stmts_prep[SQL_SET_STMT_CHANGES]; + stmt = settings_stmts[SQL_SET_STMT_CHANGES].pQuery; rc = sqlite3_step(stmt); assert(rc == SQLITE_ROW); int deleted = sqlite3_column_int(stmt, 0); @@ -343,12 +341,12 @@ BOOL CDbxSQLite::EnumContactSettings(MCONTACT hContact, DBSETTINGENUMPROC pfnEnu LIST<char> settings(100); { mir_cslock lock(m_csDbAccess); - sqlite3_stmt *stmt = settings_stmts_prep[SQL_SET_STMT_ENUMMODULE]; + sqlite3_stmt *stmt = settings_stmts[SQL_SET_STMT_ENUMMODULE].pQuery; sqlite3_bind_int64(stmt, 1, hContact); sqlite3_bind_text(stmt, 2, szModule, (int)mir_strlen(szModule), nullptr); int rc = 0; while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) { - const char *value = (const char*)sqlite3_column_text(stmt, 0); + const char *value = (const char *)sqlite3_column_text(stmt, 0); settings.insert(mir_strdup(value)); } assert(rc == SQLITE_ROW || rc == SQLITE_DONE); diff --git a/plugins/Dbx_sqlite/src/stdafx.h b/plugins/Dbx_sqlite/src/stdafx.h index a73a36ef53..d1cbb5ec17 100644 --- a/plugins/Dbx_sqlite/src/stdafx.h +++ b/plugins/Dbx_sqlite/src/stdafx.h @@ -20,6 +20,14 @@ #include "resource.h" #include "version.h" +struct CQuery +{ + const char *szQuery; + sqlite3_stmt *pQuery; +}; + +///////////////////////////////////////////////////////////////////////////////////////// + constexpr auto SQLITE_HEADER_STR = "SQLite format 3"; struct CMPlugin : public PLUGIN<CMPlugin> |