summaryrefslogtreecommitdiff
path: root/plugins/Dbx_sqlite/src/dbsettings.cpp
blob: 6adcae1bf674ab89360b1959669f9c7095c33bad (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "stdafx.h"

enum {
	SQL_SET_STMT_ENUM = 0,
	SQL_SET_STMT_GET,
	SQL_SET_STMT_REPLACE,
	SQL_SET_STMT_DELETE,
	SQL_SET_STMT_ENUMMODULE,
	SQL_SET_STMT_NUM
};

static char *settings_stmts[SQL_SET_STMT_NUM] = {
	"select distinct module from settings;",
	"select type, value from settings where contact_id = ? and module = ? and setting = ? limit 1;",
	"replace into settings(contact_id, module, setting, type, value) values (?, ?, ?, ?, ?);",
	"delete from settings where contact_id = ? and module = ? and setting = ?;select changes()",
	"select setting from settings where contact_id = ? and module = ?;"
};

static sqlite3_stmt *settings_stmts_prep[SQL_SET_STMT_NUM] = { 0 };

void CDbxSQLite::InitSettings()
{
	for (size_t i = 0; i < SQL_SET_STMT_NUM; i++)
		sqlite3_prepare_v3(m_db, settings_stmts[i], -1, SQLITE_PREPARE_PERSISTENT, &settings_stmts_prep[i], nullptr);
}

void CDbxSQLite::UninitSettings()
{
	for (size_t i = 0; i < SQL_SET_STMT_NUM; i++)
		sqlite3_finalize(settings_stmts_prep[i]);
}

BOOL CDbxSQLite::EnumModuleNames(DBMODULEENUMPROC pFunc, void *param)
{
	LIST<char> modules(100);
	{
		sqlite3_stmt *stmt = settings_stmts_prep[SQL_SET_STMT_ENUM];
		int rc = 0;
		while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
			const char *value = (const char*)sqlite3_column_text(stmt, 0);
			modules.insert(mir_strdup(value));
		}
		assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
		sqlite3_reset(stmt);
	}

	int result = -1;
	for (auto &module : modules) {
		result = pFunc(module, param);
		mir_free(module);
	}
	return result;
}

static bool ValidLookupName(LPCSTR szModule, LPCSTR szSetting)
{
	if (!strcmp(szModule, META_PROTO))
		return strcmp(szSetting, "IsSubcontact") && strcmp(szSetting, "ParentMetaID");

	if (!strcmp(szModule, "Ignore"))
		return false;

	return true;
}

BOOL CDbxSQLite::GetContactSettingWorker(MCONTACT hContact, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv, int isStatic)
{
	if (szSetting == nullptr || szModule == nullptr)
		return 1;

	DBCachedContact *cc = nullptr;
	if (hContact) {
		cc = m_cache->GetCachedContact(hContact);
		if (cc == nullptr)
			return 1;
	}

	mir_cslock lock(m_csDbAccess);

LBL_Seek:
	char *cachedSettingName = m_cache->GetCachedSetting(szModule, szSetting, mir_strlen(szModule), mir_strlen(szSetting));
	DBVARIANT *pCachedValue = m_cache->GetCachedValuePtr(hContact, cachedSettingName, 0);
	if (pCachedValue != nullptr) {
		if (pCachedValue->type == DBVT_UTF8) {
			int cbOrigLen = dbv->cchVal;
			char *cbOrigPtr = dbv->pszVal;
			memcpy(dbv, pCachedValue, sizeof(DBVARIANT));
			if (isStatic) {
				int cbLen = 0;
				if (pCachedValue->pszVal != nullptr)
					cbLen = (int)mir_strlen(pCachedValue->pszVal);

				cbOrigLen--;
				dbv->pszVal = cbOrigPtr;
				if (cbLen < cbOrigLen)
					cbOrigLen = cbLen;
				memcpy(dbv->pszVal, pCachedValue->pszVal, cbOrigLen);
				dbv->pszVal[cbOrigLen] = 0;
				dbv->cchVal = cbLen;
			}
			else {
				dbv->pszVal = (char*)mir_alloc(mir_strlen(pCachedValue->pszVal) + 1);
				mir_strcpy(dbv->pszVal, pCachedValue->pszVal);
			}
		}
		else memcpy(dbv, pCachedValue, sizeof(DBVARIANT));

		return (pCachedValue->type == DBVT_DELETED) ? 1 : 0;
	}

	// never look db for the resident variable
	if (cachedSettingName[-1] != 0)
		return 1;

	sqlite3_stmt *stmt = settings_stmts_prep[SQL_SET_STMT_GET];
	sqlite3_bind_int64(stmt, 1, hContact);
	sqlite3_bind_text(stmt, 2, szModule, mir_strlen(szModule), nullptr);
	sqlite3_bind_text(stmt, 3, szSetting, mir_strlen(szSetting), nullptr);
	int rc = sqlite3_step(stmt);
	assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
	if (rc != SQLITE_ROW) {
		sqlite3_reset(stmt);
		if (rc == SQLITE_DONE && cc && cc->IsMeta() && ValidLookupName(szModule, szSetting)) {
			if (hContact = db_mc_getDefault(hContact)) {
				if (szModule = GetContactProto(hContact))
					goto LBL_Seek;
			}
		}
		return 1;
	}
	dbv->type = (int)sqlite3_column_int(stmt, 0);
	switch (dbv->type) {
	case DBVT_DELETED:
		dbv->type = DBVT_DELETED;
		sqlite3_reset(stmt);
		return 2;
	case DBVT_BYTE:
		dbv->bVal = sqlite3_column_int(stmt, 1);
		break;
	case DBVT_WORD:
		dbv->wVal = sqlite3_column_int(stmt, 1);
		break;
	case DBVT_DWORD:
		dbv->dVal = sqlite3_column_int64(stmt, 1);
		break;
	case DBVT_UTF8:
	{
		dbv->cchVal = sqlite3_column_bytes(stmt, 1);
		const char *value = (const char*)sqlite3_column_text(stmt, 1);
		if (!isStatic)
			dbv->pszVal = (char*)mir_alloc(dbv->cchVal + 1);
		memcpy(dbv->pszVal, value, dbv->cchVal);
		dbv->pszVal[dbv->cchVal] = 0;
		break;
	}
	case DBVT_BLOB:
	{
		dbv->cpbVal = sqlite3_column_bytes(stmt, 1);
		const char *data = (const char*)sqlite3_column_blob(stmt, 1);
		if (!isStatic)
			dbv->pbVal = (BYTE*)mir_alloc(dbv->cpbVal + 1);
		memcpy(dbv->pbVal, data, dbv->cpbVal);
		break;
	}
	}
	sqlite3_reset(stmt);

	// add to cache
	if (dbv->type != DBVT_BLOB) {
		pCachedValue = m_cache->GetCachedValuePtr(hContact, cachedSettingName, 1);
		if (pCachedValue != nullptr)
			m_cache->SetCachedVariant(dbv, pCachedValue);
	}

	return 0;
}

