2018-08-11 18:48:21 +08:00
|
|
|
use {Connection, Result};
|
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> {
|
2018-08-17 00:29:46 +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()
|
2018-05-05 01:55:55 +08:00
|
|
|
.map(|_| LoadExtensionGuard { 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();
|
|
|
|
}
|
|
|
|
}
|