summaryrefslogtreecommitdiff
path: root/plugins/Dbx_sqlite/src/dbcontacts.cpp
blob: 882b04d30e42fd566f556fe5207590b7b2e4a4d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#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 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);

	sqlite3_stmt *stmt = nullptr;
	sqlite3_prepare_v2(m_db, "select contacts.id, count(event_id) from contacts left join contact_events on contact_events.contact_id = contacts.id group by contacts.id;", -1, &stmt, nullptr);
	int rc = 0;
	while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
		MCONTACT hContact = sqlite3_column_int64(stmt, 0);
		DBCachedContact *cc = (hContact)
			? m_cache->AddContactToCache(hContact)
			: &m_system;
		cc->count = sqlite3_column_int64(stmt, 1);

		DBVARIANT dbv = { DBVT_DWORD };
		cc->nSubs = (0 != GetContactSetting(cc->contactID, META_PROTO, "NumContacts", &dbv)) ? -1 : dbv.dVal;
		if (cc->nSubs != -1) {
			cc->pSubs = (MCONTACT*)mir_alloc(cc->nSubs * sizeof(MCONTACT));
			for (int k = 0; k < cc->nSubs; k++) {
				char setting[100];
				mir_snprintf(setting, _countof(setting), "Handle%d", k);
				cc->pSubs[k] = (0 != GetContactSetting(cc->contactID, META_PROTO, setting, &dbv)) ? 0 : dbv.dVal;
			}
		}
		cc->nDefault = (0 != GetContactSetting(cc->contactID, META_PROTO, "Default", &dbv)) ? -1 : dbv.dVal;
		cc->parentID = (0 != GetContactSetting(cc->contactID, META_PROTO, "ParentMeta", &dbv)) ? 0 : dbv.dVal;
	}
	assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
	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);
	assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
	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);
		assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
		sqlite3_reset(stmt);
		if (rc != SQLITE_DONE)
			return INVALID_CONTACT_ID;
		hContact = sqlite3_last_insert_rowid(m_db);
	}

	DBCachedContact *cc = m_cache->AddContactToCache(hContact);
	if (cc == nullptr)
		return INVALID_CONTACT_ID;
	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);
		assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
		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);
}