From 43b2132f4d57fac69be698d26cc8f920967065e6 Mon Sep 17 00:00:00 2001 From: Louie S Date: Sat, 2 Mar 2024 16:16:59 -0500 Subject: Render entries --- src/backend/db_sqlite.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++- src/backend/db_sqlite.h | 3 ++- 2 files changed, 57 insertions(+), 2 deletions(-) (limited to 'src/backend') diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp index 7176a95..f65cd8f 100644 --- a/src/backend/db_sqlite.cpp +++ b/src/backend/db_sqlite.cpp @@ -77,7 +77,36 @@ QList BackendDB::loadEntries() { QSqlDatabase database(this->openDB()); QSqlQuery query; - query.exec("SELECT * FROM groups"); + query.exec("SELECT * FROM entries"); + 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; +} + +QList BackendDB::loadEntries(int parent_id) { + QList output; + + { + QSqlDatabase database(this->openDB()); + QSqlQuery query; + + query.prepare("SELECT * FROM entries WHERE parent_id = ?"); + query.bindValue(0, parent_id); + query.exec(); while(query.next()) { output.append(new Entry( query.record().field("id").value().toInt(), @@ -142,6 +171,31 @@ int BackendDB::insertGroup(const Group &new_group) { return output; } +// insert group to the database (returns 0 if failed) +int BackendDB::insertEntry(const Entry &new_entry) { + int output; + + { + QSqlDatabase database(this->openDB()); + QSqlQuery query; + + query.prepare("INSERT INTO entries (parent_id, description, due_date, alt_due_date, link, color, highlight) VALUES (:p_id, :desc, :due, :alt_due, :link, :color, :highlight)"); + query.bindValue(":p_id", new_entry.parent_id); + query.bindValue(":desc", new_entry.desc); + query.bindValue(":due", new_entry.due.toString("yyyy-MM-dd")); + query.bindValue(":alt_due", new_entry.due_alt); + query.bindValue(":link", new_entry.link); + query.bindValue(":color", new_entry.color); + query.bindValue(":highlight", new_entry.highlight); + query.exec(); + + output = query.lastInsertId().toInt(); + } + + QSqlDatabase::removeDatabase("qt_sql_default_connection"); + return output; +} + QString BackendDB::getDBPath() { QSettings settings; settings.beginGroup("paths"); diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index bec534b..afaac8d 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -14,9 +14,10 @@ class BackendDB : QSqlDatabase { BackendDB(); QList loadGroups(); QList loadEntries(); + QList loadEntries(int parent_id); QList loadRules(); int insertGroup(const Group &new_group); - int insertEntry(int new_entry); // param datatype TBD + int insertEntry(const Entry &new_entry); int insertRule(int new_rule); // param datatype TBD void updateGroup(int group); // param datatype TBD void updateEntry(int entry); // param datatype TBD -- cgit