BOOL CDbxSQLite::WriteContactSetting(MCONTACT hContact, DBCONTACTWRITESETTING *dbcws)
{
	if (dbcws == nullptr || dbcws->szSetting == nullptr || dbcws->szModule == nullptr)
		return 1;

	if (hContact) {
		DBCachedContact *cc = m_cache->GetCachedContact(hContact);
		if (cc == nullptr)
			return 1;
	}

	DBCONTACTWRITESETTING dbcwNotif = *dbcws;
	// we work only with utf-8 inside
	switch (dbcwNotif.value.type) {
	case DBVT_UTF8:
		dbcwNotif.value.pszVal = mir_strdup(dbcws->value.pszVal);
		break;
	case DBVT_ASCIIZ:
	{
		ptrA value(mir_utf8encode(dbcws->value.pszVal));
		dbcwNotif.value.pszVal = NEWSTR_ALLOCA(value);
		dbcwNotif.value.type = DBVT_UTF8;
		break;
	}
	case DBVT_WCHAR:
	{
		T2Utf value(dbcwNotif.value.pwszVal);
		dbcwNotif.value.pszVal = NEWSTR_ALLOCA(value);
		dbcwNotif.value.type = DBVT_UTF8;
		break;
	}
	}
	DBCONTACTWRITESETTING dbcwWork = dbcwNotif;
	if (dbcwWork.value.type == DBVT_UTF8)
		dbcwWork.value.cchVal = (WORD)strlen(dbcwWork.value.pszVal);

	mir_cslockfull lock(m_csDbAccess);

	char *cachedSettingName = m_cache->GetCachedSetting(dbcwWork.szModule, dbcwWork.szSetting, mir_strlen(dbcwWork.szModule), mir_strlen(dbcwWork.szSetting));
	bool isResident = cachedSettingName[-1] != 0;

	// we don't cache blobs
	if (dbcwWork.value.type != DBVT_BLOB) {
		DBVARIANT *cachedValue = m_cache->GetCachedValuePtr(hContact, cachedSettingName, 1);
		if (cachedValue != nullptr) {
			bool isIdentical = false;
			if (cachedValue->type == dbcwWork.value.type) {
				switch (dbcwWork.value.type) {
				case DBVT_BYTE:
					isIdentical = cachedValue->bVal == dbcwWork.value.bVal;
					break;
				case DBVT_WORD:
					isIdentical = cachedValue->wVal == dbcwWork.value.wVal;
					break;
				case DBVT_DWORD:
					isIdentical = cachedValue->dVal == dbcwWork.value.dVal;
					break;
				case DBVT_UTF8:
					isIdentical = mir_strcmp(cachedValue->pszVal, dbcwWork.value.pszVal) == 0;
					break;
				}
				if (isIdentical)
					return 0;
			}
			m_cache->SetCachedVariant(&dbcwWork.value, cachedValue);
		}
		if (isResident) {
			lock.unlock();
			NotifyEventHooks(hSettingChangeEvent, hContact, (LPARAM)&dbcwWork);
			return 0;
		}
	}
	else m_cache->GetCachedValuePtr(hContact, cachedSettingName, -1);

	sqlite3_stmt *stmt = settings_stmts_prep[SQL_SET_STMT_REPLACE];
	sqlite3_bind_int64(stmt, 1, hContact);
	sqlite3_bind_text(stmt, 2, dbcwWork.szModule, mir_strlen(dbcwWork.szModule), nullptr);
	sqlite3_bind_text(stmt, 3, dbcwWork.szSetting, mir_strlen(dbcwWork.szSetting), nullptr);
	sqlite3_bind_int(stmt, 4, dbcwWork.value.type);
	switch (dbcwWork.value.type) {
	case DBVT_BYTE:
		sqlite3_bind_int(stmt, 5, dbcwWork.value.bVal);
		break;
	case DBVT_WORD:
		sqlite3_bind_int(stmt, 5, dbcwWork.value.wVal);
		break;
	case DBVT_DWORD:
		sqlite3_bind_int64(stmt, 5, dbcwWork.value.dVal);
		break;
	case DBVT_UTF8:
		sqlite3_bind_text(stmt, 5, dbcwWork.value.pszVal, dbcwWork.value.cchVal, nullptr);
		break;
	case DBVT_BLOB:
		sqlite3_bind_blob(stmt, 5, dbcwWork.value.pbVal, dbcwWork.value.cpbVal, nullptr);
		break;
	}
	int rc = sqlite3_step(stmt);
	assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
	sqlite3_reset(stmt);
	if (rc != SQLITE_DONE)
		return 1;

	lock.unlock();

	NotifyEventHooks(hSettingChangeEvent, hContact, (LPARAM)&dbcwNotif);

	return 0;
}

