2018-10-31 03:11:35 +08:00
|
|
|
use crate::{Connection, Result};
|
2015-02-24 09:16:49 +08:00
|
|
|
|
2021-09-06 17:49:29 +08:00
|
|
|
/// RAII guard temporarily enabling SQLite extensions to be loaded.
|
2015-02-24 09:16:49 +08:00
|
|
|
///
|
|
|
|
/// ## 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<()> {
|
2021-09-06 17:49:29 +08:00
|
|
|
/// unsafe {
|
|
|
|
/// let _guard = LoadExtensionGuard::new(conn)?;
|
|
|
|
/// conn.load_extension("trusted/sqlite/extension", None)
|
|
|
|
/// }
|
2015-02-24 09:16:49 +08:00
|
|
|
/// }
|
|
|
|
/// ```
|
2021-06-13 15:17:35 +08:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "load_extension")))]
|
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
|
|
|
}
|
|
|
|
|
2019-02-03 18:02:38 +08:00
|
|
|
impl LoadExtensionGuard<'_> {
|
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.
|
2021-09-06 17:49:29 +08:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// You must not run untrusted queries while extension loading is enabled.
|
|
|
|
///
|
|
|
|
/// See the safety comment on [`Connection::load_extension_enable`] for more
|
|
|
|
/// details.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2021-09-06 17:49:29 +08:00
|
|
|
pub unsafe 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)]
|
2019-02-03 18:02:38 +08:00
|
|
|
impl Drop for LoadExtensionGuard<'_> {
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2015-02-24 09:16:49 +08:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.conn.load_extension_disable();
|
|
|
|
}
|
|
|
|
}
|