From 842633f6d1c7a3c79dfa1cb129fbfa098833b84d Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 25 May 2022 23:45:52 +0000 Subject: [PATCH] Exposed some ways to explore databases. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3879 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/core.js | 7 +++++++ src/database.js.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/core/core.js b/core/core.js index 537eb50e4..93b62b788 100644 --- a/core/core.js +++ b/core/core.js @@ -199,6 +199,13 @@ async function getProcessBlob(blobId, key, options) { var db = new Database(process.credentials.session.name + ':' + key); return Object.fromEntries(Object.keys(db).map(x => [x, db[x].bind(db)])); }; + imports.my_shared_database = function(packageName, key) { + var db = new Database(':shared:' + process.credentials.session.name + ':' + packageName + ':' + key); + return Object.fromEntries(Object.keys(db).map(x => [x, db[x].bind(db)])); + }; + imports.databases = function() { + return [].concat(databases.list(':shared:' + process.credentials.session.name + ':%'), databases.list(process.credentials.session.name + ':%')); + }; } if (options.packageOwner && options.packageName) { imports.shared_database = function(key) { diff --git a/src/database.js.c b/src/database.js.c index 3d6024254..b8c8a8977 100644 --- a/src/database.js.c +++ b/src/database.js.c @@ -27,6 +27,7 @@ static JSValue _database_exchange(JSContext* context, JSValueConst this_val, int 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); static JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); +static JSValue _databases_list(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data); void tf_database_register(JSContext* context, sqlite3* sqlite) { @@ -46,6 +47,9 @@ void tf_database_register(JSContext* context, sqlite3* sqlite) JSValue constructor = JS_NewCFunctionData(context, _database_create, 0, 0, 1, data); JS_SetConstructorBit(context, constructor, true); JS_SetPropertyStr(context, global, "Database", constructor); + JSValue databases = JS_NewObject(context); + JS_SetPropertyStr(context, global, "databases", databases); + JS_SetPropertyStr(context, databases, "list", JS_NewCFunctionData(context, _databases_list, 0, 0, 1, data)); JS_FreeValue(context, global); } @@ -269,3 +273,29 @@ JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc, } return result; } + +static JSValue _databases_list(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data) +{ + int64_t value = 0; + JS_ToInt64(context, &value, data[0]); + sqlite3* db = (sqlite3*)(intptr_t)value; + + JSValue array = JS_UNDEFINED; + sqlite3_stmt* statement; + if (sqlite3_prepare(db, "SELECT DISTINCT id FROM properties WHERE id LIKE ?", -1, &statement, NULL) == SQLITE_OK) + { + const char* pattern = JS_ToCString(context, argv[0]); + if (sqlite3_bind_text(statement, 1, pattern, -1, NULL) == SQLITE_OK) + { + array = JS_NewArray(context); + uint32_t index = 0; + while (sqlite3_step(statement) == SQLITE_ROW) + { + JS_SetPropertyUint32(context, array, index++, JS_NewStringLen(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0))); + } + } + JS_FreeCString(context, pattern); + sqlite3_finalize(statement); + } + return array; +}