Skip to content

Commit c9b6e75

Browse files
thisalihassanaduh95
authored andcommitted
sqlite: refactor error helpers and user function pointers
Signed-off-by: Ali Hassan <ali-hassan27@outlook.com> PR-URL: #62794 Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5410d94 commit c9b6e75

2 files changed

Lines changed: 57 additions & 66 deletions

File tree

‎src/node_sqlite.cc‎

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -176,59 +176,49 @@ static constexpr const LimitInfo* GetLimitInfoFromName(std::string_view name) {
176176
return nullptr;
177177
}
178178

179-
inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate,
180-
const char* message) {
179+
namespace {
180+
MaybeLocal<Object> CreateSQLiteErrorImpl(Isolate* isolate,
181+
const char* message,
182+
const char* errstr,
183+
int errcode) {
184+
Environment* env = Environment::GetCurrent(isolate);
185+
Local<Context> context = isolate->GetCurrentContext();
181186
Local<String> js_msg;
182187
Local<Object> e;
183-
Environment* env = Environment::GetCurrent(isolate);
184188
if (!String::NewFromUtf8(isolate, message).ToLocal(&js_msg) ||
185-
!Exception::Error(js_msg)
186-
->ToObject(isolate->GetCurrentContext())
187-
.ToLocal(&e) ||
188-
e->Set(isolate->GetCurrentContext(),
189-
env->code_string(),
190-
env->err_sqlite_error_string())
189+
!Exception::Error(js_msg)->ToObject(context).ToLocal(&e) ||
190+
e->Set(context, env->code_string(), env->err_sqlite_error_string())
191191
.IsNothing()) {
192192
return MaybeLocal<Object>();
193193
}
194+
195+
if (errstr != nullptr) {
196+
Local<String> js_errstr;
197+
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errstr) ||
198+
e->Set(context, env->errcode_string(), Integer::New(isolate, errcode))
199+
.IsNothing() ||
200+
e->Set(context, env->errstr_string(), js_errstr).IsNothing()) {
201+
return MaybeLocal<Object>();
202+
}
203+
}
194204
return e;
195205
}
206+
} // namespace
207+
208+
inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate,
209+
const char* message) {
210+
return CreateSQLiteErrorImpl(isolate, message, nullptr, 0);
211+
}
196212

197213
inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, int errcode) {
198214
const char* errstr = sqlite3_errstr(errcode);
199-
Local<String> js_errmsg;
200-
Local<Object> e;
201-
Environment* env = Environment::GetCurrent(isolate);
202-
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) ||
203-
!CreateSQLiteError(isolate, errstr).ToLocal(&e) ||
204-
e->Set(env->context(),
205-
env->errcode_string(),
206-
Integer::New(isolate, errcode))
207-
.IsNothing() ||
208-
e->Set(env->context(), env->errstr_string(), js_errmsg).IsNothing()) {
209-
return MaybeLocal<Object>();
210-
}
211-
return e;
215+
return CreateSQLiteErrorImpl(isolate, errstr, errstr, errcode);
212216
}
213217

214218
inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, sqlite3* db) {
215219
int errcode = sqlite3_extended_errcode(db);
216-
const char* errstr = sqlite3_errstr(errcode);
217-
const char* errmsg = sqlite3_errmsg(db);
218-
Local<String> js_errmsg;
219-
Local<Object> e;
220-
Environment* env = Environment::GetCurrent(isolate);
221-
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) ||
222-
!CreateSQLiteError(isolate, errmsg).ToLocal(&e) ||
223-
e->Set(isolate->GetCurrentContext(),
224-
env->errcode_string(),
225-
Integer::New(isolate, errcode))
226-
.IsNothing() ||
227-
e->Set(isolate->GetCurrentContext(), env->errstr_string(), js_errmsg)
228-
.IsNothing()) {
229-
return MaybeLocal<Object>();
230-
}
231-
return e;
220+
return CreateSQLiteErrorImpl(
221+
isolate, sqlite3_errmsg(db), sqlite3_errstr(errcode), errcode);
232222
}
233223

