// // Created by quentin on 8/3/22. // #include "optHandlers/mainOptHandler.h" #include "exceptions/helpRequested.h" #include "exceptions/badValue.h" #include "sqliteDb.h" #include "main.h" #include #include #include #include using namespace Budget; const char* createTables = "CREATE TABLE IF NOT EXISTS accountName (id INTEGER CONSTRAINT account_pk PRIMARY KEY AUTOINCREMENT, name TEXT, description TEXT, cachedValue DOUBLE);" "CREATE UNIQUE INDEX IF NOT EXISTS account_name_uindex ON accountName (name);" "CREATE TABLE IF NOT EXISTS earning (id INTEGER CONSTRAINT earning_pk PRIMARY KEY AUTOINCREMENT, value DOUBLE NOT NULL, description TEXT, receipt TEXT, accountId INT NOT NULL REFERENCES accountName ON UPDATE CASCADE ON DELETE CASCADE, date INTEGER NOT NULL);" "CREATE INDEX IF NOT EXISTS earning_date_index ON earning (date DESC);" "CREATE TABLE IF NOT EXISTS payment (id INTEGER CONSTRAINT payment_pk PRIMARY KEY AUTOINCREMENT, value DOUBLE NOT NULL, description TEXT, receipt TEXT, accountId INT NOT NULL REFERENCES accountName ON UPDATE CASCADE ON DELETE CASCADE, date INTEGER NOT NULL);" "CREATE INDEX IF NOT EXISTS payment_date_index ON payment (date DESC);"; void createRequiredFolders() { std::filesystem::create_directory(configD); std::filesystem::create_directories(storageD); std::filesystem::create_directories(storageD + "receipts"); std::filesystem::create_directories(storageD + "receipts/payment"); std::filesystem::create_directories(storageD + "receipts/earn"); } int main(int argc, char *argv[]) { createRequiredFolders(); sqlite3 *db; int rc; rc = sqlite3_open(databaseFile.c_str(), &db); if (rc != SQLITE_OK) { throw std::runtime_error("Error opening database connection."); } SqliteDb dbRAII(db); rc = sqlite3_exec(db, "PRAGMA foreign_keys = 1;", nullptr, nullptr, nullptr); if (rc != SQLITE_OK) throw std::runtime_error("Error enabling foreign_keys. Database might be malformed."); rc = sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr); if (rc != SQLITE_OK) throw std::runtime_error("Couldn't begin transaction"); rc = sqlite3_exec(db, createTables, nullptr, nullptr, nullptr); if (rc != SQLITE_OK) throw std::runtime_error("Couldn't create the tables"); std::vector args(argv, argv + argc); try { OptHandlers::MainOptHandler moh(args, db); std::queue> *opts = &moh.operations; while (!opts->empty()) { opts->front()->commit(); opts->pop(); } } catch (const Budget::Exceptions::HelpRequested &e) { return 0; } catch (const Budget::Exceptions::BadValue &e) { std::cout << e.what() << std::endl; return 1; } rc = sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr); if (rc != SQLITE_OK) throw std::runtime_error("Couldn't commit transaction"); return 0; }