From 1f020a8890b766d25c2415be10fe46bfa63c5675 Mon Sep 17 00:00:00 2001 From: kysshsy Date: Wed, 18 Dec 2024 00:17:12 +0800 Subject: [PATCH] feat: add a duckdb_extensions() shows duckdb extension details --- src/api/duckdb.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/api/duckdb.rs b/src/api/duckdb.rs index 6f220816..1ae90cae 100644 --- a/src/api/duckdb.rs +++ b/src/api/duckdb.rs @@ -11,6 +11,18 @@ type DuckdbSettingsRow = ( Option, ); +type DuckdbExtensionsRow = ( + Option, + Option, + Option, + Option, + Option, + Option, + Option, + Option, + Option, +); + #[pg_extern] pub fn duckdb_execute(query: &str) { connection::execute(query, []).unwrap_or_else(|err| panic!("error executing query: {err:?}")); @@ -52,3 +64,61 @@ fn duckdb_settings_impl() -> Result> { .map(|row| row.unwrap()) .collect::>()) } + +#[allow(clippy::type_complexity)] +#[pg_extern] +pub fn duckdb_extensions() -> iter::TableIterator< + 'static, + ( + name!(extension_name, Option), + name!(loaded, Option), + name!(installed, Option), + name!(install_path, Option), + name!(description, Option), + name!(aliases, Option), + name!(extension_version, Option), + name!(install_mode, Option), + name!(installed_from, Option), + ), +> { + let rows = duckdb_extensions_impl().unwrap_or_else(|e| { + panic!("{}", e); + }); + iter::TableIterator::new(rows) +} + +#[inline] +fn duckdb_extensions_impl() -> Result> { + let conn = unsafe { &*connection::get_global_connection().get() }; + let mut stmt = conn.prepare( + "SELECT + extension_name, + loaded, + installed, + install_path, + description, + aliases::varchar, + extension_version, + install_mode, + installed_from + FROM + duckdb_extensions();", + )?; + + Ok(stmt + .query_map([], |row| { + Ok(( + row.get::<_, Option>(0)?, + row.get::<_, Option>(1)?, + row.get::<_, Option>(2)?, + row.get::<_, Option>(3)?, + row.get::<_, Option>(4)?, + row.get::<_, Option>(5)?, + row.get::<_, Option>(6)?, + row.get::<_, Option>(7)?, + row.get::<_, Option>(8)?, + )) + })? + .map(|row| row.unwrap()) + .collect::>()) +}