Обновление слоя хранилища
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include <pqxx/pqxx>
|
||||
|
||||
struct ConnectionData {
|
||||
std::string database, username, password, host, port;
|
||||
};
|
||||
|
||||
class PostgreSqlStorage {
|
||||
public:
|
||||
static PostgreSqlStorage& Get();
|
||||
static void Connect(const ConnectionData& connection_data);
|
||||
pqxx::result Do(std::string_view query) const;
|
||||
private:
|
||||
std::unique_ptr<pqxx::connection> connection_;
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include <pqxx/pqxx>
|
||||
|
||||
#include <storage/transaction.h>
|
||||
|
||||
namespace storage {
|
||||
/// @brief Данные подключения
|
||||
struct ConnectionData {
|
||||
std::string database, username, password, host, port;
|
||||
};
|
||||
|
||||
class Storage {
|
||||
public:
|
||||
static Storage& Get();
|
||||
|
||||
static void Connect(const ConnectionData& connection_data);
|
||||
|
||||
friend class Transaction;
|
||||
|
||||
private:
|
||||
std::unique_ptr<pqxx::connection> connection_;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <pqxx/pqxx>
|
||||
|
||||
namespace storage {
|
||||
class Transaction {
|
||||
public:
|
||||
~Transaction();
|
||||
|
||||
Transaction(Transaction&&) = default;
|
||||
Transaction& operator=(Transaction&&) = default;
|
||||
|
||||
static Transaction Create();
|
||||
|
||||
pqxx::result Do(std::string_view query) const;
|
||||
|
||||
private:
|
||||
Transaction() = default;
|
||||
|
||||
private:
|
||||
std::unique_ptr<pqxx::work> transaction_;
|
||||
};
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
#include <sstream>
|
||||
|
||||
#include <storage/postgresql/postgresql_storage.h>
|
||||
|
||||
PostgreSqlStorage& PostgreSqlStorage::Get() {
|
||||
static PostgreSqlStorage storage;
|
||||
return storage;
|
||||
}
|
||||
|
||||
void PostgreSqlStorage::Connect(const ConnectionData& connection_data) {
|
||||
std::ostringstream options;
|
||||
options << "dbname = " << connection_data.database
|
||||
<< " user = " << connection_data.username
|
||||
<< " password = " << connection_data.password
|
||||
<< " host = " << connection_data.host
|
||||
<< " port = " << connection_data.port;
|
||||
|
||||
Get().connection_ = std::make_unique<pqxx::connection>(options.str());
|
||||
}
|
||||
|
||||
pqxx::result PostgreSqlStorage::Do(std::string_view query) const {
|
||||
// Создаём транзакцию
|
||||
pqxx::work work(*connection_);
|
||||
|
||||
// Выполняем запрос
|
||||
pqxx::result result = work.exec(query);
|
||||
|
||||
// Завершаем транзакцию
|
||||
work.commit();
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <sstream>
|
||||
|
||||
#include <storage/storage.h>
|
||||
|
||||
using namespace storage;
|
||||
|
||||
Storage& Storage::Get() {
|
||||
static Storage storage;
|
||||
return storage;
|
||||
}
|
||||
|
||||
void Storage::Connect(const ConnectionData& connection_data) {
|
||||
std::ostringstream options;
|
||||
options << "dbname = " << connection_data.database
|
||||
<< " user = " << connection_data.username
|
||||
<< " password = " << connection_data.password
|
||||
<< " host = " << connection_data.host
|
||||
<< " port = " << connection_data.port;
|
||||
|
||||
Get().connection_ = std::make_unique<pqxx::connection>(options.str());
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <sstream>
|
||||
|
||||
#include <storage/storage.h>
|
||||
#include <storage/transaction.h>
|
||||
|
||||
using namespace storage;
|
||||
|
||||
Transaction::~Transaction() {
|
||||
transaction_->commit();
|
||||
}
|
||||
|
||||
Transaction Transaction::Create() {
|
||||
auto& storage = storage::Storage::Get();
|
||||
|
||||
Transaction transaction;
|
||||
transaction.transaction_ = std::make_unique<pqxx::work>(*storage.connection_);
|
||||
|
||||
return transaction;
|
||||
}
|
||||
|
||||
pqxx::result Transaction::Do(std::string_view query) const {
|
||||
return transaction_->exec(query);
|
||||
}
|
||||
Reference in New Issue
Block a user