summaryrefslogtreecommitdiff
path: root/plugins/Dbx_sqlite/src/dbsettings.cpp
blob: d9e33abb910146ea76e7f6f9ae4a2b60294de08b (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
#include "stdafx.h"

void CDbxSQLite::InitSettings()
{
	sqlite3_stmt *stmt = nullptr;
	sqlite3_prepare_v2(m_db, "SELECT type, value, contact_id, module, setting FROM settings;", -1, &stmt, nullptr);
	while (sqlite3_step(stmt) == SQLITE_ROW) {
		MCONTACT hContact = sqlite3_column_int64(stmt, 2);
		auto *szModule = (const char *)sqlite3_column_text(stmt, 3);
		auto *szSetting = (const char *)sqlite3_column_text(stmt, 4);

		size_t settingNameLen = strlen(szSetting);
		size_t moduleNameLen = strlen(szModule);

		char *szCachedSettingName = m_cache->GetCachedSetting(szModule, szSetting, moduleNameLen, settingNameLen);

		DBVARIANT *dbv = m_cache->GetCachedValuePtr(hContact, szCachedSettingName, 1);
		if (dbv == nullptr) // garbage! a setting for removed/non-existent contact
			continue;

		dbv->type = (int)sqlite3_column_int(stmt, 0);
		switch (dbv->type) {
		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_ASCIIZ:
		case DBVT_UTF8:
			dbv->cchVal = sqlite3_column_bytes(stmt, 1);
			{
				const char *value = (const char *)sqlite3_column_text(stmt, 1);
				dbv->pszVal = (char *)mir_alloc(dbv->cchVal + 1);
				memcpy(dbv->pszVal, value, dbv->cchVal);
				dbv->pszVal[dbv->cchVal] = 0;
			}
			break;

		case DBVT_ENCRYPTED:
		case DBVT_BLOB:
			dbv->cpbVal = sqlite3_column_bytes(stmt, 1);
			{
				const char *data = (const char *)sqlite3_column_blob(stmt, 1);
				dbv->pbVal = (uint8_t *)mir_alloc(dbv->cpbVal + 1);
				memcpy(dbv->pbVal, data, dbv->cpbVal);
				dbv->pbVal[dbv->cpbVal] = 0;
			}
			break;
		}
	}
	sqlite3_finalize(stmt);

	FillContactSettings();

	DBVARIANT dbv = { DBVT_BYTE };
	if (GetContactSettingWorker(0, "Compatibility", "Sqlite", &dbv, 0))
		dbv.bVal = 0;

	if (dbv.bVal < 1) {
		int rc = sqlite3_exec(m_db, "ALTER TABLE events ADD COLUMN is_read INTEGER NOT NULL DEFAULT 0;", 0, 0, 0);
		logError(rc, __FILE__, __LINE__);

		rc = sqlite3_exec(m_db, "CREATE INDEX idx_events_isread ON events(contact_id, is_read, timestamp);", nullptr, nullptr, nullptr);
		logError(rc, __FILE__, __LINE__);

		rc = sqlite3_exec(m_db, "UPDATE events SET is_read=1 WHERE (flags & 6) <> 0;", nullptr, nullptr, nullptr);
		logError(rc, __FILE__, __LINE__);

		dbv.bVal = 1;
	}
	
	if (dbv.bVal < 2) {
		int rc = sqlite3_exec(m_db, "ALTER TABLE events ADD COLUMN user_id TEXT NULL;", 0, 0, 0);
		logError(rc, __FILE__, __LINE__);

		dbv.bVal = 2;
		WriteContactSetting(0, "Compatibility", "Sqlite", &dbv);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

BOOL CDbxSQLite::EnumModuleNames(DBMODULEENUMPROC pFunc, void *param)
{
	LIST<char> modules(100);
	{
		sqlite3_stmt *stmt = InitQuery("SELECT DISTINCT module FROM settings;", qSettModules);
		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));
		}
		logError(rc, __FILE__, __LINE__);
		sqlite3_reset(stmt);
	}

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

/////////////////////////////////////////////////////////////////////////////////////////

BOOL CDbxSQLite::WriteContactSettingWorker(MCONTACT hContact, DBCONTACTWRITESETTING &dbcws)
{
	sqlite3_stmt *stmt = InitQuery("REPLACE INTO settings(contact_id, module, setting, type, value) VALUES (?, ?, ?, ?, ?);", qSettWrite);
	sqlite3_bind_int64(stmt, 1, hContact);
	sqlite3_bind_text(stmt, 2, dbcws.szModule, (int)mir_strlen(dbcws.szModule), nullptr);
	sqlite3_bind_text(stmt, 3, dbcws.szSetting, (int)mir_strlen(dbcws.szSetting), nullptr);
	sqlite3_bind_int(stmt, 4, dbcws.value.type);
	switch (dbcws.value.type) {
	case DBVT_BYTE:
		sqlite3_bind_int(stmt, 5, dbcws.value.bVal);
		break;
	case DBVT_WORD:
		sqlite3_bind_int(stmt, 5, dbcws.value.wVal);
		break;
	case DBVT_DWORD:
		sqlite3_bind_int64(stmt, 5, dbcws.value.dVal);
		break;
	case DBVT_ASCIIZ:
	case DBVT_UTF8:
		sqlite3_bind_text(stmt, 5, dbcws.value.pszVal, dbcws.value.cchVal, nullptr);
		break;
	case DBVT_ENCRYPTED:
	case DBVT_BLOB:
		sqlite3_bind_blob(stmt, 5, dbcws.value.pbVal, dbcws.value.cpbVal, nullptr);
		break;
	}

	int rc = sqlite3_step(stmt);
	logError(rc, __FILE__, __LINE__);
	sqlite3_reset(stmt);
	if (rc != SQLITE_DONE)
		return 1;

	DBFlush();
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

int CDbxSQLite::DeleteContactSettingWorker(MCONTACT hContact, LPCSTR szModule, LPCSTR szSetting)
{
	mir_cslock lock(m_csDbAccess);
	sqlite3_stmt *stmt = InitQuery("DELETE FROM settings WHERE contact_id = ? AND module = ? AND setting = ?;", qSettDel);
	sqlite3_bind_int64(stmt, 1, hContact);
	sqlite3_bind_text(stmt, 2, szModule, (int)mir_strlen(szModule), nullptr);
	sqlite3_bind_text(stmt, 3, szSetting, (int)mir_strlen(szSetting), nullptr);
	int rc = sqlite3_step(stmt);
	logError(rc, __FILE__, __LINE__);
	sqlite3_reset(stmt);
	return rc;
}

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;
	}

	// if a setting isn't found in cache, then return an error - we don't cache misses anymore
	char *szCachedSettingName = m_cache->GetCachedSetting(szModule, szSetting, mir_strlen(szModule), mir_strlen(szSetting));
	if (m_cache->GetCachedValuePtr(hContact, szCachedSettingName, -1) == nullptr)
		return 1;

	if (szCachedSettingName[-1] == 0) { // it's not a resident variable
		DeleteContactSettingWorker(hContact, szModule, szSetting);
		DBFlush();
	}

	// notify
	DBCONTACTWRITESETTING dbcws = { 0 };
	dbcws.szModule = szModule;
	dbcws.szSetting = szSetting;
	dbcws.value.type = DBVT_DELETED;
	NotifyEventHooks(g_hevSettingChanged, 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 = InitQuery("SELECT setting FROM settings WHERE contact_id = ? AND module = ?;", qSettEnum);
		sqlite3_bind_int64(stmt, 1, hContact);
		sqlite3_bind_text(stmt, 2, szModule, (int)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));
		}
		logError(rc, __FILE__, __LINE__);
		sqlite3_reset(stmt);
	}

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