budget/src/database.h

151 lines
5.2 KiB
C++

//
// Created by quentin on 1/18/23.
//
#ifndef BUDGET_DATABASE_H
#define BUDGET_DATABASE_H
#include <string>
#include <sqlite3.h>
class Database {
public:
/**
* @brief Checks if an account exists in the database
*
* @param account The name of the account to check for
* @param db The database connection to use
*
* @return True if the account exists, false otherwise
*
* @throws std::runtime_error If the database query fails
*/
static bool doesAccountExist(const std::string &account, sqlite3 *db);
/**
* @brief Deletes an account from the database
*
* @param account The name of the account to delete
* @param db The database connection to use
*
* @return True if the account was successfully deleted, false otherwise
*
* @throws std::runtime_error If the database query fails
*/
static void deleteAccount(const std::string &account, sqlite3 *db);
/**
* @brief Retrieves the cached value of an account from the database
*
* @param account The name of the account to retrieve the value of
* @param db The database connection to use
*
* @return The cached value of the account
*
* @throws std::runtime_error If the database query fails or the account doesn't exist
*/
static double getValue(const std::string &account, sqlite3 *db);
/**
* @brief Caches the value of an account by calculating the sum of all payments and earnings
*
* @param accountId The ID of the account to cache the value of
* @param db The database connection to use
*
* @return The cached value of the account
*
* @throws std::runtime_error If the database query fails
*/
static double cacheAccountValue(long long accountId, sqlite3 *db);
/**
* @brief Caches the value of an account by calculating the sum of all payments and earnings
*
* @param account The name of the account to cache the value of
* @param db The database connection to use
*
* @return The cached value of the account
*
* @throws std::runtime_error If the database query fails
*/
static double cacheAccountValue(const std::string &account, sqlite3 *db);
/**
* @brief Sets a new description for an account
*
* @param account The account name
* @param description The new description
* @param db The database connection to use
*
* @throws std::runtime_error If the statement fails to prepare or the step fails to execute
*/
static void accountDescription(const std::string &account, const std::string &description, sqlite3 *db);
/**
* @brief Creates a new account in the database with the given name and description
*
* @param account The name of the account to create
* @param description The description of the account to create
* @param db The sqlite3 database connection to use
*
* @throws std::runtime_error If the account could not be created in the database
*/
static void createAccount(const std::string &account, const std::string &description, sqlite3 *db);
/**
* @brief Creates a new account in the database with the given name
*
* @param account The name of the account to create
* @param db The sqlite3 database connection to use
*
* @throws std::runtime_error If the account could not be created in the database
*/
static void createAccount(const std::string &account, sqlite3 *db);
/**
* @brief The function records an earning in the database
*
* @param account The name of the account to record the earning in
* @param value The value of the earning
* @param description A description of the earning, can be an empty string
* @param receipt A receipt of the earning, can be an empty string
* @param date The date of the earning in UNIX time
* @param db The sqlite3 database connection
*
* @throws std::runtime_error If the earning could not be created in the database
*
* This function records an earning in the database by inserting a new row in the 'earning' table with the provided information.
* The accountId is retrieved from the 'account' table using the provided account name.
* If the query fails to prepare or execute, the function throws a runtime_error.
*/
static void
earn(const std::string &account, long double &value, std::string &description, std::string &receipt,
long long int date,
sqlite3 *db);
/**
* @brief The function records an payment in the database
*
* @param account The name of the account to record the payment in
* @param value The value of the payment
* @param description A description of the payment, can be an empty string
* @param receipt A receipt of the payment, can be an empty string
* @param date The date of the payment in UNIX time
* @param db The sqlite3 database connection
*
* @throws std::runtime_error If the payment could not be created in the database
*
* This function records an payment in the database by inserting a new row in the 'payment' table with the provided information.
* The accountId is retrieved from the 'account' table using the provided account name.
* If the query fails to prepare or execute, the function throws a runtime_error.
*/
static void
pay(const std::string &account, long double &value, std::string &description, std::string &receipt,
long long int date,
sqlite3 *db);
};
#endif //BUDGET_DATABASE_H