234224
void JSValueToSQLiteResult(Isolate* isolate,
@@ -313,14 +303,14 @@ inline MaybeLocal<Value> NullableSQLiteStringToValue(Isolate* isolate,
313303
class CustomAggregate {
314304
public:
315305
explicit CustomAggregate(Environment* env,
316-
DatabaseSync* db,
306+
BaseObjectWeakPtr<DatabaseSync> db,
317307
bool use_bigint_args,
318308
Local<Value> start,
319309
Local<Function> step_fn,
320310
Local<Function> inverse_fn,
321311
Local<Function> result_fn)
322312
: env_(env),
323-
db_(db),
313+
db_(std::move(db)),
324314
use_bigint_args_(use_bigint_args),
325315
start_(env->isolate(), start),
326316
step_fn_(env->isolate(), step_fn),
@@ -356,7 +346,7 @@ class CustomAggregate {
356346
Global<Function> CustomAggregate::*mptr) {
357347
CustomAggregate* self =
358348
static_cast<CustomAggregate*>(sqlite3_user_data(ctx));
359-
CallbackDepthGuard guard(self->db_);
349+
CallbackDepthGuard guard(self->db_.get());
360350
Environment* env = self->env_;
361351
Isolate* isolate = env->isolate();
362352
auto agg = self->GetAggregate(ctx);
@@ -414,7 +404,7 @@ class CustomAggregate {
414404
static inline void xValueBase(sqlite3_context* ctx, bool is_final) {
415405
CustomAggregate* self =
416406
static_cast<CustomAggregate*>(sqlite3_user_data(ctx));
417-
CallbackDepthGuard guard(self->db_);
407+
CallbackDepthGuard guard(self->db_.get());
418408
Environment* env = self->env_;
419409
Isolate* isolate = env->isolate();
420410
auto agg = self->GetAggregate(ctx);
@@ -493,7 +483,7 @@ class CustomAggregate {
493483
}
494484

495485
Environment* env_;
496-
DatabaseSync* db_;
486+
BaseObjectWeakPtr<DatabaseSync> db_;
497487
bool use_bigint_args_;
498488
Global<Value> start_;
499489
Global<Function> step_fn_;
@@ -676,11 +666,11 @@ class BackupJob : public ThreadPoolWork {
676666

677667
UserDefinedFunction::UserDefinedFunction(Environment* env,
678668
Local<Function> fn,
679-
DatabaseSync* db,
669+
BaseObjectWeakPtr<DatabaseSync> db,
680670
bool use_bigint_args)
681671
: env_(env),
682672
fn_(env->isolate(), fn),
683-
db_(db),
673+
db_(std::move(db)),
684674
use_bigint_args_(use_bigint_args) {}
685675

686676
UserDefinedFunction::~UserDefinedFunction() {}
@@ -690,7 +680,7 @@ void UserDefinedFunction::xFunc(sqlite3_context* ctx,
690680
sqlite3_value** argv) {
691681
UserDefinedFunction* self =
692682
static_cast<UserDefinedFunction*>(sqlite3_user_data(ctx));
693-
CallbackDepthGuard guard(self->db_);
683+
CallbackDepthGuard guard(self->db_.get());
694684
Environment* env = self->env_;
695685
Isolate* isolate = env->isolate();
696686
auto recv = Undefined(isolate);
@@ -1767,8 +1757,8 @@ void DatabaseSync::CustomFunction(const FunctionCallbackInfo<Value>& args) {
17671757
argc = js_len.As<Int32>()->Value();
17681758
}
17691759

1770-
UserDefinedFunction* user_data =
1771-
new UserDefinedFunction(env, fn, db, use_bigint_args);
1760+
UserDefinedFunction* user_data = new UserDefinedFunction(
1761+
env, fn, BaseObjectWeakPtr<DatabaseSync>(db), use_bigint_args);
17721762
int text_rep = SQLITE_UTF8;
17731763

17741764
if (deterministic) {
@@ -2089,22 +2079,23 @@ void DatabaseSync::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
20892079

20902080
auto xInverse = !inverseFunc.IsEmpty() ? CustomAggregate::xInverse : nullptr;
20912081
auto xValue = xInverse ? CustomAggregate::xValue : nullptr;
2092-
int r = sqlite3_create_window_function(db->connection_,
2093-
*name,
2094-
argc,
2095-
text_rep,
2096-
new CustomAggregate(env,
2097-
db,
2098-
use_bigint_args,
2099-
start_v,
2100-
stepFunction,
2101-
inverseFunc,
2102-
resultFunction),
2103-
CustomAggregate::xStep,
2104-
CustomAggregate::xFinal,
2105-
xValue,
2106-
xInverse,
2107-
CustomAggregate::xDestroy);
2082+
int r = sqlite3_create_window_function(
2083+
db->connection_,
2084+
*name,
2085+
argc,
2086+
text_rep,
2087+
new CustomAggregate(env,
2088+
BaseObjectWeakPtr<DatabaseSync>(db),
2089+
use_bigint_args,
2090+
start_v,
2091+
stepFunction,
2092+
inverseFunc,
2093+
resultFunction),
2094+
CustomAggregate::xStep,
2095+
CustomAggregate::xFinal,
2096+
xValue,
2097+
xInverse,
2098+
CustomAggregate::xDestroy);
21082099
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
21092100
}
21102101

‎src/node_sqlite.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ class UserDefinedFunction {
428428
public:
429429
UserDefinedFunction(Environment* env,
430430
v8::Local<v8::Function> fn,
431-
DatabaseSync* db,
431+
BaseObjectWeakPtr<DatabaseSync> db,
432432
bool use_bigint_args);
433433
~UserDefinedFunction();
434434
static void xFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv);
@@ -437,7 +437,7 @@ class UserDefinedFunction {
437437
private:
438438
Environment* env_;
439439
v8::Global<v8::Function> fn_;
440-
DatabaseSync* db_;
440+
BaseObjectWeakPtr<DatabaseSync> db_;
441441
bool use_bigint_args_;
442442
};
443443

0 commit comments

Comments
 (0)