diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 1202d2c8cf2464..302575f7dc8fe0 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -109,6 +109,8 @@ bool DatabaseSync::Open() { int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; int r = sqlite3_open_v2(location_.c_str(), &connection_, flags, nullptr); CHECK_ERROR_OR_THROW(env()->isolate(), connection_, r, SQLITE_OK, false); + int r2 = sqlite3_enable_load_extension(connection_, 1); + CHECK_ERROR_OR_THROW(env()->isolate(), connection_, r2, SQLITE_OK, false); return true; } @@ -211,6 +213,24 @@ void DatabaseSync::Exec(const FunctionCallbackInfo& args) { CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void()); } +void DatabaseSync::LoadExtension(const FunctionCallbackInfo& args) { + DatabaseSync* db; + ASSIGN_OR_RETURN_UNWRAP(&db, args.This()); + Environment* env = Environment::GetCurrent(args); + THROW_AND_RETURN_ON_BAD_STATE( + env, db->connection_ == nullptr, "database is not open"); + + if (!args[0]->IsString()) { + node::THROW_ERR_INVALID_ARG_TYPE(env->isolate(), + "The \"path\" argument must be a string."); + return; + } + + auto path = node::Utf8Value(env->isolate(), args[0].As()); + int r = sqlite3_load_extension(db->connection_, *path, nullptr, nullptr); + CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void()); +} + StatementSync::StatementSync(Environment* env, Local object, sqlite3* db, @@ -658,6 +678,7 @@ static void Initialize(Local target, SetProtoMethod(isolate, db_tmpl, "close", DatabaseSync::Close); SetProtoMethod(isolate, db_tmpl, "prepare", DatabaseSync::Prepare); SetProtoMethod(isolate, db_tmpl, "exec", DatabaseSync::Exec); + SetProtoMethod(isolate, db_tmpl, "load_extension", DatabaseSync::LoadExtension); SetConstructorFunction(context, target, "DatabaseSync", db_tmpl); SetConstructorFunction(context, target, diff --git a/src/node_sqlite.h b/src/node_sqlite.h index 56b937a719679b..004dd074fad080 100644 --- a/src/node_sqlite.h +++ b/src/node_sqlite.h @@ -25,6 +25,7 @@ class DatabaseSync : public BaseObject { static void Close(const v8::FunctionCallbackInfo& args); static void Prepare(const v8::FunctionCallbackInfo& args); static void Exec(const v8::FunctionCallbackInfo& args); + static void LoadExtension(const v8::FunctionCallbackInfo& args); SET_MEMORY_INFO_NAME(DatabaseSync) SET_SELF_SIZE(DatabaseSync)