some primitive database stuff

This commit is contained in:
Blue 2023-12-08 19:26:16 -03:00
parent 89b80f0656
commit 319895db64
Signed by: blue
GPG Key ID: 9B203B252A63EE38
10 changed files with 166 additions and 0 deletions

View File

@ -9,3 +9,4 @@ set(SOURCES
target_sources(pica PRIVATE ${SOURCES}) target_sources(pica PRIVATE ${SOURCES})
add_subdirectory(mysql) add_subdirectory(mysql)
add_subdirectory(migrations)

View File

@ -3,6 +3,7 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <memory> #include <memory>
#include <stdint.h>
class DBInterface { class DBInterface {
public: public:
@ -28,6 +29,10 @@ public:
virtual void setDatabase(const std::string& newDatabase) = 0; virtual void setDatabase(const std::string& newDatabase) = 0;
virtual void setCredentials(const std::string& login, const std::string& password) = 0; virtual void setCredentials(const std::string& login, const std::string& password) = 0;
virtual void executeFile(const std::string& path) = 0;
virtual uint8_t getVersion() = 0;
virtual void setVersion(uint8_t version) = 0;
protected: protected:
DBInterface(Type type); DBInterface(Type type);

View File

@ -0,0 +1 @@
configure_file(m0.sql m0.sql COPYONLY)

View File

@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS system (
`key` VARCHAR(32) PRIMARY KEY,
`value` TEXT
);
INSERT INTO system (`key`, `value`) VALUES ('version', '0');

View File

@ -1,9 +1,11 @@
set(HEADERS set(HEADERS
mysql.h mysql.h
statement.h
) )
set(SOURCES set(SOURCES
mysql.cpp mysql.cpp
statement.cpp
) )
find_package(MariaDB REQUIRED) find_package(MariaDB REQUIRED)

View File

@ -1,5 +1,19 @@
#include "mysql.h" #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(): MySQL::MySQL():
DBInterface(Type::mysql), DBInterface(Type::mysql),
connection(), connection(),
@ -83,3 +97,50 @@ void MySQL::disconnect() {
mysql_close(con); mysql_close(con);
mysql_init(con); //this is ridiculous! 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();
}

View File

@ -7,6 +7,7 @@
#include "database/dbinterface.h" #include "database/dbinterface.h"
class MySQL : public DBInterface { class MySQL : public DBInterface {
class Statement;
public: public:
MySQL(); MySQL();
~MySQL() override; ~MySQL() override;
@ -16,6 +17,10 @@ public:
void setCredentials(const std::string& login, const std::string& password) override; void setCredentials(const std::string& login, const std::string& password) override;
void setDatabase(const std::string& database) 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: protected:
MYSQL connection; MYSQL connection;
std::string login; std::string login;

View 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()));
}

View 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;
};

View File

@ -15,13 +15,27 @@ Server::Server():
db->setCredentials("pica", "pica"); db->setCredentials("pica", "pica");
db->setDatabase("pica"); db->setDatabase("pica");
bool connected = false;
try { try {
db->connect("/run/mysqld/mysqld.sock"); db->connect("/run/mysqld/mysqld.sock");
connected = true;
std::cout << "Successfully connected to the database" << std::endl; std::cout << "Successfully connected to the database" << std::endl;
} catch (const std::runtime_error& e) { } catch (const std::runtime_error& e) {
std::cerr << "Couldn't connect to the database: " << e.what() << std::endl; std::cerr << "Couldn't connect to the database: " << e.what() << std::endl;
} }
if (connected) {
uint8_t version = db->getVersion();
std::cout << "Database version is " << std::to_string(version) << std::endl;
if (version == 0) {
db->executeFile("database/migrations/m0.sql");
std::cout << "Successfully migrated to version 1" << std::endl;
db->setVersion(1);
std::cout << "Database version is " << std::to_string(db->getVersion()) << " now" << std::endl;
}
}
router.addRoute("info", Server::info); router.addRoute("info", Server::info);
router.addRoute("env", Server::printEnvironment); router.addRoute("env", Server::printEnvironment);
} }