mirror of
https://github.com/isar/rusqlite.git
synced 2025-09-16 12:42:18 +08:00
Add bindings to automatic extension loading API (#1487)
* Add bindings to automatic extension loading API it doesn't seem possible to directly register an `AutoExtension`.
This commit is contained in:
57
src/auto_extension.rs
Normal file
57
src/auto_extension.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
//! Automatic axtension loading
|
||||
use super::ffi;
|
||||
use crate::error::{check, to_sqlite_error};
|
||||
use crate::{Connection, Result};
|
||||
use std::os::raw::{c_char, c_int};
|
||||
|
||||
/// Automatic extension initialization routine
|
||||
pub type AutoExtension = fn(Connection) -> Result<()>;
|
||||
|
||||
/// Raw automatic extension initialization routine
|
||||
pub type RawAutoExtension = unsafe extern "C" fn(
|
||||
db: *mut ffi::sqlite3,
|
||||
pz_err_msg: *mut *mut c_char,
|
||||
_: *const ffi::sqlite3_api_routines,
|
||||
) -> c_int;
|
||||
|
||||
/// Bridge bewteen `RawAutoExtension` and `AutoExtension`
|
||||
///
|
||||
/// # Safety
|
||||
/// * Opening a database from an auto-extension handler will lead to
|
||||
/// an endless recursion of the auto-handler triggering itself
|
||||
/// indirectly for each newly-opened database.
|
||||
/// * Results are undefined if the given db is closed by an auto-extension.
|
||||
/// * The list of auto-extensions should not be manipulated from an auto-extension.
|
||||
pub unsafe fn init_auto_extension(
|
||||
db: *mut ffi::sqlite3,
|
||||
pz_err_msg: *mut *mut c_char,
|
||||
ax: AutoExtension,
|
||||
) -> c_int {
|
||||
let c = Connection::from_handle(db);
|
||||
match c.and_then(ax) {
|
||||
Err(e) => to_sqlite_error(&e, pz_err_msg),
|
||||
_ => ffi::SQLITE_OK,
|
||||
}
|
||||
}
|
||||
|
||||
/// Register au auto-extension
|
||||
///
|
||||
/// # Safety
|
||||
/// * Opening a database from an auto-extension handler will lead to
|
||||
/// an endless recursion of the auto-handler triggering itself
|
||||
/// indirectly for each newly-opened database.
|
||||
/// * Results are undefined if the given db is closed by an auto-extension.
|
||||
/// * The list of auto-extensions should not be manipulated from an auto-extension.
|
||||
pub unsafe fn register_auto_extension(ax: RawAutoExtension) -> Result<()> {
|
||||
check(ffi::sqlite3_auto_extension(Some(ax)))
|
||||
}
|
||||
|
||||
/// Unregister the initialization routine
|
||||
pub fn cancel_auto_extension(ax: RawAutoExtension) -> bool {
|
||||
unsafe { ffi::sqlite3_cancel_auto_extension(Some(ax)) == 1 }
|
||||
}
|
||||
|
||||
/// Disable all automatic extensions previously registered
|
||||
pub fn reset_auto_extension() {
|
||||
unsafe { ffi::sqlite3_reset_auto_extension() }
|
||||
}
|
@@ -93,6 +93,8 @@ pub use rusqlite_macros::__bind;
|
||||
|
||||
mod error;
|
||||
|
||||
#[cfg(not(feature = "loadable_extension"))]
|
||||
pub mod auto_extension;
|
||||
#[cfg(feature = "backup")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "backup")))]
|
||||
pub mod backup;
|
||||
|
Reference in New Issue
Block a user