diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2020-05-10 12:19:44 +0300 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2020-05-10 12:20:18 +0300 |
commit | 0ef19712e4a5130e58f3931f19090ca8cfe6cf3a (patch) | |
tree | 13fb537a50cf63301a35a0352fc17f46231c4d25 /plugins/Dbx_sqlite/src | |
parent | 7383a0e57fc724da19d9a6e8b56e20012cc91dac (diff) |
dbx_sqlite: implemented cursor db api
Diffstat (limited to 'plugins/Dbx_sqlite/src')
-rwxr-xr-x | plugins/Dbx_sqlite/src/dbevents.cpp | 55 | ||||
-rwxr-xr-x | plugins/Dbx_sqlite/src/dbintf.h | 13 |
2 files changed, 64 insertions, 4 deletions
diff --git a/plugins/Dbx_sqlite/src/dbevents.cpp b/plugins/Dbx_sqlite/src/dbevents.cpp index bb76739baa..42574318bd 100755 --- a/plugins/Dbx_sqlite/src/dbevents.cpp +++ b/plugins/Dbx_sqlite/src/dbevents.cpp @@ -22,7 +22,14 @@ enum { SQL_EVT_STMT_NUM }; -static char *evt_stmts[SQL_EVT_STMT_NUM] = { +//TODO: hide it inside cursor class +static const char* normal_order_query = +"select id from events_srt where contact_id = ? order by timestamp;"; +static const char* reverse_order_query = +"select id from events_srt where contact_id = ? order by timestamp desc, id desc;"; + + +static const char *evt_stmts[SQL_EVT_STMT_NUM] = { "select count(1) from events where contact_id = ? limit 1;", "insert into events(contact_id, module, timestamp, type, flags, data) values (?, ?, ?, ?, ?, ?);", "delete from events where id = ?;", @@ -32,9 +39,9 @@ static char *evt_stmts[SQL_EVT_STMT_NUM] = { "select flags from events where id = ? limit 1;", "update events set flags = ? where id = ?;", "select contact_id from events where id = ? limit 1;", - "select id from events_srt where contact_id = ? order by timestamp;", + normal_order_query, "select id, timestamp from events where contact_id = ? and (flags & ?) = 0 order by timestamp, id limit 1;", - "select id from events_srt where contact_id = ? order by timestamp desc, id desc;", + reverse_order_query, "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 (?, ?, ?);", @@ -709,4 +716,44 @@ BOOL CDbxSQLite::MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact*) return 1; return TRUE; -}
\ No newline at end of file +} + +STDMETHODIMP_(DB::EventCursor*) CDbxSQLite::EventCursor(MCONTACT hContact, DBEVENTINFO& dbei) +{ + return new CDbxSQLiteEventCursor(hContact, dbei, m_db); +} + +STDMETHODIMP_(DB::EventCursor*) CDbxSQLite::EventCursorRev(MCONTACT hContact, DBEVENTINFO& dbei) +{ + return new CDbxSQLiteEventCursor(hContact, dbei, m_db, true); +} + +CDbxSQLiteEventCursor::CDbxSQLiteEventCursor(MCONTACT _1, DBEVENTINFO& _2, sqlite3* _db, bool reverse) + : EventCursorBase(_1, _2), m_db(_db) +{ + if (reverse) + sqlite3_prepare_v2(m_db, reverse_order_query, -1, &cursor, nullptr); + else + sqlite3_prepare_v2(m_db, normal_order_query, -1, &cursor, nullptr); + sqlite3_bind_int64(cursor, 1, hContact); +} + +CDbxSQLiteEventCursor::~CDbxSQLiteEventCursor() +{ + if (cursor) + sqlite3_reset(cursor); +} +MEVENT CDbxSQLiteEventCursor::FetchNext() +{ + int rc = sqlite3_step(cursor); + assert(rc == SQLITE_ROW || rc == SQLITE_DONE); + if (rc != SQLITE_ROW) { + //empty response + //reset sql cursor + sqlite3_reset(cursor); + cursor = 0; + return 0; + } + return sqlite3_column_int64(cursor, 0); +} + diff --git a/plugins/Dbx_sqlite/src/dbintf.h b/plugins/Dbx_sqlite/src/dbintf.h index 8a2c0f98a7..99cf1fba3f 100755 --- a/plugins/Dbx_sqlite/src/dbintf.h +++ b/plugins/Dbx_sqlite/src/dbintf.h @@ -22,6 +22,16 @@ struct DBCachedContact : public DBCachedContactBase void MarkRead(MEVENT hDbEvent); }; +struct CDbxSQLiteEventCursor : public DB::EventCursorBase +{ + CDbxSQLiteEventCursor(MCONTACT _1, DBEVENTINFO& _2, sqlite3* m_db, bool reverse = false); + ~CDbxSQLiteEventCursor() override; + MEVENT FetchNext() override; +private: + sqlite3* m_db; + sqlite3_stmt* cursor; +}; + struct CDbxSQLite : public MDatabaseCommon, public MZeroedObject { private: @@ -91,4 +101,7 @@ public: STDMETHODIMP_(BOOL) Compact() override; STDMETHODIMP_(BOOL) Backup(LPCWSTR) override; + + STDMETHODIMP_(DB::EventCursor*) EventCursor(MCONTACT hContact, DBEVENTINFO& dbei) override; + STDMETHODIMP_(DB::EventCursor*) EventCursorRev(MCONTACT hContact, DBEVENTINFO& dbei) override; }; |