82 lines
2.9 KiB
C++
82 lines
2.9 KiB
C++
//
|
|
// 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 <string>
|
|
#include <filesystem>
|
|
#include <sqlite3.h>
|
|
#include <iostream>
|
|
|
|
|
|
using namespace Budget;
|
|
|
|
const char *createTables = "create table if not exists account(id INTEGER constraint account_pk primary key autoincrement, name TEXT, description TEXT, cachedDollars INT, cachedCents INT);"
|
|
"create unique index if not exists account_name_uindex on account (name);"
|
|
"create table if not exists earning(id INTEGER constraint earning_pk primary key autoincrement, dollars INT not null, cents INT not null, description TEXT, receipt TEXT, accountId INT not null references account 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, dollars INT not null, cents INT not null, description TEXT, receipt TEXT, accountId INT not null references account 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<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;
|
|
}
|
|
|
|
rc = sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
|
|
if (rc != SQLITE_OK)
|
|
throw std::runtime_error("Couldn't commit transaction");
|
|
|
|
return 0;
|
|
}
|