-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmplibdatabase.cpp
More file actions
496 lines (425 loc) · 17.9 KB
/
Copy pathsmplibdatabase.cpp
File metadata and controls
496 lines (425 loc) · 17.9 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include "smplibdatabase.h"
#include <QMap>
#include <QThread>
#include <QMessageLogContext>
#include <QSqlError>
#include <QDebug>
SmpLibDatabase* SmpLibDatabase::m_Instance = 0;
std::mutex lockReading;
SmpLibDatabase::SmpLibDatabase(){};
enum class DB_ENGINES
{
QMARIADB = 0,
QSQLITE = 1,
};
QMap<int, QString> SmpLibDatabase::DbEngines {{(int)DB_ENGINES::QMARIADB, "QMARIADB"}, {(int)DB_ENGINES::QSQLITE, "QSQLITE"}};
SmpLibDatabase* SmpLibDatabase::CreateSmpLibDatabase(QString dbPath, int Engine)
{
auto _db = new SmpLibDatabase();
QString strThread = QString("%1").arg((long)QThread::currentThread());
_db->db = QSqlDatabase::addDatabase(DbEngines[Engine],strThread);
auto isa = _db->db.drivers();
bool bValidDb = false;
if(Engine==(int)DB_ENGINES::QMARIADB)//QMYSQL
{
_db->db.setDatabaseName("smplib");
_db->db.setUserName("smplib");
_db->db.setPassword("smplib");
if((bValidDb = _db->db.open()))
{
//prepare database if first run
_db->CreateTables(false);/*if not exist*/
}
}
else if(Engine==(int)DB_ENGINES::QSQLITE)//QSQLITE
{
_db->db.setDatabaseName(dbPath);
_db->db.setUserName("smplib");
_db->db.setPassword("smplib");
if((bValidDb = _db->db.open()))
{
//prepare database if first run
_db->CreateTablesSqlite(false);/*if not exist*/
}
}
if(!bValidDb)
{
auto err = _db->db.lastError().text();
qDebug() << err;
delete _db;
_db = nullptr;
}
return _db;
}
SmpLibDatabase::~SmpLibDatabase()
{
db.commit();
db.close();
QString strThread = QString("%1").arg((long)QThread::currentThread());
QSqlDatabase::removeDatabase(strThread);
}
void SmpLibDatabase::CreateTables(bool bRecreate)
{
QStringList lstTables = QStringList() << "tblAuthor"<<"tblLibFiles"<<"tblBook";
QMap<QString, QString> tblMap;
tblMap.insert("tblAuthor", "`id` INT(11) NOT NULL AUTO_INCREMENT,"
"PRIMARY KEY (`id`), "
"`first_name` VARCHAR(50) NULL,"
"`last_name` VARCHAR(100) NULL,"
"`nickname` VARCHAR(100) NULL"
);
tblMap.insert("tblLibFiles", "`id` INT(11) NOT NULL AUTO_INCREMENT,"
"PRIMARY KEY (`id`),"
"`lib_title` VARCHAR(255) NULL,"
"`filename` VARCHAR(50) NOT NULL,"
"`filepath` VARCHAR(1000) NULL,"
"`filehash` VARCHAR(128) NULL"
);
tblMap.insert("tblBook", "`id` INT(11) NOT NULL AUTO_INCREMENT,"
"PRIMARY KEY (`id`),"
"`book_title` VARCHAR(255) NULL,"
"`authors` VARCHAR(80) NOT NULL,"
"`genre` VARCHAR(20),"
"`sequence_name` VARCHAR(50),"
"`sequence_number` VARCHAR(10),"
"`libfile_id` INT(11),"
"CONSTRAINT `fkLibFile`"
"FOREIGN KEY `fkLibFile` (`libfile_id`)"
"REFERENCES `tblLibFiles` (`id`),"
"`name_in_archive` VARCHAR(255),"
"`book_size` INT(11) NOT NULL"
);
QSqlQuery query(db);
foreach(QString sTable, lstTables)
{
db.transaction();
QString qsQuery = "";
if(bRecreate)
qsQuery = "ALTER TABLE `%1` %2";
else
qsQuery = "CREATE TABLE IF NOT EXISTS `%1` (%2)";
QString qs2 = qsQuery.arg(sTable, tblMap.value(sTable));
query.prepare(qs2);
query.exec();
db.commit();
auto err = db.lastError().text();
qDebug() << err;
}
}
void SmpLibDatabase::CreateTablesSqlite(bool bRecreate)
{
QStringList lstTables = QStringList() << "tblAuthor"<<"tblLibFiles"<<"tblBook";
QMap<QString, QString> tblMap;
tblMap.insert("tblAuthor", "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"`first_name` NVARCHAR(50) NULL,"
"`last_name` NVARCHAR(100) NULL,"
"`nickname` NVARCHAR(100) NULL"
);
tblMap.insert("tblLibFiles", "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"`lib_title` NVARCHAR(255) NULL,"
"`filename` VARCHAR(50) NOT NULL,"
"`filepath` NVARCHAR(1000) NULL"
);
tblMap.insert("tblBook", "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"`book_title` NVARCHAR(255) NULL,"
"`authors` NVARCHAR(255) NOT NULL,"
"`genre` NVARCHAR(255),"
"`sequence_name` NVARCHAR(255),"
"`sequence_number` VARCHAR(10),"
"`libfile_id` INT(11),"
"`name_in_archive` VARCHAR(255),"
"`book_size` INT(11) NOT NULL"
);
QSqlQuery query(db);
foreach(QString sTable, lstTables)
{
db.transaction();
QString qsQuery = "";
if(bRecreate)
qsQuery = "ALTER TABLE `%1` %2";
else
qsQuery = "CREATE TABLE IF NOT EXISTS `%1` (%2)";
QString qs2 = qsQuery.arg(sTable, tblMap.value(sTable));
query.prepare(qs2);
query.exec();
db.commit();
qDebug() << db.lastError().text();
}
}
void SmpLibDatabase::DropTables()
{
QStringList lstTables = QStringList() << "tblBook"<<"tblAuthor"<<"tblLibFiles";
QSqlQuery query(db);
foreach(QString sTable, lstTables)
{
db.transaction();
QString qsQuery = "";
qsQuery = "SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE `%1`; SET FOREIGN_KEY_CHECKS = 1;";
QString qs2 = qsQuery.arg(sTable);
query.prepare(qs2);
query.exec();
db.commit();
}
}
SmpLibDatabase::LIBFILERESULT SmpLibDatabase::IsLibFileExist(const LibFileStruct& LibFile)
{
std::lock_guard<std::mutex> guard(lockReading);
//LibFile is not in the database yet?
QSqlQuery query(db);
QString sSelQuery = "select ID, filehash from tblLibFiles where (filepath IS NULL and filename=:filename) or (filepath=:filepath and filename=:filename);";
query.prepare(sSelQuery);
query.bindValue(":filepath", QVariant(LibFile.filepath));
query.bindValue(":filename", QVariant(LibFile.filename));
query.bindValue(":filehash", QVariant(LibFile.filehash));
query.exec();
query.next();
int rec_count = query.size();
if(rec_count > 0)
{
if(query.record().value(1).toString() == LibFile.filehash)
return EXIST;
else
return WRONG_HASH;
}
else
return NOT_EXIST;
}
void SmpLibDatabase::RemoveBrokenEntries(int libfile_id)
{
std::lock_guard<std::mutex> guard(lockReading);
QSqlQuery query(db);
query.prepare("DELETE from tblBook where libfile_id = :libfile_id;");
query.bindValue(":libfile_id", QVariant(libfile_id));
query.exec();
query.prepare("DELETE from tblLibFiles where id = :libfile_id;");
query.bindValue(":libfile_id", QVariant(libfile_id));
query.exec();
}
int SmpLibDatabase::AddLibFile(const LibFileStruct& LibFile)
{
std::lock_guard<std::mutex> guard(lockReading);
QSqlQuery query(db);
query.prepare("INSERT INTO tblLibFiles (filepath, filename) VALUES (:filepath, :filename);");
query.bindValue(":filepath", QVariant(LibFile.filepath));
query.bindValue(":filename", QVariant(LibFile.filename));
//query.bindValue(":filehash", QVariant(LibFile.filehash));
query.exec();
auto err = query.lastError().text();
QString qs = query.executedQuery();
return query.lastInsertId().toInt();
}
void SmpLibDatabase::UpdateLibFileHash(const LibFileStruct& LibFile)
{
std::lock_guard<std::mutex> guard(lockReading);
QSqlQuery query(db);
query.prepare("UPDATE tblLibFiles SET filehash = :filehash WHERE id = :id;");
query.bindValue(":filehash", QVariant(LibFile.filehash));
query.bindValue(":id", QVariant(LibFile.id));
query.exec();
}
QString SmpLibDatabase::GetLibFileHashByPathName(const LibFileStruct& LibFile)
{
std::lock_guard<std::mutex> guard(lockReading);
//get libfile hash
QSqlQuery query(db);
query.prepare("select filehash from tblLibFiles where (filepath=:filepath and filename=:filename);");
query.bindValue(":filepath", QVariant(LibFile.filepath));
query.bindValue(":filename", QVariant(LibFile.filename));
query.exec();
query.next();
return query.value(0).toString();
}
int SmpLibDatabase::GetLibFileIdByPathName(const LibFileStruct& LibFile)
{
std::lock_guard<std::mutex> guard(lockReading);
//get libfile id
QSqlQuery query(db);
query.prepare("select id from tblLibFiles where (filepath=:filepath and filename=:filename);");
query.bindValue(":filepath", QVariant(LibFile.filepath));
query.bindValue(":filename", QVariant(LibFile.filename));
query.exec();
query.next();
return query.value(0).toInt();
}
std::unique_ptr<SmpLibDatabase::LibFileStruct> SmpLibDatabase::GetLibFile(int libfile_id)
{
std::lock_guard<std::mutex> guard(lockReading);
std::unique_ptr<LibFileStruct> LibFile = nullptr;
QSqlQuery query(db);
QString qsQuery = "select * from tblLibFiles where ID = %1;";
query.exec(qsQuery.arg( libfile_id));
if(query.next())
{
QSqlRecord Rec = query.record();
LibFile = std::make_unique<LibFileStruct>();
LibFile->id = Rec.value("id").toInt();
LibFile->lib_title = Rec.value("lib_title").toString();
LibFile->filename = Rec.value("filename").toString();
LibFile->filepath = Rec.value("filepath").toString();
}
return LibFile;
}
bool SmpLibDatabase::IsAuthorExist(const AuthorStruct& Author)
{
std::lock_guard<std::mutex> guard(lockReading);
//author is not in the database yet?
QSqlQuery query(db);
QString sSelQuery = "select COUNT(id) from tblAuthor where (first_name IS NOT NULL OR last_name IS NOT NULL OR nickname IS NOT NULL) and (first_name IS NULL OR first_name=:first_name) and (last_name IS NULL OR last_name=:last_name) and (nickname IS NULL OR nickname=:nickname);";
query.prepare(sSelQuery);
query.bindValue(":first_name", QVariant(Author.first_name));
query.bindValue(":last_name", QVariant(Author.last_name));
query.bindValue(":nickname", QVariant(Author.nickname));
query.exec();
qDebug() << db.lastError().text();
query.next();
qDebug() << db.lastError().text();
int rec_count = query.value(0).toInt();
return rec_count > 0;
}
void SmpLibDatabase::AddAuthor(const AuthorStruct& Author)
{
std::lock_guard<std::mutex> guard(lockReading);
db.transaction();
QSqlQuery query(db);
query.prepare("INSERT INTO tblAuthor (first_name, last_name, nickname) VALUES (:first_name, :last_name, :nickname);");
query.bindValue(":first_name", QVariant(Author.first_name));
query.bindValue(":last_name", QVariant(Author.last_name));
query.bindValue(":nickname", QVariant(Author.nickname));
query.exec();
QString sq = query.executedQuery();
db.commit();
//QMessageLogger.warning(db.lastError().text());
}
int SmpLibDatabase::GetAuthorIdByName(const AuthorStruct& Author)
{
std::lock_guard<std::mutex> guard(lockReading);
//get author id
QSqlQuery query(db);
query.prepare("select id from tblAuthor where (first_name IS NOT NULL OR last_name IS NOT NULL OR nickname IS NOT NULL) and (first_name IS NULL OR first_name=:first_name) and (last_name IS NULL OR last_name=:last_name) and (nickname IS NULL OR nickname=:nickname);");
query.bindValue(":first_name", QVariant(Author.first_name));
query.bindValue(":last_name", QVariant(Author.last_name));
query.bindValue(":nickname", QVariant(Author.nickname));
query.exec();
QString sq = query.executedQuery();
query.next();
int ret = query.value(0).toInt();
return ret;
}
std::unique_ptr<SmpLibDatabase::AuthorStruct> SmpLibDatabase::GetAuthorById(int idAuthor)
{
std::lock_guard<std::mutex> guard(lockReading);
QSqlQuery query(db);
QString qsQuery = "select * from tblAuthor where id = %1 " ;
//qsQuery = "select * from tblAuthor";
query.exec(qsQuery.arg(idAuthor));
QString qs = query.lastQuery();
std::unique_ptr<AuthorStruct> Author = nullptr;
if (query.next())
{
QSqlRecord Rec = query.record();
Author = std::make_unique<AuthorStruct>();
Author->id = Rec.value("id").toInt();
Author->first_name = Rec.value("first_name").toString();
Author->last_name = Rec.value("last_name").toString();
Author->nickname = Rec.value("nickname").toString();
}
return Author;
}
std::unique_ptr<QList<SmpLibDatabase::AuthorStruct>> SmpLibDatabase::GetAuthorList(const QString& qsFilter)
{
std::lock_guard<std::mutex> guard(lockReading);
auto ListA = std::make_unique<QList<AuthorStruct>>();
QSqlQuery query(db);
QString qsQuery = "select * from tblAuthor where (first_name like %1%3%2%1 OR last_name like %1%3%2%1 OR nickname like %1%3%2%1) " ;
//qsQuery = "select * from tblAuthor";
query.exec(qsQuery.arg("'", "%", qsFilter));
QString qs = query.lastQuery();
while (query.next())
{
QSqlRecord Rec = query.record();
AuthorStruct Author;
Author.id = Rec.value("id").toInt();
Author.first_name = Rec.value("first_name").toString();
Author.last_name = Rec.value("last_name").toString();
Author.nickname = Rec.value("nickname").toString();
ListA->push_back(Author);
}
return ListA;
}
bool SmpLibDatabase::IsBookExist(const QString& AuthorIds, const QString& qsBookTitle)
{
std::lock_guard<std::mutex> guard(lockReading);
QSqlQuery query(db);
query.prepare("select COUNT(ID) from tblBook where authors=:authors and book_title=:book_title;");
query.bindValue(":authors", QVariant(AuthorIds));
query.bindValue(":book_title", QVariant(qsBookTitle));
query.exec();
query.next();
int rec_count = query.value(0).toInt();
return rec_count > 0;
}
void SmpLibDatabase::AddBook(const BookStruct& Book)
{
std::lock_guard<std::mutex> guard(lockReading);
QSqlQuery query(db);
query.prepare("INSERT INTO tblBook (authors, book_title, genre, sequence_name, sequence_number, libfile_id, name_in_archive, book_size) "
"VALUES (:authors, :book_title, :genre, :sequence_name, :sequence_number, :libfile_id, :name_in_archive, :book_size);");
query.bindValue(":authors", Book.authors);
query.bindValue(":book_title", Book.book_title);
query.bindValue(":genre", Book.genre);
query.bindValue(":sequence_name", Book.sequence_name);
query.bindValue(":sequence_number", Book.sequence_number);
query.bindValue(":libfile_id", Book.libfile_id);
query.bindValue(":name_in_archive", Book.name_in_archive);
query.bindValue(":book_size", Book.book_size);
query.exec();
auto err = query.lastError().text();
QString qs = query.executedQuery();
}
std::unique_ptr<QList<SmpLibDatabase::BookStruct>> SmpLibDatabase::GetBookList(const QString& qsFilter, const QString& qsAuthor)
{
std::lock_guard<std::mutex> guard(lockReading);
auto ListA = std::make_unique<QList<BookStruct>>();
QSqlQuery query(db);
QString qsQuery = "select * from tblBook where book_title like %1%3%2%1 AND (authors LIKE %1%2,%4,%2%1);";
query.exec(qsQuery.arg("'", "%", qsFilter, qsAuthor));
while (query.next())
{
QSqlRecord Rec = query.record();
BookStruct Book;
Book.id = Rec.value("id").toInt();
Book.authors = Rec.value("authors").toString();
Book.book_title = Rec.value("book_title").toString();
Book.genre = Rec.value("genre").toString();
Book.sequence_name = Rec.value("sequence_name").toString();
Book.sequence_number = Rec.value("sequence_number").toString();
Book.libfile_id = Rec.value("libfile_id").toInt();
Book.name_in_archive = Rec.value("name_in_archive").toString();
Book.book_size = Rec.value("book_size").toInt();
ListA->push_back(Book);
}
return ListA;
}
std::unique_ptr<SmpLibDatabase::BookStruct> SmpLibDatabase::GetBook(int book_id)
{
std::lock_guard<std::mutex> guard(lockReading);
std::unique_ptr<BookStruct> Book = nullptr;
QSqlQuery query(db);
QString qsQuery = "select * from tblBook where ID = %1;";
query.exec(qsQuery.arg( book_id));
if(query.next())
{
QSqlRecord Rec = query.record();
Book = std::make_unique<BookStruct>();
Book->id = Rec.value("id").toInt();
Book->authors = Rec.value("authors").toString();
Book->book_title = Rec.value("book_title").toString();
Book->genre = Rec.value("genre").toString();
Book->sequence_name = Rec.value("sequence_name").toString();
Book->sequence_number = Rec.value("sequence_number").toString();
Book->libfile_id = Rec.value("libfile_id").toInt();
Book->name_in_archive = Rec.value("name_in_archive").toString();
Book->book_size = Rec.value("book_size").toInt();
}
return Book;
}