Skip to content

Commit

Permalink
Add method to pass any setting to DuckDB config (#238)
Browse files Browse the repository at this point in the history
* Add method to pass any setting to DuckDB config

* Accept &str and String

* Add test that invalid settings are rejected
  • Loading branch information
mlafeldt authored Dec 1, 2023
1 parent 4489432 commit 0277fb3
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>, value: impl AsRef<str>) -> Result<Config> {
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();
Expand Down Expand Up @@ -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)")?;

Expand All @@ -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(())
}
}

0 comments on commit 0277fb3

Please sign in to comment.