Merge pull request #1263 from psarna/from_handle_owned

Introduce Connection::from_handle_owned
This commit is contained in:
gwenn 2022-12-11 10:52:52 +01:00 committed by GitHub
commit a1b83423a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -936,6 +936,28 @@ impl Connection {
}) })
} }
/// Create a `Connection` from a raw owned handle.
///
/// The returned connection will attempt to close the inner connection
/// when dropped/closed. This function should only be called on connections
/// owned by the caller.
///
/// # Safety
///
/// This function is unsafe because improper use may impact the Connection.
/// In particular, it should only be called on connections created
/// and owned by the caller, e.g. as a result of calling ffi::sqlite3_open().
#[inline]
pub unsafe fn from_handle_owned(db: *mut ffi::sqlite3) -> Result<Connection> {
let db_path = db_filename(db);
let db = InnerConnection::new(db, true);
Ok(Connection {
db: RefCell::new(db),
cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY),
path: db_path,
})
}
/// Get access to a handle that can be used to interrupt long running /// Get access to a handle that can be used to interrupt long running
/// queries from another thread. /// queries from another thread.
#[inline] #[inline]
@ -1804,6 +1826,16 @@ mod test {
Ok(()) Ok(())
} }
#[test]
fn test_from_handle_owned() -> Result<()> {
let mut handle: *mut ffi::sqlite3 = std::ptr::null_mut();
let r = unsafe { ffi::sqlite3_open(":memory:\0".as_ptr() as *const i8, &mut handle) };
assert_eq!(r, ffi::SQLITE_OK);
let db = unsafe { Connection::from_handle_owned(handle) }?;
db.execute_batch("PRAGMA VACUUM")?;
Ok(())
}
mod query_and_then_tests { mod query_and_then_tests {
use super::*; use super::*;