Refactor project structure: replace User class with ChatUser, integrate Database class, and update CMake configuration for new files
This commit is contained in:
42
database.cpp
Normal file
42
database.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "database.h"
|
||||
#include <pqxx/pqxx>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
|
||||
Database::Database(std::shared_ptr<Config> config)
|
||||
{
|
||||
// Hole Verbindungsdaten aus der Config
|
||||
std::string dbname = config->value("database", "database").asString();
|
||||
std::string user = config->value("database", "user").asString();
|
||||
std::string password = config->value("database", "password").asString();
|
||||
std::string host = config->value("database", "host").asString();
|
||||
|
||||
std::string conninfo =
|
||||
"dbname=" + dbname +
|
||||
" user=" + user +
|
||||
" password=" + password +
|
||||
" host=" + host;
|
||||
|
||||
try {
|
||||
_connection = std::make_unique<pqxx::connection>(conninfo);
|
||||
if (!_connection->is_open()) {
|
||||
throw std::runtime_error("Failed to open database connection");
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
throw std::runtime_error(std::string("Database connection error: ") + e.what());
|
||||
}
|
||||
}
|
||||
|
||||
// Beispielmethode für eine Abfrage
|
||||
pqxx::result Database::exec(const std::string& query)
|
||||
{
|
||||
pqxx::work txn(*_connection);
|
||||
pqxx::result r = txn.exec(query);
|
||||
txn.commit();
|
||||
return r;
|
||||
}
|
||||
|
||||
} // namespace Lib
|
||||
} // namespace Yc
|
||||
Reference in New Issue
Block a user