2021-10-24 11:46:30 -04:00
|
|
|
#include "database.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
static JSClassID _database_class_id;
|
|
|
|
static int _database_count;
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
typedef struct _database_t
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSContext* context;
|
|
|
|
JSValue object;
|
|
|
|
void* task;
|
|
|
|
sqlite3* db;
|
|
|
|
const char* id;
|
|
|
|
} database_t;
|
|
|
|
|
|
|
|
static JSValue _database_create(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data);
|
|
|
|
static void _database_finalizer(JSRuntime *runtime, JSValue value);
|
|
|
|
|
|
|
|
static JSValue _database_get(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _database_set(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _database_remove(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _database_get_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
2021-01-05 21:56:25 -05:00
|
|
|
static JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2021-10-24 11:46:30 -04:00
|
|
|
void tf_database_register(JSContext* context, sqlite3* sqlite)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_NewClassID(&_database_class_id);
|
|
|
|
JSClassDef def = {
|
|
|
|
.class_name = "Database",
|
|
|
|
.finalizer = &_database_finalizer,
|
|
|
|
};
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_NewClass(JS_GetRuntime(context), _database_class_id, &def) != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Failed to register database.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JSValue data[] = { JS_NewInt64(context, (int64_t)(intptr_t)sqlite) };
|
|
|
|
JSValue constructor = JS_NewCFunctionData(context, _database_create, 0, 0, 1, data);
|
|
|
|
JS_SetConstructorBit(context, constructor, true);
|
|
|
|
JS_SetPropertyStr(context, global, "Database", constructor);
|
|
|
|
JS_FreeValue(context, global);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _database_create(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
++_database_count;
|
|
|
|
JSValue object = JS_NewObjectClass(context, _database_class_id);
|
|
|
|
sqlite3* db = NULL;
|
|
|
|
JS_ToInt64(context, (int64_t*)&db, data[0]);
|
|
|
|
|
|
|
|
database_t* database = malloc(sizeof(database_t));
|
2021-10-10 17:51:38 -04:00
|
|
|
*database = (database_t)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
.task = JS_GetContextOpaque(context),
|
|
|
|
.context = context,
|
|
|
|
.object = object,
|
|
|
|
.db = db,
|
|
|
|
};
|
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
database->id = strdup(id);
|
|
|
|
JS_FreeCString(context, id);
|
|
|
|
JS_SetOpaque(object, database);
|
|
|
|
|
|
|
|
JS_SetPropertyStr(context, object, "get", JS_NewCFunction(context, _database_get, "get", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "set", JS_NewCFunction(context, _database_set, "set", 2));
|
|
|
|
JS_SetPropertyStr(context, object, "remove", JS_NewCFunction(context, _database_remove, "remove", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "getAll", JS_NewCFunction(context, _database_get_all, "getAll", 0));
|
2021-01-05 21:56:25 -05:00
|
|
|
JS_SetPropertyStr(context, object, "getLike", JS_NewCFunction(context, _database_get_like, "getLike", 1));
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _database_finalizer(JSRuntime *runtime, JSValue value)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
database_t* database = JS_GetOpaque(value, _database_class_id);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (database)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
free((void*)database->id);
|
|
|
|
free(database);
|
|
|
|
}
|
|
|
|
--_database_count;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _database_get(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue entry = JS_UNDEFINED;
|
|
|
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (database)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlite3_stmt* statement;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (sqlite3_prepare(database->db, "SELECT value FROM properties WHERE id = $1 AND key = $2", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
size_t length;
|
|
|
|
const char* keyString = JS_ToCStringLen(context, &length, argv[0]);
|
|
|
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK &&
|
|
|
|
sqlite3_bind_text(statement, 2, keyString, length, NULL) == SQLITE_OK &&
|
2021-10-10 17:51:38 -04:00
|
|
|
sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
entry = JS_NewStringLen(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0));
|
|
|
|
}
|
2021-08-19 16:10:37 -04:00
|
|
|
JS_FreeCString(context, keyString);
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlite3_finalize(statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _database_set(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (database)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlite3_stmt* statement;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (sqlite3_prepare(database->db, "INSERT OR REPLACE INTO properties (id, key, value) VALUES ($1, $2, $3)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
size_t keyLength;
|
|
|
|
const char* keyString = JS_ToCStringLen(context, &keyLength, argv[0]);
|
|
|
|
size_t valueLength;
|
|
|
|
const char* valueString = JS_ToCStringLen(context, &valueLength, argv[1]);
|
|
|
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK &&
|
|
|
|
sqlite3_bind_text(statement, 2, keyString, keyLength, NULL) == SQLITE_OK &&
|
|
|
|
sqlite3_bind_text(statement, 3, valueString, valueLength, NULL) == SQLITE_OK &&
|
2021-10-10 17:51:38 -04:00
|
|
|
sqlite3_step(statement) == SQLITE_OK)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-08-19 16:10:37 -04:00
|
|
|
JS_FreeCString(context, keyString);
|
|
|
|
JS_FreeCString(context, valueString);
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlite3_finalize(statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _database_remove(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (database)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlite3_stmt* statement;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (sqlite3_prepare(database->db, "DELETE FROM properties WHERE id = $1 AND key = $2", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
size_t keyLength;
|
|
|
|
const char* keyString = JS_ToCStringLen(context, &keyLength, argv[0]);
|
|
|
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK &&
|
|
|
|
sqlite3_bind_text(statement, 2, keyString, keyLength, NULL) == SQLITE_OK &&
|
2021-10-10 17:51:38 -04:00
|
|
|
sqlite3_step(statement) == SQLITE_OK)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-08-19 16:10:37 -04:00
|
|
|
JS_FreeCString(context, keyString);
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlite3_finalize(statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _database_get_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue array = JS_UNDEFINED;
|
|
|
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (database)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlite3_stmt* statement;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (sqlite3_prepare(database->db, "SELECT key, value FROM properties WHERE id = $1", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
{
|
|
|
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
array = JS_NewArray(context);
|
|
|
|
uint32_t index = 0;
|
2021-10-10 17:51:38 -04:00
|
|
|
while (sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyUint32(context, array, index++, JS_NewStringLen(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlite3_finalize(statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
2021-01-05 21:56:25 -05:00
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-05 21:56:25 -05:00
|
|
|
JSValue result = JS_UNDEFINED;
|
|
|
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (database)
|
|
|
|
{
|
2021-01-05 21:56:25 -05:00
|
|
|
sqlite3_stmt* statement;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (sqlite3_prepare(database->db, "SELECT key, value FROM properties WHERE id = ? AND KEY LIKE ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
{
|
2021-01-05 21:56:25 -05:00
|
|
|
const char* pattern = JS_ToCString(context, argv[0]);
|
|
|
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK &&
|
2021-10-10 17:51:38 -04:00
|
|
|
sqlite3_bind_text(statement, 2, pattern, -1, NULL) == SQLITE_OK)
|
|
|
|
{
|
2021-01-05 21:56:25 -05:00
|
|
|
result = JS_NewObject(context);
|
2021-10-10 17:51:38 -04:00
|
|
|
while (sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
{
|
2021-01-05 21:56:25 -05:00
|
|
|
JS_SetPropertyStr(
|
|
|
|
context,
|
|
|
|
result,
|
|
|
|
(const char*)sqlite3_column_text(statement, 0),
|
|
|
|
JS_NewStringLen(context, (const char*)sqlite3_column_text(statement, 1), sqlite3_column_bytes(statement, 1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_FreeCString(context, pattern);
|
|
|
|
sqlite3_finalize(statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|