Skip to content

Commit 0277fb3

Browse files
authored
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
1 parent 4489432 commit 0277fb3

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/config.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ impl Config {
107107
Ok(self)
108108
}
109109

110+
/// Add any setting to the config. DuckDB will return an error if the setting is unknown or
111+
/// otherwise invalid.
112+
pub fn with(mut self, key: impl AsRef<str>, value: impl AsRef<str>) -> Result<Config> {
113+
self.set(key.as_ref(), value.as_ref())?;
114+
Ok(self)
115+
}
116+
110117
fn set(&mut self, key: &str, value: &str) -> Result<()> {
111118
if self.config.is_none() {
112119
let mut config: ffi::duckdb_config = ptr::null_mut();
@@ -184,7 +191,9 @@ mod test {
184191
.enable_autoload_extension(true)?
185192
.allow_unsigned_extensions()?
186193
.max_memory("2GB")?
187-
.threads(4)?;
194+
.threads(4)?
195+
.with("preserve_insertion_order", "true")?;
196+
188197
let db = Connection::open_in_memory_with_flags(config)?;
189198
db.execute_batch("CREATE TABLE foo(x Text)")?;
190199

@@ -208,4 +217,15 @@ mod test {
208217

209218
Ok(())
210219
}
220+
221+
#[test]
222+
fn test_invalid_setting() -> Result<()> {
223+
let config = Config::default().with("some-invalid-setting", "true")?;
224+
let res = Connection::open_in_memory_with_flags(config);
225+
assert_eq!(
226+
res.unwrap_err().to_string(),
227+
"Invalid Input Error: Unrecognized configuration property \"some-invalid-setting\""
228+
);
229+
Ok(())
230+
}
211231
}

0 commit comments

Comments
 (0)