From 3e1d2e46ceea202120b4ff814fbd872bc6fc60f3 Mon Sep 17 00:00:00 2001 From: Louie S Date: Thu, 14 Mar 2024 18:46:37 -0400 Subject: RulesDialog menu working (functionality WIP) --- src/backend/db_sqlite.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++- src/backend/db_sqlite.h | 3 ++- 2 files changed, 49 insertions(+), 2 deletions(-) (limited to 'src/backend') diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp index 4319b67..32108e1 100644 --- a/src/backend/db_sqlite.cpp +++ b/src/backend/db_sqlite.cpp @@ -134,7 +134,33 @@ QList BackendDB::loadRules() { QSqlDatabase database(this->openDB()); QSqlQuery query; - query.exec("SELECT * FROM groups"); + query.exec("SELECT * FROM rules"); + 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; +} + +// load entries +QList BackendDB::loadRules(int entry_id) { + QList output; + + { + QSqlDatabase database(this->openDB()); + QSqlQuery query; + + query.prepare("SELECT * FROM rules WHERE entry_id = ?"); + query.bindValue(0, entry_id); + query.exec(); while(query.next()) { output.append(new Rule( query.record().field("id").value().toInt(), @@ -295,6 +321,26 @@ int BackendDB::removeEntry(const Entry &entry) { return output; } +// return value: number of affected rows +int BackendDB::removeRule(const Rule &rule) { + int output; + + { + QSqlDatabase database(this->openDB()); + QSqlQuery query; + + query.prepare("DELETE FROM rules WHERE id = ?"); + query.bindValue(0, rule.id); + query.exec(); + + // FIXME not sure if this also needs to be called after the first query... + output = query.numRowsAffected(); + } + + QSqlDatabase::removeDatabase("qt_sql_default_connection"); + return output; +} + // permanently delete removed/hidden groups and entries void BackendDB::cleanHidden() { { diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index 1db61e8..ce2d900 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -16,6 +16,7 @@ class BackendDB : QSqlDatabase { QList loadEntries(); QList loadEntries(int parent_id); QList loadRules(); + QList loadRules(int entry_id); int insertGroup(const Group &new_group); int insertEntry(const Entry &new_entry); int insertRule(int new_rule); // param datatype TBD @@ -24,7 +25,7 @@ class BackendDB : QSqlDatabase { void updateRule(int rule); // param datatype TBD int removeGroup(const Group &group); int removeEntry(const Entry &entry); - void removeRule(int rule); // param datatype TBD + int removeRule(const Rule &rule); void cleanHidden(); private: -- cgit