Добавил C++-сервер
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
project(la_storage)
|
||||
|
||||
file(GLOB_RECURSE SOURCES src/*.cpp)
|
||||
|
||||
add_library(${PROJECT_NAME})
|
||||
|
||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC include PRIVATE ${THIRD_PARTY_DIR}/include)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC pqxx)
|
||||
@@ -0,0 +1,19 @@
|
||||
#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,32 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user