Skip to main content

sqlite3_ext_fn

Attribute Macro sqlite3_ext_fn 

Source
#[sqlite3_ext_fn]
Expand description

Create a FunctionOptions for an application-defined function.

This macro declares a FunctionOptions constant with the provided values. The constant will take on a name based on the function, so for example applying this attribute to a function named “count_horses” or a trait named “CountHorses” will create a constant named “COUNT_HORSES_OPTS”.

§Syntax

Arguments passed to the macro are comma-separated. The following are supported:

  • n_args=N corresponds to set_n_args.
  • risk_level=X corresponds to set_risk_level.
  • deterministic corresponds to set_desterministic with true.

§Example

use sqlite3_ext::{function::*, *};

#[sqlite3_ext_fn(n_args=0, risk_level=Innocuous)]
pub fn random_number(ctx: &Context, args: &mut [&mut ValueRef]) -> Result<()> {
    ctx.set_result(4) // chosen by fair dice roll.
}

pub fn init(db: &Connection) -> Result<()> {
    db.create_scalar_function("random_number", &RANDOM_NUMBER_OPTS, random_number)
}