From 1309c563c79551f8cd5a340687583c6c6dc7c609 Mon Sep 17 00:00:00 2001 From: phiresky Date: Fri, 18 Dec 2020 13:24:03 +0100 Subject: [PATCH] add get_connection method to function context --- src/functions.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/functions.rs b/src/functions.rs index 8482f7b..fd7e996 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -58,6 +58,8 @@ use std::panic::{catch_unwind, RefUnwindSafe, UnwindSafe}; use std::ptr; use std::slice; use std::sync::Arc; +use std::marker::PhantomData; +use std::ops::Deref; use crate::ffi; use crate::ffi::sqlite3_context; @@ -220,8 +222,43 @@ 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 + /// + /// This function is marked unsafe because there is a potential for other + /// references to the connection to be sent across threads + /// https://github.com/rusqlite/rusqlite/issues/643#issuecomment-640181213 + pub unsafe fn get_connection(&self) -> Result> { + 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; /// `feature = "functions"` Aggregate is the callback interface for user-defined