currencies request
This commit is contained in:
parent
a2c2c2a883
commit
db37abacd2
24 changed files with 243 additions and 57 deletions
|
@ -22,10 +22,13 @@ constexpr const char* createSessionQuery = "INSERT INTO sessions (`owner`, `acce
|
|||
" SELECT accounts.id, ?, ?, true, ? FROM accounts WHERE accounts.login = ?"
|
||||
" RETURNING id, owner";
|
||||
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`, `type`)"
|
||||
" VALUES (?, ?, ?, ?, ?, 1)";
|
||||
constexpr const char* selectAssets = "SELECT id, owner, currency, title, icon, color, archived FROM assets where owner = ?";
|
||||
constexpr const char* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `color`, `archived`, `type`)"
|
||||
" VALUES (?, ?, ?, ?, ?, ?, 1)";
|
||||
constexpr const char* removeAsset = "DELETE FROM assets where `id` = ? AND `owner` = ?";
|
||||
constexpr const char* selectUsedCurrencies = "SELECT c.id, c.code, c.title, c.manual, c.icon FROM currencies c"
|
||||
"JOIN assets a ON c.id = a.currency"
|
||||
"WHERE a.owner = ?";
|
||||
|
||||
static const std::filesystem::path buildSQLPath = "database";
|
||||
|
||||
|
@ -224,7 +227,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) {
|
||||
uint32_t DB::MySQL::registerAccount (const std::string& login, const std::string& hash) {
|
||||
//TODO validate filed lengths!
|
||||
MYSQL* con = &connection;
|
||||
MySQL::Transaction txn(con);
|
||||
|
@ -241,7 +244,7 @@ unsigned int DB::MySQL::registerAccount (const std::string& login, const std::st
|
|||
throw DuplicateLogin(dup.what());
|
||||
}
|
||||
|
||||
unsigned int id = lastInsertedId();
|
||||
uint32_t id = lastInsertedId();
|
||||
static std::string defaultRole("default");
|
||||
|
||||
Statement addRole(con, assignRoleQuery);
|
||||
|
@ -291,13 +294,13 @@ DB::Session DB::MySQL::createSession (const std::string& login, const std::strin
|
|||
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]);
|
||||
res.id = std::any_cast<uint32_t>(result[0][0]);
|
||||
res.owner = std::any_cast<uint32_t>(result[0][1]);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
unsigned int DB::MySQL::lastInsertedId () {
|
||||
uint32_t DB::MySQL::lastInsertedId () {
|
||||
MYSQL* con = &connection;
|
||||
int result = mysql_query(con, lastIdQuery);
|
||||
|
||||
|
@ -329,7 +332,7 @@ DB::Session DB::MySQL::findSession (const std::string& accessToken) {
|
|||
return DB::Session(result[0]);
|
||||
}
|
||||
|
||||
std::vector<DB::Asset> DB::MySQL::listAssets (unsigned int owner) {
|
||||
std::vector<DB::Asset> DB::MySQL::listAssets (uint32_t owner) {
|
||||
MYSQL* con = &connection;
|
||||
|
||||
Statement st(con, selectAssets);
|
||||
|
@ -354,6 +357,7 @@ DB::Asset DB::MySQL::addAsset(const Asset& asset) {
|
|||
add.bind(&result.currency, MYSQL_TYPE_LONG, true);
|
||||
add.bind(result.title.data(), MYSQL_TYPE_STRING);
|
||||
add.bind(result.icon.data(), MYSQL_TYPE_STRING);
|
||||
add.bind(&result.color, MYSQL_TYPE_LONG, true);
|
||||
add.bind(&result.archived, MYSQL_TYPE_TINY);
|
||||
add.execute();
|
||||
|
||||
|
@ -362,10 +366,8 @@ DB::Asset DB::MySQL::addAsset(const Asset& asset) {
|
|||
return result;
|
||||
}
|
||||
|
||||
bool DB::MySQL::deleteAsset(unsigned int assetId, unsigned int actorId) {
|
||||
MYSQL* con = &connection;
|
||||
|
||||
Statement del(con, removeAsset);
|
||||
bool DB::MySQL::deleteAsset(uint32_t assetId, uint32_t actorId) {
|
||||
Statement del(&connection, removeAsset);
|
||||
del.bind(&assetId, MYSQL_TYPE_LONG, true);
|
||||
del.bind(&actorId, MYSQL_TYPE_LONG, true);
|
||||
del.execute();
|
||||
|
@ -375,3 +377,18 @@ bool DB::MySQL::deleteAsset(unsigned int assetId, unsigned int actorId) {
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<DB::Currency> DB::MySQL::listUsedCurrencies(uint32_t owner) {
|
||||
Statement list(&connection, removeAsset);
|
||||
list.bind(&owner, MYSQL_TYPE_LONG, true);
|
||||
list.execute();
|
||||
|
||||
std::vector<std::vector<std::any>> res = list.fetchResult();
|
||||
|
||||
std::size_t size = res.size();
|
||||
std::vector<DB::Currency> result(size);
|
||||
for (std::size_t i = 0; i < size; ++i)
|
||||
result[i].parse(res[i]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ class MySQL : public Interface {
|
|||
class Statement;
|
||||
class Transaction;
|
||||
|
||||
|
||||
public:
|
||||
MySQL ();
|
||||
~MySQL () override;
|
||||
|
@ -31,18 +30,19 @@ public:
|
|||
uint8_t getVersion () override;
|
||||
void setVersion (uint8_t version) override;
|
||||
|
||||
unsigned int registerAccount (const std::string& login, const std::string& hash) override;
|
||||
uint32_t 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;
|
||||
std::vector<Asset> listAssets (uint32_t owner) override;
|
||||
Asset addAsset (const Asset& asset) override;
|
||||
bool deleteAsset(unsigned int assetId, unsigned int actorId) override;
|
||||
bool deleteAsset(uint32_t assetId, uint32_t actorId) override;
|
||||
std::vector<Currency> listUsedCurrencies(uint32_t owner) override;
|
||||
|
||||
private:
|
||||
void executeFile (const std::filesystem::path& relativePath);
|
||||
static std::optional<std::string> getComment (std::string& string);
|
||||
unsigned int lastInsertedId ();
|
||||
uint32_t lastInsertedId ();
|
||||
|
||||
protected:
|
||||
MYSQL connection;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue