From 0277fb3da2931cb18592feea8c866b34ce37033b Mon Sep 17 00:00:00 2001 From: Mathias Lafeldt Date: Fri, 1 Dec 2023 12:57:58 +0100 Subject: [PATCH] Add method to pass any setting to DuckDB config (#238) * Add method to pass any setting to DuckDB config * Accept &str and String * Add test that invalid settings are rejected --- src/config.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index d002a9c9..7c5a3b95 100644 --- a/src/config.rs +++ b/src/config.rs @@ -107,6 +107,13 @@ impl Config { Ok(self) } + /// Add any setting to the config. DuckDB will return an error if the setting is unknown or + /// otherwise invalid. + pub fn with(mut self, key: impl AsRef, value: impl AsRef) -> Result { + self.set(key.as_ref(), value.as_ref())?; + Ok(self) + } + fn set(&mut self, key: &str, value: &str) -> Result<()> { if self.config.is_none() { let mut config: ffi::duckdb_config = ptr::null_mut(); @@ -184,7 +191,9 @@ mod test { .enable_autoload_extension(true)? .allow_unsigned_extensions()? .max_memory("2GB")? - .threads(4)?; + .threads(4)? + .with("preserve_insertion_order", "true")?; + let db = Connection::open_in_memory_with_flags(config)?; db.execute_batch("CREATE TABLE foo(x Text)")?; @@ -208,4 +217,15 @@ mod test { Ok(()) } + + #[test] + fn test_invalid_setting() -> Result<()> { + let config = Config::default().with("some-invalid-setting", "true")?; + let res = Connection::open_in_memory_with_flags(config); + assert_eq!( + res.unwrap_err().to_string(), + "Invalid Input Error: Unrecognized configuration property \"some-invalid-setting\"" + ); + Ok(()) + } }