session creation
This commit is contained in:
parent
534c282226
commit
4b87b560ac
12 changed files with 152 additions and 25 deletions
|
@ -18,6 +18,8 @@ constexpr const char* registerQuery = "INSERT INTO accounts (`login`, `type`, `p
|
|||
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 = ?";
|
||||
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 = ?";
|
||||
|
||||
static const std::filesystem::path buildSQLPath = "database";
|
||||
|
||||
|
@ -253,14 +255,30 @@ std::string MySQL::getAccountHash(const std::string& login) {
|
|||
getHash.bind(l.data(), MYSQL_TYPE_STRING);
|
||||
getHash.execute();
|
||||
|
||||
std::vector<std::vector<std::string>> result = getHash.fetchResult();
|
||||
std::vector<std::vector<std::any>> result = getHash.fetchResult();
|
||||
if (result.empty())
|
||||
throw NoLogin("Couldn't find login " + l);
|
||||
|
||||
if (result[0].empty())
|
||||
throw std::runtime_error("Error with the query \"selectHash\"");
|
||||
|
||||
return result[0][0];
|
||||
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) {
|
||||
std::string l = login, a = access, r = renew;
|
||||
static std::string testingDevice("Testing...");
|
||||
|
||||
MYSQL* con = &connection;
|
||||
|
||||
Statement session(con, createSessionQuery);
|
||||
session.bind(a.data(), MYSQL_TYPE_STRING);
|
||||
session.bind(r.data(), MYSQL_TYPE_STRING);
|
||||
session.bind(testingDevice.data(), MYSQL_TYPE_STRING);
|
||||
session.bind(l.data(), MYSQL_TYPE_STRING);
|
||||
session.execute();
|
||||
|
||||
return lastInsertedId();
|
||||
}
|
||||
|
||||
unsigned int MySQL::lastInsertedId() {
|
||||
|
|
|
@ -32,6 +32,7 @@ public:
|
|||
|
||||
unsigned int registerAccount(const std::string& login, const std::string& hash) override;
|
||||
std::string getAccountHash(const std::string& login) override;
|
||||
unsigned int createSession(const std::string& login, const std::string& access, const std::string& renew) override;
|
||||
|
||||
private:
|
||||
void executeFile(const std::filesystem::path& relativePath);
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
|
||||
#include "statement.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "mysqld_error.h"
|
||||
|
||||
#include "database/exceptions.h"
|
||||
|
@ -66,7 +64,7 @@ void MySQL::Statement::execute() {
|
|||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<std::string>> MySQL::Statement::fetchResult() {
|
||||
std::vector<std::vector<std::any>> 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
|
||||
|
@ -78,9 +76,9 @@ std::vector<std::vector<std::string>> MySQL::Statement::fetchResult() {
|
|||
std::unique_ptr<MYSQL_RES, ResDeleter> mt(meta);
|
||||
unsigned int numColumns = mysql_num_fields(meta);
|
||||
MYSQL_BIND bind[numColumns];
|
||||
memset(bind, 0, sizeof(bind));
|
||||
std::memset(bind, 0, sizeof(bind));
|
||||
|
||||
std::vector<std::string> line(numColumns);
|
||||
std::vector<std::any> line(numColumns);
|
||||
std::vector<long unsigned int> lengths(numColumns);
|
||||
for (unsigned int i = 0; i < numColumns; ++i) {
|
||||
MYSQL_FIELD *field = mysql_fetch_field_direct(meta, i);
|
||||
|
@ -88,14 +86,32 @@ std::vector<std::vector<std::string>> MySQL::Statement::fetchResult() {
|
|||
switch (field->type) {
|
||||
case MYSQL_TYPE_STRING:
|
||||
case MYSQL_TYPE_VAR_STRING:
|
||||
case MYSQL_TYPE_VARCHAR:
|
||||
case MYSQL_TYPE_VARCHAR: {
|
||||
line[i] = std::string();
|
||||
std::string& str = std::any_cast<std::string&>(line[i]);
|
||||
str.resize(field->length);
|
||||
bind[i].buffer = str.data();
|
||||
} break;
|
||||
case MYSQL_TYPE_TINY:
|
||||
line[i] = uint8_t{0};
|
||||
bind[i].buffer = &std::any_cast<std::string&>(line[i]);
|
||||
break;
|
||||
case MYSQL_TYPE_SHORT:
|
||||
line[i] = uint16_t{0};
|
||||
bind[i].buffer = &std::any_cast<std::string&>(line[i]);
|
||||
break;
|
||||
case MYSQL_TYPE_LONG:
|
||||
line[i] = uint32_t{0};
|
||||
bind[i].buffer = &std::any_cast<std::string&>(line[i]);
|
||||
break;
|
||||
case MYSQL_TYPE_LONGLONG:
|
||||
line[i] = uint64_t{0};
|
||||
bind[i].buffer = &std::any_cast<std::string&>(line[i]);
|
||||
break;
|
||||
default:
|
||||
throw std::runtime_error("Unsupported data fetching statement result " + std::to_string(field->type));
|
||||
}
|
||||
line[i].resize(field->length);
|
||||
bind[i].buffer_type = field->type;
|
||||
bind[i].buffer = line[i].data();
|
||||
bind[i].buffer_length = field->length;
|
||||
bind[i].length = &lengths[i];
|
||||
}
|
||||
|
@ -103,11 +119,32 @@ std::vector<std::vector<std::string>> MySQL::Statement::fetchResult() {
|
|||
if (mysql_stmt_bind_result(raw, bind) != 0)
|
||||
throw std::runtime_error(std::string("Error binding on fetching statement result: ") + mysql_stmt_error(raw));
|
||||
|
||||
std::vector<std::vector<std::string>> result;
|
||||
std::vector<std::vector<std::any>> result;
|
||||
while (mysql_stmt_fetch(raw) == 0) {
|
||||
std::vector<std::string>& row = result.emplace_back(numColumns);
|
||||
for (unsigned int i = 0; i < numColumns; ++i)
|
||||
row[i] = std::string(line[i].data(), lengths[i]);
|
||||
std::vector<std::any>& row = result.emplace_back(numColumns);
|
||||
for (unsigned int i = 0; i < numColumns; ++i) {
|
||||
switch (bind[i].buffer_type) {
|
||||
case MYSQL_TYPE_STRING:
|
||||
case MYSQL_TYPE_VAR_STRING:
|
||||
case MYSQL_TYPE_VARCHAR: {
|
||||
row[i] = std::string(std::any_cast<const std::string&>(line[i]).data(), lengths[i]);
|
||||
} break;
|
||||
case MYSQL_TYPE_TINY:
|
||||
row[i] = std::any_cast<uint8_t>(line[i]);
|
||||
break;
|
||||
case MYSQL_TYPE_SHORT:
|
||||
row[i] = std::any_cast<uint16_t>(line[i]);
|
||||
break;
|
||||
case MYSQL_TYPE_LONG:
|
||||
row[i] = std::any_cast<uint32_t>(line[i]);
|
||||
break;
|
||||
case MYSQL_TYPE_LONGLONG:
|
||||
row[i] = std::any_cast<uint64_t>(line[i]);
|
||||
break;
|
||||
default:
|
||||
throw std::runtime_error("Unsupported data fetching statement result " + std::to_string(bind[i].buffer_type));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -3,7 +3,10 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <any>
|
||||
|
||||
#include "mysql.h"
|
||||
|
||||
|
@ -18,7 +21,7 @@ public:
|
|||
|
||||
void bind(void* value, enum_field_types type, bool usigned = false);
|
||||
void execute();
|
||||
std::vector<std::vector<std::string>> fetchResult();
|
||||
std::vector<std::vector<std::any>> fetchResult();
|
||||
|
||||
private:
|
||||
std::unique_ptr<MYSQL_STMT, STMTDeleter> stmt;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue