-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsconvodatabase.cpp
74 lines (58 loc) · 1.55 KB
/
dsconvodatabase.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
#include "dsconvodatabase.h"
#include <QVariant>
#include <QSqlError>
#include <QSqlQuery>
#include <iterator>
namespace DSConvo {
namespace Database {
QString databaseName;
bool databaseEnabled = false;
bool initializeDatabase(const QString &name = DEFAULT_DATABASE)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", DATABASE);
databaseName = name;
db.setDatabaseName(databaseName);
db.setConnectOptions("QSQLITE_OPEN_READONLY");
if (!db.open()) {
std::string errorString = db.lastError().text().toStdString();
qDebug("[DEBUG] [DSConvo::Database::initializeDatabase] failed to open (%s)",
errorString.c_str());
databaseEnabled = false;
} else {
qDebug("[DEBUG] [DSConvo::Database::initializeDatabase] db open");
databaseEnabled = true;
}
return databaseEnabled;
}
bool database(QSqlDatabase &db)
{
if (!databaseEnabled) {
return false;
}
db = QSqlDatabase::database(DATABASE);
if (!db.isOpen()) {
return false;
}
return true;
}
bool queryUsers(UsersFunction f)
{
QSqlDatabase db;
if (!database(db)) {
return false;
}
QSqlQuery query(db);
if (!query.exec("SELECT username, displayName FROM Users")) {
return false;
}
while (query.next()) {
f({query.value(0).toString(), query.value(1).toString()});
}
return true;
}
bool queryUsers(QList<User> &list)
{
return queryUsers([&list](const User &u) { list.append(u); });
}
} // namepsace DSConvo::Database
} // namespace DSConvo