|
| 1 | +use sqlite_loadable::prelude::*; |
| 2 | +use sqlite_loadable::{api, define_scalar_function, Result}; |
| 3 | + |
| 4 | +pub fn check_auxdata(context: *mut sqlite3_context, values: &[*mut sqlite3_value]) -> Result<()> { |
| 5 | + let label = api::value_text(values.first().unwrap()).unwrap(); |
| 6 | + let value = api::value_text(values.get(1).unwrap()).unwrap(); |
| 7 | + |
| 8 | + assert!(api::auxdata_get::<String>(context, 1).is_none()); |
| 9 | + |
| 10 | + let b = Box::new(String::from(value)); |
| 11 | + api::auxdata_set::<String>(context, 1, b); |
| 12 | + |
| 13 | + let entry = api::auxdata_get::<String>(context, 1).unwrap(); |
| 14 | + assert!(entry == value); |
| 15 | + |
| 16 | + api::result_text(context, format!("{label}={value}")).unwrap(); |
| 17 | + |
| 18 | + Ok(()) |
| 19 | +} |
| 20 | + |
| 21 | +#[sqlite_entrypoint] |
| 22 | +pub fn sqlite3_test_auxdata_init(db: *mut sqlite3) -> Result<()> { |
| 23 | + define_scalar_function(db, "check_auxdata", 2, check_auxdata, FunctionFlags::UTF8)?; |
| 24 | + Ok(()) |
| 25 | +} |
| 26 | + |
| 27 | +#[cfg(test)] |
| 28 | +mod tests { |
| 29 | + use super::*; |
| 30 | + |
| 31 | + use rusqlite::{ffi::sqlite3_auto_extension, Connection}; |
| 32 | + |
| 33 | + #[test] |
| 34 | + fn test_rusqlite_auto_extension() { |
| 35 | + unsafe { |
| 36 | + sqlite3_auto_extension(Some(std::mem::transmute( |
| 37 | + sqlite3_test_auxdata_init as *const (), |
| 38 | + ))); |
| 39 | + } |
| 40 | + |
| 41 | + let conn = Connection::open_in_memory().unwrap(); |
| 42 | + |
| 43 | + // NOTE: even nested expressions are evaluated in different contexts leading to an |
| 44 | + // auxdata_get miss. auxdata_get/set is not suitable for naive caching across function |
| 45 | + // evaluations. |
| 46 | + let result: String = conn |
| 47 | + .query_row( |
| 48 | + "SELECT (check_auxdata(?1, check_auxdata(?2, ?3)))", |
| 49 | + ("outer_label", "inner_label", "value"), |
| 50 | + |row| { |
| 51 | + println!("ROW {row:?}"); |
| 52 | + row.get(0) |
| 53 | + }, |
| 54 | + ) |
| 55 | + .unwrap(); |
| 56 | + |
| 57 | + assert_eq!(result, "outer_label=inner_label=value"); |
| 58 | + } |
| 59 | +} |
0 commit comments