Files
yourchat/src/lib/database.cpp
Torsten Schulz (local) 864d86aa09 Refactor chat system: Introduce ChatRoom and ChatUser classes
- Created ChatRoom class to manage chat room functionalities, including user management, message handling, and game mechanics.
- Developed ChatUser class to represent individual users, handling user-specific actions and interactions within chat rooms.
- Implemented a Config class for loading configuration settings from a JSON file.
- Established a Server class to manage connections, handle requests, and facilitate communication between users and chat rooms.
- Introduced a Database class for database interactions, utilizing PostgreSQL for user and room data management.
- Added utility functions in the Base class for JSON handling and socket communication.
- Created Object classes for Room and User to encapsulate their properties and behaviors.
- Updated main function to initialize server and load chat rooms from configuration.
2025-08-11 16:07:15 +02:00

43 lines
1.2 KiB
C++

#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