FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

sqlite: refactor error helpers and user function pointers · nodejs/node@ca88587 · GitHub

/ node Public

Commit ca88587

Browse files
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 ee7bf09 commit ca88587

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
@@ -170,59 +170,49 @@ static constexpr const LimitInfo* GetLimitInfoFromName(std::string_view name) {
170170
return nullptr;
171171
}
172172

173-
inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate,
174-
const char* message) {
173+
namespace {
174+
MaybeLocal<Object> CreateSQLiteErrorImpl(Isolate* isolate,
175+
const char* message,
176+
const char* errstr,
177+
int errcode) {
178+
Environment* env = Environment::GetCurrent(isolate);
179+
Local<Context> context = isolate->GetCurrentContext();
175180
Local<String> js_msg;
176181
Local<Object> e;
177-
Environment* env = Environment::GetCurrent(isolate);
178182
if (!String::NewFromUtf8(isolate, message).ToLocal(&js_msg) ||
179-
!Exception::Error(js_msg)
180-
->ToObject(isolate->GetCurrentContext())
181-
.ToLocal(&e) ||
182-
e->Set(isolate->GetCurrentContext(),
183-
env->code_string(),
184-
env->err_sqlite_error_string())
183+
!Exception::Error(js_msg)->ToObject(context).ToLocal(&e) ||
184+
e->Set(context, env->code_string(), env->err_sqlite_error_string())
185185
.IsNothing()) {
186186
return MaybeLocal<Object>();
187187
}
188+
189+
if (errstr != nullptr) {
190+
Local<String> js_errstr;
191+
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errstr) ||
192+
e->Set(context, env->errcode_string(), Integer::New(isolate, errcode))
193+
.IsNothing() ||
194+
e->Set(context, env->errstr_string(), js_errstr).IsNothing()) {
195+
return MaybeLocal<Object>();
196+
}
197+
}
188198
return e;
189199
}
200+
} // namespace
201+
202+
inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate,
203+
const char* message) {
204+
return CreateSQLiteErrorImpl(isolate, message, nullptr, 0);
205+
}
190206

191207
inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, int errcode) {
192208
const char* errstr = sqlite3_errstr(errcode);
193-
Local<String> js_errmsg;
194-
Local<Object> e;
195-
Environment* env = Environment::GetCurrent(isolate);
196-
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) ||
197-
!CreateSQLiteError(isolate, errstr).ToLocal(&e) ||
198-
e->Set(env->context(),
199-
env->errcode_string(),
200-
Integer::New(isolate, errcode))
201-
.IsNothing() ||
202-
e->Set(env->context(), env->errstr_string(), js_errmsg).IsNothing()) {
203-
return MaybeLocal<Object>();
204-
}
205-
return e;
209+
return CreateSQLiteErrorImpl(isolate, errstr, errstr, errcode);
206210
}
207211

208212
inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, sqlite3* db) {
209213
int errcode = sqlite3_extended_errcode(db);
210-
const char* errstr = sqlite3_errstr(errcode);
211-
const char* errmsg = sqlite3_errmsg(db);
212-
Local<String> js_errmsg;
213-
Local<Object> e;
214-
Environment* env = Environment::GetCurrent(isolate);
215-
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) ||
216-
!CreateSQLiteError(isolate, errmsg).ToLocal(&e) ||
217-
e->Set(isolate->GetCurrentContext(),
218-
env->errcode_string(),
219-
Integer::New(isolate, errcode))
220-
.IsNothing() ||
221-
e->Set(isolate->GetCurrentContext(), env->errstr_string(), js_errmsg)
222-
.IsNothing()) {
223-
return MaybeLocal<Object>();
224-
}
225-
return e;
214+
return CreateSQLiteErrorImpl(
215+
isolate, sqlite3_errmsg(db), sqlite3_errstr(errcode), errcode);
226216
}
227217

