-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.cpp
219 lines (183 loc) · 6.31 KB
/
database.cpp
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
#include "database.h"
#include "user.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QDate>
#include <QMutexLocker>
Database::Database():
mMutex(), iLogPrefix("[Database] ")
{
#ifdef LOG_CONSTRUCTORS
_log("Constructor");
#endif
}
Database::~Database(){
#ifdef LOG_CONSTRUCTORS
_log("Destructor");
#endif
if (iDb.isOpen())
iDb.close();
}
bool Database::addDatabase(DatabaseParameters * const aDbParam){
if (QSqlDatabase::contains()){
_log("addDatabase: DB already exists...");
iDb = QSqlDatabase::database();
} else {
iDb = QSqlDatabase::addDatabase(aDbParam->driver);
iDb.setHostName(aDbParam->host);
iDb.setPort(aDbParam->port);
QString userName(aDbParam->login);
if (!NntpProxy::decrypt(userName)){
_log("Error decrypting username...");
return false;
}
iDb.setUserName(userName);
QString pass(aDbParam->pass);
if (!NntpProxy::decrypt(pass)){
_log("Error decrypting password...");
return false;
}
iDb.setPassword(pass);
iDb.setDatabaseName(aDbParam->name);
_log("addDatabase: DB added!");
}
return iDb.isValid();
}
bool Database::connect(){
if(iDb.isOpen()){
#ifdef LOG_DATABASE_ACTIONS
_log("Database already connected");
#endif
return true;
}
else{
bool ret;
ushort nbTry = 0;
do {
ret = iDb.open();
if (!ret){
_log_error("connecting to the DB", iDb.lastError());
#ifdef LOG_DATABASE_ACTIONS
_log("[connect] Let's try to close the database and reOpen...");
#endif
iDb.close();
}
} while (!ret && (nbTry++ < cDatabaseConnectionTry));
#ifdef LOG_DATABASE_ACTIONS
if (ret)
_log("Database connected");
#endif
return ret;
}
}
bool Database::prepareSqlRequest(QSqlQuery &aQuery, const char * aSqlReq){
bool ret;
ushort nbTry = 0;
do{
ret = aQuery.prepare(aSqlReq);
if (!ret){
_log_error("preparing request", aQuery.lastError());
// Error #2006, MySQL server has gone away QMYSQL3: Unable to prepare statement
if (aQuery.lastError().number() == cMysqlConnectionTimeout){
#ifdef LOG_DATABASE_ACTIONS
_log("[prepareSqlRequest] MySql Timeout, let's close the connection and reopen it");
#endif
iDb.close();
connect();
// Closing the DB invalidate all QSqlQuery, we need to recreate it
aQuery = QSqlQuery();
}
}
} while (!ret && (nbTry++ < 1));
return ret;
}
bool Database::checkAuthentication(User *const aUser, const QString aPass){
QMutexLocker lock(&mMutex);
if (!connect())
return false;
QSqlQuery qCheckAuthentication;
bool ret = prepareSqlRequest(qCheckAuthentication, cSqlCheckAuthentication);
if (!ret){
_log_error("preparing request qCheckAuthentication", qCheckAuthentication.lastError());
qCheckAuthentication.finish();
return ret;
}
qCheckAuthentication.bindValue(":login", aUser->getLogin());
qCheckAuthentication.bindValue(":pass", aPass);
ret = qCheckAuthentication.exec();
if (!ret){
_log_error("executing request qCheckAuthentication", qCheckAuthentication.lastError());
qCheckAuthentication.finish();
return ret;
}
// If no record, wrong Authentication
if (!qCheckAuthentication.next()){
QTextStream &ostream = NntpProxy::acquireLog(iLogPrefix);
ostream << "There are no records for this user/pass: ("
<< aUser->getLogin() << " : " << aPass << ")";
NntpProxy::releaseLog();
qCheckAuthentication.finish();
return false;
}
// We've a match, let's fill aUser
aUser->setDbId(qCheckAuthentication.value(0).toInt());
aUser->setBlocked(qCheckAuthentication.value(1).toBool());
// qCheckAuthentication.clear();
qCheckAuthentication.finish();
_log("Authentication OK!!!");
return true;
}
uint Database::addUserSize(User *const aUser){
QMutexLocker lock(&mMutex);
if (!connect())
return 0;
QSqlQuery callStored;
QString theMonth(QDate::currentDate().toString("yyyy.MM"));
int size = aUser->getDownloadedSize()/1048576; // in MB
// Out parameter code is MySQL specific
if (!prepareSqlRequest(callStored, cSqlAddUserSize)) {
QTextStream &ostream = NntpProxy::acquireLog(iLogPrefix);
QSqlError err = callStored.lastError();
ostream << "Error #" << err.number()
<< ", preparing addUserSize(user: " << aUser->getLogin()
<< " (dbId: " << aUser->getDbId() << ") "
<< "ip: " << aUser->getIp()
<< ", download size: " << size
<< ", month: " << theMonth
<< "): " << err.text();
NntpProxy::releaseLog();
callStored.finish();
return 0;
}
callStored.bindValue(":p_user_id", aUser->getDbId(), QSql::In);
callStored.bindValue(":p_month", theMonth, QSql::In);
callStored.bindValue(":p_ip", aUser->getIp(), QSql::In);
callStored.bindValue(":p_size", size, QSql::In);
if(!callStored.exec()) {
QTextStream &ostream = NntpProxy::acquireLog(iLogPrefix);
QSqlError err = callStored.lastError();
ostream << "Error #" << err.number()
<< ", executing addUserSize(user: " << aUser->getLogin()
<< " (dbId: " << aUser->getDbId() << ") "
<< "ip: " << aUser->getIp()
<< ", download size: " << size
<< ", month: " << theMonth
<< "): " << err.text();
NntpProxy::releaseLog();
callStored.finish();
return 0;
}
callStored.exec("select @m_size");
callStored.next();
uint monthSize = callStored.value(0).toInt();
callStored.finish();
QTextStream &ostream = NntpProxy::acquireLog(iLogPrefix);
ostream << "addUserSize(user: " << aUser->getLogin()
<< " (dbId: " << aUser->getDbId() << ") "
<< "ip: " << aUser->getIp()
<< ", download size: " << size
<< ", month: " << theMonth
<< ") => new month size = " << monthSize;
NntpProxy::releaseLog();
return monthSize;
}