some ideas about database structure, began assets fetching request
This commit is contained in:
parent
a1ab1339e3
commit
d33ec5def8
13 changed files with 251 additions and 45 deletions
|
@ -19,8 +19,10 @@ 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 = ?";
|
||||
" 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* selectAssets = "SELECT id, owner, currency, title, icon, archived FROM assets where owner = ?";
|
||||
|
||||
static const std::filesystem::path buildSQLPath = "database";
|
||||
|
||||
|
@ -266,20 +268,30 @@ std::string DB::MySQL::getAccountHash(const std::string& login) {
|
|||
return std::any_cast<const std::string&>(result[0][0]);
|
||||
}
|
||||
|
||||
unsigned int DB::MySQL::createSession(const std::string& login, const std::string& access, const std::string& renew) {
|
||||
std::string l = login, a = access, r = 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;
|
||||
res.renewToken = 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(res.accessToken.data(), MYSQL_TYPE_STRING);
|
||||
session.bind(res.renewToken.data(), MYSQL_TYPE_STRING);
|
||||
session.bind(testingDevice.data(), MYSQL_TYPE_STRING);
|
||||
session.bind(l.data(), MYSQL_TYPE_STRING);
|
||||
session.execute();
|
||||
|
||||
return lastInsertedId();
|
||||
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;
|
||||
}
|
||||
|
||||
unsigned int DB::MySQL::lastInsertedId() {
|
||||
|
@ -320,4 +332,27 @@ DB::Session DB::MySQL::findSession(const std::string& accessToken) {
|
|||
return res;
|
||||
}
|
||||
|
||||
std::vector<DB::Asset> DB::MySQL::listAssets(unsigned int owner) {
|
||||
MYSQL* con = &connection;
|
||||
|
||||
Statement st(con, selectSession);
|
||||
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
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,27 +19,28 @@ class MySQL : public Interface {
|
|||
|
||||
|
||||
public:
|
||||
MySQL();
|
||||
~MySQL() override;
|
||||
MySQL ();
|
||||
~MySQL () override;
|
||||
|
||||
void connect(const std::string& path) override;
|
||||
void disconnect() override;
|
||||
void setCredentials(const std::string& login, const std::string& password) override;
|
||||
void setDatabase(const std::string& database) override;
|
||||
void connect (const std::string& path) override;
|
||||
void disconnect () override;
|
||||
void setCredentials (const std::string& login, const std::string& password) override;
|
||||
void setDatabase (const std::string& database) override;
|
||||
|
||||
void migrate(uint8_t targetVersion) override;
|
||||
uint8_t getVersion() override;
|
||||
void setVersion(uint8_t version) override;
|
||||
void migrate (uint8_t targetVersion) override;
|
||||
uint8_t getVersion () override;
|
||||
void setVersion (uint8_t version) override;
|
||||
|
||||
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;
|
||||
Session findSession(const std::string& accessToken) override;
|
||||
unsigned int registerAccount (const std::string& login, const std::string& hash) override;
|
||||
std::string getAccountHash (const std::string& login) override;
|
||||
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;
|
||||
|
||||
private:
|
||||
void executeFile(const std::filesystem::path& relativePath);
|
||||
static std::optional<std::string> getComment(std::string& string);
|
||||
unsigned int lastInsertedId();
|
||||
void executeFile (const std::filesystem::path& relativePath);
|
||||
static std::optional<std::string> getComment (std::string& string);
|
||||
unsigned int lastInsertedId ();
|
||||
|
||||
protected:
|
||||
MYSQL connection;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue