continue drafting NO_CI

This commit is contained in:
Dennis Eichhorn 2022-10-22 10:35:50 +02:00
parent 2e8343cdaf
commit 324801f79a
5 changed files with 140 additions and 29 deletions

View File

@ -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:

View File

@ -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;
}
}
}

View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#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

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}
}