2023-01-21 17:50:07 -06:00
|
|
|
//
|
|
|
|
// Created by quentin on 1/18/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "database.h"
|
|
|
|
#include <sqlite3.h>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
bool Database::doesAccountExist(const std::string &account, sqlite3 *db) {
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
int rc = sqlite3_prepare_v2(db, "SELECT * FROM account WHERE name = ?", -1, &stmt, nullptr);
|
|
|
|
|
2023-01-21 18:27:03 -06:00
|
|
|
if (rc != SQLITE_OK)
|
2023-01-21 17:50:07 -06:00
|
|
|
throw std::runtime_error("Failed to create account existing query");
|
|
|
|
|
|
|
|
sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_TRANSIENT);
|
|
|
|
|
|
|
|
rc = sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
if (rc == SQLITE_ROW) {
|
|
|
|
return true;
|
|
|
|
} else if (rc == SQLITE_DONE) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
throw std::runtime_error("Failed to step account existing statement.");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-01-21 18:27:03 -06:00
|
|
|
|
|
|
|
bool Database::deleteAccount(const std::string &account, sqlite3 *db) {
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
int rc = sqlite3_prepare_v2(db, "DELETE FROM account WHERE name = ?", -1, &stmt, nullptr);
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK)
|
|
|
|
throw std::runtime_error("Failed to delete account " + account);
|
|
|
|
|
|
|
|
sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_TRANSIENT);
|
|
|
|
|
|
|
|
rc = sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
return (rc == SQLITE_DONE);
|
|
|
|
}
|