lib: improve base example
This commit is contained in:
+6
-15
@@ -1,18 +1,15 @@
|
||||
use sqlite3_ext::{
|
||||
function::{Context, FunctionOptions},
|
||||
*,
|
||||
Connection, FromValue, Result, ValueRef, ValueType,
|
||||
function::Context,
|
||||
sqlite3_ext_fn, sqlite3_ext_main,
|
||||
};
|
||||
|
||||
fn add(a: i32, b: i32) -> i32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[sqlite3_ext_fn(n_args = 2, deterministic)]
|
||||
fn add_sqlite(ctx: &mut Context, args: &mut [&mut ValueRef]) -> Result<()> {
|
||||
if args.len() != 2 {
|
||||
return Err(sqlite3_ext::Error::Module(
|
||||
"myadd requires exactly 2 arguments".to_string(),
|
||||
));
|
||||
}
|
||||
if args[0].value_type() != ValueType::Integer || args[1].value_type() != ValueType::Integer {
|
||||
return Err(sqlite3_ext::Error::Module(
|
||||
"myadd requires both arguments to be an integer".to_string(),
|
||||
@@ -28,13 +25,7 @@ fn add_sqlite(ctx: &mut Context, args: &mut [&mut ValueRef]) -> Result<()> {
|
||||
|
||||
#[sqlite3_ext_main(persistent)]
|
||||
fn init(db: &Connection) -> Result<()> {
|
||||
db.create_scalar_function(
|
||||
"myadd",
|
||||
&FunctionOptions::default()
|
||||
.set_n_args(2)
|
||||
.set_deterministic(true),
|
||||
add_sqlite,
|
||||
)?;
|
||||
db.create_scalar_function("myadd", &ADD_SQLITE_OPTS, add_sqlite)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -42,7 +33,7 @@ fn init(db: &Connection) -> Result<()> {
|
||||
#[cfg(all(test, feature = "static"))]
|
||||
mod test {
|
||||
use super::*;
|
||||
use sqlite3_ext::Error;
|
||||
use sqlite3_ext::{Database, Error, FallibleIterator, FallibleIteratorMut};
|
||||
|
||||
fn setup() -> Result<Database> {
|
||||
let conn = Database::open(":memory:")?;
|
||||
|
||||
Reference in New Issue
Block a user