Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow vfs to be set as uri query parameter #2013

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.as_str()))
}

if !query_params.is_empty() {
filename = format!("file:{}?{}", filename, query_params.join("&"));
flags |= libsqlite3_sys::SQLITE_OPEN_URI;
}

Expand Down
4 changes: 4 additions & 0 deletions sqlx-core/src/sqlite/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod journal_mode;
mod locking_mode;
mod parse;
mod synchronous;
mod vfs;

use crate::connection::LogSettings;
pub use auto_vacuum::SqliteAutoVacuum;
Expand All @@ -15,6 +16,7 @@ use std::cmp::Ordering;
use std::sync::Arc;
use std::{borrow::Cow, time::Duration};
pub use synchronous::SqliteSynchronous;
pub use vfs::SqliteVfs;

use crate::common::DebugFn;
use crate::sqlite::connection::collation::Collation;
Expand Down Expand Up @@ -63,6 +65,7 @@ pub struct SqliteConnectOptions {
pub(crate) busy_timeout: Duration,
pub(crate) log_settings: LogSettings,
pub(crate) immutable: bool,
pub(crate) vfs: Option<SqliteVfs>,

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

Expand Down Expand Up @@ -135,6 +138,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
51 changes: 51 additions & 0 deletions sqlx-core/src/sqlite/options/parse.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::SqliteVfs;
use crate::error::Error;
use crate::sqlite::SqliteConnectOptions;
use percent_encoding::percent_decode_str;
Expand Down Expand Up @@ -108,6 +109,8 @@ impl FromStr for SqliteConnectOptions {
}
},

"vfs" => options.vfs = Some(SqliteVfs::from_str(&*value)?),

_ => {
return Err(Error::Configuration(
format!(
Expand Down Expand Up @@ -163,3 +166,51 @@ fn test_parse_shared_in_memory() -> Result<(), Error> {

Ok(())
}

#[test]
#[cfg(target_family = "unix")]
fn test_parse_vfs() -> Result<(), Error> {
let options: SqliteConnectOptions = "sqlite://a.db?vfs=unix".parse()?;
assert_eq!(options.vfs, Some(SqliteVfs::Unix));
assert_eq!(&*options.filename.to_string_lossy(), "a.db");

let options: SqliteConnectOptions = "sqlite://a.db?vfs=unix-dotfile".parse()?;
assert_eq!(options.vfs, Some(SqliteVfs::UnixDotfile));
assert_eq!(&*options.filename.to_string_lossy(), "a.db");

let options: SqliteConnectOptions = "sqlite://a.db?vfs=unix-excl".parse()?;
assert_eq!(options.vfs, Some(SqliteVfs::UnixExcl));
assert_eq!(&*options.filename.to_string_lossy(), "a.db");

let options: SqliteConnectOptions = "sqlite://a.db?vfs=unix-none".parse()?;
assert_eq!(options.vfs, Some(SqliteVfs::UnixNone));
assert_eq!(&*options.filename.to_string_lossy(), "a.db");

let options: SqliteConnectOptions = "sqlite://a.db?vfs=unix-namedsem".parse()?;
assert_eq!(options.vfs, Some(SqliteVfs::UnixNamedsem));
assert_eq!(&*options.filename.to_string_lossy(), "a.db");

Ok(())
}

#[test]
#[cfg(target_family = "windows")]
fn test_parse_vfs() -> Result<(), Error> {
let options: SqliteConnectOptions = "sqlite://a.db?vfs=win32".parse()?;
assert_eq!(options.vfs, Some(SqliteVfs::Win32));
assert_eq!(&*options.filename.to_string_lossy(), "a.db");

let options: SqliteConnectOptions = "sqlite://a.db?vfs=win32-longpath".parse()?;
assert_eq!(options.vfs, Some(SqliteVfs::Win32Longpath));
assert_eq!(&*options.filename.to_string_lossy(), "a.db");

let options: SqliteConnectOptions = "sqlite://a.db?vfs=win32-none".parse()?;
assert_eq!(options.vfs, Some(SqliteVfs::Win32None));
assert_eq!(&*options.filename.to_string_lossy(), "a.db");

let options: SqliteConnectOptions = "sqlite://a.db?vfs=win32-longpath-none".parse()?;
assert_eq!(options.vfs, Some(SqliteVfs::Win32LongpathNone));
assert_eq!(&*options.filename.to_string_lossy(), "a.db");

Ok(())
}
85 changes: 85 additions & 0 deletions sqlx-core/src/sqlite/options/vfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::error::Error;
use std::str::FromStr;

/// Refer to [SQLite documentation] for available VFSes.
/// Currently only standard VFSes are supported
///
/// [SQLite documentation]: https://www.sqlite.org/vfs.html
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SqliteVfs {
abonander marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(target_family = "unix")]
Unix,
#[cfg(target_family = "unix")]
UnixDotfile,
#[cfg(target_family = "unix")]
UnixExcl,
#[cfg(target_family = "unix")]
UnixNone,
#[cfg(target_family = "unix")]
UnixNamedsem,
#[cfg(target_family = "windows")]
Win32,
#[cfg(target_family = "windows")]
Win32Longpath,
#[cfg(target_family = "windows")]
Win32None,
#[cfg(target_family = "windows")]
Win32LongpathNone,
}

impl SqliteVfs {
pub(crate) fn as_str(&self) -> &'static str {
match self {
#[cfg(target_family = "unix")]
SqliteVfs::Unix => "unix",
#[cfg(target_family = "unix")]
SqliteVfs::UnixDotfile => "unix-dotfile",
#[cfg(target_family = "unix")]
SqliteVfs::UnixExcl => "unix-excl",
#[cfg(target_family = "unix")]
SqliteVfs::UnixNone => "unix-none",
#[cfg(target_family = "unix")]
SqliteVfs::UnixNamedsem => "unix-namedsem",
#[cfg(target_family = "windows")]
SqliteVfs::Win32 => "win32",
#[cfg(target_family = "windows")]
SqliteVfs::Win32Longpath => "win32-longpath",
#[cfg(target_family = "windows")]
SqliteVfs::Win32None => "win32-none",
#[cfg(target_family = "windows")]
SqliteVfs::Win32LongpathNone => "win32-longpath-none",
}
}
}

impl FromStr for SqliteVfs {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Error> {
Ok(match &*s.to_ascii_lowercase() {
#[cfg(target_family = "unix")]
"unix" => SqliteVfs::Unix,
#[cfg(target_family = "unix")]
"unix-dotfile" => SqliteVfs::UnixDotfile,
#[cfg(target_family = "unix")]
"unix-excl" => SqliteVfs::UnixExcl,
#[cfg(target_family = "unix")]
"unix-none" => SqliteVfs::UnixNone,
#[cfg(target_family = "unix")]
"unix-namedsem" => SqliteVfs::UnixNamedsem,
#[cfg(target_family = "windows")]
"win32" => SqliteVfs::Win32,
#[cfg(target_family = "windows")]
"win32-longpath" => SqliteVfs::Win32Longpath,
#[cfg(target_family = "windows")]
"win32-none" => SqliteVfs::Win32None,
#[cfg(target_family = "windows")]
"win32-longpath-none" => SqliteVfs::Win32LongpathNone,
_ => {
return Err(Error::Configuration(
format!("unknown value {:?} for `vfs`", s).into(),
));
}
})
}
}