add new implementation database connection
This commit is contained in:
parent
e91c0e1d90
commit
2c6d37ccea
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@ subprojects/*
|
||||
!subprojects/httplib.wrap
|
||||
!subprojects/jwt-cpp.wrap
|
||||
!subprojects/nlohmann_json.wrap
|
||||
!subprojects/pgxx.wrap
|
||||
|
@ -1,7 +1,7 @@
|
||||
project(
|
||||
meson.current_source_dir().split('/').get(-1),
|
||||
'cpp',
|
||||
version : '1.0.0',
|
||||
version : run_command('git', 'rev-parse', '--short', 'HEAD').stdout().strip(),
|
||||
default_options : [
|
||||
'warning_level=1',
|
||||
'optimization=3',
|
||||
@ -40,6 +40,7 @@ deps = [
|
||||
dependency('threads'),
|
||||
dependency('libpqxx'),
|
||||
subproject('hack').get_variable('hack_dep'),
|
||||
subproject('pgxx').get_variable('pgxx_dep'),
|
||||
subproject('nlohmann_json').get_variable('nlohmann_json_dep'),
|
||||
subproject('jwt-cpp').get_variable('jwt_dep'),
|
||||
subproject('httplib').get_variable('cpp_httplib_dep'),
|
||||
|
@ -4,7 +4,6 @@ headers = [
|
||||
'trs/helpers/execute.hpp',
|
||||
'trs/helpers/verification.hpp',
|
||||
|
||||
'trs/libs/database.hpp',
|
||||
'trs/libs/function_manager.hpp',
|
||||
'trs/libs/transaction.hpp',
|
||||
'trs/libs/inspector.hpp',
|
||||
|
@ -11,14 +11,6 @@ namespace trs
|
||||
try
|
||||
{
|
||||
fm.valid(tr.m_passport.m_function_name);
|
||||
}
|
||||
catch (hack::exception& ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ins.valid(tr);
|
||||
}
|
||||
catch (hack::exception& ex)
|
||||
|
@ -1,158 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <pqxx/pqxx>
|
||||
|
||||
#include "hack/utils/func_query.hpp"
|
||||
#include "hack/utils/singleton.hpp"
|
||||
#include "hack/logger/logger.hpp"
|
||||
#include "hack/exception/exception.hpp"
|
||||
|
||||
#include "trs/utils/var.hpp"
|
||||
#include "trs/utils/using.hpp"
|
||||
|
||||
namespace trs
|
||||
{
|
||||
class database : public hack::utils::singleton<database>
|
||||
{
|
||||
friend hack::utils::singleton<database>;
|
||||
|
||||
public:
|
||||
~database() = default;
|
||||
|
||||
private:
|
||||
database() = default;
|
||||
std::mutex m_mutex;
|
||||
|
||||
std::string DB_APP_CONNECTION;
|
||||
std::string DB_LOG_CONNECTION;
|
||||
|
||||
public:
|
||||
void set_log_connection(std::string con)
|
||||
{
|
||||
DB_LOG_CONNECTION = con;
|
||||
}
|
||||
|
||||
void set_app_connection(std::string con)
|
||||
{
|
||||
DB_APP_CONNECTION = con;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
JSON execute(const std::string func_name, const Args... args)
|
||||
{
|
||||
std::string query;
|
||||
JSON result;
|
||||
|
||||
try
|
||||
{
|
||||
query = hack::utils::make_query(func_name, args...);
|
||||
result = execute(func_name, query);
|
||||
}
|
||||
catch(const hack::exception& ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
hack::exception ex;
|
||||
ex.description("database dont create query from args");
|
||||
ex.system_error(e);
|
||||
ex.params("query", query);
|
||||
ex.variadic_params(args...);
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
JSON execute(const std::string& func_name, const std::string& query)
|
||||
{
|
||||
if (DB_APP_CONNECTION.empty() || DB_LOG_CONNECTION.empty())
|
||||
{
|
||||
hack::error()("DB_LOG_CONNECTION or DB_LOG_CONNECTION is empty");
|
||||
hack::log("")("DB_APP_CONNECTION = ", DB_APP_CONNECTION);
|
||||
hack::log("")("DB_LOG_CONNECTION = ", DB_LOG_CONNECTION);
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
JSON result;
|
||||
pqxx::connection* app_con;
|
||||
pqxx::connection* log_con;
|
||||
|
||||
try
|
||||
{
|
||||
app_con = new pqxx::connection(DB_APP_CONNECTION);
|
||||
log_con = new pqxx::connection(DB_LOG_CONNECTION);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
hack::error()("Dont create connection");
|
||||
hack::error()(e.what());
|
||||
std::terminate();
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
hack::error()("Dont create connection and no get error in description");
|
||||
hack::error()("Good luck my friend! ...");
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
pqxx::result r;
|
||||
pqxx::nontransaction work { func_name == var::FUNC_LOGGER ? *log_con : *app_con };
|
||||
|
||||
try
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
r = work.exec(query);
|
||||
std::string r_str;
|
||||
for (auto row : r) r_str = row[func_name].c_str();
|
||||
work.commit();
|
||||
result = JSON::parse(r_str);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
if (func_name != var::FUNC_LOGGER)
|
||||
{
|
||||
hack::exception ex;
|
||||
ex.description("No exec query to database");
|
||||
ex.message(var::EXECUTE_ERROR);
|
||||
ex.system_error(e);
|
||||
ex.params("query", query);
|
||||
ex.params("result", result);
|
||||
throw ex;
|
||||
}
|
||||
else
|
||||
{
|
||||
hack::error()("WARNING!!! ERROR LOG TO DATABASE");
|
||||
hack::error()("query", query);
|
||||
hack::error()(e.what());
|
||||
std::terminate();
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
if (func_name != var::FUNC_LOGGER)
|
||||
{
|
||||
hack::exception ex;
|
||||
ex.description(var::ALIEN_SYSTEM_ERROR);
|
||||
ex.message(var::EXECUTE_ERROR);
|
||||
ex.params("query", query);
|
||||
ex.params("result", result);
|
||||
throw ex;
|
||||
}
|
||||
else
|
||||
{
|
||||
hack::error()("WARNING!!! ERROR LOG TO DATABASE");
|
||||
hack::error()("query", query);
|
||||
std::terminate();
|
||||
}
|
||||
}
|
||||
|
||||
delete app_con;
|
||||
delete log_con;
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
#include "httplib.h"
|
||||
#include "hack/logger/logger.hpp"
|
||||
#include "hack/exception/exception.hpp"
|
||||
#include "pgxx/pgxx.hpp"
|
||||
|
||||
#include "trs/utils/define.hpp"
|
||||
#include "trs/utils/helpers.hpp"
|
||||
@ -13,7 +14,6 @@
|
||||
#include "trs/libs/transaction.hpp"
|
||||
#include "trs/libs/function_manager.hpp"
|
||||
#include "trs/libs/inspector.hpp"
|
||||
#include "trs/libs/database.hpp"
|
||||
#include "trs/helpers/verification.hpp"
|
||||
#include "trs/helpers/execute.hpp"
|
||||
|
||||
@ -28,6 +28,13 @@ namespace trs
|
||||
public:
|
||||
void init(std::string service_name)
|
||||
{
|
||||
if (!PGXX().ready())
|
||||
{
|
||||
hack::error()("data base manager connection not initialazed");
|
||||
hack::warn()("please add conection to PGXX().init method");
|
||||
return;
|
||||
}
|
||||
|
||||
set_read_timeout(5, 0);
|
||||
set_write_timeout(5, 0);
|
||||
set_idle_interval(0, 1'000'000);
|
||||
@ -37,9 +44,6 @@ namespace trs
|
||||
m_service_name = service_name;
|
||||
}
|
||||
|
||||
void set_log_connection(std::string connection) { database::instance().set_log_connection(connection); }
|
||||
void set_app_connection(std::string connection) { database::instance().set_app_connection(connection); }
|
||||
|
||||
void log_to_console(bool v) { m_log_to_console = v; }
|
||||
|
||||
template<typename Function>
|
||||
@ -107,7 +111,10 @@ namespace trs
|
||||
ex.service(m_service_name);
|
||||
if (m_log_to_console)
|
||||
ex.log();
|
||||
ex.commit<database>();
|
||||
auto query = PGXX().prepare(var::LOGGER, ex.convert_to_json());
|
||||
auto r = PGXX().execute(var::LOGGER, query);
|
||||
if (m_log_to_console)
|
||||
hack::error()(r);
|
||||
}
|
||||
|
||||
res.set_content(nlohmann::to_string(tr.m_data.m_result), var::HEADER_FLAG_JSON);
|
||||
|
@ -13,5 +13,5 @@ namespace trs::var
|
||||
inline const std::string EXECUTE_ERROR = "execute error";
|
||||
inline const std::string HEADER_FLAG_JSON = "application/json";
|
||||
|
||||
inline const std::string FUNC_LOGGER = "logger";
|
||||
inline const std::string LOGGER = "logger";
|
||||
}
|
||||
|
6
subprojects/pgxx.wrap
Executable file
6
subprojects/pgxx.wrap
Executable file
@ -0,0 +1,6 @@
|
||||
[wrap-git]
|
||||
url = https://gitcast.ru/chatlanin/pgxx.git
|
||||
revision = master
|
||||
|
||||
[provide]
|
||||
pgxx = pgxx_dep
|
@ -26,6 +26,23 @@ namespace worckspaces
|
||||
|
||||
auto main(int argc, char* args[]) -> int
|
||||
{
|
||||
// ATTENTION !!!
|
||||
// обязательно реализовать инициализацию БД
|
||||
|
||||
const std::string con = "postgres://chatlanin:password_for_connection_to_test_db@localhost:5423/test.db?sslmode=disable";
|
||||
|
||||
try
|
||||
{
|
||||
PGXX().init("con_1", 300, con);
|
||||
PGXX().init("con_2", 300, con);
|
||||
}
|
||||
catch(hack::exception& ex)
|
||||
{
|
||||
ex.log();
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
// trs::client cli {"localhost:5000"};
|
||||
// cli.set_token("token");
|
||||
// trs::client cli_2 { std::move(cli) };
|
||||
@ -33,8 +50,6 @@ auto main(int argc, char* args[]) -> int
|
||||
|
||||
trs::server srv;
|
||||
srv.init("test service");
|
||||
srv.set_log_connection("con");
|
||||
srv.set_app_connection("con");
|
||||
|
||||
srv.registration("healthcheck", worckspaces::inspector, worckspaces::provider);
|
||||
srv.run();
|
||||
|
Loading…
Reference in New Issue
Block a user