-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_worker.cpp
144 lines (85 loc) · 3.22 KB
/
db_worker.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
#include "db_worker.h"
#include <algorithm>
#include <string>
#include <QDebug>
#include <QPair>
using namespace std;
DBWorker::DBWorker(QTableView* acid_limit_table, QObject *parent)
: QObject(parent) {
data_base = QSqlDatabase::addDatabase("QSQLITE");
data_base.setDatabaseName(db_name);
if(!data_base.open()){
qDebug() << "DBWorker Constructor error: DB did't opend";
}
query = new QSqlQuery(data_base);
query->exec("CREATE TABLE IF NOT EXISTS " + table_name + " ("
"'formula' TEXT NOT NULL UNIQUE, "
"'equivalent' INTEGER NOT NULL, "
"PRIMARY KEY('formula') "
");");
model = new QSqlTableModel(this, data_base);
model->setTable(table_name);
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("Кислотный\nостаток"));
model->setHeaderData(1, Qt::Horizontal, tr("Эквивалент"));
acid_limit_table->setModel(model);
};
bool DBWorker::DeleteEquivalent(const QString& formula){
query->clear();
if(!query->exec("DELETE FROM " + table_name + ""
" WHERE (formula = '" + formula + "')")) {
return 0;
}
model->select();
return 1;
}
bool DBWorker::AddNewEquivalent (const QString& formula, const size_t equivalent){
query->clear();
if(!query->exec("INSERT INTO " + table_name + ""
" VALUES ('" + formula + "' , "
"" + QString::number(equivalent) + ")")) {
return 0;
}
model->select();
return 1;
}
size_t DBWorker::FindEquivalent(const QString& formula) {
size_t molecules = 0;
size_t max_tail_after_taple = 2;
QPair<size_t, QString> equivalent_taple = GetEquivalentFromDB(formula);
if((formula.back().isDigit() && formula[formula.size() - max_tail_after_taple] == ')') ||
equivalent_taple.second.front() != formula.back()) {
molecules = formula.back().digitValue();
}
return molecules > 1 ? equivalent_taple.first * molecules : equivalent_taple.first;
}
QPair<size_t, QString> DBWorker::GetEquivalentFromDB(const QString& formula) {
GetAllFromDB();
bool contains = false;
while (query->next()) {
contains = formula.contains(query->value(0).toString());
if(contains) {
return {query->value(1).toInt(), query->value(0).toString()};
}
}
return {0, ""};
}
bool DBWorker::GetAllFromDB() {
query->clear();
if(!query->exec("SELECT * FROM " + table_name + " ORDER BY formula DESC")) {
return 0;
}
return 1;
}
size_t DBWorker::GetEquivalent(const QString& formula){
query->clear();
if(!query->exec("SELECT equivalent FROM " + table_name + ""
" WHERE formula = '" + formula + "'")) {
return 0;
}
query->next();
return query->value(0).toInt();
}
DBWorker::~DBWorker(){
delete query;
}