some primitive database stuff
This commit is contained in:
parent
89b80f0656
commit
319895db64
10 changed files with 166 additions and 0 deletions
|
@ -1,9 +1,11 @@
|
|||
set(HEADERS
|
||||
mysql.h
|
||||
statement.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
mysql.cpp
|
||||
statement.cpp
|
||||
)
|
||||
|
||||
find_package(MariaDB REQUIRED)
|
||||
|
|
|
@ -1,5 +1,19 @@
|
|||
#include "mysql.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "mysqld_error.h"
|
||||
|
||||
#include "statement.h"
|
||||
|
||||
constexpr const char* updateQuery = "UPDATE system SET `value` = ? WHERE `key` = 'version'";
|
||||
|
||||
struct ResDeleter {
|
||||
void operator () (MYSQL_RES* res) {
|
||||
mysql_free_result(res);
|
||||
}
|
||||
};
|
||||
|
||||
MySQL::MySQL():
|
||||
DBInterface(Type::mysql),
|
||||
connection(),
|
||||
|
@ -83,3 +97,50 @@ void MySQL::disconnect() {
|
|||
mysql_close(con);
|
||||
mysql_init(con); //this is ridiculous!
|
||||
}
|
||||
|
||||
void MySQL::executeFile(const std::string& path) {
|
||||
MYSQL* con = &connection;
|
||||
std::ifstream inputFile(path);
|
||||
std::string query;
|
||||
while (std::getline(inputFile, query, ';')) {
|
||||
int result = mysql_query(con, query.c_str());
|
||||
if (result != 0) {
|
||||
int errcode = mysql_errno(con);
|
||||
if (errcode == ER_EMPTY_QUERY)
|
||||
continue;
|
||||
|
||||
throw std::runtime_error("Error executing file " + path + ": " + mysql_error(con));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t MySQL::getVersion() {
|
||||
MYSQL* con = &connection;
|
||||
int result = mysql_query(con, "SELECT value FROM system WHERE `key` = 'version'");
|
||||
|
||||
if (result != 0) {
|
||||
unsigned int errcode = mysql_errno(con);
|
||||
if (errcode == ER_NO_SUCH_TABLE)
|
||||
return 0;
|
||||
|
||||
throw std::runtime_error(std::string("Error executing retreiving version: ") + mysql_error(con));
|
||||
}
|
||||
|
||||
std::unique_ptr<MYSQL_RES, ResDeleter> res(mysql_store_result(con));
|
||||
if (!res)
|
||||
throw std::runtime_error(std::string("Querying version returned no result: ") + mysql_error(con));
|
||||
|
||||
MYSQL_ROW row = mysql_fetch_row(res.get());
|
||||
if (row)
|
||||
return std::stoi(row[0]);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void 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();
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "database/dbinterface.h"
|
||||
|
||||
class MySQL : public DBInterface {
|
||||
class Statement;
|
||||
public:
|
||||
MySQL();
|
||||
~MySQL() override;
|
||||
|
@ -16,6 +17,10 @@ public:
|
|||
void setCredentials(const std::string& login, const std::string& password) override;
|
||||
void setDatabase(const std::string& database) override;
|
||||
|
||||
void executeFile(const std::string& path) override;
|
||||
uint8_t getVersion() override;
|
||||
void setVersion(uint8_t version) override;
|
||||
|
||||
protected:
|
||||
MYSQL connection;
|
||||
std::string login;
|
||||
|
|
47
database/mysql/statement.cpp
Normal file
47
database/mysql/statement.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "statement.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
static uint64_t TIME_LENGTH = sizeof(MYSQL_TIME);
|
||||
|
||||
MySQL::Statement::Statement(MYSQL* connection, const char* statement):
|
||||
stmt(mysql_stmt_init(connection)),
|
||||
param(),
|
||||
lengths()
|
||||
{
|
||||
int result = mysql_stmt_prepare(stmt.get(), statement, strlen(statement));
|
||||
if (result != 0)
|
||||
throw std::runtime_error(std::string("Error preparing statement: ") + mysql_stmt_error(stmt.get()));
|
||||
}
|
||||
|
||||
void MySQL::Statement::bind(void* value, enum_field_types type) {
|
||||
MYSQL_BIND& result = param.emplace_back();
|
||||
std::memset(&result, 0, sizeof(result));
|
||||
|
||||
result.buffer_type = type;
|
||||
result.buffer = value;
|
||||
|
||||
switch (type) {
|
||||
case MYSQL_TYPE_STRING:
|
||||
case MYSQL_TYPE_VAR_STRING:
|
||||
result.length = &lengths.emplace_back(strlen(static_cast<char*>(value)));
|
||||
break;
|
||||
case MYSQL_TYPE_DATE:
|
||||
result.length = &TIME_LENGTH;
|
||||
break;
|
||||
default:
|
||||
lengths.pop_back();
|
||||
throw std::runtime_error("Type: " + std::to_string(type) + " is not yet supported in bind");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void MySQL::Statement::execute() {
|
||||
int result = mysql_stmt_bind_param(stmt.get(), param.data());
|
||||
if (result != 0)
|
||||
throw std::runtime_error(std::string("Error binding statement: ") + mysql_stmt_error(stmt.get()));
|
||||
|
||||
result = mysql_stmt_execute(stmt.get());
|
||||
if (result != 0)
|
||||
throw std::runtime_error(std::string("Error executing statement: ") + mysql_stmt_error(stmt.get()));
|
||||
}
|
24
database/mysql/statement.h
Normal file
24
database/mysql/statement.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "mysql.h"
|
||||
|
||||
|
||||
class MySQL::Statement {
|
||||
struct STMTDeleter {
|
||||
void operator () (MYSQL_STMT* stmt) {
|
||||
mysql_stmt_close(stmt);
|
||||
};
|
||||
};
|
||||
public:
|
||||
Statement(MYSQL* connection, const char* statement);
|
||||
|
||||
void bind(void* value, enum_field_types type);
|
||||
void execute();
|
||||
|
||||
private:
|
||||
std::unique_ptr<MYSQL_STMT, STMTDeleter> stmt;
|
||||
std::vector<MYSQL_BIND> param;
|
||||
std::vector<uint64_t> lengths;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue