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
|
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QSettings>
#include <QSqlDatabase>
#include <QSqlField>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlError>
#include "db_sqlite.h"
BackendDB::BackendDB() {
QString db_path(getDBPath());
int i;
// Check if database already exists
if(QFile::exists(db_path))
return;
// Check if directory exists
if(!QFileInfo(db_path).dir().exists()) {
if(!QFileInfo(db_path).dir().mkpath(QFileInfo(db_path).dir().absolutePath())) {
qDebug() << "Error creating db file";
std::exit(1);
}
}
// Create database
{
QSqlDatabase database(this->openDB());
QSqlQuery query;
// Erase database contents so that we don't have duplicates
query.exec("DROP TABLE groups");
query.exec("DROP TABLE entries");
query.exec("DROP TABLE rules");
for(i = 0; i < BackendDB::create_table_queries.length(); ++i)
query.exec(BackendDB::create_table_queries[i]);
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
}
// load groups
QList<Group *> BackendDB::loadGroups() {
QList<Group *> output;
{
QSqlDatabase database(this->openDB());
QSqlQuery query;
query.exec("SELECT * FROM groups");
while(query.next()) {
output.append(new Group(
query.record().field("id").value().toInt(),
query.record().field("name").value().toString(),
query.record().field("column").value().toString(),
query.record().field("link").value().toString(),
query.record().field("hidden").value().toBool()));
}
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
return output;
}
// load entries
QList<Entry *> BackendDB::loadEntries() {
QList<Entry *> output;
{
QSqlDatabase database(this->openDB());
QSqlQuery query;
query.exec("SELECT * FROM groups");
while(query.next()) {
output.append(new Entry(
query.record().field("id").value().toInt(),
query.record().field("parent_id").value().toInt(),
query.record().field("description").value().toString(),
query.record().field("due_date").value().toDateTime(),
query.record().field("alt_due_date").value().toString(),
query.record().field("link").value().toUrl(),
query.record().field("color").value().toString(),
query.record().field("highlight").value().toString(),
query.record().field("done").value().toBool(),
query.record().field("hidden").value().toBool()));
}
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
return output;
}
// load entries
QList<Rule *> BackendDB::loadRules() {
QList<Rule *> output;
{
QSqlDatabase database(this->openDB());
QSqlQuery query;
query.exec("SELECT * FROM groups");
while(query.next()) {
output.append(new Rule(
query.record().field("id").value().toInt(),
query.record().field("entry_id").value().toInt(),
(Rule::When) query.record().field("before_after").value().toInt(),
query.record().field("date").value().toDateTime(),
query.record().field("color").value().toString(),
query.record().field("highlight").value().toString()));
}
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
return output;
}
// insert group to the database (returns 0 if failed)
int BackendDB::insertGroup(const Group &new_group) {
int output;
{
QSqlDatabase database(this->openDB());
QSqlQuery query;
query.prepare("INSERT INTO groups (name, column, link) VALUES (?, ?, ?)");
query.bindValue(0, new_group.name);
query.bindValue(1, new_group.column);
query.bindValue(2, new_group.link);
query.exec();
output = query.lastInsertId().toInt();
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
return output;
}
QString BackendDB::getDBPath() {
QSettings settings;
settings.beginGroup("paths");
return settings.value("db_path").toString();
}
QSqlDatabase BackendDB::openDB() {
QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName(getDBPath());
database.open();
if(database.isOpenError()) {
// FIXME end-user friendly error message
qDebug() << database.lastError();
std::exit(1);
}
return database;
}
|