From 3162244c2bfbca39cbbd6b014f2a8ca529767018 Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Sat, 18 Apr 2026 00:47:54 +0500 Subject: [PATCH] sqlite: refactor error helpers and user function pointers Signed-off-by: Ali Hassan --- src/node_sqlite.cc | 113 +++++++++++++++++++++------------------------ src/node_sqlite.h | 4 +- 2 files changed, 54 insertions(+), 63 deletions(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 4d1a35d753230a..b790064e98f1bd 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -170,59 +170,49 @@ static constexpr const LimitInfo* GetLimitInfoFromName(std::string_view name) { return nullptr; } -inline MaybeLocal CreateSQLiteError(Isolate* isolate, - const char* message) { +namespace { +MaybeLocal CreateSQLiteErrorImpl(Isolate* isolate, + const char* message, + const char* errstr, + int errcode) { + Environment* env = Environment::GetCurrent(isolate); + Local context = isolate->GetCurrentContext(); Local js_msg; Local e; - Environment* env = Environment::GetCurrent(isolate); if (!String::NewFromUtf8(isolate, message).ToLocal(&js_msg) || - !Exception::Error(js_msg) - ->ToObject(isolate->GetCurrentContext()) - .ToLocal(&e) || - e->Set(isolate->GetCurrentContext(), - env->code_string(), - env->err_sqlite_error_string()) + !Exception::Error(js_msg)->ToObject(context).ToLocal(&e) || + e->Set(context, env->code_string(), env->err_sqlite_error_string()) .IsNothing()) { return MaybeLocal(); } + + if (errstr != nullptr) { + Local js_errstr; + if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errstr) || + e->Set(context, env->errcode_string(), Integer::New(isolate, errcode)) + .IsNothing() || + e->Set(context, env->errstr_string(), js_errstr).IsNothing()) { + return MaybeLocal(); + } + } return e; } +} // namespace + +inline MaybeLocal CreateSQLiteError(Isolate* isolate, + const char* message) { + return CreateSQLiteErrorImpl(isolate, message, nullptr, 0); +} inline MaybeLocal CreateSQLiteError(Isolate* isolate, int errcode) { const char* errstr = sqlite3_errstr(errcode); - Local js_errmsg; - Local e; - Environment* env = Environment::GetCurrent(isolate); - if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) || - !CreateSQLiteError(isolate, errstr).ToLocal(&e) || - e->Set(env->context(), - env->errcode_string(), - Integer::New(isolate, errcode)) - .IsNothing() || - e->Set(env->context(), env->errstr_string(), js_errmsg).IsNothing()) { - return MaybeLocal(); - } - return e; + return CreateSQLiteErrorImpl(isolate, errstr, errstr, errcode); } inline MaybeLocal CreateSQLiteError(Isolate* isolate, sqlite3* db) { int errcode = sqlite3_extended_errcode(db); - const char* errstr = sqlite3_errstr(errcode); - const char* errmsg = sqlite3_errmsg(db); - Local js_errmsg; - Local e; - Environment* env = Environment::GetCurrent(isolate); - if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) || - !CreateSQLiteError(isolate, errmsg).ToLocal(&e) || - e->Set(isolate->GetCurrentContext(), - env->errcode_string(), - Integer::New(isolate, errcode)) - .IsNothing() || - e->Set(isolate->GetCurrentContext(), env->errstr_string(), js_errmsg) - .IsNothing()) { - return MaybeLocal(); - } - return e; + return CreateSQLiteErrorImpl( + isolate, sqlite3_errmsg(db), sqlite3_errstr(errcode), errcode); } void JSValueToSQLiteResult(Isolate* isolate, @@ -306,14 +296,14 @@ inline MaybeLocal NullableSQLiteStringToValue(Isolate* isolate, class CustomAggregate { public: explicit CustomAggregate(Environment* env, - DatabaseSync* db, + BaseObjectWeakPtr db, bool use_bigint_args, Local start, Local step_fn, Local inverse_fn, Local result_fn) : env_(env), - db_(db), + db_(std::move(db)), use_bigint_args_(use_bigint_args), start_(env->isolate(), start), step_fn_(env->isolate(), step_fn), @@ -472,7 +462,7 @@ class CustomAggregate { } Environment* env_; - DatabaseSync* db_; + BaseObjectWeakPtr db_; bool use_bigint_args_; Global start_; Global step_fn_; @@ -645,11 +635,11 @@ class BackupJob : public ThreadPoolWork { UserDefinedFunction::UserDefinedFunction(Environment* env, Local fn, - DatabaseSync* db, + BaseObjectWeakPtr db, bool use_bigint_args) : env_(env), fn_(env->isolate(), fn), - db_(db), + db_(std::move(db)), use_bigint_args_(use_bigint_args) {} UserDefinedFunction::~UserDefinedFunction() {} @@ -1684,8 +1674,8 @@ void DatabaseSync::CustomFunction(const FunctionCallbackInfo& args) { argc = js_len.As()->Value(); } - UserDefinedFunction* user_data = - new UserDefinedFunction(env, fn, db, use_bigint_args); + UserDefinedFunction* user_data = new UserDefinedFunction( + env, fn, BaseObjectWeakPtr(db), use_bigint_args); int text_rep = SQLITE_UTF8; if (deterministic) { @@ -2006,22 +1996,23 @@ void DatabaseSync::AggregateFunction(const FunctionCallbackInfo& args) { auto xInverse = !inverseFunc.IsEmpty() ? CustomAggregate::xInverse : nullptr; auto xValue = xInverse ? CustomAggregate::xValue : nullptr; - int r = sqlite3_create_window_function(db->connection_, - *name, - argc, - text_rep, - new CustomAggregate(env, - db, - use_bigint_args, - start_v, - stepFunction, - inverseFunc, - resultFunction), - CustomAggregate::xStep, - CustomAggregate::xFinal, - xValue, - xInverse, - CustomAggregate::xDestroy); + int r = sqlite3_create_window_function( + db->connection_, + *name, + argc, + text_rep, + new CustomAggregate(env, + BaseObjectWeakPtr(db), + use_bigint_args, + start_v, + stepFunction, + inverseFunc, + resultFunction), + CustomAggregate::xStep, + CustomAggregate::xFinal, + xValue, + xInverse, + CustomAggregate::xDestroy); CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void()); } diff --git a/src/node_sqlite.h b/src/node_sqlite.h index e7281ed266af5d..3f432d6ace4c3a 100644 --- a/src/node_sqlite.h +++ b/src/node_sqlite.h @@ -402,7 +402,7 @@ class UserDefinedFunction { public: UserDefinedFunction(Environment* env, v8::Local fn, - DatabaseSync* db, + BaseObjectWeakPtr db, bool use_bigint_args); ~UserDefinedFunction(); static void xFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv); @@ -411,7 +411,7 @@ class UserDefinedFunction { private: Environment* env_; v8::Global fn_; - DatabaseSync* db_; + BaseObjectWeakPtr db_; bool use_bigint_args_; };