228218
void JSValueToSQLiteResult(Isolate* isolate,
@@ -307,14 +297,14 @@ inline MaybeLocal<Value> NullableSQLiteStringToValue(Isolate* isolate,
307297
class CustomAggregate {
308298
public:
309299
explicit CustomAggregate(Environment* env,
310-
DatabaseSync* db,
300+
BaseObjectWeakPtr<DatabaseSync> db,
311301
bool use_bigint_args,
312302
Local<Value> start,
313303
Local<Function> step_fn,
314304
Local<Function> inverse_fn,
315305
Local<Function> result_fn)
316306
: env_(env),
317-
db_(db),
307+
db_(std::move(db)),
318308
use_bigint_args_(use_bigint_args),
319309
start_(env->isolate(), start),
320310
step_fn_(env->isolate(), step_fn),
@@ -350,7 +340,7 @@ class CustomAggregate {
350340
Global<Function> CustomAggregate::*mptr) {
351341
CustomAggregate* self =
352342
static_cast<CustomAggregate*>(sqlite3_user_data(ctx));
353-
CallbackDepthGuard guard(self->db_);
343+
CallbackDepthGuard guard(self->db_.get());
354344
Environment* env = self->env_;
355345
Isolate* isolate = env->isolate();
356346
auto agg = self->GetAggregate(ctx);
@@ -408,7 +398,7 @@ class CustomAggregate {
408398
static inline void xValueBase(sqlite3_context* ctx, bool is_final) {
409399
CustomAggregate* self =
410400
static_cast<CustomAggregate*>(sqlite3_user_data(ctx));
411-
CallbackDepthGuard guard(self->db_);
401+
CallbackDepthGuard guard(self->db_.get());
412402
Environment* env = self->env_;
413403
Isolate* isolate = env->isolate();
414404
auto agg = self->GetAggregate(ctx);
@@ -487,7 +477,7 @@ class CustomAggregate {
487477
}
488478

489479
Environment* env_;
490-
DatabaseSync* db_;
480+
BaseObjectWeakPtr<DatabaseSync> db_;
491481
bool use_bigint_args_;
492482
Global<Value> start_;
493483
Global<Function> step_fn_;
@@ -670,11 +660,11 @@ class BackupJob : public ThreadPoolWork {
670660

671661
UserDefinedFunction::UserDefinedFunction(Environment* env,
672662
Local<Function> fn,
673-
DatabaseSync* db,
663+
BaseObjectWeakPtr<DatabaseSync> db,
674664
bool use_bigint_args)
675665
: env_(env),
676666
fn_(env->isolate(), fn),
677-
db_(db),
667+
db_(std::move(db)),
678668
use_bigint_args_(use_bigint_args) {}
679669

680670
UserDefinedFunction::~UserDefinedFunction() {}
@@ -684,7 +674,7 @@ void UserDefinedFunction::xFunc(sqlite3_context* ctx,
684674
sqlite3_value** argv) {
685675
UserDefinedFunction* self =
686676
static_cast<UserDefinedFunction*>(sqlite3_user_data(ctx));
687-
CallbackDepthGuard guard(self->db_);
677+
CallbackDepthGuard guard(self->db_.get());
688678
Environment* env = self->env_;
689679
Isolate* isolate = env->isolate();
690680
auto recv = Undefined(isolate);
@@ -1735,8 +1725,8 @@ void DatabaseSync::CustomFunction(const FunctionCallbackInfo<Value>& args) {
17351725
argc = js_len.As<Int32>()->Value();
17361726
}
17371727

1738-
UserDefinedFunction* user_data =
1739-
new UserDefinedFunction(env, fn, db, use_bigint_args);
1728+
UserDefinedFunction* user_data = new UserDefinedFunction(
1729+
env, fn, BaseObjectWeakPtr<DatabaseSync>(db), use_bigint_args);
17401730
int text_rep = SQLITE_UTF8;
17411731

17421732
if (deterministic) {
@@ -2057,22 +2047,23 @@ void DatabaseSync::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
20572047

20582048
auto xInverse = !inverseFunc.IsEmpty() ? CustomAggregate::xInverse : nullptr;
20592049
auto xValue = xInverse ? CustomAggregate::xValue : nullptr;
2060-
int r = sqlite3_create_window_function(db->connection_,
2061-
*name,
2062-
argc,
2063-
text_rep,
2064-
new CustomAggregate(env,
2065-
db,
2066-
use_bigint_args,
2067-
start_v,
2068-
stepFunction,
2069-
inverseFunc,
2070-
resultFunction),
2071-
CustomAggregate::xStep,
2072-
CustomAggregate::xFinal,
2073-
xValue,
2074-
xInverse,
2075-
CustomAggregate::xDestroy);
2050+
int r = sqlite3_create_window_function(
2051+
db->connection_,
2052+
*name,
2053+
argc,
2054+
text_rep,
2055+
new CustomAggregate(env,
2056+
BaseObjectWeakPtr<DatabaseSync>(db),
2057+
use_bigint_args,
2058+
start_v,
2059+
stepFunction,
2060+
inverseFunc,
2061+
resultFunction),
2062+
CustomAggregate::xStep,
2063+
CustomAggregate::xFinal,
2064+
xValue,
2065+
xInverse,
2066+
CustomAggregate::xDestroy);
20762067
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
20772068
}
20782069

‎src/node_sqlite.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ class UserDefinedFunction {
430430
public:
431431
UserDefinedFunction(Environment* env,
432432
v8::Local<v8::Function> fn,
433-
DatabaseSync* db,
433+
BaseObjectWeakPtr<DatabaseSync> db,
434434
bool use_bigint_args);
435435
~UserDefinedFunction();
436436
static void xFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv);
@@ -439,7 +439,7 @@ class UserDefinedFunction {
439439
private:
440440
Environment* env_;
441441
v8::Global<v8::Function> fn_;
442-
DatabaseSync* db_;
442+
BaseObjectWeakPtr<DatabaseSync> db_;
443443
bool use_bigint_args_;
444444
};
445445

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL