-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabasemanager.cpp
104 lines (87 loc) · 2.74 KB
/
databasemanager.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
/*
* This file is part of the PrismaticOutpost project.
* Copyright 2024. Isaac Raway. All rights reserved.
* This file is licensed under the terms of the AGPL-3.0,
* which you can find a copy of in the LICENSE.md file
* https://www.gnu.org/licenses/agpl-3.0.md.
* IMPORTANT: This license ALSO applies to all scripts
* and databases packaged with this distribution of
* PrismaticOutpost. If you wish to distribute proprietary
* scripts or databases, then they MUST NOT be packaged
* with this distribution or any binary distribution built
* from this distribution or any derivative of this
* distribution.
*/
// databasemanager.cpp
#include "databasemanager.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
DatabaseManager::DatabaseManager(QObject *parent) : QObject(parent)
{
}
DatabaseManager::~DatabaseManager()
{
closeDatabase();
}
bool DatabaseManager::openDatabase(const QString &path)
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);
if (!db.open()) {
qDebug() << "Error: connection with database failed";
return false;
}
return initTables();
}
void DatabaseManager::closeDatabase()
{
db.close();
}
bool DatabaseManager::initTables()
{
QSqlQuery query;
return query.exec("CREATE TABLE IF NOT EXISTS nodes "
"(key TEXT PRIMARY KEY, parent TEXT, value BLOB)");
}
QVariant DatabaseManager::getValue(const QString &key)
{
QSqlQuery query;
query.prepare("SELECT value FROM nodes WHERE key = :key");
query.bindValue(":key", key);
if (query.exec() && query.next()) {
return query.value(0);
}
return QVariant();
}
bool DatabaseManager::setValue(const QString &key, const QVariant &value)
{
QSqlQuery query;
query.prepare("INSERT OR REPLACE INTO nodes (key, parent, value) "
"VALUES (:key, :parent, :value)");
query.bindValue(":key", key);
query.bindValue(":parent", key.section('.', 0, -2));
query.bindValue(":value", value);
return query.exec();
}
bool DatabaseManager::removeValue(const QString &key)
{
QSqlQuery query;
query.prepare("DELETE FROM nodes WHERE key = :key OR key LIKE :key_prefix");
query.bindValue(":key", key);
query.bindValue(":key_prefix", key + ".%");
return query.exec();
}
QStringList DatabaseManager::getChildKeys(const QString &parentKey)
{
QStringList children;
QSqlQuery query;
query.prepare("SELECT key FROM nodes WHERE parent = :parent");
query.bindValue(":parent", parentKey);
if (query.exec()) {
while (query.next()) {
children << query.value(0).toString();
}
}
return children;
}