From f40058b2b76047f3f92b41971acb85f7b1bf4998 Mon Sep 17 00:00:00 2001 From: Louisa H <54686345+hrl20@users.noreply.github.com> Date: Thu, 1 Aug 2024 07:07:49 -0400 Subject: [PATCH] feat: set duckdb api to rust and add custom_user_agent config (#360) * Add custom user agent config * add test * run formatter --- crates/duckdb/src/config.rs | 11 +++++++++++ crates/duckdb/src/lib.rs | 1 + 2 files changed, 12 insertions(+) diff --git a/crates/duckdb/src/config.rs b/crates/duckdb/src/config.rs index 66e37c4b..62df2c57 100644 --- a/crates/duckdb/src/config.rs +++ b/crates/duckdb/src/config.rs @@ -65,6 +65,12 @@ impl Config { Ok(self) } + /// Metadata from DuckDB callers + pub fn custom_user_agent(mut self, custom_user_agent: &str) -> Result { + 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 { self.set("default_order", &order.to_string())?; @@ -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")?; @@ -215,6 +222,10 @@ mod test { assert!(iter.next().unwrap().is_none()); assert_eq!(iter.next(), None); + let user_agent: Result = 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(()) } diff --git a/crates/duckdb/src/lib.rs b/crates/duckdb/src/lib.rs index 49ca966f..6bc59316 100644 --- a/crates/duckdb/src/lib.rs +++ b/crates/duckdb/src/lib.rs @@ -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),