From 324801f79a04c39558c4ca0d5dcb88236a851d26 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 22 Oct 2022 10:35:50 +0200 Subject: [PATCH] continue drafting NO_CI --- .../Database/Connection/ConnectionFactory.h | 5 +- .../Database/Connection/MysqlConnection.h | 28 +++--- .../Connection/PostgresqlConnection.h | 94 +++++++++++++++++++ .../Database/Connection/SQLiteConnection.h | 16 ++-- DataStorage/Database/DatabaseType.h | 26 ++++- 5 files changed, 140 insertions(+), 29 deletions(-) create mode 100644 DataStorage/Database/Connection/PostgresqlConnection.h diff --git a/DataStorage/Database/Connection/ConnectionFactory.h b/DataStorage/Database/Connection/ConnectionFactory.h index c923dc2..741cd28 100644 --- a/DataStorage/Database/Connection/ConnectionFactory.h +++ b/DataStorage/Database/Connection/ConnectionFactory.h @@ -18,6 +18,7 @@ #include "ConnectionAbstract.h" #include "DbConnectionConfig.h" #include "MysqlConnection.h" +#include "PostgresqlConnection.h" #include "SQLiteConnection.h" namespace DataStorage { @@ -28,7 +29,7 @@ namespace DataStorage { case DatabaseType::MYSQL: return new MysqlConnection(dbdata); case DatabaseType::PGSQL: - return NULL; + return new PostgresqlConnection(dbdata); case DatabaseType::SQLSRV: return NULL; case DatabaseType::SQLITE: @@ -44,7 +45,7 @@ namespace DataStorage { case DatabaseType::MYSQL: return ((MysqlConnection *) db)->close(); case DatabaseType::PGSQL: - return; + return ((PostgresqlConnection *) db)->close(); case DatabaseType::SQLSRV: return; case DatabaseType::SQLITE: diff --git a/DataStorage/Database/Connection/MysqlConnection.h b/DataStorage/Database/Connection/MysqlConnection.h index 9572400..9d39750 100644 --- a/DataStorage/Database/Connection/MysqlConnection.h +++ b/DataStorage/Database/Connection/MysqlConnection.h @@ -34,11 +34,11 @@ namespace DataStorage { this->dbdata = dbdata == NULL ? this->dbdata : *dbdata; if (this->dbdata.db == NULL - || this->dbdata.host == NULL + || this->dbdata->host == NULL || this->dbdata.port == 0 - || this->dbdata.database == NULL - || this->dbdata.login == NULL - || this->dbdata.password == NULL + || this->dbdata->database == NULL + || this->dbdata->login == NULL + || this->dbdata->password == NULL ) { this->status = DatabaseStatus::FAILURE; @@ -49,27 +49,27 @@ namespace DataStorage { } this->close(); - this->con = mysql_init(NULL); - ::MYSQL *stat = mysql_real_connect( + this->con = mysql_init(NULL); + this->con = mysql_real_connect( (::MYSQL *) this->con, - this->dbdata.host, - this->dbdata.login, - this->dbdata.password, - this->dbdata.database, + this->dbdata->host, + this->dbdata->login, + this->dbdata->password, + this->dbdata->database, this->dbdata.port, NULL, 0 ); - if (!stat) { + if (!this->con) { this->status = DatabaseStatus::MISSING_DATABASE; mysql_close((::MYSQL *) this->con); this->con = NULL; - if (this->dbdata.password != NULL) { - free(this->dbdata.password); - this->dbdata.password = NULL; + if (this->dbdata->password != NULL) { + free(this->dbdata->password); + this->dbdata->password = NULL; } } } diff --git a/DataStorage/Database/Connection/PostgresqlConnection.h b/DataStorage/Database/Connection/PostgresqlConnection.h new file mode 100644 index 0000000..e69b4b8 --- /dev/null +++ b/DataStorage/Database/Connection/PostgresqlConnection.h @@ -0,0 +1,94 @@ +/** + * Karaka + * + * @package Utils + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://karaka.app + */ +#ifndef DATASTORAGE_DATABASE_MYSQL_CONNECTION_H +#define DATASTORAGE_DATABASE_MYSQL_CONNECTION_H + +#include +#include + +#include + +#include "ConnectionAbstract.h" +#include "DbConnectionConfig.h" +#include "../DatabaseType.h" +#include "../DatabaseStatus.h" + +namespace DataStorage { + namespace Database { + struct PostgresqlConnection : ConnectionAbstract { + PostgresqlConnection(DbConnectionConfig dbdata) + { + this->type = DatabaseType::PGSQL; + this->dbdata = dbdata; + } + + void connect(DbConnectionConfig *dbdata = NULL) + { + this->dbdata = dbdata == NULL ? this->dbdata : *dbdata; + + if (this->dbdata.db == NULL + || this->dbdata->host == NULL + || this->dbdata.port == 0 + || this->dbdata->database == NULL + || this->dbdata->login == NULL + || this->dbdata->password == NULL + ) { + this->status = DatabaseStatus::FAILURE; + + if (this->dbdata.password != NULL) { + free(this->dbdata.password); + this->dbdata.password = NULL; + } + } + + this->close(); + + char port[12]; + sprintf(port, "%d", this->dbdata.port); + + this->con = PQsetdbLogin( + this->dbdata->host, + port, + NULL, + NULL, + this->dbdata->database, + this->dbdata->login, + this->dbdata->password + ); + + ConnStatusType stat = PQstatus((PGconn *) this->con); + + if (stat != ConnStatusType::CONNECTION_OK) { + this->status = DatabaseStatus::MISSING_DATABASE; + + PQfinish((PGconn *) this->con) + this->con = NULL; + + if (this->dbdata->password != NULL) { + free(this->dbdata->password); + this->dbdata->password = NULL; + } + } + } + + void close() + { + if (this->con != NULL) { + PQfinish((PGconn *) this->con) + } + + this->con = NULL; + this->status = DatabaseStatus::CLOSED; + } + }; + } +} + +#endif \ No newline at end of file diff --git a/DataStorage/Database/Connection/SQLiteConnection.h b/DataStorage/Database/Connection/SQLiteConnection.h index a474190..01334d5 100644 --- a/DataStorage/Database/Connection/SQLiteConnection.h +++ b/DataStorage/Database/Connection/SQLiteConnection.h @@ -34,28 +34,28 @@ namespace DataStorage { this->dbdata = dbdata == NULL ? this->dbdata : *dbdata; if (this->dbdata.db == NULL - || this->dbdata.database == NULL + || this->dbdata->database == NULL ) { this->status = DatabaseStatus::FAILURE; - if (this->dbdata.password != NULL) { - free(this->dbdata.password); - this->dbdata.password = NULL; + if (this->dbdata->password != NULL) { + free(this->dbdata->password); + this->dbdata->password = NULL; } } this->close(); - int stat = sqlite3_open(this->dbdata.host, (sqlite3 **) &this->con); + int stat = sqlite3_open(this->dbdata->host, (sqlite3 **) &this->con); if (stat != SQLITE_OK) { this->status = DatabaseStatus::MISSING_DATABASE; sqlite3_close((sqlite3 *) this->con); this->con = NULL; - if (this->dbdata.password != NULL) { - free(this->dbdata.password); - this->dbdata.password = NULL; + if (this->dbdata->password != NULL) { + free(this->dbdata->password); + this->dbdata->password = NULL; } } } diff --git a/DataStorage/Database/DatabaseType.h b/DataStorage/Database/DatabaseType.h index 54ce96e..09a5807 100644 --- a/DataStorage/Database/DatabaseType.h +++ b/DataStorage/Database/DatabaseType.h @@ -13,12 +13,28 @@ namespace DataStorage { namespace Database { typedef enum { - MYSQL = 0, - SQLITE = 1, - PGSQL = 2, - SQLSRV = 3, - UNDEFINED = 4 + MYSQL = 1, + SQLITE = 2, + PGSQL = 3, + SQLSRV = 4, + UNDEFINED = 5 } DatabaseType; + + DatabaseType database_type_from_str(const char* type) + { + switch(type) { + case strcmp(type, "mysql") == 0: + return DatabaseType::MYSQL; + case strcmp(type, "sqlite") == 0: + return DatabaseType::SQLITE; + case strcmp(type, "pqsql") == 0: + return DatabaseType::PGSQL; + case strcmp(type, "mssql") == 0: + return DatabaseType::SQLSRV; + default: + return DatabaseType::UNDEFINED; + } + } } }