mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-22 10:04:02 +00:00
feat: add to_url_lossy to connect options (#2902)
* feat: add get_url to connect options Add a get_url to connect options and implement it for all needed types; include get_filename for sqlite. These changes make it easier to test sqlx. * refactor: use expect with message * refactor: change method name to `to_url_lossy` * fix: remove unused imports
This commit is contained in:
@@ -24,6 +24,10 @@ impl ConnectOptions for SqliteConnectOptions {
|
||||
Self::from_str(url.as_str())
|
||||
}
|
||||
|
||||
fn to_url_lossy(&self) -> Url {
|
||||
self.build_url()
|
||||
}
|
||||
|
||||
fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
|
||||
where
|
||||
Self::Connection: Sized,
|
||||
|
||||
@@ -211,6 +211,11 @@ impl SqliteConnectOptions {
|
||||
self
|
||||
}
|
||||
|
||||
/// Gets the current name of the database file.
|
||||
pub fn get_filename(self) -> Cow<'static, Path> {
|
||||
self.filename
|
||||
}
|
||||
|
||||
/// Set the enforcement of [foreign key constraints](https://www.sqlite.org/pragma.html#pragma_foreign_keys).
|
||||
///
|
||||
/// SQLx chooses to enable this by default so that foreign keys function as expected,
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use crate::error::Error;
|
||||
use crate::SqliteConnectOptions;
|
||||
use percent_encoding::percent_decode_str;
|
||||
use percent_encoding::{percent_decode_str, utf8_percent_encode, NON_ALPHANUMERIC};
|
||||
use std::borrow::Cow;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str::FromStr;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use url::Url;
|
||||
|
||||
// https://www.sqlite.org/uri.html
|
||||
|
||||
@@ -111,6 +112,36 @@ impl SqliteConnectOptions {
|
||||
|
||||
Ok(options)
|
||||
}
|
||||
|
||||
pub(crate) fn build_url(&self) -> Url {
|
||||
let filename =
|
||||
utf8_percent_encode(&self.filename.to_string_lossy(), NON_ALPHANUMERIC).to_string();
|
||||
let mut url =
|
||||
Url::parse(&format!("sqlite://{}", filename)).expect("BUG: generated un-parseable URL");
|
||||
|
||||
let mode = match (self.in_memory, self.create_if_missing, self.read_only) {
|
||||
(true, _, _) => "memory",
|
||||
(false, true, _) => "rwc",
|
||||
(false, false, true) => "ro",
|
||||
(false, false, false) => "rw",
|
||||
};
|
||||
url.query_pairs_mut().append_pair("mode", mode);
|
||||
|
||||
let cache = match self.shared_cache {
|
||||
true => "shared",
|
||||
false => "private",
|
||||
};
|
||||
url.query_pairs_mut().append_pair("cache", cache);
|
||||
|
||||
url.query_pairs_mut()
|
||||
.append_pair("immutable", &self.immutable.to_string());
|
||||
|
||||
if let Some(vfs) = &self.vfs {
|
||||
url.query_pairs_mut().append_pair("vfs", &vfs);
|
||||
}
|
||||
|
||||
url
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for SqliteConnectOptions {
|
||||
@@ -169,3 +200,14 @@ fn test_parse_shared_in_memory() -> Result<(), Error> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_returns_the_parsed_url() -> Result<(), Error> {
|
||||
let url = "sqlite://test.db?mode=rw&cache=shared";
|
||||
let options: SqliteConnectOptions = url.parse()?;
|
||||
|
||||
let expected_url = Url::parse(url).unwrap();
|
||||
assert_eq!(options.build_url(), expected_url);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user