Skip to content

Commit f1d4c4e

Browse files
authored
Merge pull request #1612 from tursodatabase/extensions_bindings
C bindings: Loading extensions
2 parents 5dcb708 + 3439af2 commit f1d4c4e

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

bindings/c/include/libsql.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ void libsql_close(libsql_database_t db);
9191

9292
int libsql_connect(libsql_database_t db, libsql_connection_t *out_conn, const char **out_err_msg);
9393

94+
int libsql_load_extension(libsql_connection_t conn,
95+
const char *path,
96+
const char *entry_point,
97+
const char **out_err_msg);
98+
9499
int libsql_reset(libsql_connection_t conn, const char **out_err_msg);
95100

96101
void libsql_disconnect(libsql_connection_t conn);

bindings/c/src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate lazy_static;
66
mod types;
77

88
use crate::types::libsql_config;
9+
use libsql::{errors, LoadExtensionGuard};
910
use tokio::runtime::Runtime;
1011
use types::{
1112
blob, libsql_connection, libsql_connection_t, libsql_database, libsql_database_t, libsql_row,
@@ -301,6 +302,55 @@ pub unsafe extern "C" fn libsql_connect(
301302
0
302303
}
303304

305+
#[no_mangle]
306+
pub unsafe extern "C" fn libsql_load_extension(
307+
conn: libsql_connection_t,
308+
path: *const std::ffi::c_char,
309+
entry_point: *const std::ffi::c_char,
310+
out_err_msg: *mut *const std::ffi::c_char,
311+
) -> std::ffi::c_int {
312+
if path.is_null() {
313+
set_err_msg("Null path".to_string(), out_err_msg);
314+
return 1;
315+
}
316+
let path = unsafe { std::ffi::CStr::from_ptr(path) };
317+
let path = match path.to_str() {
318+
Ok(path) => path,
319+
Err(e) => {
320+
set_err_msg(format!("Wrong path: {}", e), out_err_msg);
321+
return 2;
322+
}
323+
};
324+
let mut entry_point_option = None;
325+
if !entry_point.is_null() {
326+
let entry_point = unsafe { std::ffi::CStr::from_ptr(entry_point) };
327+
entry_point_option = match entry_point.to_str() {
328+
Ok(entry_point) => Some(entry_point),
329+
Err(e) => {
330+
set_err_msg(format!("Wrong entry point: {}", e), out_err_msg);
331+
return 4;
332+
}
333+
};
334+
}
335+
if conn.is_null() {
336+
set_err_msg("Null connection".to_string(), out_err_msg);
337+
return 5;
338+
}
339+
let conn = conn.get_ref();
340+
match RT.block_on(async move {
341+
let _guard = LoadExtensionGuard::new(conn)?;
342+
conn.load_extension(path, entry_point_option)?;
343+
Ok::<(), errors::Error>(())
344+
}) {
345+
Ok(()) => {}
346+
Err(e) => {
347+
set_err_msg(format!("Error loading extension: {}", e), out_err_msg);
348+
return 6;
349+
}
350+
};
351+
0
352+
}
353+
304354
#[no_mangle]
305355
pub unsafe extern "C" fn libsql_reset(
306356
conn: libsql_connection_t,

0 commit comments

Comments
 (0)