diff --git a/DataStorage/Database/Connection/ConnectionFacotry.h b/DataStorage/Database/Connection/ConnectionFactory.h similarity index 65% rename from DataStorage/Database/Connection/ConnectionFacotry.h rename to DataStorage/Database/Connection/ConnectionFactory.h index 386b0ca..c923dc2 100644 --- a/DataStorage/Database/Connection/ConnectionFacotry.h +++ b/DataStorage/Database/Connection/ConnectionFactory.h @@ -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; + } + } } } diff --git a/DataStorage/Database/Connection/DbConnectionConfig.h b/DataStorage/Database/Connection/DbConnectionConfig.h index facfb10..d475336 100644 --- a/DataStorage/Database/Connection/DbConnectionConfig.h +++ b/DataStorage/Database/Connection/DbConnectionConfig.h @@ -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; + } + } } } diff --git a/DataStorage/Database/Connection/MysqlConnection.h b/DataStorage/Database/Connection/MysqlConnection.h index fbc8ef7..9572400 100644 --- a/DataStorage/Database/Connection/MysqlConnection.h +++ b/DataStorage/Database/Connection/MysqlConnection.h @@ -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; } diff --git a/DataStorage/Database/Connection/SQLiteConnection.h b/DataStorage/Database/Connection/SQLiteConnection.h index 83ea7d8..a474190 100644 --- a/DataStorage/Database/Connection/SQLiteConnection.h +++ b/DataStorage/Database/Connection/SQLiteConnection.h @@ -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; } diff --git a/DataStorage/Database/Query/Builder.h b/DataStorage/Database/Query/Builder.h new file mode 100644 index 0000000..e69de29 diff --git a/DataStorage/Database/Schema/Builder.h b/DataStorage/Database/Schema/Builder.h new file mode 100644 index 0000000..e69de29 diff --git a/DataStorage/Database/Schema/DbField.h b/DataStorage/Database/Schema/DbField.h new file mode 100644 index 0000000..6566c78 --- /dev/null +++ b/DataStorage/Database/Schema/DbField.h @@ -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 +#include + +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 \ No newline at end of file diff --git a/DataStorage/Database/Schema/DbSchema.h b/DataStorage/Database/Schema/DbSchema.h new file mode 100644 index 0000000..b4a369b --- /dev/null +++ b/DataStorage/Database/Schema/DbSchema.h @@ -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 +#include + +#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 \ No newline at end of file diff --git a/Stdlib/HashTable.h b/Stdlib/HashTable.h index 0007436..33d4b1b 100644 --- a/Stdlib/HashTable.h +++ b/Stdlib/HashTable.h @@ -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; } }; } diff --git a/Utils/ApplicationUtils.h b/Utils/ApplicationUtils.h new file mode 100644 index 0000000..484e9f7 --- /dev/null +++ b/Utils/ApplicationUtils.h @@ -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 +#include +#include + +#ifdef _WIN32 + #include +#elif defined __linux__ + #include +#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 \ No newline at end of file