Add a helper for getting array length: tf_util_get_length.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3925 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-07-09 15:13:35 +00:00
parent 982b5817a2
commit 7f350a3d87
8 changed files with 86 additions and 102 deletions

View File

@ -3,6 +3,7 @@
#include "mem.h"
#include "ssb.h"
#include "trace.h"
#include "util.js.h"
#include <base64c.h>
#include <sodium/crypto_hash_sha256.h>
@ -425,68 +426,65 @@ bool tf_ssb_db_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, i
static bool _tf_ssb_sqlite_bind_json(JSContext* context, sqlite3* db, sqlite3_stmt* statement, JSValue binds)
{
bool all_bound = true;
int32_t length = 0;
if (JS_IsUndefined(binds))
{
return true;
}
JSValue lengthval = JS_GetPropertyStr(context, binds, "length");
if (JS_ToInt32(context, &length, lengthval) == 0)
if (!JS_IsArray(context, binds))
{
for (int i = 0; i < length; i++)
printf("Expected bind parameters to be an array.\n");
return false;
}
bool all_bound = true;
int32_t length = tf_util_get_length(context, binds);
for (int i = 0; i < length; i++)
{
JSValue value = JS_GetPropertyUint32(context, binds, i);
if (JS_IsString(value))
{
JSValue value = JS_GetPropertyUint32(context, binds, i);
if (JS_IsString(value))
size_t str_len = 0;
const char* str = JS_ToCStringLen(context, &str_len, value);
if (str)
{
size_t str_len = 0;
const char* str = JS_ToCStringLen(context, &str_len, value);
if (str)
{
if (sqlite3_bind_text(statement, i + 1, str, str_len, SQLITE_TRANSIENT) != SQLITE_OK)
{
printf("failed to bind: %s\n", sqlite3_errmsg(db));
all_bound = false;
}
JS_FreeCString(context, str);
}
else
{
printf("expected cstring\n");
}
}
else if (JS_IsNumber(value))
{
int64_t number = 0;
JS_ToInt64(context, &number, value);
if (sqlite3_bind_int64(statement, i + 1, number) != SQLITE_OK)
{
printf("failed to bind: %s\n", sqlite3_errmsg(db));
all_bound = false;
}
}
else if (JS_IsNull(value))
{
if (sqlite3_bind_null(statement, i + 1) != SQLITE_OK)
if (sqlite3_bind_text(statement, i + 1, str, str_len, SQLITE_TRANSIENT) != SQLITE_OK)
{
printf("failed to bind: %s\n", sqlite3_errmsg(db));
all_bound = false;
}
JS_FreeCString(context, str);
}
else
{
const char* str = JS_ToCString(context, value);
printf("expected string: %s\n", str);
JS_FreeCString(context, str);
printf("expected cstring\n");
}
JS_FreeValue(context, value);
}
else if (JS_IsNumber(value))
{
int64_t number = 0;
JS_ToInt64(context, &number, value);
if (sqlite3_bind_int64(statement, i + 1, number) != SQLITE_OK)
{
printf("failed to bind: %s\n", sqlite3_errmsg(db));
all_bound = false;
}
}
else if (JS_IsNull(value))
{
if (sqlite3_bind_null(statement, i + 1) != SQLITE_OK)
{
printf("failed to bind: %s\n", sqlite3_errmsg(db));
all_bound = false;
}
}
else
{
const char* str = JS_ToCString(context, value);
printf("expected string: %s\n", str);
JS_FreeCString(context, str);
}
JS_FreeValue(context, value);
}
else
{
printf("expected array\n");
}
JS_FreeValue(context, lengthval);
return all_bound;
}