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
|
@ -6,7 +6,8 @@
|
|||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
namespace DB {
|
||||
struct Session {
|
||||
|
@ -16,6 +17,18 @@ struct Session {
|
|||
std::string renewToken;
|
||||
};
|
||||
|
||||
struct Asset {
|
||||
unsigned int id;
|
||||
unsigned int owner;
|
||||
unsigned int currency;
|
||||
std::string title;
|
||||
std::string icon;
|
||||
// `color` INTEGER UNSIGNED DEFAULT 0,
|
||||
// `balance` DECIMAL (20, 5) DEFAULT 0,
|
||||
// `type` INTEGER UNSIGNED NOT NULL,
|
||||
bool archived;
|
||||
};
|
||||
|
||||
class Interface {
|
||||
public:
|
||||
enum class Type {
|
||||
|
@ -46,8 +59,9 @@ public:
|
|||
|
||||
virtual unsigned int registerAccount(const std::string& login, const std::string& hash) = 0;
|
||||
virtual std::string getAccountHash(const std::string& login) = 0;
|
||||
virtual unsigned int createSession(const std::string& login, const std::string& access, const std::string& renew) = 0;
|
||||
virtual Session createSession(const std::string& login, const std::string& access, const std::string& renew) = 0;
|
||||
virtual Session findSession(const std::string& accessToken) = 0;
|
||||
virtual std::vector<Asset> listAssets(unsigned int owner) = 0;
|
||||
|
||||
protected:
|
||||
Interface(Type type);
|
||||
|
|
|
@ -33,7 +33,7 @@ CREATE TABLE IF NOT EXISTS roleBindings (
|
|||
|
||||
--creating sessings table
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
`id` INTEGER AUTO_INCREMENT PRIMARY KEY,
|
||||
`id` INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
`owner` INTEGER UNSIGNED NOT NULL,
|
||||
`started` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
|
||||
`latest` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
|
||||
|
@ -45,13 +45,90 @@ CREATE TABLE IF NOT EXISTS sessions (
|
|||
FOREIGN KEY (owner) REFERENCES accounts(id)
|
||||
);
|
||||
|
||||
--creating currencies table
|
||||
CREATE TABLE IF NOT EXISTS currencies (
|
||||
`id` INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
`code` VARCHAR(16) NOT NULL UNIQUE,
|
||||
`title` VARCHAR(256),
|
||||
`manual` BOOLEAN NOT NULL,
|
||||
`added` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
|
||||
`type` INTEGER UNSIGNED NOT NULL,
|
||||
`value` DECIMAL (20, 5) NOT NULL,
|
||||
`source` TEXT,
|
||||
`description` TEXT,
|
||||
|
||||
INDEX manual_idx (manual)
|
||||
);
|
||||
|
||||
--creating assests table
|
||||
CREATE TABLE IF NOT EXISTS assets (
|
||||
`id` INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
`owner` INTEGER UNSIGNED NOT NULL,
|
||||
`currency` INTEGER UNSIGNED NOT NULL,
|
||||
`title` VARCHAR(256),
|
||||
`icon` VARCHAR(256),
|
||||
`color` INTEGER UNSIGNED DEFAULT 0,
|
||||
`balance` DECIMAL (20, 5) DEFAULT 0,
|
||||
`type` INTEGER UNSIGNED NOT NULL,
|
||||
`archived` BOOLEAN DEFAULT FALSE,
|
||||
|
||||
INDEX owner_idx (owner),
|
||||
INDEX archived_idx (archived),
|
||||
|
||||
FOREIGN KEY (owner) REFERENCES accounts(id),
|
||||
FOREIGN KEY (currency) REFERENCES currencies(id)
|
||||
);
|
||||
|
||||
--creating parties table
|
||||
CREATE TABLE IF NOT EXISTS parties (
|
||||
`id` INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
`title` VARCHAR(256) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
--creating transactions table
|
||||
CREATE TABLE IF NOT EXISTS transactions (
|
||||
`id` INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
`initiator` INTEGER UNSIGNED NOT NULL,
|
||||
`type` INTEGER UNSIGNED NOT NULL,
|
||||
`asset` INTEGER UNSIGNED NOT NULL,
|
||||
`parent` INTEGER UNSIGNED,
|
||||
`value` DECIMAL (20, 5) NOT NULL,
|
||||
`state` INTEGER UNSIGNED DEFAULT 0,
|
||||
`modified` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
|
||||
`performed` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
|
||||
`party` INTEGER UNSIGNED,
|
||||
`notes` TEXT,
|
||||
|
||||
INDEX initiator_idx (initiator),
|
||||
INDEX parent_idx (parent),
|
||||
INDEX asset_idx (asset),
|
||||
INDEX performed_idx (performed),
|
||||
INDEX modified_idx (modified),
|
||||
INDEX party_idx (party),
|
||||
|
||||
FOREIGN KEY (initiator) REFERENCES accounts(id),
|
||||
FOREIGN KEY (asset) REFERENCES assets(id),
|
||||
FOREIGN KEY (parent) REFERENCES transactions(id),
|
||||
FOREIGN KEY (party) REFERENCES parties(id)
|
||||
);
|
||||
|
||||
--creating defailt roles
|
||||
INSERT IGNORE INTO roles (`name`)
|
||||
INSERT IGNORE INTO
|
||||
roles (`name`)
|
||||
VALUES ('root'),
|
||||
('default');
|
||||
|
||||
--inserting initial version
|
||||
INSERT INTO system (`key`, `value`) VALUES ('version', '0');
|
||||
INSERT IGNORE INTO
|
||||
system (`key`, `value`)
|
||||
VALUES ('version', '0');
|
||||
|
||||
--recording initial time
|
||||
INSERT INTO system (`key`, `value`) VALUES ('created', UTC_TIMESTAMP());
|
||||
INSERT IGNORE INTO
|
||||
system (`key`, `value`)
|
||||
VALUES ('created', UTC_TIMESTAMP());
|
||||
|
||||
--creating default currencies
|
||||
INSERT IGNORE INTO
|
||||
currencies (`code`, `title`, `manual`, `description`, `type`, `value`)
|
||||
VALUES ('USD', 'United States Dollar', TRUE, 'Base currency', 0, 1);
|
||||
|
|
|
@ -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