budget/src/main.cpp

82 lines
2.8 KiB
C++
Raw Normal View History

2022-08-04 18:22:48 -05:00
//
// 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"
2022-08-04 18:22:48 -05:00
#include <string>
#include <filesystem>
#include <sqlite3.h>
#include <iostream>
2023-01-15 17:50:01 -06:00
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() {
2022-09-05 20:38:07 -05:00
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.");
2023-01-28 12:28:43 -06:00
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<char *> args(argv, argv + argc);
try {
OptHandlers::MainOptHandler moh(args, db);
std::queue<std::unique_ptr<OptHandlers::Operation>> *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;
}
2023-01-28 12:28:43 -06:00
rc = sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
if (rc != SQLITE_OK)
throw std::runtime_error("Couldn't commit transaction");
return 0;
}