Skip to content

Commit b630d5c

Browse files
authored
feat: allow vfs to be set as uri query parameter (launchbadge#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
1 parent 054f619 commit b630d5c

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

sqlx-core/src/sqlite/connection/establish.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,18 @@ impl EstablishParams {
6767
SQLITE_OPEN_PRIVATECACHE
6868
};
6969

70+
let mut query_params: Vec<String> = vec![];
71+
7072
if options.immutable {
71-
filename = format!("file:{}?immutable=true", filename);
73+
query_params.push("immutable=true".into())
74+
}
75+
76+
if let Some(vfs) = &options.vfs {
77+
query_params.push(format!("vfs={}", vfs))
78+
}
79+
80+
if !query_params.is_empty() {
81+
filename = format!("file:{}?{}", filename, query_params.join("&"));
7282
flags |= libsqlite3_sys::SQLITE_OPEN_URI;
7383
}
7484

sqlx-core/src/sqlite/options/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub struct SqliteConnectOptions {
6363
pub(crate) busy_timeout: Duration,
6464
pub(crate) log_settings: LogSettings,
6565
pub(crate) immutable: bool,
66+
pub(crate) vfs: Option<Cow<'static, str>>,
6667

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

@@ -135,6 +136,7 @@ impl SqliteConnectOptions {
135136
busy_timeout: Duration::from_secs(5),
136137
log_settings: Default::default(),
137138
immutable: false,
139+
vfs: None,
138140
pragmas,
139141
collations: Default::default(),
140142
serialized: false,
@@ -367,4 +369,13 @@ impl SqliteConnectOptions {
367369
self.row_channel_size = size;
368370
self
369371
}
372+
373+
/// Sets the [`vfs`](https://www.sqlite.org/vfs.html) parameter of the database connection.
374+
///
375+
/// The default value is empty, and sqlite will use the default VFS object dependeing on the
376+
/// operating system.
377+
pub fn vfs(mut self, vfs_name: impl Into<Cow<'static, str>>) -> Self {
378+
self.vfs = Some(vfs_name.into());
379+
self
380+
}
370381
}

sqlx-core/src/sqlite/options/parse.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ impl FromStr for SqliteConnectOptions {
108108
}
109109
},
110110

111+
"vfs" => options.vfs = Some(Cow::Owned(value.into_owned())),
112+
111113
_ => {
112114
return Err(Error::Configuration(
113115
format!(

0 commit comments

Comments
 (0)