sqlite3_db_filename is available in SQLite >= 3.7.10

This commit is contained in:
gwenn 2019-01-06 14:01:17 +01:00
parent 0042f2514f
commit 92a22b7842

View File

@ -191,6 +191,9 @@ pub enum DatabaseName<'a> {
Attached(&'a str), Attached(&'a str),
} }
// Currently DatabaseName is only used by the backup and blob mods, so hide
// this (private) impl to avoid dead code warnings.
#[cfg(any(feature = "backup", feature = "blob", feature = "bundled"))]
impl<'a> DatabaseName<'a> { impl<'a> DatabaseName<'a> {
fn to_cstring(&self) -> Result<CString> { fn to_cstring(&self) -> Result<CString> {
use self::DatabaseName::{Attached, Main, Temp}; use self::DatabaseName::{Attached, Main, Temp};
@ -593,13 +596,7 @@ impl Connection {
/// The underlying SQLite database connection handle will not be closed when /// The underlying SQLite database connection handle will not be closed when
/// the returned connection is dropped/closed. /// the returned connection is dropped/closed.
pub unsafe fn from_handle(db: *mut ffi::sqlite3) -> Result<Connection> { pub unsafe fn from_handle(db: *mut ffi::sqlite3) -> Result<Connection> {
let db_name = DatabaseName::Main.to_cstring()?; let db_path = db_filename(db);
let db_filename = ffi::sqlite3_db_filename(db, db_name.as_ptr());
let db_path = if db_filename.is_null() {
None
} else {
CStr::from_ptr(db_filename).to_str().ok().map(PathBuf::from)
};
let db = InnerConnection::new(db, false); let db = InnerConnection::new(db, false);
Ok(Connection { Ok(Connection {
db: RefCell::new(db), db: RefCell::new(db),
@ -1102,6 +1099,21 @@ impl Drop for InnerConnection {
} }
} }
#[cfg(feature = "bundled")] // 3.7.10
unsafe fn db_filename(db: *mut ffi::sqlite3) -> Option<PathBuf> {
let db_name = DatabaseName::Main.to_cstring().unwrap();
let db_filename = ffi::sqlite3_db_filename(db, db_name.as_ptr());
if db_filename.is_null() {
None
} else {
CStr::from_ptr(db_filename).to_str().ok().map(PathBuf::from)
}
}
#[cfg(not(feature = "bundled"))]
unsafe fn db_filename(_: *mut ffi::sqlite3) -> Option<PathBuf> {
None
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use self::tempdir::TempDir; use self::tempdir::TempDir;