mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-25 02:21:37 +08:00
Merge pull request #1263 from psarna/from_handle_owned
Introduce Connection::from_handle_owned
This commit is contained in:
commit
a1b83423a5
32
src/lib.rs
32
src/lib.rs
@ -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::*;
|
||||||
|
Loading…
Reference in New Issue
Block a user