-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPMaildCoreMySQL.cpp
More file actions
146 lines (116 loc) · 4.64 KB
/
PMaildCoreMySQL.cpp
File metadata and controls
146 lines (116 loc) · 4.64 KB
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
#include "PMaildCoreMySQL.hpp"
#include "PMaildDomain.hpp"
#include "PMaildUser.hpp"
#include "PMaildMail.hpp"
#include <QSettings>
#include <QtSql>
PMaildCoreMySQL::PMaildCoreMySQL(QSettings &settings): PMaildCore(settings) {
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(settings.value("mysql/hostname", "localhost").toString());
db.setUserName(settings.value("mysql/username", "root").toString());
db.setPassword(settings.value("mysql/password", "").toString());
db.setDatabaseName(settings.value("mysql/database", "pmaild").toString());
if (!db.open()) {
qDebug("Fatal error while initializing database backend, giving up!");
qDebug("Error: %s", qPrintable(db.lastError().text()));
exit(1); // ensure we exit *now*, since we are before app.exec(), calling QCoreApplication::exit() won't work
}
query_get_domain_by_domain = QSqlQuery(db);
query_get_domain_by_domain.prepare("SELECT * FROM domains WHERE domain = :domain");
query_get_domain_by_domainid = QSqlQuery(db);
query_get_domain_by_domainid.prepare("SELECT * FROM domains WHERE domainid = :domainid");
query_get_domainalias = QSqlQuery(db);
query_get_domainalias.prepare("SELECT * FROM domainaliases WHERE domain = :domain");
}
bool PMaildCoreMySQL::check() {
if (!QSqlDatabase::isDriverAvailable("QMYSQL")) {
qDebug("QMYSQL driver not available, giving up");
return false;
}
return true;
}
PMaildDomain PMaildCoreMySQL::getDomain(QString domain) {
// get domain from db
QVariantMap params;
params.insert(":domain", domain);
QVariantMap data = execQueryGetFirst(query_get_domain_by_domain, params);
if (data.isEmpty()) {
// check for domain aliases
data = execQueryGetFirst(query_get_domainalias, params);
if (data.isEmpty()) return PMaildDomain();
params.clear();
params.insert(":domainid", data.value("domainid").toString());
data = execQueryGetFirst(query_get_domain_by_domainid, params);
if (data.isEmpty()) return PMaildDomain();
}
return PMaildDomain(this, data);
}
PMaildUser PMaildCoreMySQL::getUser(const PMaildDomain &domain, QString user) {
QSqlQuery q(db);
q.prepare(QString("SELECT * FROM z%1_accounts WHERE user = :user").arg(domain.getId()));
QVariantMap params;
params.insert(":user", user);
QVariantMap res = execQueryGetFirst(q, params);
if (res.isEmpty()) return PMaildUser();
return PMaildUser(this, domain, res);
}
QVariantMap PMaildCoreMySQL::execQueryGetFirst(QSqlQuery &query, const QVariantMap ¶ms) {
for(auto i = params.begin(); i != params.end(); i++)
query.bindValue(i.key(), i.value());
if (!query.exec()) {
qDebug("MySQL error: %s", qPrintable(query.lastError().text()));
return QVariantMap();
}
if ((!query.isActive()) || (!query.isSelect()) || (!query.first())) {
return QVariantMap();
}
QSqlRecord rec = query.record();
QVariantMap res;
for(int i = 0; i < rec.count(); i++) {
res.insert(rec.fieldName(i), query.value(i));
}
query.finish();
return res;
}
QList<PMaildMail> PMaildCoreMySQL::listEmailsByUserFolder(const PMaildUser&user, int folder) {
QSqlQuery q(db);
q.prepare(QString("SELECT * FROM z%1_mails WHERE userid = :userid AND folder = :folder").arg(user.getDomain().getId()));
q.bindValue(":userid", user.getId());
q.bindValue(":folder", folder);
if (!q.exec()) {
qDebug("MySQL error: %s", qPrintable(q.lastError().text()));
return QList<PMaildMail>();
}
if ((!q.isActive()) || (!q.isSelect()) || (!q.first()))
return QList<PMaildMail>();
QSqlRecord rec = q.record();
QList<PMaildMail> res;
do {
QVariantMap tmp;
for(int i = 0; i < rec.count(); i++)
tmp.insert(rec.fieldName(i), q.value(i));
res.append(PMaildMail(this, user, tmp));
} while(q.next());
return res;
}
PMaildMail PMaildCoreMySQL::getEmailByUserId(const PMaildUser&user, int id) {
QSqlQuery q(db);
q.prepare(QString("SELECT * FROM z%1_mails WHERE userid = :userid AND mailid = :mailid").arg(user.getDomain().getId()));
QVariantMap params;
params.insert(":userid", user.getId());
params.insert(":mailid", id);
QVariantMap res = execQueryGetFirst(q, params);
if (res.isEmpty()) return PMaildMail();
return PMaildMail(this, user, res);
}
bool PMaildCoreMySQL::eraseMail(const PMaildMail&mail) {
// 3 requests needed
QSqlQuery q1(db), q2(db), q3(db);
q1.prepare(QString("DELETE FROM z%1_mails WHERE userid = :userid AND mailid = :mailid").arg(mail.getUser().getDomain().getId()));
q2.prepare(QString("DELETE FROM z%1_mails_mime WHERE userid = :userid AND mailid = :mailid").arg(mail.getUser().getDomain().getId()));
q3.prepare(QString("DELETE FROM z%1_mails_mime_header WHERE userid = :userid AND mailid = :mailid").arg(mail.getUser().getDomain().getId()));
if (!q1.exec()) return false;
q2.exec();
q3.exec();
return true;
}