First ideas over transaction

This commit is contained in:
Blue 2024-04-10 20:09:45 -03:00
parent 4914a467e5
commit 973deaefd9
Signed by: blue
GPG Key ID: 9B203B252A63EE38
6 changed files with 133 additions and 0 deletions

View File

@ -12,6 +12,7 @@
#include "schema/session.h"
#include "schema/asset.h"
#include "schema/currency.h"
#include "schema/transaction.h"
namespace DB {
class Interface {
@ -44,14 +45,21 @@ public:
virtual uint32_t registerAccount(const std::string& login, const std::string& hash) = 0;
virtual std::string getAccountHash(const std::string& login) = 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(uint32_t owner) = 0;
virtual Asset addAsset(const Asset& asset) = 0;
virtual void updateAsset(const Asset& asset) = 0;
virtual bool deleteAsset(uint32_t assetId, uint32_t actorId) = 0;
virtual std::vector<Currency> listUsedCurrencies(uint32_t owner) = 0;
virtual DB::Transaction addTransaction(const DB::Transaction& transaction) = 0;
virtual void updateTransaction(const DB::Transaction& transaction) = 0;
virtual std::vector<DB::Transaction> listTransactions(uint32_t owner) = 0;
protected:
Interface(Type type);

View File

@ -31,6 +31,10 @@ constexpr const char* removeAsset = "DELETE FROM assets where `id` = ? AND `owne
constexpr const char* selectUsedCurrencies = "SELECT DISTINCT c.id, c.code, c.title, c.manual, c.icon FROM currencies c"
" JOIN assets a ON c.id = a.currency"
" WHERE a.owner = ?";
constexpr const char* addTransactionQuery = "INSERT INTO transactions"
" (`initiator`, `type`, `asset`, `parent`, `value`, `performed`)"
" VALUES (?, 1, ?, ?, ?, ?)";
static const std::filesystem::path buildSQLPath = "database";
@ -409,3 +413,30 @@ std::vector<DB::Currency> DB::MySQL::listUsedCurrencies(uint32_t owner) {
return result;
}
DB::Transaction DB::MySQL::addTransaction(const DB::Transaction& transaction) {
MYSQL* con = &connection;
DB::Transaction result = transaction;
std::string value = std::to_string(result.value);
Statement add(con, addTransactionQuery);
add.bind(&result.initiator, MYSQL_TYPE_LONG, true);
add.bind(&result.asset, MYSQL_TYPE_LONG, true);
add.bind(&result.parent, MYSQL_TYPE_LONG, true);
add.bind(value.data(), MYSQL_TYPE_STRING);
add.bind(&result.performed, MYSQL_TYPE_LONG, true);
add.execute();
result.id = lastInsertedId();
//todo retreive timestamp and actual value which could have changed after insertion
return result;
}
void DB::MySQL::updateTransaction(const DB::Transaction& transaction) {
}
std::vector<DB::Transaction> DB::MySQL::listTransactions(uint32_t owner) {
return std::vector<DB::Transaction>();
}

View File

@ -32,14 +32,21 @@ public:
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 (uint32_t owner) override;
Asset addAsset (const Asset& asset) override;
void updateAsset (const Asset& asset) override;
bool deleteAsset(uint32_t assetId, uint32_t actorId) override;
std::vector<Currency> listUsedCurrencies(uint32_t owner) override;
DB::Transaction addTransaction(const DB::Transaction& transaction) override;
void updateTransaction(const DB::Transaction& transaction) override;
std::vector<DB::Transaction> listTransactions(uint32_t owner) override;
private:
void executeFile (const std::filesystem::path& relativePath);
static std::optional<std::string> getComment (std::string& string);

View File

@ -5,12 +5,14 @@ set(HEADERS
session.h
asset.h
currency.h
transaction.h
)
set(SOURCES
session.cpp
asset.cpp
currency.cpp
transaction.cpp
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

View File

@ -0,0 +1,50 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "transaction.h"
DB::Transaction::Transaction():
id(0),
initiator(0),
asset(0),
parent(0),
value(0),
modified(0),
performed(0),
notes()
{}
DB::Transaction::Transaction(const std::vector<std::any>& vec):
id(std::any_cast<uint32_t>(vec[0])),
initiator(std::any_cast<uint32_t>(vec[1])),
asset(std::any_cast<uint32_t>(vec[2])),
parent(std::any_cast<uint32_t>(vec[3])),
value(std::any_cast<double>(vec[4])),
modified(std::any_cast<uint32_t>(vec[5])),
performed(std::any_cast<uint32_t>(vec[6])),
notes()
{}
void DB::Transaction::parse(const std::vector<std::any>& vec) {
id = std::any_cast<uint32_t>(vec[0]);
initiator = std::any_cast<uint32_t>(vec[1]);
asset = std::any_cast<uint32_t>(vec[2]);
parent = std::any_cast<uint32_t>(vec[3]);
value = std::any_cast<double>(vec[4]);
modified = std::any_cast<uint32_t>(vec[5]);
performed = std::any_cast<uint32_t>(vec[6]);
}
nlohmann::json DB::Transaction::toJSON() const {
nlohmann::json result = nlohmann::json::object();
result["id"] = id;
result["initiator"] = initiator;
result["asset"] = asset;
result["parent"] = parent;
result["value"] = value;
result["modified"] = modified;
result["performed"] = performed;
return result;
}

View File

@ -0,0 +1,35 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <string>
#include <vector>
#include <any>
#include <cstdint>
#include <nlohmann/json.hpp>
namespace DB {
class Transaction {
public:
Transaction ();
Transaction (const std::vector<std::any>& vec);
void parse (const std::vector<std::any>& vec);
nlohmann::json toJSON () const;
public:
uint32_t id;
uint32_t initiator;
// `type` INTEGER UNSIGNED NOT NULL,
uint32_t asset;
uint32_t parent;
double value;
// `state` INTEGER UNSIGNED DEFAULT 0,
uint32_t modified;
uint32_t performed;
// `party` INTEGER UNSIGNED,
std::string notes;
};
}