summaryrefslogtreecommitdiff
path: root/src/backend/db_sqlite.cpp
diff options
context:
space:
mode:
authorLouie S <louie@example.com>2024-02-29 18:01:05 -0500
committerLouie S <louie@example.com>2024-02-29 18:01:05 -0500
commit4b99428a0e73c9e573624b2784464ce8ae632e33 (patch)
tree5214b80e74afc4c6844c50852cdad6de4b908912 /src/backend/db_sqlite.cpp
parent9852a0ec1b73e1bce0c3f77743b426b29550e01c (diff)
Rewrite db_sqlite as class
Diffstat (limited to 'src/backend/db_sqlite.cpp')
-rw-r--r--src/backend/db_sqlite.cpp177
1 files changed, 93 insertions, 84 deletions
diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp
index 4479b09..7176a95 100644
--- a/src/backend/db_sqlite.cpp
+++ b/src/backend/db_sqlite.cpp
@@ -13,31 +13,7 @@
#include "db_sqlite.h"
-QString getDBPath();
-QSqlDatabase openDB();
-
-QString getDBPath() {
- QSettings settings;
- settings.beginGroup("paths");
- return settings.value("db_path").toString();
-}
-
-
-QSqlDatabase 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;
-}
-
-void BackendDB::init() {
+BackendDB::BackendDB() {
QString db_path(getDBPath());
int i;
@@ -54,101 +30,134 @@ void BackendDB::init() {
}
// Create database
- QSqlDatabase database = 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");
+ {
+ QSqlDatabase database(this->openDB());
+ QSqlQuery query;
- for(i = 0; i < BackendDB::create_table_queries.length(); ++i)
- query.exec(BackendDB::create_table_queries[i]);
+ // 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");
- qDebug() << database.lastError();
+ for(i = 0; i < BackendDB::create_table_queries.length(); ++i)
+ query.exec(BackendDB::create_table_queries[i]);
+ }
- database.close();
+ QSqlDatabase::removeDatabase("qt_sql_default_connection");
}
// load groups
QList<Group *> BackendDB::loadGroups() {
- QSqlDatabase database = openDB();
- QSqlQuery query;
QList<Group *> output;
- 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 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()));
+ }
}
- database.close();
+ QSqlDatabase::removeDatabase("qt_sql_default_connection");
return output;
}
// load entries
QList<Entry *> BackendDB::loadEntries() {
- QSqlDatabase database = openDB();
- QSqlQuery query;
QList<Entry *> output;
- 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 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()));
+ }
}
- database.close();
+ QSqlDatabase::removeDatabase("qt_sql_default_connection");
return output;
}
// load entries
QList<Rule *> BackendDB::loadRules() {
- QSqlDatabase database = openDB();
- QSqlQuery query;
QList<Rule *> output;
- 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 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()));
+ }
}
- database.close();
+ QSqlDatabase::removeDatabase("qt_sql_default_connection");
return output;
}
// insert group to the database (returns 0 if failed)
int BackendDB::insertGroup(const Group &new_group) {
- QSqlDatabase database = openDB();
- QSqlQuery query;
int output;
- 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();
+ {
+ 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();
- database.close();
+ 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;
+}