From bdb594ada83058ae35edb5edf01703afa203e593 Mon Sep 17 00:00:00 2001 From: Louie S Date: Mon, 26 Feb 2024 17:04:26 -0500 Subject: Loading groups from db --- src/backend/db_sqlite.cpp | 103 +++++++++++++++++++++++++++++++++++++++------- src/backend/db_sqlite.h | 9 +++- 2 files changed, 96 insertions(+), 16 deletions(-) (limited to 'src/backend') diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp index 05dc702..9e4a6e7 100644 --- a/src/backend/db_sqlite.cpp +++ b/src/backend/db_sqlite.cpp @@ -4,21 +4,40 @@ #include #include #include +#include #include -#include +#include #include #include "db_sqlite.h" -QString BackendDB::getDBPath() { +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() { - QString db_path(BackendDB::getDBPath()); + QString db_path(getDBPath()); int i; // Check if database already exists @@ -34,17 +53,7 @@ void BackendDB::init() { } // Create database - QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE"); - database.setDatabaseName(db_path); - - // TODO see if explicitly creating the parent directory is necessary - database.open(); - if(database.isOpenError()) { - // FIXME end-user friendly error message - qDebug() << database.lastError(); - std::exit(1); - } - + QSqlDatabase database = openDB(); QSqlQuery query; // Erase database contents so that we don't have duplicates @@ -59,3 +68,69 @@ void BackendDB::init() { database.close(); } + +// load groups +QList BackendDB::loadGroups() { + QSqlDatabase database = openDB(); + QSqlQuery query; + QList 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())); + } + + database.close(); + return output; +} + +// load entries +QList loadEntries() { + QSqlDatabase database = openDB(); + QSqlQuery query; + QList 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())); + } + + database.close(); + return output; +} + +// load entries +QList loadRules() { + QSqlDatabase database = openDB(); + QSqlQuery query; + QList 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())); + } + + database.close(); + return output; +} diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index 8563223..6e94fdd 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -4,6 +4,10 @@ #include #include +#include "../entry.h" +#include "../group.h" +#include "../rule.h" + namespace BackendDB { const QStringList create_table_queries = { "CREATE TABLE groups (" @@ -35,9 +39,10 @@ namespace BackendDB { ")" }; - QString getDBPath(); void init(); - void load(); + QList loadGroups(); + QList loadEntries(); + QList loadRules(); void insertGroup(int new_group); // param datatype TBD void insertEntry(int new_entry); // param datatype TBD void insertRule(int new_rule); // param datatype TBD -- cgit