2015-12-13 03:06:03 +08:00
|
|
|
use {Result, Connection};
|
2015-02-24 09:16:49 +08:00
|
|
|
|
2015-12-13 03:20:11 +08:00
|
|
|
/// Old name for `LoadExtensionGuard`. `SqliteLoadExtensionGuard` is deprecated.
|
2016-05-30 08:36:20 +08:00
|
|
|
#[deprecated(since = "0.6.0", note = "Use LoadExtensionGuard instead")]
|
2015-12-13 03:20:11 +08:00
|
|
|
pub type SqliteLoadExtensionGuard<'conn> = LoadExtensionGuard<'conn>;
|
|
|
|
|
2015-02-24 09:16:49 +08:00
|
|
|
/// RAII guard temporarily enabling SQLite extensions to be loaded.
|
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2015-12-13 03:20:11 +08:00
|
|
|
/// # use rusqlite::{Connection, Result, LoadExtensionGuard};
|
2015-02-24 09:16:49 +08:00
|
|
|
/// # use std::path::{Path};
|
2015-12-13 03:06:03 +08:00
|
|
|
/// fn load_my_extension(conn: &Connection) -> Result<()> {
|
2015-12-13 03:20:11 +08:00
|
|
|
/// let _guard = try!(LoadExtensionGuard::new(conn));
|
2015-02-24 09:16:49 +08:00
|
|
|
///
|
|
|
|
/// conn.load_extension(Path::new("my_sqlite_extension"), None)
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-12-13 03:20:11 +08:00
|
|
|
pub struct LoadExtensionGuard<'conn> {
|
2015-12-13 02:50:12 +08:00
|
|
|
conn: &'conn Connection,
|
2015-02-24 09:16:49 +08:00
|
|
|
}
|
|
|
|
|
2015-12-13 03:20:11 +08:00
|
|
|
impl<'conn> LoadExtensionGuard<'conn> {
|
2015-02-24 09:16:49 +08:00
|
|
|
/// Attempt to enable loading extensions. Loading extensions will be disabled when this
|
|
|
|
/// guard goes out of scope. Cannot be meaningfully nested.
|
2015-12-13 03:20:11 +08:00
|
|
|
pub fn new(conn: &Connection) -> Result<LoadExtensionGuard> {
|
2017-04-08 01:43:24 +08:00
|
|
|
conn.load_extension_enable()
|
|
|
|
.map(|_| LoadExtensionGuard { conn: conn })
|
2015-02-24 09:16:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused_must_use)]
|
2015-12-13 03:20:11 +08:00
|
|
|
impl<'conn> Drop for LoadExtensionGuard<'conn> {
|
2015-02-24 09:16:49 +08:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.conn.load_extension_disable();
|
|
|
|
}
|
|
|
|
}
|