Database Pool

This commit is contained in:
Blue 2023-12-29 14:40:00 -03:00
parent 59c1ffd027
commit fe2fbb9ad0
Signed by: blue
GPG key ID: 9B203B252A63EE38
23 changed files with 268 additions and 63 deletions

View file

@ -23,8 +23,8 @@ constexpr const char* createSessionQuery = "INSERT INTO sessions (`owner`, `acce
static const std::filesystem::path buildSQLPath = "database";
MySQL::MySQL():
DBInterface(Type::mysql),
DB::MySQL::MySQL():
Interface(Type::mysql),
connection(),
login(),
password(),
@ -33,11 +33,11 @@ MySQL::MySQL():
mysql_init(&connection);
}
MySQL::~MySQL() {
DB::MySQL::~MySQL() {
mysql_close(&connection);
}
void MySQL::connect(const std::string& path) {
void DB::MySQL::connect(const std::string& path) {
if (state != State::disconnected)
return;
@ -59,7 +59,7 @@ void MySQL::connect(const std::string& path) {
state = State::connected;
}
void MySQL::setCredentials(const std::string& login, const std::string& password) {
void DB::MySQL::setCredentials(const std::string& login, const std::string& password) {
if (MySQL::login == login && MySQL::password == password)
return;
@ -81,7 +81,7 @@ void MySQL::setCredentials(const std::string& login, const std::string& password
throw std::runtime_error(std::string("Error changing credetials: ") + mysql_error(con));
}
void MySQL::setDatabase(const std::string& database) {
void DB::MySQL::setDatabase(const std::string& database) {
if (MySQL::database == database)
return;
@ -97,7 +97,7 @@ void MySQL::setDatabase(const std::string& database) {
throw std::runtime_error(std::string("Error changing db: ") + mysql_error(con));
}
void MySQL::disconnect() {
void DB::MySQL::disconnect() {
if (state == State::disconnected)
return;
@ -106,7 +106,7 @@ void MySQL::disconnect() {
mysql_init(con); //this is ridiculous!
}
void MySQL::executeFile(const std::filesystem::path& relativePath) {
void DB::MySQL::executeFile(const std::filesystem::path& relativePath) {
MYSQL* con = &connection;
std::filesystem::path path = sharedPath() / relativePath;
if (!std::filesystem::exists(path))
@ -138,7 +138,7 @@ void MySQL::executeFile(const std::filesystem::path& relativePath) {
}
}
uint8_t MySQL::getVersion() {
uint8_t DB::MySQL::getVersion() {
MYSQL* con = &connection;
int result = mysql_query(con, versionQuery);
@ -161,14 +161,14 @@ uint8_t MySQL::getVersion() {
return 0;
}
void MySQL::setVersion(uint8_t version) {
void DB::MySQL::setVersion(uint8_t version) {
std::string strVersion = std::to_string(version);
Statement statement(&connection, updateQuery);
statement.bind(strVersion.data(), MYSQL_TYPE_VAR_STRING);
statement.execute();
}
void MySQL::migrate(uint8_t targetVersion) {
void DB::MySQL::migrate(uint8_t targetVersion) {
uint8_t currentVersion = getVersion();
while (currentVersion < targetVersion) {
@ -190,7 +190,7 @@ void MySQL::migrate(uint8_t targetVersion) {
std::cout << "Database is now on actual version " << std::to_string(targetVersion) << std::endl;
}
std::optional<std::string> MySQL::getComment(std::string& string) {
std::optional<std::string> DB::MySQL::getComment(std::string& string) {
ltrim(string);
if (string.length() < 2)
return std::nullopt;
@ -218,7 +218,7 @@ std::optional<std::string> MySQL::getComment(std::string& string) {
return std::nullopt;
}
unsigned int MySQL::registerAccount(const std::string& login, const std::string& hash) {
unsigned int DB::MySQL::registerAccount(const std::string& login, const std::string& hash) {
//TODO validate filed lengths!
MYSQL* con = &connection;
MySQL::Transaction txn(con);
@ -247,7 +247,7 @@ unsigned int MySQL::registerAccount(const std::string& login, const std::string&
return id;
}
std::string MySQL::getAccountHash(const std::string& login) {
std::string DB::MySQL::getAccountHash(const std::string& login) {
std::string l = login;
MYSQL* con = &connection;
@ -265,7 +265,7 @@ std::string MySQL::getAccountHash(const std::string& login) {
return std::any_cast<const std::string&>(result[0][0]);
}
unsigned int MySQL::createSession(const std::string& login, const std::string& access, const std::string& renew) {
unsigned int DB::MySQL::createSession(const std::string& login, const std::string& access, const std::string& renew) {
std::string l = login, a = access, r = renew;
static std::string testingDevice("Testing...");
@ -281,7 +281,7 @@ unsigned int MySQL::createSession(const std::string& login, const std::string& a
return lastInsertedId();
}
unsigned int MySQL::lastInsertedId() {
unsigned int DB::MySQL::lastInsertedId() {
MYSQL* con = &connection;
int result = mysql_query(con, lastIdQuery);

View file

@ -9,10 +9,11 @@
#include <mysql.h>
#include "database/dbinterface.h"
#include "database/interface.h"
#include "utils/helpers.h"
class MySQL : public DBInterface {
namespace DB {
class MySQL : public Interface {
class Statement;
class Transaction;
@ -51,3 +52,4 @@ struct ResDeleter {
}
};
};
}

View file

@ -9,7 +9,7 @@
static uint64_t TIME_LENGTH = sizeof(MYSQL_TIME);
MySQL::Statement::Statement(MYSQL* connection, const char* statement):
DB::MySQL::Statement::Statement(MYSQL* connection, const char* statement):
stmt(mysql_stmt_init(connection)),
param()
{
@ -18,7 +18,7 @@ MySQL::Statement::Statement(MYSQL* connection, const char* statement):
throw std::runtime_error(std::string("Error preparing statement: ") + mysql_stmt_error(stmt.get()));
}
void MySQL::Statement::bind(void* value, enum_field_types type, bool usigned) {
void DB::MySQL::Statement::bind(void* value, enum_field_types type, bool usigned) {
MYSQL_BIND& result = param.emplace_back();
std::memset(&result, 0, sizeof(result));
@ -45,7 +45,7 @@ void MySQL::Statement::bind(void* value, enum_field_types type, bool usigned) {
}
}
void MySQL::Statement::execute() {
void DB::MySQL::Statement::execute() {
MYSQL_STMT* raw = stmt.get();
int result = mysql_stmt_bind_param(raw, param.data());
if (result != 0)
@ -64,7 +64,7 @@ void MySQL::Statement::execute() {
}
}
std::vector<std::vector<std::any>> MySQL::Statement::fetchResult() {
std::vector<std::vector<std::any>> DB::MySQL::Statement::fetchResult() {
MYSQL_STMT* raw = stmt.get();
if (mysql_stmt_store_result(raw) != 0)
throw std::runtime_error(std::string("Error fetching statement result: ") + mysql_stmt_error(raw)); //TODO not sure if it's valid here

View file

@ -10,6 +10,7 @@
#include "mysql.h"
namespace DB {
class MySQL::Statement {
struct STMTDeleter {
void operator () (MYSQL_STMT* stmt) {
@ -27,3 +28,4 @@ private:
std::unique_ptr<MYSQL_STMT, STMTDeleter> stmt;
std::vector<MYSQL_BIND> param;
};
}

View file

@ -3,7 +3,7 @@
#include "transaction.h"
MySQL::Transaction::Transaction(MYSQL* connection):
DB::MySQL::Transaction::Transaction(MYSQL* connection):
con(connection),
opened(false)
{
@ -13,12 +13,12 @@ MySQL::Transaction::Transaction(MYSQL* connection):
opened = true;
}
MySQL::Transaction::~Transaction() {
DB::MySQL::Transaction::~Transaction() {
if (opened)
abort();
}
void MySQL::Transaction::commit() {
void DB::MySQL::Transaction::commit() {
if (mysql_commit(con) != 0)
throw std::runtime_error(std::string("Failed to commit transaction") + mysql_error(con));
@ -27,7 +27,7 @@ void MySQL::Transaction::commit() {
throw std::runtime_error(std::string("Failed to return autocommit") + mysql_error(con));
}
void MySQL::Transaction::abort() {
void DB::MySQL::Transaction::abort() {
opened = false;
if (mysql_rollback(con) != 0)
throw std::runtime_error(std::string("Failed to rollback transaction") + mysql_error(con));

View file

@ -5,6 +5,7 @@
#include "mysql.h"
namespace DB {
class MySQL::Transaction {
public:
Transaction(MYSQL* connection);
@ -17,3 +18,4 @@ private:
MYSQL* con;
bool opened;
};
}