Compare commits
2 Commits
983f1827e6
...
434eaf0ce0
Author | SHA1 | Date | |
---|---|---|---|
434eaf0ce0 | |||
3941e43e7f |
@ -11,7 +11,7 @@ bool Database::doesAccountExist(const std::string &account, sqlite3 *db) {
|
||||
int rc = sqlite3_prepare_v2(db, "SELECT * FROM account WHERE name = ?", -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
throw std::runtime_error("Failed to create account existing query");
|
||||
throw std::runtime_error("Failed to prepare account exist query.");
|
||||
|
||||
sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_TRANSIENT);
|
||||
|
||||
@ -42,3 +42,67 @@ bool Database::deleteAccount(const std::string &account, sqlite3 *db) {
|
||||
|
||||
return (rc == SQLITE_DONE);
|
||||
}
|
||||
|
||||
double Database::getValue(const std::string &account, sqlite3 *db) {
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, "SELECT id, cachedValue FROM account WHERE name = ?", -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
throw std::runtime_error("Failed preparing cashedValue " + account);
|
||||
|
||||
sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_TRANSIENT);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
|
||||
if (rc == SQLITE_ROW) {
|
||||
double value;
|
||||
if (sqlite3_column_type(stmt, 1) == SQLITE_NULL) {
|
||||
value = cacheAccountValue(sqlite3_column_int64(stmt, 0), db);
|
||||
sqlite3_finalize(stmt);
|
||||
return value;
|
||||
}
|
||||
value = sqlite3_column_double(stmt, 1);
|
||||
sqlite3_finalize(stmt);
|
||||
return value;
|
||||
} else if (rc == SQLITE_DONE)
|
||||
throw std::runtime_error("Account doesnt exist? Shouldn't be possible in this call.");
|
||||
else
|
||||
throw std::runtime_error("Failed to step getValue statement.");
|
||||
}
|
||||
|
||||
double Database::cacheAccountValue(long long accountId, sqlite3 *db) {
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, "SELECT SUM(value) FROM ("
|
||||
"SELECT value * -1 as value FROM payment WHERE accountId = ? "
|
||||
"UNION ALL "
|
||||
"SELECT value FROM earning WHERE accountId = ?);", -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
throw std::runtime_error("Failed preparing get cashedValue " + std::to_string(accountId));
|
||||
|
||||
sqlite3_bind_int64(stmt, 1, accountId);
|
||||
sqlite3_bind_int64(stmt, 2, accountId);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
|
||||
double value;
|
||||
if (rc == SQLITE_ROW)
|
||||
value = sqlite3_column_double(stmt, 0);
|
||||
else if (rc == SQLITE_DONE)
|
||||
value = 0;
|
||||
else
|
||||
throw std::runtime_error("Failed get cashedValue " + std::to_string(accountId));
|
||||
|
||||
sqlite3_reset(stmt);
|
||||
rc = sqlite3_prepare_v2(db, "UPDATE account SET cachedValue = ? WHERE id = ?", -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK)
|
||||
throw std::runtime_error("Failed preparing set cashedValue " + std::to_string(accountId));
|
||||
|
||||
sqlite3_bind_double(stmt, 1, value);
|
||||
sqlite3_bind_int64(stmt, 2, accountId);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc == SQLITE_DONE)
|
||||
return value;
|
||||
throw std::runtime_error("Failed to set cashedValue for " + std::to_string(accountId));
|
||||
}
|
||||
|
@ -11,9 +11,53 @@
|
||||
|
||||
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 bool 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);
|
||||
};
|
||||
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "../utilities.h"
|
||||
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
|
||||
using namespace Budget::OptHandlers;
|
||||
|
||||
@ -21,6 +22,10 @@ void AccountOperation::commit() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (flags.value) {
|
||||
std::cout << Database::getValue(account, db) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
AccountOperation::AccountOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {
|
||||
|
@ -12,8 +12,21 @@
|
||||
#include "operation.h"
|
||||
|
||||
namespace Budget::OptHandlers {
|
||||
/**
|
||||
* @class MainOptHandler
|
||||
* @brief Handles command line options for the main menu of the budget application
|
||||
*
|
||||
* The MainOptHandler class handles command line options passed to the budget application. It uses the getopt_long function
|
||||
* to parse the options and perform the corresponding actions.
|
||||
*/
|
||||
class MainOptHandler {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for MainOptHandler
|
||||
*
|
||||
* @param argv Vector of arguments passed to the application
|
||||
* @param pSqlite3 The database connection to use
|
||||
*/
|
||||
explicit MainOptHandler(const std::vector<char *> &argv, sqlite3 *pSqlite3);
|
||||
|
||||
void help();
|
||||
|
@ -10,6 +10,13 @@
|
||||
|
||||
class Utilities {
|
||||
public:
|
||||
/**
|
||||
* @brief Asks the user to confirm an action with a yes or no question
|
||||
*
|
||||
* @param question The question to ask the user
|
||||
*
|
||||
* @return True if the user confirms the action, false otherwise
|
||||
*/
|
||||
static bool confirm(const std::string &question);
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user