Skip to content

Commit 10faf88

Browse files
committed
sqlite: support db.loadExtension
1 parent 2bcf999 commit 10faf88

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/node_sqlite.cc

+23
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ bool DatabaseSync::Open() {
109109
int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
110110
int r = sqlite3_open_v2(location_.c_str(), &connection_, flags, nullptr);
111111
CHECK_ERROR_OR_THROW(env()->isolate(), connection_, r, SQLITE_OK, false);
112+
int r2 = sqlite3_db_config(
113+
connection_, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, NULL);
114+
CHECK_ERROR_OR_THROW(env()->isolate(), connection_, r2, SQLITE_OK, false);
112115
return true;
113116
}
114117

@@ -211,6 +214,24 @@ void DatabaseSync::Exec(const FunctionCallbackInfo<Value>& args) {
211214
CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void());
212215
}
213216

217+
void DatabaseSync::LoadExtension(const FunctionCallbackInfo<Value>& args) {
218+
DatabaseSync* db;
219+
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
220+
Environment* env = Environment::GetCurrent(args);
221+
THROW_AND_RETURN_ON_BAD_STATE(
222+
env, db->connection_ == nullptr, "database is not open");
223+
224+
if (!args[0]->IsString()) {
225+
node::THROW_ERR_INVALID_ARG_TYPE(env->isolate(),
226+
"The \"path\" argument must be a string.");
227+
return;
228+
}
229+
230+
auto path = node::Utf8Value(env->isolate(), args[0].As<String>());
231+
int r = sqlite3_load_extension(db->connection_, *path, nullptr, nullptr);
232+
CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void());
233+
}
234+
214235
StatementSync::StatementSync(Environment* env,
215236
Local<Object> object,
216237
sqlite3* db,
@@ -668,6 +689,8 @@ static void Initialize(Local<Object> target,
668689
SetProtoMethod(isolate, db_tmpl, "close", DatabaseSync::Close);
669690
SetProtoMethod(isolate, db_tmpl, "prepare", DatabaseSync::Prepare);
670691
SetProtoMethod(isolate, db_tmpl, "exec", DatabaseSync::Exec);
692+
SetProtoMethod(
693+
isolate, db_tmpl, "loadExtension", DatabaseSync::LoadExtension);
671694
SetConstructorFunction(context, target, "DatabaseSync", db_tmpl);
672695
SetConstructorFunction(context,
673696
target,

src/node_sqlite.h

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class DatabaseSync : public BaseObject {
2525
static void Close(const v8::FunctionCallbackInfo<v8::Value>& args);
2626
static void Prepare(const v8::FunctionCallbackInfo<v8::Value>& args);
2727
static void Exec(const v8::FunctionCallbackInfo<v8::Value>& args);
28+
static void LoadExtension(const v8::FunctionCallbackInfo<v8::Value>& args);
2829

2930
SET_MEMORY_INFO_NAME(DatabaseSync)
3031
SET_SELF_SIZE(DatabaseSync)

0 commit comments

Comments
 (0)