Skip to content

Commit

Permalink
feat: set duckdb api to rust and add custom_user_agent config (#360)
Browse files Browse the repository at this point in the history
* Add custom user agent config

* add test

* run formatter
  • Loading branch information
hrl20 authored Aug 1, 2024
1 parent 1c73aef commit f40058b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/duckdb/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ impl Config {
Ok(self)
}

/// Metadata from DuckDB callers
pub fn custom_user_agent(mut self, custom_user_agent: &str) -> Result<Config> {
self.set("custom_user_agent", custom_user_agent)?;
Ok(self)
}

/// The order type used when none is specified ([ASC] or DESC)
pub fn default_order(mut self, order: DefaultOrder) -> Result<Config> {
self.set("default_order", &order.to_string())?;
Expand Down Expand Up @@ -190,6 +196,7 @@ mod test {
.enable_object_cache(false)?
.enable_autoload_extension(true)?
.allow_unsigned_extensions()?
.custom_user_agent("test_user_agent")?
.max_memory("2GB")?
.threads(4)?
.with("preserve_insertion_order", "true")?;
Expand All @@ -215,6 +222,10 @@ mod test {
assert!(iter.next().unwrap().is_none());
assert_eq!(iter.next(), None);

let user_agent: Result<String> = db.query_row("PRAGMA USER_AGENT", [], |row| row.get(0));
let user_agent = user_agent.unwrap();
assert!(&user_agent.ends_with("rust test_user_agent"));

Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions crates/duckdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ impl Connection {
}

let c_path = path_to_cstring(path.as_ref())?;
let config = config.with("duckdb_api", "rust").unwrap();
InnerConnection::open_with_flags(&c_path, config).map(|db| Connection {
db: RefCell::new(db),
cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY),
Expand Down

0 comments on commit f40058b

Please sign in to comment.