fully featured json file database
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 11s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Successful in 8s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Successful in 12s

This commit is contained in:
wt
2025-10-23 14:16:03 +07:00
parent 5fda74ab1d
commit bed968b4e4
11 changed files with 107 additions and 76 deletions
-1
View File
@@ -13,7 +13,6 @@
sqlite3 *db = nullptr;
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
#else
FILE *users_file = nullptr;
pthread_mutex_t users_file_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
-2
View File
@@ -7,8 +7,6 @@
extern sqlite3 *db;
extern pthread_mutex_t db_mutex;
#else
#include <stdio.h>
extern FILE *users_file;
extern pthread_mutex_t users_file_mutex;
#endif
+2 -5
View File
@@ -1,15 +1,12 @@
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#endif
#ifndef USE_SQLITE3
#else
#include "file.h"
#endif
#include "user.h"
bool db_delete_user(const int id) {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
+2 -5
View File
@@ -1,12 +1,9 @@
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#endif
#include "user.h"
#ifndef USE_SQLITE3
#else
#include "file.h"
#endif
+3 -9
View File
@@ -1,16 +1,10 @@
#ifndef USE_SQLITE3
#include "cJSON.h"
#endif
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#endif
#include "user.h"
#ifndef USE_SQLITE3
#else
#include "cJSON.h"
#include "file.h"
#endif
+8 -14
View File
@@ -1,16 +1,10 @@
#ifndef USE_SQLITE3
#include "cJSON.h"
#endif
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#endif
#include "user.h"
#ifndef USE_SQLITE3
#else
#include "cJSON.h"
#include "file.h"
#endif
@@ -75,11 +69,11 @@ User *db_get_user_by_username(const char *username) {
}
}
if (found == nullptr) {
pthread_mutex_unlock(&users_file_mutex);
// if (user != nullptr) cJSON_Delete(user);
cJSON_Delete(users);
free(data);
return nullptr;
pthread_mutex_unlock(&users_file_mutex);
// if (user != nullptr) cJSON_Delete(user);
cJSON_Delete(users);
free(data);
return nullptr;
}
pthread_mutex_unlock(&users_file_mutex);
// if (user != nullptr) cJSON_Delete(user);
+2 -6
View File
@@ -1,14 +1,10 @@
#include <pthread.h>
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#endif
#include "user.h"
#ifndef USE_SQLITE3
#else
#include "file.h"
#endif
+11 -22
View File
@@ -1,16 +1,10 @@
#ifndef USE_SQLITE3
#include "cJSON.h"
#endif
#include "database.h"
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#endif
#include "user.h"
#ifndef USE_SQLITE3
#else
#include "cJSON.h"
#include "file.h"
#endif
@@ -98,18 +92,12 @@ User *db_insert_user(const char *username, const char *email,
return nullptr;
}
cJSON *new_user_data = cJSON_CreateObject();
cJSON *new_user_id = cJSON_CreateNumber(users_size);
cJSON *new_user_username = cJSON_CreateString(username);
cJSON *new_user_password = cJSON_CreateString(password);
cJSON *new_user_email = cJSON_CreateString(email);
cJSON *new_user_group = cJSON_CreateString(group);
cJSON *new_user_is_superuser = cJSON_CreateBool(is_superuser?1:0);
cJSON_AddItemToObject(new_user_data, "id", new_user_id);
cJSON_AddItemToObject(new_user_data, "username", new_user_username);
cJSON_AddItemToObject(new_user_data, "password", new_user_password);
cJSON_AddItemToObject(new_user_data, "email", new_user_email);
cJSON_AddItemToObject(new_user_data, "group", new_user_group);
cJSON_AddItemToObject(new_user_data, "is_superuser", new_user_is_superuser);
cJSON_AddNumberToObject(new_user_data, "id", users_size);
cJSON_AddStringToObject(new_user_data, "username", username);
cJSON_AddStringToObject(new_user_data, "password", password);
cJSON_AddStringToObject(new_user_data, "email", email);
cJSON_AddStringToObject(new_user_data, "group", group);
cJSON_AddBoolToObject(new_user_data, "is_superuser", *is_superuser);
char *new_user_data_string = cJSON_Print(new_user_data);
if (users_size == 0) {
users = cJSON_CreateArray();
@@ -123,7 +111,8 @@ User *db_insert_user(const char *username, const char *email,
free(new_data);
free(data);
free(new_user_data_string);
cJSON_Delete(new_user_data);
// cJSON_Delete(new_user_data);
cJSON_Delete(users);
if (user != nullptr) cJSON_Delete(user);
return return_user;
#endif
+40 -6
View File
@@ -1,10 +1,14 @@
#ifdef USE_SQLITE3
#include <pthread.h>
#include "database.h"
#include "sql.h"
#endif
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#else
#include "cJSON.h"
#include "file.h"
#endif
User *db_update_user(const int userid, const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser) {
@@ -57,7 +61,37 @@ User *db_update_user(const int userid, const char *username, const char *email,
pthread_mutex_unlock(&db_mutex);
return new_user(userid, username, email, password, group, is_superuser);
#else
User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false);
return asdf;
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
cJSON_ArrayForEach(user, users) {
cJSON *user_id = cJSON_GetObjectItem(user, "id");
if (user_id->valueint == userid) {
cJSON *user_username = cJSON_GetObjectItem(user, "username");
cJSON *user_password = cJSON_GetObjectItem(user, "password");
cJSON *user_email = cJSON_GetObjectItem(user, "email");
cJSON *user_group = cJSON_GetObjectItem(user, "group");
cJSON_SetValuestring(user_username, username);
cJSON_SetValuestring(user_password, password);
cJSON_SetValuestring(user_email, email);
cJSON_SetValuestring(user_group, group);
cJSON_DeleteItemFromObject(user, "is_superuser");
cJSON_AddBoolToObject(user, "is_superuser", *is_superuser);
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
free(new_data);
free(data);
cJSON_Delete(users);
User *ret =
new_user(userid, username, password, email, group, is_superuser);
pthread_mutex_unlock(&users_file_mutex);
return ret;
}
}
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return nullptr;
#endif
}
+38 -5
View File
@@ -1,10 +1,13 @@
#ifdef USE_SQLITE3
#include "database.h"
#include "sql.h"
#endif
#include "user.h"
#ifdef USE_SQLITE3
#include "sql.h"
#else
#include "cJSON.h"
#include "file.h"
#endif
bool db_update_user_field(const int id, const char *field, const char *value) {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
@@ -37,6 +40,36 @@ bool db_update_user_field(const int id, const char *field, const char *value) {
pthread_mutex_unlock(&db_mutex);
return sqlite3_changes(db) > 0;
#else
return 0;
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
cJSON_ArrayForEach(user, users) {
cJSON *user_id = cJSON_GetObjectItem(user, "id");
if (user_id->valueint == id) {
cJSON *user_field = cJSON_GetObjectItem(user, field);
if (cJSON_IsNumber(user_field)) {
// cJSON_SetNumberValue(user_field, value);
return false;
} else if (cJSON_IsBool(user_field)) {
// cJSON_DeleteItemFromObject(user, field);
// cJSON_AddBoolToObject(user, field, value);
return false;
} else if (cJSON_IsString(user_field)) {
cJSON_SetValuestring(user_field, value);
}
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
free(new_data);
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return true;
}
}
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return false;
#endif
}
+1 -1
View File
@@ -28,7 +28,7 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
const bool is_superuser_value = is_superuser->valueint ? true : false;
User *new_user_data = db_insert_user(
username->valuestring, email->valuestring, password->valuestring,
group->valuestring, (const bool *)is_superuser_value);
group->valuestring, &is_superuser_value);
if (new_user_data == nullptr) {
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("failed to create user"));