summaryrefslogtreecommitdiff
path: root/plugins/Dbx_sqlite/src
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/Dbx_sqlite/src')
-rw-r--r--plugins/Dbx_sqlite/src/dbcontacts.cpp42
-rw-r--r--plugins/Dbx_sqlite/src/dbevents.cpp79
-rw-r--r--plugins/Dbx_sqlite/src/dbintf.cpp8
-rw-r--r--plugins/Dbx_sqlite/src/dbintf.h13
-rw-r--r--plugins/Dbx_sqlite/src/dbsettings.cpp23
-rw-r--r--plugins/Dbx_sqlite/src/version.h4
6 files changed, 53 insertions, 116 deletions
diff --git a/plugins/Dbx_sqlite/src/dbcontacts.cpp b/plugins/Dbx_sqlite/src/dbcontacts.cpp
index 8ea6d122ae..1cd6229da3 100644
--- a/plugins/Dbx_sqlite/src/dbcontacts.cpp
+++ b/plugins/Dbx_sqlite/src/dbcontacts.cpp
@@ -111,45 +111,3 @@ int CDbxSQLite::GetContactSize(void)
{
return sizeof(DBCachedContact);
}
-
-/////////////////////////////////////
-
-void DBCachedContact::AddEvent(MEVENT hDbEvent, uint32_t timestamp, bool unread)
-{
- m_count = HasCount() ? m_count + 1 : 1;
-
- if (unread && m_unreadTimestamp > timestamp) {
- m_unread = hDbEvent;
- m_unreadTimestamp = timestamp;
- }
-}
-
-void DBCachedContact::EditEvent(MEVENT hDbEvent, uint32_t timestamp, bool unread)
-{
- if (m_unread = hDbEvent && (!unread || m_unreadTimestamp != timestamp)) {
- m_unread = 0;
- m_unreadTimestamp = 0;
- }
- else if (unread && timestamp > m_unreadTimestamp) {
- m_unread = hDbEvent;
- m_unreadTimestamp = timestamp;
- }
-}
-
-void DBCachedContact::DeleteEvent(MEVENT hDbEvent)
-{
- if (m_count > 0)
- m_count--;
- if (m_unread == hDbEvent) {
- m_unread = 0;
- m_unreadTimestamp = 0;
- }
-}
-
-void DBCachedContact::MarkRead(MEVENT hDbEvent)
-{
- if (m_unread == hDbEvent) {
- m_unread = 0;
- m_unreadTimestamp = 0;
- }
-}
diff --git a/plugins/Dbx_sqlite/src/dbevents.cpp b/plugins/Dbx_sqlite/src/dbevents.cpp
index 155b32dc70..0645faf96a 100644
--- a/plugins/Dbx_sqlite/src/dbevents.cpp
+++ b/plugins/Dbx_sqlite/src/dbevents.cpp
@@ -104,14 +104,15 @@ MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, const DBEVENTINFO *dbei)
}
mir_cslockfull lock(m_csDbAccess);
- sqlite3_stmt *stmt = InitQuery("INSERT INTO events(contact_id, module, timestamp, type, flags, data, server_id) VALUES (?, ?, ?, ?, ?, ?, ?);", qEvAdd);
+ sqlite3_stmt *stmt = InitQuery("INSERT INTO events(contact_id, module, timestamp, type, flags, data, server_id, is_read) VALUES (?, ?, ?, ?, ?, ?, ?, ?);", qEvAdd);
sqlite3_bind_int64(stmt, 1, hContact);
sqlite3_bind_text(stmt, 2, tmp.szModule, (int)mir_strlen(tmp.szModule), nullptr);
sqlite3_bind_int64(stmt, 3, tmp.timestamp);
sqlite3_bind_int(stmt, 4, tmp.eventType);
- sqlite3_bind_int64(stmt, 5, tmp.flags);
+ sqlite3_bind_int(stmt, 5, tmp.flags);
sqlite3_bind_blob(stmt, 6, tmp.pBlob, tmp.cbBlob, nullptr);
sqlite3_bind_text(stmt, 7, szEventId, (int)mir_strlen(szEventId), nullptr);
+ sqlite3_bind_int(stmt, 8, tmp.markedRead());
int rc = sqlite3_step(stmt);
logError(rc, __FILE__, __LINE__);
sqlite3_reset(stmt);
@@ -126,7 +127,6 @@ MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, const DBEVENTINFO *dbei)
logError(rc, __FILE__, __LINE__);
sqlite3_reset(stmt);
- cc->AddEvent(hDbEvent, tmp.timestamp, !tmp.markedRead());
if (ccSub != nullptr) {
stmt = InitQuery(add_event_sort_query, qEvAddSrt);
sqlite3_bind_int64(stmt, 1, hDbEvent);
@@ -135,8 +135,6 @@ MEVENT CDbxSQLite::AddEvent(MCONTACT hContact, const DBEVENTINFO *dbei)
rc = sqlite3_step(stmt);
logError(rc, __FILE__, __LINE__);
sqlite3_reset(stmt); //is this necessary ?
-
- ccSub->AddEvent(hDbEvent, tmp.timestamp, !tmp.markedRead());
}
char *module = m_modules.find((char *)tmp.szModule);
@@ -193,10 +191,6 @@ BOOL CDbxSQLite::DeleteEvent(MEVENT hDbEvent)
if (rc != SQLITE_DONE)
return 1;
- cc->DeleteEvent(hDbEvent);
- if (cc->IsSub() && (cc = m_cache->GetCachedContact(cc->parentID)))
- cc->DeleteEvent(hDbEvent);
-
lock.unlock();
DBFlush();
@@ -231,21 +225,18 @@ BOOL CDbxSQLite::EditEvent(MCONTACT hContact, MEVENT hDbEvent, const DBEVENTINFO
}
mir_cslockfull lock(m_csDbAccess);
- sqlite3_stmt *stmt = InitQuery("UPDATE events SET module = ?, timestamp = ?, type = ?, flags = ?, data = ? WHERE id = ?;", qEvEdit);
+ sqlite3_stmt *stmt = InitQuery("UPDATE events SET module = ?, timestamp = ?, type = ?, flags = ?, data = ?, is_read = ? WHERE id = ?;", qEvEdit);
sqlite3_bind_text(stmt, 1, tmp.szModule, (int)mir_strlen(tmp.szModule), nullptr);
sqlite3_bind_int64(stmt, 2, tmp.timestamp);
sqlite3_bind_int(stmt, 3, tmp.eventType);
- sqlite3_bind_int64(stmt, 4, tmp.flags);
+ sqlite3_bind_int(stmt, 4, tmp.flags);
sqlite3_bind_blob(stmt, 5, tmp.pBlob, tmp.cbBlob, nullptr);
sqlite3_bind_int64(stmt, 6, hDbEvent);
+ sqlite3_bind_int(stmt, 7, tmp.markedRead());
int rc = sqlite3_step(stmt);
logError(rc, __FILE__, __LINE__);
sqlite3_reset(stmt);
- cc->EditEvent(hDbEvent, tmp.timestamp, !tmp.markedRead());
- if (cc->IsSub() && (cc = m_cache->GetCachedContact(cc->parentID)))
- cc->EditEvent(hDbEvent, tmp.timestamp, !tmp.markedRead());
-
char *module = m_modules.find((char *)tmp.szModule);
if (module == nullptr)
m_modules.insert(mir_strdup(tmp.szModule));
@@ -309,7 +300,7 @@ BOOL CDbxSQLite::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei)
dbei->eventType = sqlite3_column_int(stmt, 2);
dbei->flags = sqlite3_column_int64(stmt, 3);
- uint32_t cbBlob = sqlite3_column_int64(stmt, 4);
+ int32_t cbBlob = sqlite3_column_int64(stmt, 4);
size_t bytesToCopy = cbBlob;
if (dbei->cbBlob == -1)
dbei->pBlob = (uint8_t*)mir_calloc(cbBlob + 2);
@@ -348,44 +339,22 @@ BOOL CDbxSQLite::MarkEventRead(MCONTACT hContact, MEVENT hDbEvent)
if (cc == nullptr)
return -1;
- uint32_t flags = 0;
+ int rows;
{
mir_cslock lock(m_csDbAccess);
- sqlite3_stmt *stmt = InitQuery("SELECT flags FROM events WHERE id = ? LIMIT 1;", qEvGetFlags);
+ sqlite3_stmt *stmt = InitQuery("UPDATE events SET flags = flags OR 4, is_read = 1 WHERE id = ? AND is_read = 0;", qEvSetFlags);
sqlite3_bind_int64(stmt, 1, hDbEvent);
int rc = sqlite3_step(stmt);
- logError(rc, __FILE__, __LINE__);
- if (rc != SQLITE_ROW) {
- sqlite3_reset(stmt);
- return -1;
- }
- flags = sqlite3_column_int64(stmt, 0);
- sqlite3_reset(stmt);
- }
-
- if ((flags & DBEF_READ) == DBEF_READ)
- return flags;
-
- flags |= DBEF_READ;
- {
- mir_cslock lock(m_csDbAccess);
- sqlite3_stmt *stmt = InitQuery("UPDATE events SET flags = ? WHERE id = ?;", qEvSetFlags);
- sqlite3_bind_int(stmt, 1, flags);
- sqlite3_bind_int64(stmt, 2, hDbEvent);
- int rc = sqlite3_step(stmt);
+ rows = sqlite3_changes(m_db);
logError(rc, __FILE__, __LINE__);
sqlite3_reset(stmt);
if (rc != SQLITE_DONE)
return -1;
-
- cc->MarkRead(hDbEvent);
- if (cc->IsSub() && (cc = m_cache->GetCachedContact(cc->parentID)))
- cc->MarkRead(hDbEvent);
}
DBFlush();
NotifyEventHooks(g_hevMarkedRead, hContact, (LPARAM)hDbEvent);
- return flags;
+ return (rows == 0 ? 0 : DBEF_READ);
}
MCONTACT CDbxSQLite::GetEventContact(MEVENT hDbEvent)
@@ -413,23 +382,17 @@ MEVENT CDbxSQLite::FindFirstUnreadEvent(MCONTACT hContact)
if (cc == nullptr)
return 0;
- if (cc->m_unread)
- return cc->m_unread;
-
mir_cslock lock(m_csDbAccess);
if (cc->IsMeta()) {
- if (cc->nSubs == 0) {
- cc->m_unread = 0;
- cc->m_unreadTimestamp = 0;
+ if (cc->nSubs == 0)
return 0;
- }
- CMStringA query(FORMAT, "SELECT id FROM events WHERE (flags & %d) = 0 AND contact_id IN (", DBEF_READ | DBEF_SENT);
+ CMStringA query("SELECT id FROM events WHERE is_read = 0 AND contact_id IN (");
for (int k = 0; k < cc->nSubs; k++)
query.AppendFormat("%lu, ", cc->pSubs[k]);
query.Delete(query.GetLength() - 2, 2);
- query.Append(") ORDER BY timestamp, id LIMIT 1;");
+ query.Append(") ORDER BY timestamp LIMIT 1;");
sqlite3_stmt *stmt;
sqlite3_prepare_v2(m_db, query, -1, &stmt, nullptr);
@@ -439,13 +402,13 @@ MEVENT CDbxSQLite::FindFirstUnreadEvent(MCONTACT hContact)
sqlite3_finalize(stmt);
return 0;
}
- cc->m_unread = sqlite3_column_int64(stmt, 0);
- cc->m_unreadTimestamp = sqlite3_column_int64(stmt, 1);
+
+ MEVENT ret = sqlite3_column_int64(stmt, 0);
sqlite3_finalize(stmt);
- return cc->m_unread;
+ return ret;
}
- sqlite3_stmt *stmt = InitQuery("SELECT id, timestamp FROM events WHERE contact_id = ? AND (flags & ?) = 0 ORDER BY timestamp, id LIMIT 1;", qEvFindUnread);
+ sqlite3_stmt *stmt = InitQuery("SELECT id FROM events WHERE contact_id = ? AND is_read = 0 ORDER BY timestamp LIMIT 1;", qEvFindUnread);
sqlite3_bind_int64(stmt, 1, hContact);
sqlite3_bind_int(stmt, 2, DBEF_READ | DBEF_SENT);
int rc = sqlite3_step(stmt);
@@ -454,10 +417,10 @@ MEVENT CDbxSQLite::FindFirstUnreadEvent(MCONTACT hContact)
sqlite3_reset(stmt);
return 0;
}
- cc->m_unread = sqlite3_column_int64(stmt, 0);
- cc->m_unreadTimestamp = sqlite3_column_int64(stmt, 1);
+
+ MEVENT ret = sqlite3_column_int64(stmt, 0);
sqlite3_reset(stmt);
- return cc->m_unread;
+ return ret;
}
/////////////////////////////////////////////////////////////////////////////////////////
diff --git a/plugins/Dbx_sqlite/src/dbintf.cpp b/plugins/Dbx_sqlite/src/dbintf.cpp
index 16d7ca8783..a34551b617 100644
--- a/plugins/Dbx_sqlite/src/dbintf.cpp
+++ b/plugins/Dbx_sqlite/src/dbintf.cpp
@@ -46,15 +46,17 @@ int CDbxSQLite::Create()
logError(rc, __FILE__, __LINE__);
rc = sqlite3_exec(m_db, "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);
+ "timestamp INTEGER NOT NULL, type INTEGER NOT NULL, flags INTEGER NOT NULL, data BLOB, server_id TEXT, is_read INTEGER NOT NULL DEFAULT 0);", nullptr, nullptr, nullptr);
logError(rc, __FILE__, __LINE__);
rc = sqlite3_exec(m_db, "CREATE INDEX idx_events_contactid_timestamp ON events(contact_id, timestamp);", nullptr, nullptr, nullptr);
logError(rc, __FILE__, __LINE__);
rc = sqlite3_exec(m_db, "CREATE INDEX idx_events_module_serverid ON events(module, server_id);", nullptr, nullptr, nullptr);
- if (rc != SQLITE_OK)
- logError(rc, __FILE__, __LINE__);
+ 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, "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, __FILE__, __LINE__);
diff --git a/plugins/Dbx_sqlite/src/dbintf.h b/plugins/Dbx_sqlite/src/dbintf.h
index e0f74a165e..fbeb18b493 100644
--- a/plugins/Dbx_sqlite/src/dbintf.h
+++ b/plugins/Dbx_sqlite/src/dbintf.h
@@ -14,21 +14,14 @@ struct CQuery
struct DBCachedContact : public DBCachedContactBase
{
int32_t m_count;
- MEVENT m_unread;
- uint32_t m_unreadTimestamp;
- DBCachedContact()
- : m_count(-1), m_unread(0) { }
+ DBCachedContact() :
+ m_count(-1)
+ {}
__forceinline bool HasCount() const {
return m_count > -1;
}
-
- void AddEvent(MEVENT hDbEvent, uint32_t timestamp, bool unread);
- void EditEvent(MEVENT hDbEvent, uint32_t timestamp, bool unread);
- void DeleteEvent(MEVENT hDbEvent);
-
- void MarkRead(MEVENT hDbEvent);
};
struct CDbxSQLiteEventCursor : public DB::EventCursor
diff --git a/plugins/Dbx_sqlite/src/dbsettings.cpp b/plugins/Dbx_sqlite/src/dbsettings.cpp
index b4b5551cea..4b73fcb020 100644
--- a/plugins/Dbx_sqlite/src/dbsettings.cpp
+++ b/plugins/Dbx_sqlite/src/dbsettings.cpp
@@ -58,8 +58,29 @@ void CDbxSQLite::InitSettings()
sqlite3_finalize(stmt);
FillContactSettings();
-}
+ DBVARIANT dbv; dbv.type = DBVT_DWORD;
+ if (GetContactSetting(0, "Compatibility", "Sqlite", &dbv))
+ dbv.dVal = 0;
+
+ if (dbv.dVal < 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__);
+
+ DBCONTACTWRITESETTING dbcws = {};
+ dbcws.szModule = "Compatibility";
+ dbcws.szSetting = "Sqlite";
+ dbcws.value.type = DBVT_BYTE;
+ dbcws.value.dVal = 1;
+ WriteContactSetting(0, &dbcws);
+ }
+}
/////////////////////////////////////////////////////////////////////////////////////////
diff --git a/plugins/Dbx_sqlite/src/version.h b/plugins/Dbx_sqlite/src/version.h
index 1e4cfbbaf8..00fd440b6f 100644
--- a/plugins/Dbx_sqlite/src/version.h
+++ b/plugins/Dbx_sqlite/src/version.h
@@ -1,7 +1,7 @@
#define __MAJOR_VERSION 0
#define __MINOR_VERSION 96
-#define __RELEASE_NUM 1
-#define __BUILD_NUM 2
+#define __RELEASE_NUM 2
+#define __BUILD_NUM 1
#include <stdver.h>