summaryrefslogtreecommitdiff
path: root/plugins/Dbx_sqlite/src
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2019-06-05 18:19:27 +0300
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2019-06-05 18:20:04 +0300
commit40a2107efb3b0c17e54ad7d611fa964f2e703a14 (patch)
tree5579a60ba63280aae4e2674d3e72fcbd1cd49d47 /plugins/Dbx_sqlite/src
parente1d7db348d184d901532d0aad1c9024f3113ca9c (diff)
dbx_sqlite: reimplemented FindLastEvent, FindPrevEvent
Diffstat (limited to 'plugins/Dbx_sqlite/src')
-rwxr-xr-xplugins/Dbx_sqlite/src/dbevents.cpp108
-rwxr-xr-xplugins/Dbx_sqlite/src/dbintf.h2
2 files changed, 35 insertions, 75 deletions
diff --git a/plugins/Dbx_sqlite/src/dbevents.cpp b/plugins/Dbx_sqlite/src/dbevents.cpp
index 190888263e..7f3077e6a6 100755
--- a/plugins/Dbx_sqlite/src/dbevents.cpp
+++ b/plugins/Dbx_sqlite/src/dbevents.cpp
@@ -13,7 +13,6 @@ enum {
SQL_EVT_STMT_FINDFIRST,
SQL_EVT_STMT_FINDFIRSTUNREAD,
SQL_EVT_STMT_FINDLAST,
- SQL_EVT_STMT_FINDPREV,
SQL_EVT_STMT_GETIDBYSRVID,
SQL_EVT_STMT_SETSRVID,
SQL_EVT_STMT_ADDEVENT_SRT,
@@ -35,8 +34,7 @@ static char *evt_stmts[SQL_EVT_STMT_NUM] = {
"select contact_id from events where id = ? limit 1;",
"select id, timestamp from events_srt where contact_id = ? order by timestamp;",
"select id, timestamp from events where contact_id = ? and (flags & ?) = 0 order by timestamp, id limit 1;",
- "select id, timestamp from events where contact_id = ? order by timestamp desc, id desc limit 1;",
- "select id, timestamp from events where contact_id = ?1 and id <> ?2 and timestamp < (select timestamp from events where contact_id = ?1 and id = ?2 limit 1) order by timestamp desc, id desc limit 1;",
+ "select id, timestamp from events_srt where contact_id = ? order by timestamp desc, id desc;",
"select id, timestamp from events where module = ? and server_id = ? limit 1;",
"update events set server_id = ? where id = ?;",
"insert into events_srt(id, contact_id, timestamp) values (?, ?, ?);",
@@ -521,44 +519,23 @@ MEVENT CDbxSQLite::FindLastEvent(MCONTACT hContact)
mir_cslock lock(m_csDbAccess);
- if (cc->IsMeta()) {
- if (cc->nSubs == 0) {
- cc->m_last = 0;
- cc->m_lastTimestamp = 0;
- return 0;
- }
-
- CMStringA query = "select id from events where 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 desc, id desc limit 1;");
-
- sqlite3_stmt *stmt;
- sqlite3_prepare_v2(m_db, query, -1, &stmt, nullptr);
- int rc = sqlite3_step(stmt);
- assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
- if (rc != SQLITE_ROW) {
- sqlite3_finalize(stmt);
- return 0;
- }
- cc->m_last = sqlite3_column_int64(stmt, 0);
- cc->m_lastTimestamp = sqlite3_column_int64(stmt, 1);
- sqlite3_finalize(stmt);
- return cc->m_last;
+ if (evt_cur_backwd)
+ {
+ sqlite3_reset(evt_cur_backwd);
}
- sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_FINDLAST];
- sqlite3_bind_int64(stmt, 1, hContact);
- int rc = sqlite3_step(stmt);
+ evt_cur_backwd = evt_stmts_prep[SQL_EVT_STMT_FINDLAST];
+ sqlite3_bind_int64(evt_cur_backwd, 1, hContact);
+ int rc = sqlite3_step(evt_cur_backwd);
assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
+ evt_cnt_backwd = hContact;
if (rc != SQLITE_ROW) {
- sqlite3_reset(stmt);
+ sqlite3_reset(evt_cur_fwd);
+ evt_cur_fwd = 0;
return 0;
}
- cc->m_last = sqlite3_column_int64(stmt, 0);
- cc->m_lastTimestamp = sqlite3_column_int64(stmt, 1);
- sqlite3_reset(stmt);
+ cc->m_last = sqlite3_column_int64(evt_cur_backwd, 0);
+ cc->m_lastTimestamp = sqlite3_column_int64(evt_cur_backwd, 1);
return cc->m_last;
}
@@ -593,16 +570,12 @@ MEVENT CDbxSQLite::FindNextEvent(MCONTACT hContact, MEVENT hDbEvent)
int rc = sqlite3_step(evt_cur_fwd);
assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
-/* if (rc != SQLITE_ROW) {
+ if (rc != SQLITE_ROW) {
sqlite3_reset(evt_cur_fwd);
+ evt_cur_fwd = 0;
return 0;
- } */
+ }
hDbEvent = sqlite3_column_int64(evt_cur_fwd, 0);
- if (rc == SQLITE_DONE)
- {
- sqlite3_reset(evt_cur_fwd);
- evt_cur_fwd = 0;
- }
return hDbEvent;
}
@@ -616,48 +589,35 @@ MEVENT CDbxSQLite::FindPrevEvent(MCONTACT hContact, MEVENT hDbEvent)
if (cc == nullptr)
return 0;
- mir_cslock lock(m_csDbAccess);
-
- if (cc->IsMeta()) {
- if (cc->nSubs == 0)
- return 0;
-
- CMStringA in = "(";
- for (int k = 0; k < cc->nSubs; k++)
- in.AppendFormat("%lu, ", cc->pSubs[k]);
- in.Delete(in.GetLength() - 2, 2);
- in.Append(")");
-
- CMStringA query(FORMAT, "select id from events where contact_id in %s and id <> %lu and timestamp < (select timestamp from events where contact_id in %s and id = %lu limit 1) order by timestamp desc, id desc limit 1;",
- in.c_str(),
- hDbEvent,
- in.c_str(),
- hDbEvent);
+ if (!evt_cur_fwd)
+ {
+ return 0;
+ }
+ if (hContact != evt_cnt_backwd)
+ {
+ return 0;
+ }
- sqlite3_stmt *stmt;
- sqlite3_prepare_v2(m_db, query, -1, &stmt, nullptr);
- int rc = sqlite3_step(stmt);
+ while (hDbEvent != sqlite3_column_int64(evt_cur_backwd, 0))
+ {
+ int rc = sqlite3_step(evt_cur_backwd);
assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
- if (rc != SQLITE_ROW) {
- sqlite3_finalize(stmt);
+ if (rc == SQLITE_DONE)
+ {
+ sqlite3_reset(evt_cur_backwd);
return 0;
}
- hDbEvent = sqlite3_column_int64(stmt, 0);
- sqlite3_finalize(stmt);
- return hDbEvent;
}
- sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_FINDPREV];
- sqlite3_bind_int64(stmt, 1, hContact);
- sqlite3_bind_int64(stmt, 2, hDbEvent);
- int rc = sqlite3_step(stmt);
+ int rc = sqlite3_step(evt_cur_backwd);
assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
if (rc != SQLITE_ROW) {
- sqlite3_reset(stmt);
+ sqlite3_reset(evt_cur_fwd);
+ evt_cur_fwd = 0;
return 0;
}
- hDbEvent = sqlite3_column_int64(stmt, 0);
- sqlite3_reset(stmt);
+ hDbEvent = sqlite3_column_int64(evt_cur_backwd, 0);
+
return hDbEvent;
}
diff --git a/plugins/Dbx_sqlite/src/dbintf.h b/plugins/Dbx_sqlite/src/dbintf.h
index 8f2532c15d..e164be6cd8 100755
--- a/plugins/Dbx_sqlite/src/dbintf.h
+++ b/plugins/Dbx_sqlite/src/dbintf.h
@@ -31,7 +31,7 @@ struct CDbxSQLite : public MDatabaseCommon, public MZeroedObject
private:
sqlite3 *m_db;
- sqlite3_stmt *evt_cur_fwd = nullptr, *evt_curr_backwd = nullptr;
+ sqlite3_stmt *evt_cur_fwd = nullptr, *evt_cur_backwd = nullptr;
MCONTACT evt_cnt_fwd = 0, evt_cnt_backwd = 0;
DBCachedContact m_system;