Skip to content

Commit fe5a33c

Browse files
committed
sqlite: refactor error helpers and user function pointers
Signed-off-by: Ali Hassan <ali-hassan27@outlook.com>
1 parent 5316f44 commit fe5a33c

2 files changed

Lines changed: 54 additions & 63 deletions

File tree

src/node_sqlite.cc

Lines changed: 52 additions & 61 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,
@@ -306,14 +296,14 @@ inline MaybeLocal<Value> NullableSQLiteStringToValue(Isolate* isolate,
306296
class CustomAggregate {
307297
public:
308298
explicit CustomAggregate(Environment* env,
309-
DatabaseSync* db,
299+
BaseObjectWeakPtr<DatabaseSync> db,
310300
bool use_bigint_args,
311301
Local<Value> start,
312302
Local<Function> step_fn,
313303
Local<Function> inverse_fn,
314304
Local<Function> result_fn)
315305
: env_(env),
316-
db_(db),
306+
db_(std::move(db)),
317307
use_bigint_args_(use_bigint_args),
318308
start_(env->isolate(), start),
319309
step_fn_(env->isolate(), step_fn),
@@ -472,7 +462,7 @@ class CustomAggregate {
472462
}
473463

474464
Environment* env_;
475-
DatabaseSync* db_;
465+
BaseObjectWeakPtr<DatabaseSync> db_;
476466
bool use_bigint_args_;
477467
Global<Value> start_;
478468
Global<Function> step_fn_;
@@ -655,11 +645,11 @@ class BackupJob : public ThreadPoolWork {
655645

656646
UserDefinedFunction::UserDefinedFunction(Environment* env,
657647
Local<Function> fn,
658-
DatabaseSync* db,
648+
BaseObjectWeakPtr<DatabaseSync> db,
659649
bool use_bigint_args)
660650
: env_(env),
661651
fn_(env->isolate(), fn),
662-
db_(db),
652+
db_(std::move(db)),
663653
use_bigint_args_(use_bigint_args) {}
664654

665655
UserDefinedFunction::~UserDefinedFunction() {}
@@ -1715,8 +1705,8 @@ void DatabaseSync::CustomFunction(const FunctionCallbackInfo<Value>& args) {
17151705
argc = js_len.As<Int32>()->Value();
17161706
}
17171707

1718-
UserDefinedFunction* user_data =
1719-
new UserDefinedFunction(env, fn, db, use_bigint_args);
1708+
UserDefinedFunction* user_data = new UserDefinedFunction(
1709+
env, fn, BaseObjectWeakPtr<DatabaseSync>(db), use_bigint_args);
17201710
int text_rep = SQLITE_UTF8;
17211711

17221712
if (deterministic) {
@@ -2037,22 +2027,23 @@ void DatabaseSync::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
20372027

20382028
auto xInverse = !inverseFunc.IsEmpty() ? CustomAggregate::xInverse : nullptr;
20392029
auto xValue = xInverse ? CustomAggregate::xValue : nullptr;
2040-
int r = sqlite3_create_window_function(db->connection_,
2041-
*name,
2042-
argc,
2043-
text_rep,
2044-
new CustomAggregate(env,
2045-
db,
2046-
use_bigint_args,
2047-
start_v,
2048-
stepFunction,
2049-
inverseFunc,
2050-
resultFunction),
2051-
CustomAggregate::xStep,
2052-
CustomAggregate::xFinal,
2053-
xValue,
2054-
xInverse,
2055-
CustomAggregate::xDestroy);
2030+
int r = sqlite3_create_window_function(
2031+
db->connection_,
2032+
*name,
2033+
argc,
2034+
text_rep,
2035+
new CustomAggregate(env,
2036+
BaseObjectWeakPtr<DatabaseSync>(db),
2037+
use_bigint_args,
2038+
start_v,
2039+
stepFunction,
2040+
inverseFunc,
2041+
resultFunction),
2042+
CustomAggregate::xStep,
2043+
CustomAggregate::xFinal,
2044+
xValue,
2045+
xInverse,
2046+
CustomAggregate::xDestroy);
20562047
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
20572048
}
20582049

src/node_sqlite.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class UserDefinedFunction {
402402
public:
403403
UserDefinedFunction(Environment* env,
404404
v8::Local<v8::Function> fn,
405-
DatabaseSync* db,
405+
BaseObjectWeakPtr<DatabaseSync> db,
406406
bool use_bigint_args);
407407
~UserDefinedFunction();
408408
static void xFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv);
@@ -411,7 +411,7 @@ class UserDefinedFunction {
411411
private:
412412
Environment* env_;
413413
v8::Global<v8::Function> fn_;
414-
DatabaseSync* db_;
414+
BaseObjectWeakPtr<DatabaseSync> db_;
415415
bool use_bigint_args_;
416416
};
417417

0 commit comments

Comments
 (0)