Code cleanup
This commit is contained in:
@@ -7,92 +7,82 @@
|
||||
#include "manager/DirectoryManager.h"
|
||||
|
||||
namespace database {
|
||||
BaseRepository::BaseRepository()
|
||||
{ }
|
||||
|
||||
BaseRepository::BaseRepository(const std::string& path) : path(path)
|
||||
{
|
||||
intitalizeDetails();
|
||||
}
|
||||
|
||||
BaseRepository::BaseRepository(const model::BinaryPath& bConf)
|
||||
{
|
||||
initializeDetails(bConf);
|
||||
}
|
||||
|
||||
|
||||
bool BaseRepository::testConnection()
|
||||
{
|
||||
auto conn = mysql_init(nullptr);
|
||||
if (!mysql_real_connect(conn, details.server.c_str(), details.username.c_str(),
|
||||
details.password.c_str(), details.database.c_str(), 0,
|
||||
nullptr, 0)) {
|
||||
std::cout << "failed to connect to the database" << std::endl;
|
||||
return false;
|
||||
BaseRepository::BaseRepository(const std::string& path) : path(path) {
|
||||
intitalizeDetails();
|
||||
}
|
||||
|
||||
mysql_close(conn);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
MYSQL* BaseRepository::setupMysqlConnection()
|
||||
{
|
||||
MYSQL *conn = mysql_init(nullptr);
|
||||
|
||||
if (!mysql_real_connect(conn, details.server.c_str(), details.username.c_str(),
|
||||
details.password.c_str(), details.database.c_str(), 0, nullptr, 0)) {
|
||||
std::cout << "connection error" << std::endl;
|
||||
BaseRepository::BaseRepository(const model::BinaryPath& bConf) {
|
||||
initializeDetails(bConf);
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
MYSQL* BaseRepository::setupMysqlConnection(model::DatabaseConnection details)
|
||||
{
|
||||
MYSQL *connection = mysql_init(NULL);
|
||||
bool BaseRepository::testConnection() {
|
||||
auto conn = mysql_init(nullptr);
|
||||
if (!mysql_real_connect(conn, details.server.c_str(), details.username.c_str(),
|
||||
details.password.c_str(), details.database.c_str(), 0, nullptr, 0)) {
|
||||
std::cout << "failed to connect to the database\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
// connect to the database with the details attached.
|
||||
if (!mysql_real_connect(connection,details.server.c_str(), details.username.c_str(), details.password.c_str(), details.database.c_str(), 0, NULL, 0)) {
|
||||
printf("Conection error : %s\n", mysql_error(connection));
|
||||
exit(1);
|
||||
mysql_close(conn);
|
||||
|
||||
return true;
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
||||
MYSQL_RES* BaseRepository::performMysqlQuery(MYSQL *conn, const std::string& query)
|
||||
{
|
||||
// send the query to the database
|
||||
if (mysql_query(conn, query.c_str()))
|
||||
{
|
||||
printf("MySQL query error : %s\n", mysql_error(conn));
|
||||
exit(1);
|
||||
}
|
||||
MYSQL* BaseRepository::setupMysqlConnection() {
|
||||
MYSQL *conn = mysql_init(nullptr);
|
||||
|
||||
return mysql_use_result(conn);
|
||||
}
|
||||
if (!mysql_real_connect(conn, details.server.c_str(), details.username.c_str(),
|
||||
details.password.c_str(), details.database.c_str(), 0, nullptr, 0)) {
|
||||
std::cout << "connection error\n";
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
MYSQL* BaseRepository::setupMysqlConnection(model::DatabaseConnection details) {
|
||||
MYSQL *connection = mysql_init(NULL);
|
||||
|
||||
// connect to the database with the details attached.
|
||||
if (!mysql_real_connect(connection,details.server.c_str(), details.username.c_str(),
|
||||
details.password.c_str(), details.database.c_str(), 0, NULL, 0)) {
|
||||
std::cout << "Connection error: " << mysql_error(connection) << "\n";
|
||||
std::exit(-1);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
||||
void BaseRepository::intitalizeDetails()
|
||||
{
|
||||
auto databaseConfig = manager::DirectoryManager::databaseConfigContent(path);
|
||||
MYSQL_RES* BaseRepository::performMysqlQuery(MYSQL *conn, const std::string& query) {
|
||||
// send the query to the database
|
||||
if (mysql_query(conn, query.c_str())) {
|
||||
printf("MySQL query error : %s\n", mysql_error(conn));
|
||||
std::cout << "MySQL query error : " << mysql_error(conn) << "\n";
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
return mysql_use_result(conn);
|
||||
}
|
||||
|
||||
|
||||
void BaseRepository::intitalizeDetails() {
|
||||
auto databaseConfig = manager::DirectoryManager::databaseConfigContent(path);
|
||||
|
||||
details.database = databaseConfig["database"].get<std::string>();
|
||||
details.password = databaseConfig["password"].get<std::string>();
|
||||
details.server = databaseConfig["server"].get<std::string>();
|
||||
details.username = databaseConfig["username"].get<std::string>();
|
||||
}
|
||||
void BaseRepository::initializeDetails(const model::BinaryPath& bConf)
|
||||
{
|
||||
auto databaseConfig = manager::DirectoryManager::databaseConfigContent(bConf);
|
||||
details.database = databaseConfig["database"].get<std::string>();
|
||||
details.password = databaseConfig["password"].get<std::string>();
|
||||
details.server = databaseConfig["server"].get<std::string>();
|
||||
details.username = databaseConfig["username"].get<std::string>();
|
||||
}
|
||||
void BaseRepository::initializeDetails(const model::BinaryPath& bConf)
|
||||
{
|
||||
auto databaseConfig = manager::DirectoryManager::databaseConfigContent(bConf);
|
||||
|
||||
details.database = databaseConfig["database"].get<std::string>();
|
||||
details.server = databaseConfig["server"].get<std::string>();
|
||||
details.username = databaseConfig["username"].get<std::string>();
|
||||
details.password = databaseConfig["password"].get<std::string>();
|
||||
details.database = databaseConfig["database"].get<std::string>();
|
||||
details.server = databaseConfig["server"].get<std::string>();
|
||||
details.username = databaseConfig["username"].get<std::string>();
|
||||
details.password = databaseConfig["password"].get<std::string>();
|
||||
|
||||
std::cout << "retrieved database details" << std::endl;
|
||||
}
|
||||
std::cout << "retrieved database details\n";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user