BOOL CDbxSQLite::DeleteContactSetting(MCONTACT hContact, LPCSTR szModule, LPCSTR szSetting)
{
	if (szSetting == nullptr || szModule == nullptr)
		return 1;

	if (hContact) {
		DBCachedContact *cc = m_cache->GetCachedContact(hContact);
		if (cc == nullptr)
			return 1;
	}
	
	char *szCachedSettingName = m_cache->GetCachedSetting(szModule, szSetting, mir_strlen(szModule), mir_strlen(szSetting));
	if (szCachedSettingName[-1] == 0)  // it's not a resident variable
	{
		mir_cslock lock(m_csDbAccess);
		sqlite3_stmt *stmt = settings_stmts_prep[SQL_SET_STMT_DELETE];
		sqlite3_bind_int64(stmt, 1, hContact);
		sqlite3_bind_text(stmt, 2, szModule, mir_strlen(szModule), nullptr);
		sqlite3_bind_text(stmt, 3, szSetting, mir_strlen(szSetting), nullptr);
		int rc = sqlite3_step(stmt);
		assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
		auto deleted = sqlite3_column_int64(stmt, 0);
		sqlite3_reset(stmt);
		if (rc != SQLITE_ROW || deleted == 0)
			return 1;
	}
	m_cache->GetCachedValuePtr(hContact, szCachedSettingName, -1);

	// notify
	DBCONTACTWRITESETTING dbcws = { 0 };
	dbcws.szModule = szModule;
	dbcws.szSetting = szSetting;
	dbcws.value.type = DBVT_DELETED;
	NotifyEventHooks(hSettingChangeEvent, hContact, (LPARAM)&dbcws);

	return 0;
}

BOOL CDbxSQLite::EnumContactSettings(MCONTACT hContact, DBSETTINGENUMPROC pfnEnumProc, const char *szModule, void *param)
{
	if (szModule == nullptr)
		return -1;

	if (hContact) {
		DBCachedContact *cc = m_cache->GetCachedContact(hContact);
		if (cc == nullptr)
			return -1;
	}

	LIST<char> settings(100);
	{
		mir_cslock lock(m_csDbAccess);
		sqlite3_stmt *stmt = settings_stmts_prep[SQL_SET_STMT_ENUMMODULE];
		sqlite3_bind_int64(stmt, 1, hContact);
		sqlite3_bind_text(stmt, 2, szModule, mir_strlen(szModule), nullptr);
		int rc = 0;
		while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
			const char *value = (const char*)sqlite3_column_text(stmt, 0);
			settings.insert(mir_strdup(value));
		}
		assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
		sqlite3_reset(stmt);
	}

	int result = -1;
	for (auto &setting : settings) {
		result = pfnEnumProc(setting, param);
		mir_free(setting);
	}
	return result;
}