From d732bd0f74225828455c5863444b0211faf586d0 Mon Sep 17 00:00:00 2001 From: Louie S Date: Fri, 15 Mar 2024 15:41:31 -0400 Subject: Add backend for rules dialog --- src/backend/db_sqlite.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/backend/db_sqlite.h | 4 ++-- 2 files changed, 48 insertions(+), 3 deletions(-) (limited to 'src/backend') diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp index 32108e1..f48089b 100644 --- a/src/backend/db_sqlite.cpp +++ b/src/backend/db_sqlite.cpp @@ -197,7 +197,7 @@ int BackendDB::insertGroup(const Group &new_group) { return output; } -// insert group to the database (returns 0 if failed) +// insert entry to the database (returns 0 if failed) int BackendDB::insertEntry(const Entry &new_entry) { int output; @@ -222,6 +222,29 @@ int BackendDB::insertEntry(const Entry &new_entry) { return output; } +// insert rule to the database (returns 0 if failed) +int BackendDB::insertRule(const Rule &new_rule) { + int output; + + { + QSqlDatabase database(this->openDB()); + QSqlQuery query; + + query.prepare("INSERT INTO rules (entry_id, before_after, date, color, highlight) VALUES (:e_id, :when, :date, :color, :highlight)"); + query.bindValue(":e_id", new_rule.entry_id); + query.bindValue(":when", new_rule.when); + query.bindValue(":date", new_rule.date.toString("yyyy-MM-dd")); + query.bindValue(":color", new_rule.color); + query.bindValue(":highlight", new_rule.highlight); + query.exec(); + + output = query.lastInsertId().toInt(); + } + + QSqlDatabase::removeDatabase("qt_sql_default_connection"); + return output; +} + void BackendDB::updateGroup(const Group &group) { { QSqlDatabase database(this->openDB()); @@ -274,6 +297,28 @@ void BackendDB::updateEntry(const Entry &entry) { QSqlDatabase::removeDatabase("qt_sql_default_connection"); } +void BackendDB::updateRule(const Rule &rule) { + { + QSqlDatabase database(this->openDB()); + QSqlQuery query; + + query.prepare("UPDATE rules SET " + "before_after = :when, " + "date = :date, " + "color = :color, " + "highlight = :highlight " + "WHERE id = :id"); + query.bindValue(":when", rule.when); + query.bindValue(":date", rule.date.toString("yyyy-MM-dd")); + query.bindValue(":color", rule.color); + query.bindValue(":highlight", rule.highlight); + query.bindValue(":id", rule.id); + query.exec(); + } + + QSqlDatabase::removeDatabase("qt_sql_default_connection"); +} + // hide group and entries belonging to group // return value: number of affected rows int BackendDB::removeGroup(const Group &group) { diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index ce2d900..93ae050 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -19,10 +19,10 @@ class BackendDB : QSqlDatabase { 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 + int insertRule(const Rule &new_rule); void updateGroup(const Group &group); void updateEntry(const Entry &entry); - void updateRule(int rule); // param datatype TBD + void updateRule(const Rule &rule); int removeGroup(const Group &group); int removeEntry(const Entry &entry); int removeRule(const Rule &rule); -- cgit