From 1ae02726cf1a7daf4caf4632f95059417f05e272 Mon Sep 17 00:00:00 2001 From: "Joshua C. Randall" Date: Mon, 17 Jun 2019 10:42:54 +0100 Subject: [PATCH] add ability to open with specified vfs --- src/inner_connection.rs | 9 +++++++-- src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/inner_connection.rs b/src/inner_connection.rs index b54f3d4..0ca7db8 100644 --- a/src/inner_connection.rs +++ b/src/inner_connection.rs @@ -57,7 +57,7 @@ impl InnerConnection { } } - pub fn open_with_flags(c_path: &CString, flags: OpenFlags) -> Result { + pub fn open_with_flags(c_path: &CString, flags: OpenFlags, vfs: Option(&CString)) -> Result { #[cfg(not(feature = "bundled"))] ensure_valid_sqlite_version(); ensure_safe_sqlite_threading_mode()?; @@ -77,9 +77,14 @@ impl InnerConnection { )); } + z_vfs = match(vfs) { + Some(c_vfs) => c_vfs.as_ptr(), + None => ptr::null() + } + unsafe { let mut db: *mut ffi::sqlite3 = mem::uninitialized(); - let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), ptr::null()); + let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), z_vfs); if r != ffi::SQLITE_OK { let e = if db.is_null() { error_from_sqlite_code(r, None) diff --git a/src/lib.rs b/src/lib.rs index 250635e..aa7f547 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -342,7 +342,7 @@ impl Connection { /// string or if the underlying SQLite open call fails. pub fn open>(path: P) -> Result { let flags = OpenFlags::default(); - Connection::open_with_flags(path, flags) + Connection::open_with_flags(path, flags, None) } /// Open a new connection to an in-memory SQLite database. @@ -366,7 +366,26 @@ impl Connection { /// string or if the underlying SQLite open call fails. pub fn open_with_flags>(path: P, flags: OpenFlags) -> Result { let c_path = path_to_cstring(path.as_ref())?; - InnerConnection::open_with_flags(&c_path, flags).map(|db| Connection { + InnerConnection::open_with_flags(&c_path, flags, None).map(|db| Connection { + db: RefCell::new(db), + cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), + path: Some(path.as_ref().to_path_buf()), + }) + } + + /// Open a new connection to a SQLite database using the specific flags and vfs name. + /// + /// [Database Connection](http://www.sqlite.org/c3ref/open.html) for a description of valid + /// flag combinations. + /// + /// # Failure + /// + /// Will return `Err` if either `path` or `vfs` cannot be converted to a C-compatible + /// string or if the underlying SQLite open call fails. + pub fn open_with_flags_and_vfs>(path: P, flags: OpenFlags, vfs: &str) -> Result { + let c_path = path_to_cstring(path.as_ref())?; + let c_vfs = str_to_cstring(vfs)?; + InnerConnection::open_with_flags(&c_path, flags, Some(c_vfs)).map(|db| Connection { db: RefCell::new(db), cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), path: Some(path.as_ref().to_path_buf()), @@ -383,7 +402,26 @@ impl Connection { /// Will return `Err` if the underlying SQLite open call fails. pub fn open_in_memory_with_flags(flags: OpenFlags) -> Result { let c_memory = str_to_cstring(":memory:")?; - InnerConnection::open_with_flags(&c_memory, flags).map(|db| Connection { + InnerConnection::open_with_flags(&c_memory, flags, None).map(|db| Connection { + db: RefCell::new(db), + cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), + path: None, + }) + } + + /// Open a new connection to an in-memory SQLite database using the specific flags and vfs name. + /// + /// [Database Connection](http://www.sqlite.org/c3ref/open.html) for a description of valid + /// flag combinations. + /// + /// # Failure + /// + /// Will return `Err` if vfs` cannot be converted to a C-compatible + /// string or if the underlying SQLite open call fails. + pub fn open_in_memory_with_flags_and_vfs(flags: OpenFlags, vfs: &str) -> Result { + let c_memory = str_to_cstring(":memory:")?; + let c_vfs = str_to_cstring(vfs)?; + InnerConnection::open_with_flags(&c_memory, flags, Some(c_vfs)).map(|db| Connection { db: RefCell::new(db), cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), path: None,