-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlmanager.cc
169 lines (142 loc) · 4.76 KB
/
sqlmanager.cc
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
#include "sqlmanager.h"
#include "utils.h"
#include <QDebug>
#include <QRegularExpression>
#include <QSqlQuery>
#include <QSqlResult>
#include <QSqlError>
SQLManager::SQLManager() {
QString m_location = datadir().absoluteFilePath("words.sqlite3");
// Open the database connection
m_sqldb = QSqlDatabase::addDatabase("QSQLITE");
m_sqldb.setDatabaseName(m_location);
bool ok = m_sqldb.open();
if (!ok)
qFatal(
"Fatal error establishing a connection with Vibrato's sqlite3 database. :(");
}
void SQLManager::init() {
// Check if 'notes' table exists. If not, create it and import tutorial note.
// Create any tables that are non-existent.
runScript(":sql/create.sql");
init_id();
// import_txt_with_level(datadir().absoluteFilePath("plain_known.txt"), WORD_IS_KNOWN);
// import_txt_with_level(datadir().absoluteFilePath("plain_ignore.txt"), WORD_IS_IGNORED);
}
void SQLManager::init_id() {}
bool SQLManager::runScript(QString fileName) {
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Unable to load SQL file" << fileName;
return false;
}
QSqlQuery query;
return runScript(&file, &query);
}
bool SQLManager::runScript(QFile *file, QSqlQuery *query) {
QString contents = file->readAll();
// Remove sql comments and
contents = contents.replace(QRegularExpression("--[^\\n]+\\n"), "\n");
// Remove unnecesary blank lines
contents = contents.replace(QRegularExpression("\\n\\n+"), "\n");
QStringList queryList = contents.split(";");
for (QString line : queryList) {
line = line.trimmed();
if (line.isEmpty()) continue;
bool success = query->exec(line);
if (!success) {
qDebug() << "current line is:" << line;
logSqlError(query->lastError());
}
}
return query->isActive();
}
bool SQLManager::logSqlError(QSqlError error, bool fatal) {
if (!error.isValid()) return true;
QString status = "SQL Error";
if (fatal) status = "FATAL SQL Error";
QString msg =
QString("[%1] %2 %3").arg(status, error.driverText(), error.databaseText());
// Print the message. Must convert it from QString to char*.
if (fatal)
qFatal("%s", msg.toLatin1().constData());
else
qWarning("%s", msg.toLatin1().constData());
return false;
}
void SQLManager::import_txt_with_level(const QString &filename, wordlevel_t lv) {
QFile f(filename);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning("file read error.");
return;
}
QTextStream in(&f);
while (!in.atEnd()) {
QString line = in.readLine();
this->addword(line, lv);
}
}
void SQLManager::export_level_to_text(wordlevel_t lv, const QString &filename) {
}
bool SQLManager::addword(const QString &word, wordlevel_t lv) {
// qDebug() <<__PRETTY_FUNCTION__<<":"<<__LINE__;
QSqlQuery q;
QString cmdstr =
QString("INSERT INTO word_info (word, proficiency) VALUES (%1, %2)")
.arg(":word")
.arg(":proficiency");
q.prepare(cmdstr);
q.bindValue(":word", word);
q.bindValue(":proficiency", static_cast<int>(lv));
auto ok = q.exec();
checkReturn(ok, q, "add word");
if(not ok)
assert(0);
return ok;
}
// UPDATE COMPANY SET ADDRESS = 'Texas' WHERE ID = 6
bool SQLManager::updateword(const QString &word, wordlevel_t lv) {
// qDebug() <<__PRETTY_FUNCTION__<<":"<<__LINE__;
QSqlQuery q;
QString cmdstr =
QString("update word_info set proficiency = %1 where word=\"%2\"").arg(static_cast<int>(lv)).arg(word);
q.prepare(cmdstr);
auto ok = q.exec();
checkReturn(ok, q, "add word");
if(not ok)
assert(0);
return ok;
}
wordlevel_t SQLManager::findword(const QString &word) {
QSqlQuery q;
QString cmdstr =
QString("select proficiency from word_info where word = \"%1\"").arg(1);
q.prepare(cmdstr);
auto ok = q.exec();
checkReturn(ok, q, "find word");
if(!ok){
assert(0);
}
if(q.next()){
auto val = q.value("proficiency").toInt();
return static_cast<wordlevel_t>(val);
}
return LEVEL_UNKOWN;
}
void SQLManager::checkReturn(bool ok, QSqlQuery &q, const QString &msg, int line) {
if (not ok) {
if (line == -1)
qDebug() << QString("[sql error]: %1").arg(msg);
else
qDebug() << QString("[sql error at L%1]: %2").arg(line).arg(msg);
logSqlError(q.lastError());
}
}
void SQLManager::getwords(QMap<QString, wordlevel_t> &res) {
QSqlQuery q("SELECT word, proficiency from word_info");
while(q.next()){
auto word = q.value(0).toString();
auto lv = q.value(1).toInt();
res.insert(word, static_cast<wordlevel_t>(lv));
}
}