From f63416b787b4b6a4a2495662d8f22b44c92ff0e4 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Wed, 29 Dec 2021 16:00:32 +0100 Subject: [PATCH] . --- Database.cpp | 31 +++++++++++++++++++------------ Database.hpp | 11 ++++++++--- main.cpp | 2 -- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Database.cpp b/Database.cpp index 4c07f12..ecdf580 100644 --- a/Database.cpp +++ b/Database.cpp @@ -3,24 +3,31 @@ Database::Database(){ Config conf("config.txt"); - std::ostringstream pgsqlConnectingString; + std::string pgsqlConnectingString; - pgsqlConnectingString - << " host=" << conf.getValue("db_host") - << " dbname=" << conf.getValue("db_name") - << " user=" << conf.getValue("db_user") - << " password=" << conf.getValue("db_pass"); + pgsqlConnectingString + .append(" host=").append(conf.getValue("db_host")) + .append(" dbname=").append(conf.getValue("db_name")) + .append(" user=").append(conf.getValue("db_user")) + .append(" password=").append(conf.getValue("db_pass")); + std::cout << pgsqlConnectingString << std::endl; - dbconn = new pqxx::connection(pgsqlConnectingString.str()); + dbconn = PQconnectdb(pgsqlConnectingString.c_str()); } -pqxx::result Database::execute(std::string cmd){ - pqxx::work w(*dbconn); - pqxx::result r(w.exec(cmd)); - w.commit(); +void Database::execute(std::string cmd){ + PQexec(dbconn, cmd.c_str()); +} + +PGresult* Database::fetch(std::string cmd){ + PGresult *r = PQexec(dbconn, cmd.c_str()); return r; + + // Don't forget to close after using this request + + } void Database::disconnect(){ - dbconn->close(); + PQfinish(dbconn); } diff --git a/Database.hpp b/Database.hpp index cf6d289..5ab5a32 100644 --- a/Database.hpp +++ b/Database.hpp @@ -1,4 +1,7 @@ -#include +#include +#include +#include +#include #ifndef DATABASE_H #define DATABASE_H @@ -7,12 +10,14 @@ class Database { private: - pqxx::connection *dbconn; + PGconn *dbconn; public: Database(); - pqxx::result execute(std::string cmd); + void execute(std::string cmd); + + PGresult* fetch(std::string cmd); void disconnect(); }; diff --git a/main.cpp b/main.cpp index ee903eb..e50f52d 100644 --- a/main.cpp +++ b/main.cpp @@ -12,8 +12,6 @@ Database db; Network net; - - net.run(); }