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
|
#include "stdafx.h"
/////////////////////////////////////////////////////////////////////////////////////////
// first try to find events with wrong contact ids
int CDbxSQLite::CheckPhase1()
{
sqlite3_stmt *pQuery;
int rc = sqlite3_prepare_v2(m_db, "SELECT id, contact_id FROM events WHERE contact_id <> 0 AND contact_id NOT IN (SELECT id FROM contacts)", -1, &pQuery, nullptr);
logError(rc, __FILE__, __LINE__);
if (rc)
return rc;
while (sqlite3_step(pQuery) == SQLITE_ROW) {
MEVENT hDbEvent = sqlite3_column_int(pQuery, 0);
MCONTACT hContact = sqlite3_column_int(pQuery, 1);
cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned event with wrong contact ID %d, deleting"), hContact));
DeleteEvent(hDbEvent);
}
sqlite3_finalize(pQuery);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// then we're wiping orphaned records from Events_srt which have no parent record in Events
int CDbxSQLite::CheckPhase2()
{
sqlite3_stmt *pQuery;
int rc = sqlite3_prepare_v2(m_db, "SELECT id, contact_id FROM events_srt WHERE id NOT IN (SELECT id FROM events)", -1, &pQuery, nullptr);
logError(rc, __FILE__, __LINE__);
if (rc)
return rc;
while (sqlite3_step(pQuery) == SQLITE_ROW) {
MEVENT hDbEvent = sqlite3_column_int(pQuery, 0);
MCONTACT hContact = sqlite3_column_int(pQuery, 1);
DeleteEventSrt(hDbEvent);
cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned sorting event with wrong event ID %d:%08X, deleting"), hContact, hDbEvent));
}
sqlite3_finalize(pQuery);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// now try to find orphans in backward direction: from Events_srt to Events
int CDbxSQLite::CheckPhase3()
{
sqlite3_stmt *pQuery;
int rc = sqlite3_prepare_v2(m_db, "SELECT id, contact_id FROM events WHERE id NOT IN (SELECT id FROM events_srt)", -1, &pQuery, nullptr);
logError(rc, __FILE__, __LINE__);
if (rc)
return rc;
while (sqlite3_step(pQuery) == SQLITE_ROW) {
MEVENT hDbEvent = sqlite3_column_int(pQuery, 0);
MCONTACT hContact = sqlite3_column_int(pQuery, 1);
DeleteEventMain(hDbEvent);
cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned event with wrong event ID %d:%08X, deleting"), hContact, hDbEvent));
}
sqlite3_finalize(pQuery);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// remove settings with wrong contact ids
int CDbxSQLite::CheckPhase4()
{
sqlite3_stmt *pQuery;
int rc = sqlite3_prepare_v2(m_db, "SELECT contact_id,module,setting FROM settings WHERE contact_id <> 0 AND contact_id NOT IN (SELECT id FROM contacts)", -1, &pQuery, nullptr);
logError(rc, __FILE__, __LINE__);
if (rc)
return rc;
while (sqlite3_step(pQuery) == SQLITE_ROW) {
MCONTACT hContact = sqlite3_column_int(pQuery, 0);
auto *szModule = (const char *)sqlite3_column_text(pQuery, 1);
auto *szSetting = (const char *)sqlite3_column_text(pQuery, 2);
cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned setting [%S:%S] with wrong contact ID %d, deleting"), szModule, szSetting, hContact));
DeleteContactSettingWorker(hContact, szModule, szSetting);
}
sqlite3_finalize(pQuery);
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// MIDatabaseChecker
int CDbxSQLite::CheckDb(int phase)
{
switch (phase) {
case 0: return CheckPhase1();
case 1: return CheckPhase2();
case 2: return CheckPhase3();
case 3: return CheckPhase4();
}
DBFlush(true);
return ERROR_OUT_OF_PAPER;
}
|