2023-12-30 22:42:11 +00:00
|
|
|
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
|
|
|
//SPDX-License-Identifier: GPL-3.0-or-later
|
2023-12-10 23:23:15 +00:00
|
|
|
|
2023-12-07 20:32:43 +00:00
|
|
|
#include "mysql.h"
|
|
|
|
|
2023-12-08 22:26:16 +00:00
|
|
|
#include <fstream>
|
2023-12-10 23:23:15 +00:00
|
|
|
#include <iostream>
|
2023-12-08 22:26:16 +00:00
|
|
|
|
|
|
|
#include "mysqld_error.h"
|
|
|
|
|
|
|
|
#include "statement.h"
|
2023-12-20 22:42:13 +00:00
|
|
|
#include "transaction.h"
|
2023-12-22 23:25:20 +00:00
|
|
|
#include "database/exceptions.h"
|
2023-12-08 22:26:16 +00:00
|
|
|
|
2023-12-20 22:42:13 +00:00
|
|
|
constexpr const char* versionQuery = "SELECT value FROM system WHERE `key` = 'version'";
|
2023-12-08 22:26:16 +00:00
|
|
|
constexpr const char* updateQuery = "UPDATE system SET `value` = ? WHERE `key` = 'version'";
|
2023-12-20 22:42:13 +00:00
|
|
|
constexpr const char* registerQuery = "INSERT INTO accounts (`login`, `type`, `password`) VALUES (?, 1, ?)";
|
|
|
|
constexpr const char* lastIdQuery = "SELECT LAST_INSERT_ID() AS id";
|
|
|
|
constexpr const char* assignRoleQuery = "INSERT INTO roleBindings (`account`, `role`) SELECT ?, roles.id FROM roles WHERE roles.name = ?";
|
2023-12-22 23:25:20 +00:00
|
|
|
constexpr const char* selectHash = "SELECT password FROM accounts where login = ?";
|
2023-12-23 20:23:38 +00:00
|
|
|
constexpr const char* createSessionQuery = "INSERT INTO sessions (`owner`, `access`, `renew`, `persist`, `device`)"
|
2024-01-11 21:33:46 +00:00
|
|
|
" SELECT accounts.id, ?, ?, true, ? FROM accounts WHERE accounts.login = ?"
|
|
|
|
" RETURNING id, owner";
|
2024-01-12 23:39:41 +00:00
|
|
|
constexpr const char* selectSession = "SELECT id, owner, access, renew FROM sessions where access = ?";
|
2024-01-11 21:33:46 +00:00
|
|
|
constexpr const char* selectAssets = "SELECT id, owner, currency, title, icon, archived FROM assets where owner = ?";
|
2024-01-12 23:39:41 +00:00
|
|
|
constexpr const char* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `archived`)"
|
|
|
|
" VALUES (?, ?, ?, ?, ?)";
|
2023-12-08 22:26:16 +00:00
|
|
|
|
2023-12-10 23:23:15 +00:00
|
|
|
static const std::filesystem::path buildSQLPath = "database";
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
DB::MySQL::MySQL ():
|
2023-12-29 17:40:00 +00:00
|
|
|
Interface(Type::mysql),
|
2023-12-07 20:32:43 +00:00
|
|
|
connection(),
|
|
|
|
login(),
|
|
|
|
password(),
|
|
|
|
database()
|
|
|
|
{
|
|
|
|
mysql_init(&connection);
|
|
|
|
}
|
|
|
|
|
2023-12-29 17:40:00 +00:00
|
|
|
DB::MySQL::~MySQL() {
|
2023-12-07 20:32:43 +00:00
|
|
|
mysql_close(&connection);
|
|
|
|
}
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
void DB::MySQL::connect (const std::string& path) {
|
2023-12-07 20:32:43 +00:00
|
|
|
if (state != State::disconnected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MYSQL* con = &connection;
|
|
|
|
MYSQL* res = mysql_real_connect(
|
|
|
|
con,
|
|
|
|
NULL,
|
|
|
|
login.c_str(),
|
|
|
|
password.c_str(),
|
|
|
|
database.empty() ? NULL : database.c_str(),
|
|
|
|
0,
|
|
|
|
path.c_str(),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res != con)
|
|
|
|
throw std::runtime_error(std::string("Error changing connecting: ") + mysql_error(con));
|
|
|
|
|
|
|
|
state = State::connected;
|
|
|
|
}
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
void DB::MySQL::setCredentials (const std::string& login, const std::string& password) {
|
2023-12-07 20:32:43 +00:00
|
|
|
if (MySQL::login == login && MySQL::password == password)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MySQL::login = login;
|
|
|
|
MySQL::password = password;
|
|
|
|
|
|
|
|
if (state == State::disconnected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MYSQL* con = &connection;
|
|
|
|
int result = mysql_change_user(
|
|
|
|
con,
|
|
|
|
login.c_str(),
|
|
|
|
password.c_str(),
|
|
|
|
database.empty() ? NULL : database.c_str()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (result != 0)
|
|
|
|
throw std::runtime_error(std::string("Error changing credetials: ") + mysql_error(con));
|
|
|
|
}
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
void DB::MySQL::setDatabase (const std::string& database) {
|
2023-12-07 20:32:43 +00:00
|
|
|
if (MySQL::database == database)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MySQL::database = database;
|
|
|
|
|
|
|
|
if (state == State::disconnected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MYSQL* con = &connection;
|
|
|
|
int result = mysql_select_db(con, database.c_str());
|
|
|
|
|
|
|
|
if (result != 0)
|
|
|
|
throw std::runtime_error(std::string("Error changing db: ") + mysql_error(con));
|
|
|
|
}
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
void DB::MySQL::disconnect () {
|
2023-12-07 20:32:43 +00:00
|
|
|
if (state == State::disconnected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MYSQL* con = &connection;
|
|
|
|
mysql_close(con);
|
|
|
|
mysql_init(con); //this is ridiculous!
|
|
|
|
}
|
2023-12-08 22:26:16 +00:00
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
void DB::MySQL::executeFile (const std::filesystem::path& relativePath) {
|
2023-12-08 22:26:16 +00:00
|
|
|
MYSQL* con = &connection;
|
2023-12-10 23:23:15 +00:00
|
|
|
std::filesystem::path path = sharedPath() / relativePath;
|
|
|
|
if (!std::filesystem::exists(path))
|
|
|
|
throw std::runtime_error("Error executing file "
|
|
|
|
+ std::filesystem::absolute(path).string()
|
|
|
|
+ ": file doesn't exist");
|
|
|
|
|
|
|
|
std::cout << "Executing file " << path << std::endl;
|
2023-12-08 22:26:16 +00:00
|
|
|
std::ifstream inputFile(path);
|
|
|
|
std::string query;
|
|
|
|
while (std::getline(inputFile, query, ';')) {
|
2023-12-11 23:29:55 +00:00
|
|
|
std::optional<std::string> comment = getComment(query);
|
|
|
|
while (comment) {
|
|
|
|
std::cout << '\t' << comment.value() << std::endl;
|
|
|
|
comment = getComment(query);
|
|
|
|
}
|
|
|
|
if (query.empty())
|
|
|
|
continue;
|
|
|
|
|
2023-12-08 22:26:16 +00:00
|
|
|
int result = mysql_query(con, query.c_str());
|
|
|
|
if (result != 0) {
|
|
|
|
int errcode = mysql_errno(con);
|
|
|
|
if (errcode == ER_EMPTY_QUERY)
|
|
|
|
continue;
|
|
|
|
|
2023-12-10 23:23:15 +00:00
|
|
|
throw std::runtime_error("Error executing file " + path.string() + ": " + mysql_error(con));
|
2023-12-08 22:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
uint8_t DB::MySQL::getVersion () {
|
2023-12-08 22:26:16 +00:00
|
|
|
MYSQL* con = &connection;
|
2023-12-20 22:42:13 +00:00
|
|
|
int result = mysql_query(con, versionQuery);
|
2023-12-08 22:26:16 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
void DB::MySQL::setVersion (uint8_t version) {
|
2023-12-08 22:26:16 +00:00
|
|
|
std::string strVersion = std::to_string(version);
|
|
|
|
Statement statement(&connection, updateQuery);
|
|
|
|
statement.bind(strVersion.data(), MYSQL_TYPE_VAR_STRING);
|
|
|
|
statement.execute();
|
|
|
|
}
|
2023-12-10 23:23:15 +00:00
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
void DB::MySQL::migrate (uint8_t targetVersion) {
|
2023-12-10 23:23:15 +00:00
|
|
|
uint8_t currentVersion = getVersion();
|
|
|
|
|
|
|
|
while (currentVersion < targetVersion) {
|
2023-12-11 23:29:55 +00:00
|
|
|
if (currentVersion == 255)
|
|
|
|
throw std::runtime_error("Maximum possible database version reached");
|
|
|
|
|
|
|
|
uint8_t nextVersion = currentVersion + 1;
|
2023-12-10 23:23:15 +00:00
|
|
|
std::string fileName = "migrations/m" + std::to_string(currentVersion) + ".sql";
|
|
|
|
std::cout << "Performing migration "
|
|
|
|
<< std::to_string(currentVersion)
|
|
|
|
<< " -> "
|
2023-12-11 23:29:55 +00:00
|
|
|
<< std::to_string(nextVersion)
|
2023-12-10 23:23:15 +00:00
|
|
|
<< std::endl;
|
|
|
|
executeFile(fileName);
|
2023-12-11 23:29:55 +00:00
|
|
|
setVersion(nextVersion);
|
|
|
|
currentVersion = nextVersion;
|
2023-12-10 23:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "Database is now on actual version " << std::to_string(targetVersion) << std::endl;
|
|
|
|
}
|
2023-12-11 23:29:55 +00:00
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
std::optional<std::string> DB::MySQL::getComment (std::string& string) {
|
2023-12-11 23:29:55 +00:00
|
|
|
ltrim(string);
|
|
|
|
if (string.length() < 2)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
if (string[0] == '-') {
|
|
|
|
if (string[1] == '-') {
|
|
|
|
string.erase(0, 2);
|
|
|
|
std::string::size_type eol = string.find('\n');
|
|
|
|
return extract(string, 0, eol);
|
|
|
|
}
|
|
|
|
} else if (string[0] == '/') {
|
|
|
|
if (string[1] == '*') {
|
|
|
|
string.erase(0, 2);
|
|
|
|
std::string::size_type end = 0;
|
|
|
|
do {
|
|
|
|
end = string.find(end, '*');
|
|
|
|
} while (end != std::string::npos && end < string.size() - 1 && string[end + 1] == '/');
|
|
|
|
if (end < string.size() - 1)
|
|
|
|
end = std::string::npos;
|
|
|
|
|
|
|
|
return extract(string, 0, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
unsigned int DB::MySQL::registerAccount (const std::string& login, const std::string& hash) {
|
2023-12-20 22:42:13 +00:00
|
|
|
//TODO validate filed lengths!
|
|
|
|
MYSQL* con = &connection;
|
|
|
|
MySQL::Transaction txn(con);
|
|
|
|
|
|
|
|
Statement addAcc(con, registerQuery);
|
|
|
|
|
|
|
|
std::string l = login; //I hate copying just to please this horible API
|
|
|
|
std::string h = hash;
|
|
|
|
addAcc.bind(l.data(), MYSQL_TYPE_STRING);
|
|
|
|
addAcc.bind(h.data(), MYSQL_TYPE_STRING);
|
2023-12-22 23:25:20 +00:00
|
|
|
try {
|
|
|
|
addAcc.execute();
|
|
|
|
} catch (const Duplicate& dup) {
|
|
|
|
throw DuplicateLogin(dup.what());
|
|
|
|
}
|
2023-12-20 22:42:13 +00:00
|
|
|
|
|
|
|
unsigned int id = lastInsertedId();
|
|
|
|
static std::string defaultRole("default");
|
|
|
|
|
|
|
|
Statement addRole(con, assignRoleQuery);
|
|
|
|
addRole.bind(&id, MYSQL_TYPE_LONG, true);
|
|
|
|
addRole.bind(defaultRole.data(), MYSQL_TYPE_STRING);
|
|
|
|
addRole.execute();
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
std::string DB::MySQL::getAccountHash (const std::string& login) {
|
2023-12-22 23:25:20 +00:00
|
|
|
std::string l = login;
|
|
|
|
MYSQL* con = &connection;
|
|
|
|
|
|
|
|
Statement getHash(con, selectHash);
|
|
|
|
getHash.bind(l.data(), MYSQL_TYPE_STRING);
|
|
|
|
getHash.execute();
|
|
|
|
|
2023-12-23 20:23:38 +00:00
|
|
|
std::vector<std::vector<std::any>> result = getHash.fetchResult();
|
2023-12-22 23:25:20 +00:00
|
|
|
if (result.empty())
|
|
|
|
throw NoLogin("Couldn't find login " + l);
|
|
|
|
|
|
|
|
if (result[0].empty())
|
|
|
|
throw std::runtime_error("Error with the query \"selectHash\"");
|
|
|
|
|
2023-12-23 20:23:38 +00:00
|
|
|
return std::any_cast<const std::string&>(result[0][0]);
|
|
|
|
}
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
DB::Session DB::MySQL::createSession (const std::string& login, const std::string& access, const std::string& renew) {
|
2024-01-11 21:33:46 +00:00
|
|
|
std::string l = login;
|
|
|
|
DB::Session res;
|
|
|
|
res.accessToken = access;
|
|
|
|
res.renewToken = renew;
|
2023-12-23 20:23:38 +00:00
|
|
|
static std::string testingDevice("Testing...");
|
|
|
|
|
|
|
|
MYSQL* con = &connection;
|
|
|
|
|
|
|
|
Statement session(con, createSessionQuery);
|
2024-01-11 21:33:46 +00:00
|
|
|
session.bind(res.accessToken.data(), MYSQL_TYPE_STRING);
|
|
|
|
session.bind(res.renewToken.data(), MYSQL_TYPE_STRING);
|
2023-12-23 20:23:38 +00:00
|
|
|
session.bind(testingDevice.data(), MYSQL_TYPE_STRING);
|
|
|
|
session.bind(l.data(), MYSQL_TYPE_STRING);
|
|
|
|
session.execute();
|
|
|
|
|
2024-01-11 21:33:46 +00:00
|
|
|
std::vector<std::vector<std::any>> result = session.fetchResult();
|
|
|
|
if (result.empty())
|
|
|
|
throw std::runtime_error("Error returning ids after insertion in sessions table");
|
|
|
|
|
|
|
|
res.id = std::any_cast<unsigned int>(result[0][0]);
|
|
|
|
res.owner = std::any_cast<unsigned int>(result[0][1]);
|
|
|
|
|
|
|
|
return res;
|
2023-12-22 23:25:20 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
unsigned int DB::MySQL::lastInsertedId () {
|
2023-12-20 22:42:13 +00:00
|
|
|
MYSQL* con = &connection;
|
|
|
|
int result = mysql_query(con, lastIdQuery);
|
|
|
|
|
|
|
|
if (result != 0)
|
|
|
|
throw std::runtime_error(std::string("Error executing last inserted id: ") + mysql_error(con));
|
|
|
|
|
|
|
|
std::unique_ptr<MYSQL_RES, ResDeleter> res(mysql_store_result(con));
|
|
|
|
if (!res)
|
|
|
|
throw std::runtime_error(std::string("Querying last inserted id returned no result: ") + mysql_error(con));
|
|
|
|
|
|
|
|
MYSQL_ROW row = mysql_fetch_row(res.get());
|
|
|
|
if (row)
|
|
|
|
return std::stoi(row[0]);
|
|
|
|
else
|
|
|
|
throw std::runtime_error(std::string("Querying last inserted id returned no rows"));
|
|
|
|
}
|
2024-01-12 23:39:41 +00:00
|
|
|
DB::Session DB::MySQL::findSession (const std::string& accessToken) {
|
2024-01-03 22:20:01 +00:00
|
|
|
std::string a = accessToken;
|
|
|
|
MYSQL* con = &connection;
|
|
|
|
|
|
|
|
Statement session(con, selectSession);
|
|
|
|
session.bind(a.data(), MYSQL_TYPE_STRING);
|
2024-01-09 17:02:56 +00:00
|
|
|
session.execute();
|
2023-12-20 22:42:13 +00:00
|
|
|
|
2024-01-03 22:20:01 +00:00
|
|
|
std::vector<std::vector<std::any>> result = session.fetchResult();
|
|
|
|
if (result.empty())
|
|
|
|
throw NoSession("Couldn't find session with token " + a);
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
return DB::Session(result[0]);
|
2024-01-03 22:20:01 +00:00
|
|
|
}
|
2023-12-20 22:42:13 +00:00
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
std::vector<DB::Asset> DB::MySQL::listAssets (unsigned int owner) {
|
2024-01-11 21:33:46 +00:00
|
|
|
MYSQL* con = &connection;
|
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
Statement st(con, selectAssets);
|
2024-01-11 21:33:46 +00:00
|
|
|
st.bind(&owner, MYSQL_TYPE_LONG, true);
|
|
|
|
st.execute();
|
|
|
|
std::vector<std::vector<std::any>> res = st.fetchResult();
|
|
|
|
|
|
|
|
std::size_t size = res.size();
|
|
|
|
std::vector<DB::Asset> result(size);
|
2024-01-12 23:39:41 +00:00
|
|
|
for (std::size_t i = 0; i < size; ++i)
|
|
|
|
result[i].parse(res[i]);
|
2024-01-11 21:33:46 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-12-20 22:42:13 +00:00
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
DB::Asset DB::MySQL::addAsset(const Asset& asset) {
|
|
|
|
MYSQL* con = &connection;
|
|
|
|
Asset result = asset;
|
|
|
|
|
|
|
|
Statement session(con, insertAsset);
|
|
|
|
session.bind(&result.owner, MYSQL_TYPE_LONG, true);
|
|
|
|
session.bind(&result.currency, MYSQL_TYPE_LONG, true);
|
|
|
|
session.bind(result.title.data(), MYSQL_TYPE_STRING);
|
|
|
|
session.bind(result.icon.data(), MYSQL_TYPE_STRING);
|
|
|
|
session.bind(&result.archived, MYSQL_TYPE_TINY);
|
|
|
|
session.execute();
|
|
|
|
|
|
|
|
result.id = lastInsertedId();
|
|
|
|
|
|
|
|
return asset;
|
|
|
|
}
|