hello/hello.rs
1//! cargo build --example hello
2//! sqlite3 :memory: '.read examples/test.sql'
3
4use sqlite_loadable::prelude::*;
5use sqlite_loadable::{api, define_scalar_function, Result};
6
7// This function will be registered as a scalar function named "hello", and will be called on
8// every invocation. It's goal is to return a string of "hello, NAME!" where NAME is the
9// text value of the 1st argument.
10pub fn hello(context: *mut sqlite3_context, values: &[*mut sqlite3_value]) -> Result<()> {
11 let name = api::value_text(values.get(0).expect("1st argument as name"))?;
12
13 api::result_text(context, format!("hello, {}!", name))?;
14 Ok(())
15}
16
17// Exposes a extern C function named "sqlite3_hello_init" in the compiled dynamic library,
18// the "entrypoint" that SQLite will use to load the extension.
19// Notice the naming sequence - "sqlite3_" followed by "hello" then "_init". Since the
20// compiled file is named "libhello.dylib" (or .so/.dll depending on your operating system),
21// SQLite by default will look for an entrypoint called "sqlite3_hello_init".
22// See "Loading an Extension" for more details <https://www.sqlite.org/loadext.html#loading_an_extension>
23#[sqlite_entrypoint]
24pub fn sqlite3_hello_init(db: *mut sqlite3) -> Result<()> {
25 let flags = FunctionFlags::UTF8 | FunctionFlags::DETERMINISTIC;
26 define_scalar_function(db, "hello", 1, hello, flags)?;
27 Ok(())
28}