schema directory for all datastructures of database, add-list assets requests, not tested

This commit is contained in:
Blue 2024-01-12 20:39:41 -03:00
parent d33ec5def8
commit 4df8d4319e
Signed by: blue
GPG key ID: 9B203B252A63EE38
15 changed files with 306 additions and 62 deletions

View file

@ -21,12 +21,14 @@ constexpr const char* selectHash = "SELECT password FROM accounts where login =
constexpr const char* createSessionQuery = "INSERT INTO sessions (`owner`, `access`, `renew`, `persist`, `device`)"
" SELECT accounts.id, ?, ?, true, ? FROM accounts WHERE accounts.login = ?"
" RETURNING id, owner";
constexpr const char* selectSession = "SELECT id, owner, renew FROM sessions where access = ?";
constexpr const char* selectSession = "SELECT id, owner, access, renew FROM sessions where access = ?";
constexpr const char* selectAssets = "SELECT id, owner, currency, title, icon, archived FROM assets where owner = ?";
constexpr const char* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `archived`)"
" VALUES (?, ?, ?, ?, ?)";
static const std::filesystem::path buildSQLPath = "database";
DB::MySQL::MySQL():
DB::MySQL::MySQL ():
Interface(Type::mysql),
connection(),
login(),
@ -40,7 +42,7 @@ DB::MySQL::~MySQL() {
mysql_close(&connection);
}
void DB::MySQL::connect(const std::string& path) {
void DB::MySQL::connect (const std::string& path) {
if (state != State::disconnected)
return;
@ -62,7 +64,7 @@ void DB::MySQL::connect(const std::string& path) {
state = State::connected;
}
void DB::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;
@ -84,7 +86,7 @@ void DB::MySQL::setCredentials(const std::string& login, const std::string& pass
throw std::runtime_error(std::string("Error changing credetials: ") + mysql_error(con));
}
void DB::MySQL::setDatabase(const std::string& database) {
void DB::MySQL::setDatabase (const std::string& database) {
if (MySQL::database == database)
return;
@ -100,7 +102,7 @@ void DB::MySQL::setDatabase(const std::string& database) {
throw std::runtime_error(std::string("Error changing db: ") + mysql_error(con));
}
void DB::MySQL::disconnect() {
void DB::MySQL::disconnect () {
if (state == State::disconnected)
return;
@ -109,7 +111,7 @@ void DB::MySQL::disconnect() {
mysql_init(con); //this is ridiculous!
}
void DB::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))
@ -141,7 +143,7 @@ void DB::MySQL::executeFile(const std::filesystem::path& relativePath) {
}
}
uint8_t DB::MySQL::getVersion() {
uint8_t DB::MySQL::getVersion () {
MYSQL* con = &connection;
int result = mysql_query(con, versionQuery);
@ -164,14 +166,14 @@ uint8_t DB::MySQL::getVersion() {
return 0;
}
void DB::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 DB::MySQL::migrate(uint8_t targetVersion) {
void DB::MySQL::migrate (uint8_t targetVersion) {
uint8_t currentVersion = getVersion();
while (currentVersion < targetVersion) {
@ -193,7 +195,7 @@ void DB::MySQL::migrate(uint8_t targetVersion) {
std::cout << "Database is now on actual version " << std::to_string(targetVersion) << std::endl;
}
std::optional<std::string> DB::MySQL::getComment(std::string& string) {
std::optional<std::string> DB::MySQL::getComment (std::string& string) {
ltrim(string);
if (string.length() < 2)
return std::nullopt;
@ -221,7 +223,7 @@ std::optional<std::string> DB::MySQL::getComment(std::string& string) {
return std::nullopt;
}
unsigned int DB::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);
@ -250,7 +252,7 @@ unsigned int DB::MySQL::registerAccount(const std::string& login, const std::str
return id;
}
std::string DB::MySQL::getAccountHash(const std::string& login) {
std::string DB::MySQL::getAccountHash (const std::string& login) {
std::string l = login;
MYSQL* con = &connection;
@ -268,7 +270,7 @@ std::string DB::MySQL::getAccountHash(const std::string& login) {
return std::any_cast<const std::string&>(result[0][0]);
}
DB::Session DB::MySQL::createSession(const std::string& login, const std::string& access, const std::string& renew) {
DB::Session DB::MySQL::createSession (const std::string& login, const std::string& access, const std::string& renew) {
std::string l = login;
DB::Session res;
res.accessToken = access;
@ -294,7 +296,7 @@ DB::Session DB::MySQL::createSession(const std::string& login, const std::string
return res;
}
unsigned int DB::MySQL::lastInsertedId() {
unsigned int DB::MySQL::lastInsertedId () {
MYSQL* con = &connection;
int result = mysql_query(con, lastIdQuery);
@ -311,7 +313,7 @@ unsigned int DB::MySQL::lastInsertedId() {
else
throw std::runtime_error(std::string("Querying last inserted id returned no rows"));
}
DB::Session DB::MySQL::findSession(const std::string& accessToken) {
DB::Session DB::MySQL::findSession (const std::string& accessToken) {
std::string a = accessToken;
MYSQL* con = &connection;
@ -323,36 +325,38 @@ DB::Session DB::MySQL::findSession(const std::string& accessToken) {
if (result.empty())
throw NoSession("Couldn't find session with token " + a);
DB::Session res;
res.id = std::any_cast<unsigned int>(result[0][0]);
res.owner = std::any_cast<unsigned int>(result[0][1]);
res.renewToken = std::any_cast<const std::string&>(result[0][2]);
res.accessToken = a;
return res;
return DB::Session(result[0]);
}
std::vector<DB::Asset> DB::MySQL::listAssets(unsigned int owner) {
std::vector<DB::Asset> DB::MySQL::listAssets (unsigned int owner) {
MYSQL* con = &connection;
Statement st(con, selectSession);
Statement st(con, selectAssets);
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);
for (std::size_t i = 0; i < size; ++i) {
const std::vector<std::any>& proto = res[i];
DB::Asset& asset = result[i];
asset.id = std::any_cast<unsigned int>(proto[0]);
asset.owner = std::any_cast<unsigned int>(proto[1]);
asset.currency = std::any_cast<unsigned int>(proto[2]);
asset.title = std::any_cast<const std::string&>(proto[3]);
asset.icon = std::any_cast<const std::string&>(proto[4]);
asset.archived = std::any_cast<bool>(proto[5]); //TODO
}
for (std::size_t i = 0; i < size; ++i)
result[i].parse(res[i]);
return result;
}
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;
}

View file

@ -36,6 +36,7 @@ public:
Session createSession (const std::string& login, const std::string& access, const std::string& renew) override;
Session findSession (const std::string& accessToken) override;
std::vector<Asset> listAssets (unsigned int owner) override;
Asset addAsset (const Asset& asset) override;
private:
void executeFile (const std::filesystem::path& relativePath);