Skip to content

Commit

Permalink
SQLite extension loading support
Browse files Browse the repository at this point in the history
  • Loading branch information
penberg committed Jun 10, 2024
1 parent 78b014c commit 518e374
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crate-type = ["cdylib"]

[dependencies]
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
libsql = { git = "https://github.com/libsql/libsql/", rev = "84ff2f7578cb634e531ac0075c0286893273238b", features = ["encryption"] }
libsql = { git = "https://github.com/libsql/libsql/", rev = "21cb5c80fd8b06ee5f087309b35859f156f29ef0", features = ["encryption"] }
tracing = "0.1"
once_cell = "1.18.0"
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }
Expand Down
3 changes: 1 addition & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ This function is currently not supported.

### loadExtension(path, [entryPoint]) ⇒ this

This function is currently not supported.

Loads a SQLite3 extension
### exec(sql) ⇒ this

Executes a SQL statement.
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const {
databaseExecSync,
databasePrepareSync,
databaseDefaultSafeIntegers,
databaseLoadExtension,
statementRaw,
statementIsReader,
statementGet,
Expand Down Expand Up @@ -216,7 +217,7 @@ class Database {
}

loadExtension(...args) {
throw new Error("not implemented");
databaseLoadExtension.call(this.db, ...args);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,32 @@ impl Database {
self.default_safe_integers.replace(toggle);
}

pub fn js_load_extension(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let db: Handle<'_, JsBox<Database>> = cx.this()?;
let extension = cx.argument::<JsString>(0)?.value(&mut cx);
let entry_point: Option<&str> = match cx.argument_opt(1) {
Some(arg) => todo!(),
None => None,
};
trace!("Loading extension: {}", extension);
let conn = match db.get_conn(&mut cx) {
Some(conn) => conn,
None => throw_database_closed_error(&mut cx)?,
};
let conn = conn.blocking_lock();
if let Err(err) = conn.load_extension_enable() {
throw_libsql_error(&mut cx, err)?;
}
if let Err(err) = conn.load_extension(&extension, entry_point) {
let _ = conn.load_extension_disable();
throw_libsql_error(&mut cx, err)?;
}
if let Err(err) = conn.load_extension_disable() {
throw_libsql_error(&mut cx, err)?;
}
Ok(cx.undefined())
}

fn get_conn(&self, cx: &mut FunctionContext) -> Option<Arc<Mutex<libsql::Connection>>> {
let conn = self.conn.borrow();
conn.as_ref().map(|conn| conn.clone())
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
"databaseDefaultSafeIntegers",
Database::js_default_safe_integers,
)?;
cx.export_function("databaseLoadExtension", Database::js_load_extension)?;
cx.export_function("statementRaw", Statement::js_raw)?;
cx.export_function("statementIsReader", Statement::js_is_reader)?;
cx.export_function("statementRun", Statement::js_run)?;
Expand Down

0 comments on commit 518e374

Please sign in to comment.