Implemented account --description

This commit is contained in:
Quentin Snow 2023-01-26 18:54:38 -06:00
parent e635d80869
commit d3f3ebac3f
4 changed files with 35 additions and 3 deletions

View File

@ -107,3 +107,20 @@ double Database::cacheAccountValue(long long accountId, sqlite3 *db) {
return value;
throw std::runtime_error("Failed to set cashedValue for " + std::to_string(accountId));
}
void Database::accountDescription(const std::string& account, const std::string& description, sqlite3 *db) {
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, "UPDATE account SET description = ? WHERE name = ?", -1, &stmt, nullptr);
if (rc != SQLITE_OK)
throw std::runtime_error("Failed preparing accountDescription statement");
sqlite3_bind_text(stmt, 1, description.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, account.c_str(), -1, SQLITE_TRANSIENT);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE)
throw std::runtime_error("Failed to set description on " + account);
}

View File

@ -58,6 +58,17 @@ public:
* @throws std::runtime_error If the database query fails
*/
static double cacheAccountValue(long long accountId, 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);
};

View File

@ -26,6 +26,10 @@ void AccountOperation::commit() {
if (flags.value) {
std::cout << Database::getValue(account, db) << std::endl;
}
if (!flags.description.empty()) {
Database::accountDescription(account, flags.description, db);
}
}
AccountOperation::AccountOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {

View File

@ -64,13 +64,13 @@ void MainOptHandler::accountOptHandler(std::string account) {
{"delete", no_argument, nullptr, 'd'},
{"force-delete", no_argument, nullptr, 'F'},
{"value", no_argument, nullptr, 'v'},
{"description", no_argument, nullptr, 'D'},
{"description", required_argument, nullptr, 'D'},
};
auto acctOperation = std::make_unique<AccountOperation>(db, account);
while (true) {
int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:dFvD", accountLongOpts, nullptr);
int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:dFvD:", accountLongOpts, nullptr);
if (opt == -1)
break;
@ -238,7 +238,7 @@ void MainOptHandler::paymentOptHandler(std::string account) {
auto payOperation = std::make_unique<PaymentOperation>(db, account);
while (true) {
int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:v:d:r:D", paymentLongOpts, nullptr);
int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:v:d:r:D:", paymentLongOpts, nullptr);
if (opt == -1)
break;