forked from YACReader/yacreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comic_query_result_procesor.cpp
112 lines (89 loc) · 3.8 KB
/
comic_query_result_procesor.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
#include "comic_query_result_procesor.h"
#include "comic_item.h"
#include "comic_model.h"
#include "data_base_management.h"
#include "qnaturalsorting.h"
#include "db_helper.h"
#include "query_parser.h"
#include "QsLog.h"
QString getLastExecutedQuery(const QSqlQuery &query)
{
QString str = query.lastQuery();
QMapIterator<QString, QVariant> it(query.boundValues());
while (it.hasNext()) {
it.next();
str.replace(it.key(), it.value().toString());
}
return str;
}
YACReader::ComicQueryResultProcesor::ComicQueryResultProcesor()
: querySearchQueue(1)
{
}
void YACReader::ComicQueryResultProcesor::createModelData(const YACReader::SearchModifiers modifier, const QString &filter, const QString &databasePath)
{
querySearchQueue.cancellPending();
querySearchQueue.enqueue([=] {
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(databasePath);
QSqlQuery selectQuery(db);
std::string queryString("SELECT ci.number,ci.title,c.fileName,ci.numPages,c.id,c.parentId,c.path,ci.hash,ci.read,ci.isBis,ci.currentPage,ci.rating,ci.hasBeenOpened "
"FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) LEFT JOIN folder f ON (f.id == c.parentId) WHERE ");
try {
QueryParser parser;
auto result = parser.parse(filter.toStdString());
result.buildSqlString(queryString);
switch (modifier) {
case YACReader::NoModifiers:
queryString += " LIMIT :limit";
break;
case YACReader::OnlyRead:
queryString += " AND ci.read = 1 LIMIT :limit";
break;
case YACReader::OnlyUnread:
queryString += " AND ci.read = 0 LIMIT :limit";
break;
default:
queryString += " LIMIT :limit";
QLOG_ERROR() << "not implemented";
break;
}
selectQuery.prepare(queryString.c_str());
selectQuery.bindValue(":limit", 500); //TODO, load this value from settings
result.bindValues(selectQuery);
selectQuery.exec();
auto data = modelData(selectQuery);
emit newData(data, databasePath);
} catch (const std::exception &e) {
//Do nothing, uncomplete search string will end here and it is part of how the QueryParser works
//I don't like the idea of using exceptions for this though
}
connectionName = db.connectionName();
}
QSqlDatabase::removeDatabase(connectionName);
});
}
QList<ComicItem *> *YACReader::ComicQueryResultProcesor::modelData(QSqlQuery &sqlquery)
{
auto list = new QList<ComicItem *>();
int numColumns = sqlquery.record().count();
while (sqlquery.next()) {
QList<QVariant> data;
for (int i = 0; i < numColumns; i++)
data << sqlquery.value(i);
list->append(new ComicItem(data));
}
std::sort(list->begin(), list->end(), [](const ComicItem *c1, const ComicItem *c2) {
if (c1->data(ComicModel::Number).isNull() && c2->data(ComicModel::Number).isNull()) {
return naturalSortLessThanCI(c1->data(ComicModel::FileName).toString(), c2->data(ComicModel::FileName).toString());
} else {
if (c1->data(ComicModel::Number).isNull() == false && c2->data(ComicModel::Number).isNull() == false) {
return c1->data(ComicModel::Number).toInt() < c2->data(ComicModel::Number).toInt();
} else {
return c2->data(ComicModel::Number).isNull();
}
}
});
return list;
}