Skip to main content

sqlite_functions/
register.rs

1use std::panic::{RefUnwindSafe, UnwindSafe};
2
3use rusqlite::functions::{Aggregate, Context, SqlFnOutput};
4use rusqlite::{Connection, Result};
5
6use crate::FunctionOptions;
7
8/// Register a scalar callback on one SQLite connection.
9///
10/// Registration belongs to the connection and lasts until it is closed or the
11/// function is explicitly removed.
12pub fn register_scalar<F, T>(
13    connection: &Connection,
14    options: FunctionOptions,
15    callback: F,
16) -> Result<()>
17where
18    F: Fn(&Context<'_>) -> Result<T> + Send + 'static,
19    T: SqlFnOutput,
20{
21    let (name, arity, flags) = options.into_parts();
22    connection.create_scalar_function(name.as_str(), arity, flags, callback)
23}
24
25/// Register a stateful aggregate callback on one SQLite connection.
26pub fn register_aggregate<A, D, T>(
27    connection: &Connection,
28    options: FunctionOptions,
29    aggregate: D,
30) -> Result<()>
31where
32    A: RefUnwindSafe + UnwindSafe,
33    D: Aggregate<A, T> + 'static,
34    T: SqlFnOutput,
35{
36    let (name, arity, flags) = options.into_parts();
37    connection.create_aggregate_function(name.as_str(), arity, flags, aggregate)
38}