From 2a716a6521428d62ab3e25dfae54a72257428f42 Mon Sep 17 00:00:00 2001 From: Marcelo Altmann Date: Fri, 8 Sep 2023 15:51:04 -0300 Subject: [PATCH] Usability improvement: leading forward slash empty database A leading forward slash in the connection URL makes get_opts_db_name_from_url set db_name to Some("") rather than None. This makes code pulling the db from config requiring extra checks as db_name.is_some() will evaluate to true. Empty database parameter is invalid for MySQL. --- src/opts/mod.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/opts/mod.rs b/src/opts/mod.rs index 9d1e849b..976fa9e0 100644 --- a/src/opts/mod.rs +++ b/src/opts/mod.rs @@ -1340,11 +1340,14 @@ fn get_opts_pass_from_url(url: &Url) -> Option { fn get_opts_db_name_from_url(url: &Url) -> Option { if let Some(mut segments) = url.path_segments() { - segments.next().map(|db_name| { - percent_decode(db_name.as_ref()) - .decode_utf8_lossy() - .into_owned() - }) + segments + .next() + .map(|db_name| { + percent_decode(db_name.as_ref()) + .decode_utf8_lossy() + .into_owned() + }) + .and_then(|db| if db.is_empty() { None } else { Some(db) }) } else { None } @@ -1813,4 +1816,18 @@ mod test { let opts = Opts::from_url("mysql://localhost/foo?compression=9").unwrap(); assert_eq!(opts.compression(), Some(crate::Compression::new(9))); } + + #[test] + fn test_builder_eq_url_empty_db() { + let builder = super::OptsBuilder::default(); + let builder_opts = Opts::from(builder); + + let url: &str = "mysql://iq-controller@localhost"; + let url_opts = super::Opts::from_str(url).unwrap(); + assert_eq!(url_opts.db_name(), builder_opts.db_name()); + + let url: &str = "mysql://iq-controller@localhost/"; + let url_opts = super::Opts::from_str(url).unwrap(); + assert_eq!(url_opts.db_name(), builder_opts.db_name()); + } }