mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Merge pull request #867 from phiresky/function-context-db-handle
This commit is contained in:
commit
4657ddc8e2
@ -53,6 +53,8 @@
|
||||
//! }
|
||||
//! ```
|
||||
use std::any::Any;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
use std::os::raw::{c_int, c_void};
|
||||
use std::panic::{catch_unwind, RefUnwindSafe, UnwindSafe};
|
||||
use std::ptr;
|
||||
@ -220,6 +222,37 @@ impl Context<'_> {
|
||||
.map_err(|_| Error::GetAuxWrongType)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the db connection handle via [sqlite3_context_db_handle](https://www.sqlite.org/c3ref/context_db_handle.html)
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function is marked unsafe because there is a potential for other
|
||||
/// references to the connection to be sent across threads, [see this comment](https://github.com/rusqlite/rusqlite/issues/643#issuecomment-640181213).
|
||||
pub unsafe fn get_connection(&self) -> Result<ConnectionRef<'_>> {
|
||||
let handle = ffi::sqlite3_context_db_handle(self.ctx);
|
||||
Ok(ConnectionRef {
|
||||
conn: Connection::from_handle(handle)?,
|
||||
phantom: PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A reference to a connection handle with a lifetime bound to something.
|
||||
pub struct ConnectionRef<'ctx> {
|
||||
// comes from Connection::from_handle(sqlite3_context_db_handle(...))
|
||||
// and is non-owning
|
||||
conn: Connection,
|
||||
phantom: PhantomData<&'ctx Context<'ctx>>,
|
||||
}
|
||||
|
||||
impl Deref for ConnectionRef<'_> {
|
||||
type Target = Connection;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &Connection {
|
||||
&self.conn
|
||||
}
|
||||
}
|
||||
|
||||
type AuxInner = Arc<dyn Any + Send + Sync + 'static>;
|
||||
|
Loading…
Reference in New Issue
Block a user