diff options
Diffstat (limited to 'plugins/Dbx_sqlite/src/dbcontacts.cpp')
-rw-r--r-- | plugins/Dbx_sqlite/src/dbcontacts.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/plugins/Dbx_sqlite/src/dbcontacts.cpp b/plugins/Dbx_sqlite/src/dbcontacts.cpp new file mode 100644 index 0000000000..ce9af1ddc4 --- /dev/null +++ b/plugins/Dbx_sqlite/src/dbcontacts.cpp @@ -0,0 +1,101 @@ +#include "stdafx.h" + +enum { + SQL_CTC_STMT_COUNT = 0, + SQL_CTC_STMT_ADD, + SQL_CTC_STMT_DELETE, + 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 events where contactid = ?; delete from settings where contactid = ?; delete from contacts where 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); + + // add global contact + //m_cache->AddContactToCache(0); + + sqlite3_stmt *stmt = nullptr; + sqlite3_prepare_v2(m_db, "select id from contacts;", -1, &stmt, nullptr); + while (sqlite3_step(stmt) == SQLITE_ROW) { + MCONTACT hContact = sqlite3_column_int64(stmt, 0); + DBCachedContact *cc = m_cache->AddContactToCache(hContact); + } + sqlite3_finalize(stmt); +} + +void CDbxSQLite::UninitContacts() +{ + for (size_t i = 0; i < SQL_CTC_STMT_NUM; i++) + sqlite3_finalize(ctc_stmts_prep[i]); +} + +LONG CDbxSQLite::GetContactCount() +{ + mir_cslock lock(m_csDbAccess); + sqlite3_stmt *stmt = ctc_stmts_prep[SQL_CTC_STMT_COUNT]; + int rc = sqlite3_step(stmt); + int count = sqlite3_column_int(stmt, 0); + sqlite3_reset(stmt); + return count; +} + +MCONTACT CDbxSQLite::AddContact() +{ + MCONTACT hContact = INVALID_CONTACT_ID; + { + mir_cslock lock(m_csDbAccess); + sqlite3_stmt *stmt = ctc_stmts_prep[SQL_CTC_STMT_ADD]; + int rc = sqlite3_step(stmt); + sqlite3_reset(stmt); + if (rc != SQLITE_DONE) + return INVALID_CONTACT_ID; + hContact = sqlite3_last_insert_rowid(m_db); + } + + DBCachedContact *cc = m_cache->AddContactToCache(hContact); + NotifyEventHooks(hContactAddedEvent, hContact); + + return hContact; +} + +LONG CDbxSQLite::DeleteContact(MCONTACT hContact) +{ + // global contact cannot be removed + if (hContact == 0) + return 1; + + { + mir_cslock lock(m_csDbAccess); + sqlite3_stmt *stmt = ctc_stmts_prep[SQL_CTC_STMT_DELETE]; + sqlite3_bind_int64(stmt, 1, hContact); + int rc = sqlite3_step(stmt); + sqlite3_reset(stmt); + if (rc != SQLITE_DONE) + return 1; + } + + m_cache->FreeCachedContact(hContact); + NotifyEventHooks(hContactDeletedEvent, hContact); + + return 0; +} + +BOOL CDbxSQLite::IsDbContact(MCONTACT hContact) +{ + DBCachedContact *cc = m_cache->GetCachedContact(hContact); + return (cc != nullptr); +} + +LONG CDbxSQLite::GetContactSize(void) +{ + return sizeof(DBCachedContact); +} |