mirror of
https://github.com/Karaka-Management/cOMS.git
synced 2026-01-26 17:48:40 +00:00
prepare db handling
This commit is contained in:
parent
641ff4e4e2
commit
7b31df32bc
|
|
@ -37,6 +37,22 @@ namespace DataStorage {
|
|||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void close(ConnectionAbstract *db, DbConnectionConfig dbdata)
|
||||
{
|
||||
switch (dbdata.db) {
|
||||
case DatabaseType::MYSQL:
|
||||
return ((MysqlConnection *) db)->close();
|
||||
case DatabaseType::PGSQL:
|
||||
return;
|
||||
case DatabaseType::SQLSRV:
|
||||
return;
|
||||
case DatabaseType::SQLITE:
|
||||
return ((SQLiteConnection *) db)->close();
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -30,6 +30,29 @@ namespace DataStorage {
|
|||
|
||||
char *password = NULL;
|
||||
} DbConnectionConfig;
|
||||
|
||||
void free_DbConnectionConfig(DbConnectionConfig *dbdata)
|
||||
{
|
||||
if (dbdata->database != NULL) {
|
||||
free(dbdata->database);
|
||||
dbdata->database = NULL;
|
||||
}
|
||||
|
||||
if (dbdata->host != NULL) {
|
||||
free(dbdata->host);
|
||||
dbdata->host = NULL;
|
||||
}
|
||||
|
||||
if (dbdata->login != NULL) {
|
||||
free(dbdata->login);
|
||||
dbdata->login = NULL;
|
||||
}
|
||||
|
||||
if (dbdata->password != NULL) {
|
||||
free(dbdata->password);
|
||||
dbdata->password = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -49,7 +49,6 @@ namespace DataStorage {
|
|||
}
|
||||
|
||||
this->close();
|
||||
|
||||
this->con = mysql_init(NULL);
|
||||
|
||||
::MYSQL *stat = mysql_real_connect(
|
||||
|
|
@ -77,7 +76,10 @@ namespace DataStorage {
|
|||
|
||||
void close()
|
||||
{
|
||||
mysql_close((::MYSQL *) this->con);
|
||||
if (this->con != NULL) {
|
||||
mysql_close((::MYSQL *) this->con);
|
||||
}
|
||||
|
||||
this->con = NULL;
|
||||
this->status = DatabaseStatus::CLOSED;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,7 +62,10 @@ namespace DataStorage {
|
|||
|
||||
void close()
|
||||
{
|
||||
sqlite3_close((sqlite3 *) this->con);
|
||||
if (this->con != NULL) {
|
||||
sqlite3_close((sqlite3 *) this->con);
|
||||
}
|
||||
|
||||
this->con = NULL;
|
||||
this->status = DatabaseStatus::CLOSED;
|
||||
}
|
||||
|
|
|
|||
0
DataStorage/Database/Query/Builder.h
Normal file
0
DataStorage/Database/Query/Builder.h
Normal file
0
DataStorage/Database/Schema/Builder.h
Normal file
0
DataStorage/Database/Schema/Builder.h
Normal file
60
DataStorage/Database/Schema/DbField.h
Normal file
60
DataStorage/Database/Schema/DbField.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/**
|
||||
* Karaka
|
||||
*
|
||||
* @package Models
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://karaka.app
|
||||
*/
|
||||
#ifndef DATASTORAGE_DATABASE_FIELD_H
|
||||
#define DATASTORAGE_DATABASE_FIELD_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace DataStorage {
|
||||
namespace Database {
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *type;
|
||||
void *def;
|
||||
bool is_nullable = true;
|
||||
bool is_primary = false;
|
||||
bool is_unique = false;
|
||||
bool autoincrement = false;
|
||||
char *foreignTable;
|
||||
char *foreignKey;
|
||||
} DbField;
|
||||
|
||||
void free_DbField(DbField *field)
|
||||
{
|
||||
if (field->name != NULL) {
|
||||
free(field->name);
|
||||
field->name = NULL;
|
||||
}
|
||||
|
||||
if (field->type != NULL) {
|
||||
free(field->type);
|
||||
field->type = NULL;
|
||||
}
|
||||
|
||||
if (field->def != NULL) {
|
||||
free(field->def);
|
||||
field->def = NULL;
|
||||
}
|
||||
|
||||
if (field->foreignTable != NULL) {
|
||||
free(field->foreignTable);
|
||||
field->foreignTable = NULL;
|
||||
}
|
||||
|
||||
if (field->foreignKey != NULL) {
|
||||
free(field->foreignKey);
|
||||
field->foreignKey = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
45
DataStorage/Database/Schema/DbSchema.h
Normal file
45
DataStorage/Database/Schema/DbSchema.h
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
* Karaka
|
||||
*
|
||||
* @package Models
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://karaka.app
|
||||
*/
|
||||
#ifndef DATASTORAGE_DATABASE_SCHEMA_H
|
||||
#define DATASTORAGE_DATABASE_SCHEMA_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "DbField.h"
|
||||
|
||||
namespace DataStorage {
|
||||
namespace Database {
|
||||
typedef struct {
|
||||
char *name;
|
||||
DbField *fields;
|
||||
size_t field_size = 0;
|
||||
} DbSchema;
|
||||
|
||||
void free_DbSchema(DbSchema *schema)
|
||||
{
|
||||
if (schema->name != NULL) {
|
||||
free(schema->name);
|
||||
schema->name = NULL;
|
||||
}
|
||||
|
||||
if (schema->fields != NULL) {
|
||||
for (int i = 0; i < schema->field_size; ++i) {
|
||||
free_DbField(&schema->fields[i]);
|
||||
}
|
||||
|
||||
free(schema->fields);
|
||||
schema->fields = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -23,6 +23,7 @@ namespace Stdlib {
|
|||
} entry;
|
||||
|
||||
struct ht {
|
||||
bool is_fixed = false;
|
||||
entry *entries;
|
||||
size_t max;
|
||||
size_t size;
|
||||
|
|
@ -48,7 +49,7 @@ namespace Stdlib {
|
|||
return hash;
|
||||
}
|
||||
|
||||
ht *create_table(void)
|
||||
ht *create_table(int max = 0, bool is_fixed = false)
|
||||
{
|
||||
ht *table = (ht *) malloc(sizeof(ht));
|
||||
if (table == NULL) {
|
||||
|
|
@ -56,7 +57,8 @@ namespace Stdlib {
|
|||
}
|
||||
|
||||
table->size = 0;
|
||||
table->max = 16;
|
||||
table->max = max == 0 ? 16 : max;
|
||||
table->is_fixed = is_fixed;
|
||||
|
||||
table->entries = (entry *) calloc(table->max, sizeof(entry));
|
||||
if (table->entries == NULL) {
|
||||
|
|
@ -151,7 +153,11 @@ namespace Stdlib {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if (table->size >= table->max / 2) {
|
||||
if (table->is_fixed && table->size == table->max) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!table->is_fixed && table->size >= table->max / 2) {
|
||||
if (!expand_table(table)) {
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -195,9 +201,6 @@ namespace Stdlib {
|
|||
}
|
||||
|
||||
free(table->entries);
|
||||
free(table);
|
||||
|
||||
table = NULL;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
104
Utils/ApplicationUtils.h
Normal file
104
Utils/ApplicationUtils.h
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/**
|
||||
* Karaka
|
||||
*
|
||||
* @package Utils
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://karaka.app
|
||||
*/
|
||||
#ifndef UTILS_APPLICATION_UTILS_H
|
||||
#define UTILS_APPLICATION_UTILS_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#elif defined __linux__
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace Utils {
|
||||
namespace ApplicationUtils {
|
||||
inline
|
||||
char *cwd()
|
||||
{
|
||||
char *cwd = (char *) malloc(sizeof(char) * 4096);
|
||||
if (cwd == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
_getcwd(cwd, sizeof(char));
|
||||
#else
|
||||
getcwd(cwd, sizeof(char));
|
||||
#endif
|
||||
|
||||
return cwd;
|
||||
}
|
||||
|
||||
void chdir_application(char *cwd, char *arg)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
char *dir;
|
||||
|
||||
char *pos = strrchr(arg, '/');
|
||||
if (pos == NULL) {
|
||||
pos = strrchr(arg, '\\');
|
||||
}
|
||||
|
||||
if (pos != NULL) {
|
||||
memcpy(dir, cwd, (cwd - pos + 1) * sizeof(char));
|
||||
|
||||
_chdir(dir);
|
||||
|
||||
free(dir);
|
||||
free(pos);
|
||||
#else
|
||||
char *dir;
|
||||
|
||||
char *pos = strrchr(arg, '/');
|
||||
if (pos == NULL) {
|
||||
pos = strrchr(arg, '\\');
|
||||
}
|
||||
|
||||
if (pos != NULL) {
|
||||
memcpy(dir, cwd, (cwd - pos + 1) * sizeof(char));
|
||||
|
||||
chdir(dir);
|
||||
|
||||
free(dir);
|
||||
free(pos);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
char *compile_arg_line(int argc, char **argv)
|
||||
{
|
||||
size_t max = 512;
|
||||
size_t length = 0;
|
||||
char *arg = (char *) calloc(max, sizeof(char));
|
||||
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
size_t argv_length = strlen(argv[i]);
|
||||
if (length + strlen(argv[i]) + 1 > max) {
|
||||
char *tmp = (char *) calloc(max + 128, sizeof(char));
|
||||
memcpy(tmp, arg, (length + 1) * sizeof(char));
|
||||
|
||||
free(arg);
|
||||
arg = tmp;
|
||||
max += 128;
|
||||
}
|
||||
|
||||
strcat(arg, argv[i]);
|
||||
length += argv_length;
|
||||
}
|
||||
|
||||
return arg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue
Block a user