2022-08-04 18:22:48 -05:00
//
// Created by quentin on 8/3/22.
//
2022-08-13 18:42:16 -05:00
# include "optHandlers/mainOptHandler.h"
2023-01-21 17:50:07 -06:00
# include "exceptions/helpRequested.h"
# include "exceptions/badValue.h"
2023-01-22 18:09:30 -06:00
# include "sqliteDb.h"
2023-01-31 18:51:22 -06:00
# include "main.h"
2022-08-04 18:22:48 -05:00
2022-08-12 15:26:25 -05:00
# include <string>
# include <filesystem>
2023-01-21 17:50:07 -06:00
# include <sqlite3.h>
# include <iostream>
2023-01-15 17:50:01 -06:00
2022-08-13 16:35:01 -05:00
using namespace Budget ;
2022-08-12 15:26:25 -05:00
2023-01-31 23:22:54 -06:00
const char * createTables = " CREATE TABLE IF NOT EXISTS account (id INTEGER CONSTRAINT account_pk PRIMARY KEY AUTOINCREMENT, name TEXT, description TEXT, cachedValue DOUBLE); "
" 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, value DOUBLE 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, value DOUBLE 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); " ;
2022-08-12 15:26:25 -05:00
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 " ) ;
2023-01-31 18:51:22 -06:00
std : : filesystem : : create_directories ( storageD + " receipts/payment " ) ;
std : : filesystem : : create_directories ( storageD + " receipts/earn " ) ;
2022-08-12 15:26:25 -05:00
}
2022-08-13 16:35:01 -05:00
int main ( int argc , char * argv [ ] ) {
2023-01-31 18:51:22 -06:00
createRequiredFolders ( ) ;
2023-01-21 17:50:07 -06:00
sqlite3 * db ;
2023-01-22 18:09:30 -06:00
2023-01-21 17:50:07 -06:00
int rc ;
rc = sqlite3_open ( databaseFile . c_str ( ) , & db ) ;
2023-01-31 23:22:54 -06:00
if ( rc ! = SQLITE_OK ) {
2023-01-21 17:50:07 -06:00
throw std : : runtime_error ( " Error opening database connection. " ) ;
2023-01-31 23:22:54 -06:00
}
SqliteDb dbRAII ( db ) ;
2023-01-22 18:09:30 -06:00
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-21 17:50:07 -06:00
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 " ) ;
2023-01-31 18:51:22 -06:00
rc = sqlite3_exec ( db , createTables , nullptr , nullptr , nullptr ) ;
if ( rc ! = SQLITE_OK )
throw std : : runtime_error ( " Couldn't create the tables " ) ;
2022-08-13 16:35:01 -05:00
std : : vector < char * > args ( argv , argv + argc ) ;
2023-01-21 17:50:07 -06:00
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 " ) ;
2022-08-12 15:26:25 -05:00
return 0 ;
}