summaryrefslogtreecommitdiff
path: root/plugins/Dbx_sqlite/src/dbintf.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/Dbx_sqlite/src/dbintf.cpp')
-rw-r--r--plugins/Dbx_sqlite/src/dbintf.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/plugins/Dbx_sqlite/src/dbintf.cpp b/plugins/Dbx_sqlite/src/dbintf.cpp
index 70d35e9fbf..5d663be534 100644
--- a/plugins/Dbx_sqlite/src/dbintf.cpp
+++ b/plugins/Dbx_sqlite/src/dbintf.cpp
@@ -78,6 +78,120 @@ int CDbxSQLite::Create()
/////////////////////////////////////////////////////////////////////////////////////////
+#define CURRVER 5
+
+static INT_PTR CALLBACK MsbBoxWndProc(HWND, UINT uMsg, WPARAM, LPARAM)
+{
+ if (uMsg == WM_INITDIALOG)
+ return TRUE;
+
+ return FALSE;
+}
+
+void CDbxSQLite::CheckConversion()
+{
+ DBVARIANT dbv = { DBVT_BYTE };
+ if (GetContactSettingWorker(0, "Compatibility", "Sqlite", &dbv, 0))
+ dbv.bVal = 0;
+
+ if (dbv.bVal >= CURRVER)
+ return;
+
+ HWND hwndMsgBox = CreateDialogW(g_plugin.getInst(), MAKEINTRESOURCE(IDD_MSGBOX), 0, MsbBoxWndProc);
+ ShowWindow(hwndMsgBox, SW_NORMAL);
+ SetForegroundWindow(hwndMsgBox);
+
+ if (dbv.bVal < 1) {
+ int rc = sqlite3_exec(m_db, "ALTER TABLE events ADD COLUMN is_read INTEGER NOT NULL DEFAULT 0;", 0, 0, 0);
+ logError(rc, __FILE__, __LINE__);
+
+ rc = sqlite3_exec(m_db, "CREATE INDEX idx_events_isread ON events(contact_id, is_read, timestamp);", nullptr, nullptr, nullptr);
+ logError(rc, __FILE__, __LINE__);
+
+ rc = sqlite3_exec(m_db, "UPDATE events SET is_read=1 WHERE (flags & 6) <> 0;", nullptr, nullptr, nullptr);
+ logError(rc, __FILE__, __LINE__);
+ }
+
+ if (dbv.bVal < 2) {
+ int rc = sqlite3_exec(m_db, "ALTER TABLE events ADD COLUMN user_id TEXT NULL;", 0, 0, 0);
+ logError(rc, __FILE__, __LINE__);
+ }
+
+ if (dbv.bVal < 3) {
+ int rc = sqlite3_exec(m_db, "UPDATE events_srt SET timestamp=timestamp*1000;", 0, 0, 0);
+ logError(rc, __FILE__, __LINE__);
+
+ CQuery updateSql;
+ sqlite3_stmt *updateStmt = InitQuery("UPDATE events_srt SET timestamp=timestamp+? "
+ " WHERE rowid IN (SELECT rowid FROM events_srt WHERE contact_id=? AND timestamp=? LIMIT 1);", updateSql);
+
+ sqlite3_stmt *pQuery;
+ rc = sqlite3_prepare_v2(m_db, "SELECT contact_id, timestamp, count(*) AS boo FROM events_srt GROUP BY contact_id, timestamp HAVING boo > 1;", -1, &pQuery, nullptr);
+ logError(rc, __FILE__, __LINE__);
+
+ struct MConvert
+ {
+ MConvert(MCONTACT _1, int64_t _2, int _3) :
+ hContact(_1),
+ ts(_2),
+ iCount(_3)
+ {}
+
+ MCONTACT hContact;
+ int iCount;
+ int64_t ts;
+ };
+
+ OBJLIST<MConvert> recs(10000);
+ while (sqlite3_step(pQuery) == SQLITE_ROW) {
+ MCONTACT hContact = sqlite3_column_int(pQuery, 0);
+ int64_t ts = sqlite3_column_int64(pQuery, 1);
+ int iCount = sqlite3_column_int(pQuery, 2);
+ recs.insert(new MConvert(hContact, ts, iCount));
+ }
+
+ for (auto &it : recs) {
+ for (int i = it->iCount - 1; i > 0; i--) {
+ sqlite3_bind_int64(updateStmt, 1, i);
+ sqlite3_bind_int64(updateStmt, 2, it->hContact);
+ sqlite3_bind_int64(updateStmt, 3, it->ts);
+ rc = sqlite3_step(updateStmt);
+ logError(rc, __FILE__, __LINE__);
+ sqlite3_reset(updateStmt);
+ }
+ }
+
+ rc = sqlite3_exec(m_db, "CREATE TABLE tmp(id INTEGER NOT NULL, contact_id INTEGER NOT NULL, timestamp INTEGER, PRIMARY KEY(contact_id, timestamp));", 0, 0, 0);
+ logError(rc, __FILE__, __LINE__);
+
+ rc = sqlite3_exec(m_db, "INSERT INTO tmp SELECT * FROM events_srt;", 0, 0, 0);
+ logError(rc, __FILE__, __LINE__);
+ if (rc != 0)
+ return;
+
+ rc = sqlite3_exec(m_db, "DROP TABLE events_srt;", 0, 0, 0);
+ logError(rc, __FILE__, __LINE__);
+
+ rc = sqlite3_exec(m_db, "ALTER TABLE tmp RENAME TO events_srt;", 0, 0, 0);
+ logError(rc, __FILE__, __LINE__);
+ }
+
+ if (dbv.bVal < 5) {
+ int rc = sqlite3_exec(m_db, "DROP INDEX IF EXISTS i1_srt;", 0, 0, 0);
+ logError(rc, __FILE__, __LINE__);
+
+ rc = sqlite3_exec(m_db, "CREATE INDEX i1_srt ON events_srt(id);", 0, 0, 0);
+ logError(rc, __FILE__, __LINE__);
+ }
+
+ dbv.bVal = CURRVER;
+ WriteContactSetting(0, "Compatibility", "Sqlite", &dbv);
+
+ DestroyWindow(hwndMsgBox);
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
int CDbxSQLite::Check()
{
FILE *hFile = _wfopen(m_wszFileName, L"rb");