From 5269c53dc801c1099ad91a12d716d570315fca5b Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Tue, 28 Dec 2021 15:32:39 +0100 Subject: [PATCH] Database header --- Database.cpp | 22 ++++++++++++++++++++++ Database.hpp | 7 ++++++- Makefile | 2 +- main.cpp | 5 +++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Database.cpp b/Database.cpp index fe106ee..4c07f12 100644 --- a/Database.cpp +++ b/Database.cpp @@ -1,4 +1,26 @@ #include "Database.hpp" +#include "Config.hpp" Database::Database(){ + Config conf("config.txt"); + std::ostringstream pgsqlConnectingString; + + pgsqlConnectingString + << " host=" << conf.getValue("db_host") + << " dbname=" << conf.getValue("db_name") + << " user=" << conf.getValue("db_user") + << " password=" << conf.getValue("db_pass"); + + dbconn = new pqxx::connection(pgsqlConnectingString.str()); +} + +pqxx::result Database::execute(std::string cmd){ + pqxx::work w(*dbconn); + pqxx::result r(w.exec(cmd)); + w.commit(); + return r; +} + +void Database::disconnect(){ + dbconn->close(); } diff --git a/Database.hpp b/Database.hpp index 47aa1b7..cf6d289 100644 --- a/Database.hpp +++ b/Database.hpp @@ -1,4 +1,4 @@ -#include +#include #ifndef DATABASE_H #define DATABASE_H @@ -7,9 +7,14 @@ class Database { private: + pqxx::connection *dbconn; public: Database(); + + pqxx::result execute(std::string cmd); + + void disconnect(); }; #endif /* DATABASE_H */ diff --git a/Makefile b/Makefile index 86c8229..a616cad 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=g++ CFLAGS= -Wall -g -LDLIBS= -lpqxx +LDLIBS= -lpqxx -lpq SOURCES= $(wildcard *.cpp) all: main diff --git a/main.cpp b/main.cpp index 2222d54..ee903eb 100644 --- a/main.cpp +++ b/main.cpp @@ -8,7 +8,12 @@ int main( int carg, char* varg[] ){ Config conf("config.txt"); +Database db; + Network net; + + + net.run(); }