From 3ef9931e59ef38d9bbf045b07a4dbab2fd52e3f5 Mon Sep 17 00:00:00 2001 From: Louie S Date: Fri, 23 Feb 2024 19:06:45 -0500 Subject: Implement db creation --- CMakeLists.txt | 4 ++- src/assignmentList.cpp | 3 ++ src/backend/db_sqlite.cpp | 61 ++++++++++++++++++++++++++++++++ src/backend/db_sqlite.h | 88 +++++++++++++++++++++++------------------------ 4 files changed, 111 insertions(+), 45 deletions(-) create mode 100644 src/backend/db_sqlite.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index feec929..8fea537 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ set(CMAKE_AUTOUIC ON) option(USE_QT5 "Use Qt5, instead of Qt6" no) option(USE_QT6 "Use Qt6, instead of Qt5" no) -set(qt_components "Core" "Gui" "UiTools" "Widgets") +set(qt_components "Core" "Gui" "Sql" "UiTools" "Widgets") set(qt_names "Qt6" "Qt5") if(USE_QT5) set(qt_names "Qt5") @@ -33,6 +33,8 @@ endforeach() set(project_sources "src/add_entry_form.ui" "src/add_group_form.ui" + "src/backend/db_sqlite.cpp" + "src/backend/db_sqlite.h" "src/assignmentList.cpp" "src/assignmentList.h" "src/assignmentList.ui" diff --git a/src/assignmentList.cpp b/src/assignmentList.cpp index d755da7..17ec3cc 100644 --- a/src/assignmentList.cpp +++ b/src/assignmentList.cpp @@ -9,9 +9,11 @@ #include #include +#include #include #include "assignmentList.h" +#include "backend/db_sqlite.h" #include "settings.h" AssignmentList::AssignmentList() { @@ -56,6 +58,7 @@ void AssignmentList::initializeUI() { } void AssignmentList::setupDB() { + BackendDB::init(); qDebug() << "WIP"; } diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp new file mode 100644 index 0000000..05dc702 --- /dev/null +++ b/src/backend/db_sqlite.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "db_sqlite.h" + +QString BackendDB::getDBPath() { + QSettings settings; + settings.beginGroup("paths"); + return settings.value("db_path").toString(); +} + +void BackendDB::init() { + QString db_path(BackendDB::getDBPath()); + int i; + + // Check if database already exists + if(QFile::exists(db_path)) + return; + + // Check if directory exists + if(!QFileInfo(db_path).dir().exists()) { + if(!QFileInfo(db_path).dir().mkpath(QFileInfo(db_path).dir().absolutePath())) { + qDebug() << "Error creating db file"; + std::exit(1); + } + } + + // 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); + } + + 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"); + + for(i = 0; i < BackendDB::create_table_queries.length(); ++i) + query.exec(BackendDB::create_table_queries[i]); + + qDebug() << database.lastError(); + + database.close(); +} diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index 5c55fef..8563223 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -1,53 +1,53 @@ #ifndef BACKEND_DB_SQLITE_H #define BACKEND_DB_SQLITE_H +#include #include -class BackendDB { - public: - void init(); - void load(); - void insertGroup(int new_group); // param datatype TBD - void insertEntry(int new_entry); // param datatype TBD - void 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" - ")" - }; + QString getDBPath(); + void init(); + void load(); + void insertGroup(int new_group); // param datatype TBD + void insertEntry(int new_entry); // param datatype TBD + void 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(); }; #endif -- cgit