From b630d5c748c042f6e08a50715e5cc25aec41c703 Mon Sep 17 00:00:00 2001 From: Lining Pan Date: Tue, 2 Aug 2022 18:59:43 -0400 Subject: [PATCH] feat: allow vfs to be set as uri query parameter (#2013) * feat: allow vfs to be set as uri query parameter * fix: handle VFS name as a string * fix: avoid unnecessary copies of vfs name --- sqlx-core/src/sqlite/connection/establish.rs | 12 +++++++++++- sqlx-core/src/sqlite/options/mod.rs | 11 +++++++++++ sqlx-core/src/sqlite/options/parse.rs | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/sqlx-core/src/sqlite/connection/establish.rs b/sqlx-core/src/sqlite/connection/establish.rs index ae093fada5..ead59a9dfa 100644 --- a/sqlx-core/src/sqlite/connection/establish.rs +++ b/sqlx-core/src/sqlite/connection/establish.rs @@ -67,8 +67,18 @@ impl EstablishParams { SQLITE_OPEN_PRIVATECACHE }; + let mut query_params: Vec = vec![]; + if options.immutable { - filename = format!("file:{}?immutable=true", filename); + query_params.push("immutable=true".into()) + } + + if let Some(vfs) = &options.vfs { + query_params.push(format!("vfs={}", vfs)) + } + + if !query_params.is_empty() { + filename = format!("file:{}?{}", filename, query_params.join("&")); flags |= libsqlite3_sys::SQLITE_OPEN_URI; } diff --git a/sqlx-core/src/sqlite/options/mod.rs b/sqlx-core/src/sqlite/options/mod.rs index b3bb53e5a4..d5ec65c271 100644 --- a/sqlx-core/src/sqlite/options/mod.rs +++ b/sqlx-core/src/sqlite/options/mod.rs @@ -63,6 +63,7 @@ pub struct SqliteConnectOptions { pub(crate) busy_timeout: Duration, pub(crate) log_settings: LogSettings, pub(crate) immutable: bool, + pub(crate) vfs: Option>, pub(crate) pragmas: IndexMap, Option>>, @@ -135,6 +136,7 @@ impl SqliteConnectOptions { busy_timeout: Duration::from_secs(5), log_settings: Default::default(), immutable: false, + vfs: None, pragmas, collations: Default::default(), serialized: false, @@ -367,4 +369,13 @@ impl SqliteConnectOptions { self.row_channel_size = size; self } + + /// Sets the [`vfs`](https://www.sqlite.org/vfs.html) parameter of the database connection. + /// + /// The default value is empty, and sqlite will use the default VFS object dependeing on the + /// operating system. + pub fn vfs(mut self, vfs_name: impl Into>) -> Self { + self.vfs = Some(vfs_name.into()); + self + } } diff --git a/sqlx-core/src/sqlite/options/parse.rs b/sqlx-core/src/sqlite/options/parse.rs index 7f80dfd7d0..94cf0cd397 100644 --- a/sqlx-core/src/sqlite/options/parse.rs +++ b/sqlx-core/src/sqlite/options/parse.rs @@ -108,6 +108,8 @@ impl FromStr for SqliteConnectOptions { } }, + "vfs" => options.vfs = Some(Cow::Owned(value.into_owned())), + _ => { return Err(Error::Configuration( format!(