mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Add scoped guard for enabling extension loading
This commit is contained in:
parent
348f94e109
commit
31d629070e
17
src/lib.rs
17
src/lib.rs
@ -74,8 +74,11 @@ pub use transaction::{SqliteTransactionBehavior,
|
|||||||
SqliteTransactionImmediate,
|
SqliteTransactionImmediate,
|
||||||
SqliteTransactionExclusive};
|
SqliteTransactionExclusive};
|
||||||
|
|
||||||
|
#[cfg(feature = "load_extension")] pub use load_extension_guard::{SqliteLoadExtensionGuard};
|
||||||
|
|
||||||
pub mod types;
|
pub mod types;
|
||||||
mod transaction;
|
mod transaction;
|
||||||
|
#[cfg(feature = "load_extension")] mod load_extension_guard;
|
||||||
|
|
||||||
/// A typedef of the result returned by many methods.
|
/// A typedef of the result returned by many methods.
|
||||||
pub type SqliteResult<T> = Result<T, SqliteError>;
|
pub type SqliteResult<T> = Result<T, SqliteError>;
|
||||||
@ -335,7 +338,8 @@ impl SqliteConnection {
|
|||||||
db.close()
|
db.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enable loading of SQLite extensions.
|
/// Enable loading of SQLite extensions. Strongly consider using `SqliteLoadExtensionGuard`
|
||||||
|
/// instead of this function.
|
||||||
///
|
///
|
||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
@ -367,6 +371,17 @@ impl SqliteConnection {
|
|||||||
///
|
///
|
||||||
/// If `entry_point` is `None`, SQLite will attempt to find the entry point. If it is not
|
/// If `entry_point` is `None`, SQLite will attempt to find the entry point. If it is not
|
||||||
/// `None`, the entry point will be passed through to `sqlite3_load_extension`.
|
/// `None`, the entry point will be passed through to `sqlite3_load_extension`.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
///
|
||||||
|
/// ```rust,no_run
|
||||||
|
/// # use rusqlite::{SqliteConnection, SqliteResult, SqliteLoadExtensionGuard};
|
||||||
|
/// # use std::path::{Path};
|
||||||
|
/// fn load_my_extension(conn: &SqliteConnection) -> SqliteResult<()> {
|
||||||
|
/// let _guard = try!(SqliteLoadExtensionGuard::new(conn));
|
||||||
|
///
|
||||||
|
/// conn.load_extension(Path::new("my_sqlite_extension"), None)
|
||||||
|
/// }
|
||||||
#[cfg(feature = "load_extension")]
|
#[cfg(feature = "load_extension")]
|
||||||
pub fn load_extension(&self, dylib_path: &Path, entry_point: Option<&str>) -> SqliteResult<()> {
|
pub fn load_extension(&self, dylib_path: &Path, entry_point: Option<&str>) -> SqliteResult<()> {
|
||||||
self.db.borrow_mut().load_extension(dylib_path, entry_point)
|
self.db.borrow_mut().load_extension(dylib_path, entry_point)
|
||||||
|
34
src/load_extension_guard.rs
Normal file
34
src/load_extension_guard.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
use {SqliteResult, SqliteConnection};
|
||||||
|
|
||||||
|
/// RAII guard temporarily enabling SQLite extensions to be loaded.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
///
|
||||||
|
/// ```rust,no_run
|
||||||
|
/// # use rusqlite::{SqliteConnection, SqliteResult, SqliteLoadExtensionGuard};
|
||||||
|
/// # use std::path::{Path};
|
||||||
|
/// fn load_my_extension(conn: &SqliteConnection) -> SqliteResult<()> {
|
||||||
|
/// let _guard = try!(SqliteLoadExtensionGuard::new(conn));
|
||||||
|
///
|
||||||
|
/// conn.load_extension(Path::new("my_sqlite_extension"), None)
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub struct SqliteLoadExtensionGuard<'conn> {
|
||||||
|
conn: &'conn SqliteConnection,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'conn> SqliteLoadExtensionGuard<'conn> {
|
||||||
|
/// Attempt to enable loading extensions. Loading extensions will be disabled when this
|
||||||
|
/// guard goes out of scope. Cannot be meaningfully nested.
|
||||||
|
pub fn new(conn: &SqliteConnection) -> SqliteResult<SqliteLoadExtensionGuard> {
|
||||||
|
conn.load_extension_enable().map(|_| SqliteLoadExtensionGuard{ conn: conn })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[unsafe_destructor]
|
||||||
|
#[allow(unused_must_use)]
|
||||||
|
impl<'conn> Drop for SqliteLoadExtensionGuard<'conn> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.conn.load_extension_disable();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user