diff options
author | Louie S <louie@example.com> | 2024-02-29 18:01:05 -0500 |
---|---|---|
committer | Louie S <louie@example.com> | 2024-02-29 18:01:05 -0500 |
commit | 4b99428a0e73c9e573624b2784464ce8ae632e33 (patch) | |
tree | 5214b80e74afc4c6844c50852cdad6de4b908912 | |
parent | 9852a0ec1b73e1bce0c3f77743b426b29550e01c (diff) |
Rewrite db_sqlite as class
-rw-r--r-- | src/add_group_form.cpp | 3 | ||||
-rw-r--r-- | src/assignmentList.cpp | 6 | ||||
-rw-r--r-- | src/backend/db_sqlite.cpp | 177 | ||||
-rw-r--r-- | src/backend/db_sqlite.h | 94 |
4 files changed, 146 insertions, 134 deletions
diff --git a/src/add_group_form.cpp b/src/add_group_form.cpp index 75ecd29..b20713d 100644 --- a/src/add_group_form.cpp +++ b/src/add_group_form.cpp @@ -15,6 +15,7 @@ void AddGroupForm::accept() { QString column_text = ui.new_group_column->currentText(); QString link_text = ui.new_group_link->text(); QMessageBox error_message; + BackendDB database; int new_id; if(name_text.isEmpty()) { @@ -26,7 +27,7 @@ void AddGroupForm::accept() { return; } - new_id = BackendDB::insertGroup(Group(0, name_text, column_text, link_text)); + new_id = database.insertGroup(Group(0, name_text, column_text, link_text)); // TODO redraw the main window QDialog::accept(); diff --git a/src/assignmentList.cpp b/src/assignmentList.cpp index 909b023..26af7ce 100644 --- a/src/assignmentList.cpp +++ b/src/assignmentList.cpp @@ -52,9 +52,6 @@ void AssignmentList::initializeUI() { // create toolbar ui.toolBar->addAction(ui.actionAdd_Group); - // setup database - BackendDB::init(); - this->displayDate(); this->displayWidgets(); this->show(); @@ -68,7 +65,8 @@ void AssignmentList::displayDate() { void AssignmentList::displayWidgets() { QVBoxLayout *column_left = new QVBoxLayout(); QVBoxLayout *column_right = new QVBoxLayout(); - QList<Group *> groups = BackendDB::loadGroups(); + BackendDB database; + QList<Group *> groups = database.loadGroups(); int i; // clear out old layouts if they exist 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; +} diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index fb04c00..bec534b 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -3,58 +3,62 @@ #include <QString> #include <QStringList> +#include <qsqldatabase.h> #include "../entry.h" #include "../group.h" #include "../rule.h" -// TODO rewrite to be a class (see difference between ::addDatabase and ::database) +class BackendDB : QSqlDatabase { + public: + BackendDB(); + QList<Group *> loadGroups(); + QList<Entry *> loadEntries(); + QList<Rule *> loadRules(); + int insertGroup(const Group &new_group); + int insertEntry(int new_entry); // param datatype TBD + int insertRule(int new_rule); // param datatype TBD + void updateGroup(int group); // param datatype TBD + void updateEntry(int entry); // param datatype TBD + void updateRule(int rule); // param datatype TBD + void removeGroup(int group); // param datatype TBD + void removeEntry(int entry); // param datatype TBD + void removeRule(int rule); // param datatype TBD + void cleanHidden(); -namespace BackendDB { - const QStringList create_table_queries = { - "CREATE TABLE groups (" - "id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL," - "name VARCHAR(255) NOT NULL," - "column TINYINT(1) DEFAULT FALSE," - "link VARCHAR(255) NOT NULL," - "hidden TINYINT(1) DEFAULT FALSE" - ")", - "CREATE TABLE entries (" - "id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL," - "parent_id REFERENCES groups (id)," - "description VARCHAR(255) NOT NULL," - "due_date TEXT DEFAULT NULL," - "alt_due_date VARCHAR(255) DEFAULT NULL," - "link VARCHAR(255) DEFAULT NULL," - "color VARCHAR(255) DEFAULT NULL," - "highlight VARCHAR(255) DEFAULT NULL," - "done TINYINT(1) DEFAULT FALSE," - "hidden TINYINT(1) DEFAULT FALSE" - ")", - "CREATE TABLE rules (" - "id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL," - "entry_id REFERENCES entries (id)," - "before_after TINYINT(1) DEFAULT TRUE," - "date TEXT NOT NULL," - "color VARCHAR(255) DEFAULT NULL," - "highlight VARCHAR(255) DEFAULT NULL" - ")" - }; + private: + const QStringList create_table_queries = { + "CREATE TABLE groups (" + "id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL," + "name VARCHAR(255) NOT NULL," + "column TINYINT(1) DEFAULT FALSE," + "link VARCHAR(255) NOT NULL," + "hidden TINYINT(1) DEFAULT FALSE" + ")", + "CREATE TABLE entries (" + "id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL," + "parent_id REFERENCES groups (id)," + "description VARCHAR(255) NOT NULL," + "due_date TEXT DEFAULT NULL," + "alt_due_date VARCHAR(255) DEFAULT NULL," + "link VARCHAR(255) DEFAULT NULL," + "color VARCHAR(255) DEFAULT NULL," + "highlight VARCHAR(255) DEFAULT NULL," + "done TINYINT(1) DEFAULT FALSE," + "hidden TINYINT(1) DEFAULT FALSE" + ")", + "CREATE TABLE rules (" + "id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL," + "entry_id REFERENCES entries (id)," + "before_after TINYINT(1) DEFAULT TRUE," + "date TEXT NOT NULL," + "color VARCHAR(255) DEFAULT NULL," + "highlight VARCHAR(255) DEFAULT NULL" + ")" + }; - void init(); - QList<Group *> loadGroups(); - QList<Entry *> loadEntries(); - QList<Rule *> loadRules(); - int insertGroup(const Group &new_group); // param datatype TBD - int insertEntry(int new_entry); // param datatype TBD - int insertRule(int new_rule); // param datatype TBD - void updateGroup(int group); // param datatype TBD - void updateEntry(int entry); // param datatype TBD - void updateRule(int rule); // param datatype TBD - void removeGroup(int group); // param datatype TBD - void removeEntry(int entry); // param datatype TBD - void removeRule(int rule); // param datatype TBD - void cleanHidden(); + QString getDBPath(); + QSqlDatabase openDB(); }; #endif |