@@ -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
191207inline 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
208212inline 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
228218void JSValueToSQLiteResult (Isolate* isolate,
@@ -306,14 +296,14 @@ inline MaybeLocal<Value> NullableSQLiteStringToValue(Isolate* isolate,
306296class 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_;
@@ -645,11 +635,11 @@ class BackupJob : public ThreadPoolWork {
645635
646636UserDefinedFunction::UserDefinedFunction (Environment* env,
647637 Local<Function> fn,
648- DatabaseSync* db,
638+ BaseObjectWeakPtr< DatabaseSync> db,
649639 bool use_bigint_args)
650640 : env_(env),
651641 fn_(env->isolate (), fn),
652- db_(db ),
642+ db_(std::move(db) ),
653643 use_bigint_args_(use_bigint_args) {}
654644
655645UserDefinedFunction::~UserDefinedFunction () {}
@@ -1684,8 +1674,8 @@ void DatabaseSync::CustomFunction(const FunctionCallbackInfo<Value>& args) {
16841674 argc = js_len.As <Int32>()->Value ();
16851675 }
16861676
1687- UserDefinedFunction* user_data =
1688- new UserDefinedFunction ( env, fn, db , use_bigint_args);
1677+ UserDefinedFunction* user_data = new UserDefinedFunction (
1678+ env, fn, BaseObjectWeakPtr<DatabaseSync>(db) , use_bigint_args);
16891679 int text_rep = SQLITE_UTF8 ;
16901680
16911681 if (deterministic) {
@@ -2006,22 +1996,23 @@ void DatabaseSync::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
20061996
20071997 auto xInverse = !inverseFunc.IsEmpty () ? CustomAggregate::xInverse : nullptr ;
20081998 auto xValue = xInverse ? CustomAggregate::xValue : nullptr ;
2009- int r = sqlite3_create_window_function (db->connection_ ,
2010- *name,
2011- argc,
2012- text_rep,
2013- new CustomAggregate (env,
2014- db,
2015- use_bigint_args,
2016- start_v,
2017- stepFunction,
2018- inverseFunc,
2019- resultFunction),
2020- CustomAggregate::xStep,
2021- CustomAggregate::xFinal,
2022- xValue,
2023- xInverse,
2024- CustomAggregate::xDestroy);
1999+ int r = sqlite3_create_window_function (
2000+ db->connection_ ,
2001+ *name,
2002+ argc,
2003+ text_rep,
2004+ new CustomAggregate (env,
2005+ BaseObjectWeakPtr<DatabaseSync>(db),
2006+ use_bigint_args,
2007+ start_v,
2008+ stepFunction,
2009+ inverseFunc,
2010+ resultFunction),
2011+ CustomAggregate::xStep,
2012+ CustomAggregate::xFinal,
2013+ xValue,
2014+ xInverse,
2015+ CustomAggregate::xDestroy);
20252016 CHECK_ERROR_OR_THROW (env->isolate (), db, r, SQLITE_OK , void ());
20262017}
20272018
0 commit comments