-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.cpp
More file actions
executable file
·363 lines (320 loc) · 11.5 KB
/
Copy pathdb.cpp
File metadata and controls
executable file
·363 lines (320 loc) · 11.5 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
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
359
360
361
362
363
#include "db.h"
const auto USERS_SQL = QLatin1String(R"(
create table users_table
(
user_id int not null auto_increment,
username varchar(50) not null,
password varchar(150) not null,
name varchar(30) not null,
surname varchar(30) not null,
email varchar(50) not null unique,
image LONGBLOB not null,
primary key (user_id)
);
)");
const auto INSERT_USER_SQL = QLatin1String(R"(
insert into users_table(username, password, name, surname, email, image)
values(?, ?, ?, ?, ?, ?)
)");
const auto CHECK_USER_CREDENTIAL_SQL = QLatin1String(R"(
SELECT username, name, surname, email, image
FROM users_table
WHERE email=? AND password=?
)");
const auto UPDATE_USER_IMAGE_SQL = QLatin1String(R"(
UPDATE users_table
SET image = ?
WHERE email = ?
)");
const auto UPDATE_USER_NAME_SQL = QLatin1String(R"(
UPDATE users_table
SET name = ?
WHERE email = ?
)");
const auto UPDATE_USER_SURNAME_SQL = QLatin1String(R"(
UPDATE users_table
SET surname = ?
WHERE email = ?
)");
const auto UPDATE_USER_PASSWORD_SQL = QLatin1String(R"(
UPDATE users_table
SET password = ?
WHERE email = ?
)");
const auto DOCUMENTS_SQL = QLatin1String(R"(
create table documents_table
(
document_id varchar(50) not null,
owner_email varchar(50) not null,
name varchar(150) not null,
path varchar(260) not null,
date varchar(50) not null,
primary key (document_id)
);
)");
const auto INSERT_DOCUMENT_SQL = QLatin1String(R"(
insert into documents_table(document_id, owner_email, name, path, date)
values(?, ?, ?, ?, ?)
)");
const auto DELETE_DOCUMENT_SQL = QLatin1String(R"(
DELETE FROM documents_table WHERE document_id=?;
)");
const auto FIND_DOCUMENT_BY_ID_SQL = QLatin1String(R"(
SELECT document_id, owner_email, name, path, date
FROM documents_table
WHERE document_id=?
)");
const auto FIND_DOCUMENT_BY_EMAIL_SQL = QLatin1String(R"(
SELECT document_id, owner_email, name, path, date
FROM documents_table
WHERE owner_email=?
)");
QString hostname;
QString dbname;
QString port;
QString username;
QString password;
QDir documentsDirectory;
void addUser(QSqlQuery &q, User &user)
{
if (!q.prepare(INSERT_USER_SQL))
return;
q.addBindValue(user.getUsername());
q.addBindValue(user.getPassword());
q.addBindValue(user.getName());
q.addBindValue(user.getSurname());
q.addBindValue(user.getEmail());
q.addBindValue(user.getImage());
q.exec();
}
void updateImg(QSqlQuery &q, QString &email, QByteArray &newImg)
{
if (!q.prepare(UPDATE_USER_IMAGE_SQL))
return;
q.addBindValue(newImg);
q.addBindValue(email);
q.exec();
}
void updateName(QSqlQuery &q, QString &email, QString &newName)
{
if (!q.prepare(UPDATE_USER_NAME_SQL))
return;
q.addBindValue(newName);
q.addBindValue(email);
q.exec();
}
void updateSurname(QSqlQuery &q, QString &email, QString &newSurname)
{
if (!q.prepare(UPDATE_USER_SURNAME_SQL))
return;
q.addBindValue(newSurname);
q.addBindValue(email);
q.exec();
}
void updatePass(QSqlQuery &q, QString &email, QString &newPass)
{
if (!q.prepare(UPDATE_USER_PASSWORD_SQL))
return;
q.addBindValue(newPass);
q.addBindValue(email);
q.exec();
}
bool signUser(QSqlDatabase db, User &user){
QSqlQuery q(db);
addUser(q,user);
if (q.lastError().type() != QSqlError::NoError){
qDebug() << q.lastError().text();
return false;
}
return true;
}
int checkCredentials(QSqlQuery &q, User &user)
{
if (!q.prepare(CHECK_USER_CREDENTIAL_SQL))
return -1;
q.addBindValue(user.getEmail());
q.addBindValue(user.getPassword());
q.exec();
return q.size();
}
bool loginUser (QSqlDatabase db, User &user)
{
QSqlQuery q(db);
int ret = checkCredentials(q,user);
if (ret == -1 || ret == 0) return false;
q.first();
user = User(q.value(0).toString(),q.value(1).toString(),q.value(2).toString(),q.value(3).toString(),q.value(4).toByteArray());
return true;
}
bool deleteFile (QSqlDatabase db, DocumentMessage docm)
{
QSqlQuery q(db);
if (!q.prepare(DELETE_DOCUMENT_SQL))
return false;
q.addBindValue(docm.getDocumentId());
if(q.exec())
return true;
return false;
}
bool updateImgUser (QSqlDatabase db, User &user , QByteArray &newImg){
QSqlQuery q(db);
updateImg(q,user.getEmail(),newImg);
if (q.lastError().type() != QSqlError::NoError){
qDebug() << q.lastError().text();
return false;
}
return true;
}
bool updateNameUser (QSqlDatabase db, User &user , QString &newName){
QSqlQuery q(db);
updateName(q,user.getEmail(),newName);
if (q.lastError().type() != QSqlError::NoError){
qDebug() << q.lastError().text();
return false;
}
return true;
}
bool updateSurnameUser (QSqlDatabase db, User &user , QString &newSurname){
QSqlQuery q(db);
updateSurname(q,user.getEmail(),newSurname);
if (q.lastError().type() != QSqlError::NoError){
qDebug() << q.lastError().text();
return false;
}
return true;
}
bool updatePasswordUser (QSqlDatabase db, User &user , QString &newPass){
QSqlQuery q(db);
updatePass(q,user.getEmail(),newPass);
if (q.lastError().type() != QSqlError::NoError){
qDebug() << q.lastError().text();
return false;
}
return true;
}
void initCred(QString& hostnamei, QString& dbnamei, QString& porti, QString& usernamei, QString& passwordi)
{
hostname = hostnamei;
dbname = dbnamei;
port = porti;
username = usernamei;
password = passwordi;
}
QSqlError initDb(QString& hostname, QString& dbname, QString& port, QString& username, QString& password)
{
initCred(hostname,dbname,port,username,password);
//QString name = QStringLiteral("myConnection_%1").arg(qintptr(QThread::currentThreadId()), 0, 16);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(hostname);
db.setDatabaseName(dbname);
db.setUserName(username);
db.setPort(port.toInt());
db.setPassword(password);
if(!db.open())
return db.lastError();
QSqlQuery q;
QStringList tables = db.tables();
if (!tables.contains("users_table", Qt::CaseInsensitive))
{
qDebug() << "Users table does not exists";
if (!q.exec(USERS_SQL))
{
qDebug() << q.lastError().text();
return q.lastError();
}
}
if (!tables.contains("documents_table", Qt::CaseInsensitive))
{
qDebug() << "Documents table does not exists";
if (!q.exec(DOCUMENTS_SQL))
{
qDebug() << q.lastError().text();
return q.lastError();
}
}
return q.lastError();
}
QSqlDatabase startDb ()
{
qDebug()<<"starting db in thread "<<QThread::currentThread();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL",QStringLiteral("myConnection_%1").arg(qintptr(QThread::currentThreadId()), 0, 16));
db.setHostName(hostname);
db.setDatabaseName(dbname);
db.setUserName(username);
db.setPort(port.toInt());
db.setPassword(password);
return db;
}
void closeDb(QSqlDatabase db)
{
db.close();
db.removeDatabase(db.connectionName());
return;
}
void addDocumentExec(QSqlQuery &q, DocumentEntity &document) {
if (!q.prepare(INSERT_DOCUMENT_SQL))
return;
q.addBindValue(document.getDocumentId());
q.addBindValue(document.getOwnerEmail());
q.addBindValue(document.getName());
q.addBindValue(document.getPath());
q.addBindValue(document.getDate());
q.exec();
}
bool addDocument(QSqlDatabase db, DocumentEntity &document) {
QSqlQuery q(db);
addDocumentExec(q, document);
if (q.lastError().type() != QSqlError::NoError){
qDebug() << q.lastError().text();
return false;
}
return true;
}
int findDocumentByIdExec(QSqlQuery &q, DocumentEntity &document) {
if (!q.prepare(FIND_DOCUMENT_BY_ID_SQL))
return -1;
q.addBindValue(document.getDocumentId());
q.exec();
return q.size();
}
bool findDocumentById(QSqlDatabase db, DocumentEntity &document) {
QSqlQuery q(db);
int ret = findDocumentByIdExec(q, document);
if (ret == -1 || ret == 0) return false;
q.first();
document = DocumentEntity(q.value(0).toString(),
q.value(1).toString(),
q.value(2).toString(),
q.value(3).toString(),
q.value(4).toString());
return true;
}
int findDocumentByOwnerExec(QSqlQuery &q, QString ownerEmail) {
if (!q.prepare(FIND_DOCUMENT_BY_EMAIL_SQL))
return -1;
q.addBindValue(ownerEmail);
q.exec();
return q.size();
}
int findDocumentByOwner(QSqlDatabase db, QVector<DocumentEntity> &documents, QString ownerEmail) {
QSqlQuery q(db);
int ret = findDocumentByOwnerExec(q, ownerEmail);
if (ret == -1 || ret == 0) return ret;
q.first();
documents.clear();
do {
DocumentEntity document(q.value(0).toString(),
q.value(1).toString(),
q.value(2).toString(),
q.value(3).toString(),
q.value(4).toString());
documents.append(document);
} while (q.next());
return documents.size();
}
void setDocumentsDirectory(QString dir) {
documentsDirectory = QDir(dir);
}
QString documentIdToDocumentPath(QUuid documentId) {
return documentsDirectory.filePath(documentId.toString());
}