mirror of
https://github.com/Karaka-Management/cOMS.git
synced 2026-02-15 18:08:40 +00:00
continue drafting NO_CI
This commit is contained in:
parent
2e8343cdaf
commit
324801f79a
|
|
@ -18,6 +18,7 @@
|
||||||
#include "ConnectionAbstract.h"
|
#include "ConnectionAbstract.h"
|
||||||
#include "DbConnectionConfig.h"
|
#include "DbConnectionConfig.h"
|
||||||
#include "MysqlConnection.h"
|
#include "MysqlConnection.h"
|
||||||
|
#include "PostgresqlConnection.h"
|
||||||
#include "SQLiteConnection.h"
|
#include "SQLiteConnection.h"
|
||||||
|
|
||||||
namespace DataStorage {
|
namespace DataStorage {
|
||||||
|
|
@ -28,7 +29,7 @@ namespace DataStorage {
|
||||||
case DatabaseType::MYSQL:
|
case DatabaseType::MYSQL:
|
||||||
return new MysqlConnection(dbdata);
|
return new MysqlConnection(dbdata);
|
||||||
case DatabaseType::PGSQL:
|
case DatabaseType::PGSQL:
|
||||||
return NULL;
|
return new PostgresqlConnection(dbdata);
|
||||||
case DatabaseType::SQLSRV:
|
case DatabaseType::SQLSRV:
|
||||||
return NULL;
|
return NULL;
|
||||||
case DatabaseType::SQLITE:
|
case DatabaseType::SQLITE:
|
||||||
|
|
@ -44,7 +45,7 @@ namespace DataStorage {
|
||||||
case DatabaseType::MYSQL:
|
case DatabaseType::MYSQL:
|
||||||
return ((MysqlConnection *) db)->close();
|
return ((MysqlConnection *) db)->close();
|
||||||
case DatabaseType::PGSQL:
|
case DatabaseType::PGSQL:
|
||||||
return;
|
return ((PostgresqlConnection *) db)->close();
|
||||||
case DatabaseType::SQLSRV:
|
case DatabaseType::SQLSRV:
|
||||||
return;
|
return;
|
||||||
case DatabaseType::SQLITE:
|
case DatabaseType::SQLITE:
|
||||||
|
|
|
||||||
|
|
@ -34,11 +34,11 @@ namespace DataStorage {
|
||||||
this->dbdata = dbdata == NULL ? this->dbdata : *dbdata;
|
this->dbdata = dbdata == NULL ? this->dbdata : *dbdata;
|
||||||
|
|
||||||
if (this->dbdata.db == NULL
|
if (this->dbdata.db == NULL
|
||||||
|| this->dbdata.host == NULL
|
|| this->dbdata->host == NULL
|
||||||
|| this->dbdata.port == 0
|
|| this->dbdata.port == 0
|
||||||
|| this->dbdata.database == NULL
|
|| this->dbdata->database == NULL
|
||||||
|| this->dbdata.login == NULL
|
|| this->dbdata->login == NULL
|
||||||
|| this->dbdata.password == NULL
|
|| this->dbdata->password == NULL
|
||||||
) {
|
) {
|
||||||
this->status = DatabaseStatus::FAILURE;
|
this->status = DatabaseStatus::FAILURE;
|
||||||
|
|
||||||
|
|
@ -49,27 +49,27 @@ namespace DataStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
this->close();
|
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,
|
(::MYSQL *) this->con,
|
||||||
this->dbdata.host,
|
this->dbdata->host,
|
||||||
this->dbdata.login,
|
this->dbdata->login,
|
||||||
this->dbdata.password,
|
this->dbdata->password,
|
||||||
this->dbdata.database,
|
this->dbdata->database,
|
||||||
this->dbdata.port,
|
this->dbdata.port,
|
||||||
NULL, 0
|
NULL, 0
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!stat) {
|
if (!this->con) {
|
||||||
this->status = DatabaseStatus::MISSING_DATABASE;
|
this->status = DatabaseStatus::MISSING_DATABASE;
|
||||||
|
|
||||||
mysql_close((::MYSQL *) this->con);
|
mysql_close((::MYSQL *) this->con);
|
||||||
this->con = NULL;
|
this->con = NULL;
|
||||||
|
|
||||||
if (this->dbdata.password != NULL) {
|
if (this->dbdata->password != NULL) {
|
||||||
free(this->dbdata.password);
|
free(this->dbdata->password);
|
||||||
this->dbdata.password = NULL;
|
this->dbdata->password = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
94
DataStorage/Database/Connection/PostgresqlConnection.h
Normal file
94
DataStorage/Database/Connection/PostgresqlConnection.h
Normal 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
|
||||||
|
|
@ -34,28 +34,28 @@ namespace DataStorage {
|
||||||
this->dbdata = dbdata == NULL ? this->dbdata : *dbdata;
|
this->dbdata = dbdata == NULL ? this->dbdata : *dbdata;
|
||||||
|
|
||||||
if (this->dbdata.db == NULL
|
if (this->dbdata.db == NULL
|
||||||
|| this->dbdata.database == NULL
|
|| this->dbdata->database == NULL
|
||||||
) {
|
) {
|
||||||
this->status = DatabaseStatus::FAILURE;
|
this->status = DatabaseStatus::FAILURE;
|
||||||
|
|
||||||
if (this->dbdata.password != NULL) {
|
if (this->dbdata->password != NULL) {
|
||||||
free(this->dbdata.password);
|
free(this->dbdata->password);
|
||||||
this->dbdata.password = NULL;
|
this->dbdata->password = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this->close();
|
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) {
|
if (stat != SQLITE_OK) {
|
||||||
this->status = DatabaseStatus::MISSING_DATABASE;
|
this->status = DatabaseStatus::MISSING_DATABASE;
|
||||||
|
|
||||||
sqlite3_close((sqlite3 *) this->con);
|
sqlite3_close((sqlite3 *) this->con);
|
||||||
this->con = NULL;
|
this->con = NULL;
|
||||||
|
|
||||||
if (this->dbdata.password != NULL) {
|
if (this->dbdata->password != NULL) {
|
||||||
free(this->dbdata.password);
|
free(this->dbdata->password);
|
||||||
this->dbdata.password = NULL;
|
this->dbdata->password = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,12 +13,28 @@
|
||||||
namespace DataStorage {
|
namespace DataStorage {
|
||||||
namespace Database {
|
namespace Database {
|
||||||
typedef enum {
|
typedef enum {
|
||||||
MYSQL = 0,
|
MYSQL = 1,
|
||||||
SQLITE = 1,
|
SQLITE = 2,
|
||||||
PGSQL = 2,
|
PGSQL = 3,
|
||||||
SQLSRV = 3,
|
SQLSRV = 4,
|
||||||
UNDEFINED = 4
|
UNDEFINED = 5
|
||||||
} DatabaseType;
|
} 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user