Skip to content

Commit

Permalink
feat: allow vfs to be set as uri query parameter (#2013)
Browse files Browse the repository at this point in the history
* feat: allow vfs to be set as uri query parameter

* fix: handle VFS name as a string

* fix: avoid unnecessary copies of vfs name
  • Loading branch information
liningpan authored Aug 2, 2022
1 parent 054f619 commit b630d5c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion sqlx-core/src/sqlite/connection/establish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,18 @@ impl EstablishParams {
SQLITE_OPEN_PRIVATECACHE
};

let mut query_params: Vec<String> = 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;
}

Expand Down
11 changes: 11 additions & 0 deletions sqlx-core/src/sqlite/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cow<'static, str>>,

pub(crate) pragmas: IndexMap<Cow<'static, str>, Option<Cow<'static, str>>>,

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Cow<'static, str>>) -> Self {
self.vfs = Some(vfs_name.into());
self
}
}
2 changes: 2 additions & 0 deletions sqlx-core/src/sqlite/options/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ impl FromStr for SqliteConnectOptions {
}
},

"vfs" => options.vfs = Some(Cow::Owned(value.into_owned())),

_ => {
return Err(Error::Configuration(
format!(
Expand Down

0 comments on commit b630d5c

Please sign in to comment.