Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ libsqlite3-sys = {version="0.26.0", optional=true, features=["bundled"]}
[dev-dependencies]
rusqlite = "0.29.0"
libsqlite3-sys = {version="0.26.0", default-features = false, features=["bundled"]}
rand = "0.8.5"

[features]
static = ["libsqlite3-sys"]
Expand Down
12 changes: 11 additions & 1 deletion examples/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! cargo build --example scalar
//! sqlite3 :memory: '.read examples/test.sql'

use sqlite_loadable::prelude::*;
use sqlite_loadable::{prelude::*, define_scalar_function_with_aux};
use sqlite_loadable::{api, define_scalar_function, Result};

use rand::{self, Rng};

// yo()
fn yo(context: *mut sqlite3_context, _values: &[*mut sqlite3_value]) -> Result<()> {
api::result_text(context, "yo")?;
Expand Down Expand Up @@ -38,12 +40,20 @@ fn connect(context: *mut sqlite3_context, values: &[*mut sqlite3_value]) -> Resu
Ok(())
}

fn random_int64(context: *mut sqlite3_context, _values: &[*mut sqlite3_value], rng: &mut rand::rngs::ThreadRng) -> Result<()> {
let random_int64: i64 = rng.gen();
api::result_int64(context, random_int64);
Ok(())
}

#[sqlite_entrypoint]
pub fn sqlite3_scalarrs_init(db: *mut sqlite3) -> Result<()> {
let flags = FunctionFlags::UTF8 | FunctionFlags::DETERMINISTIC;
define_scalar_function(db, "surround_rs", 1, surround, flags)?;
define_scalar_function(db, "connect", -1, connect, flags)?;
define_scalar_function(db, "yo_rs", 0, yo, flags)?;
define_scalar_function(db, "add_rs", 2, add, flags)?;
let rng = rand::thread_rng();
define_scalar_function_with_aux(db, "random_int64", 0, random_int64, flags, rng)?;
Ok(())
}
50 changes: 31 additions & 19 deletions src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,54 +141,66 @@ where
/// application "pointer" as any rust type. Can be accessed in the callback
/// function as the 3rd argument, as a reference.
/// <https://www.sqlite.org/c3ref/create_function.html#:~:text=The%20fifth%20parameter%20is%20an%20arbitrary%20pointer.>
pub fn define_scalar_function_with_aux<F, T>(
type ValueScalarCallbackWithAux<T> = fn(*mut sqlite3_context, &[*mut sqlite3_value], &mut T) -> Result<()>;

struct ScalarCallbackWithAux<T> {
x_func: ValueScalarCallbackWithAux<T>,
aux: T,
}

pub fn define_scalar_function_with_aux<T>(
db: *mut sqlite3,
name: &str,
num_args: c_int,
x_func: F,
x_func: ValueScalarCallbackWithAux<T>,
func_flags: FunctionFlags,
aux: T,
) -> Result<()>
where
F: Fn(*mut sqlite3_context, &[*mut sqlite3_value], &T) -> Result<()>,
{
let function_pointer: *mut F = Box::into_raw(Box::new(x_func));
let aux_pointer: *mut T = Box::into_raw(Box::new(aux));
let app_pointer = Box::into_raw(Box::new((function_pointer, aux_pointer)));
let app_pointer = Box::into_raw(
Box::new(
ScalarCallbackWithAux { x_func, aux }
)
);

unsafe extern "C" fn x_func_wrapper<F, T>(
unsafe extern "C" fn x_func_wrapper<T>(
context: *mut sqlite3_context,
argc: c_int,
argv: *mut *mut sqlite3_value,
) where
F: Fn(*mut sqlite3_context, &[*mut sqlite3_value], &T) -> Result<()>,
)
{
let x = sqlite3ext_user_data(context).cast::<(*mut F, *mut T)>();
let boxed_function = (*x).0;
let aux = (*x).1;
// .collect slows things waaaay down, so stick with slice for now
let x = sqlite3ext_user_data(context).cast::<ScalarCallbackWithAux<T>>();

let args = slice::from_raw_parts(argv, argc as usize);
let b = Box::from_raw(aux);
match (*boxed_function)(context, args, &*b) {

match ((*x).x_func)(context, args, &mut (*x).aux) {
Ok(()) => (),
Err(e) => {
if api::result_error(context, &e.result_error_message()).is_err() {
api::result_error_code(context, SQLITE_INTERNAL);
}
}
}
Box::into_raw(b);
}

unsafe extern "C" fn destroy<T>(
p_app: *mut c_void,
)
{
let callbacks = p_app.cast::<ScalarCallbackWithAux<T>>();
let _ = Box::from_raw(callbacks); // drop
}

create_function_v2(
db,
name,
num_args,
func_flags,
app_pointer.cast::<c_void>(),
Some(x_func_wrapper::<F, T>),
None,
Some(x_func_wrapper::<T>),
None,
None,
Some(destroy::<T>),
)
}

Expand Down