Skip to content

Commit

Permalink
feat: add a duckdb_extensions() shows duckdb extension details (#183)
Browse files Browse the repository at this point in the history
* feat: add a duckdb_extensions() shows duckdb extension details

* test: add duckdb_extensions() test

* test: fix test azure is installed during execution
  • Loading branch information
kysshsy authored Dec 19, 2024
1 parent 5d15b7b commit eb8b205
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/api/duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ type DuckdbSettingsRow = (
Option<String>,
);

type DuckdbExtensionsRow = (
Option<String>,
Option<bool>,
Option<bool>,
Option<String>,
Option<String>,
Option<String>,
Option<String>,
Option<String>,
Option<String>,
);

#[pg_extern]
pub fn duckdb_execute(query: &str) {
connection::execute(query, []).unwrap_or_else(|err| panic!("error executing query: {err:?}"));
Expand Down Expand Up @@ -52,3 +64,61 @@ fn duckdb_settings_impl() -> Result<Vec<DuckdbSettingsRow>> {
.map(|row| row.unwrap())
.collect::<Vec<DuckdbSettingsRow>>())
}

#[allow(clippy::type_complexity)]
#[pg_extern]
pub fn duckdb_extensions() -> iter::TableIterator<
'static,
(
name!(extension_name, Option<String>),
name!(loaded, Option<bool>),
name!(installed, Option<bool>),
name!(install_path, Option<String>),
name!(description, Option<String>),
name!(aliases, Option<String>),
name!(extension_version, Option<String>),
name!(install_mode, Option<String>),
name!(installed_from, Option<String>),
),
> {
let rows = duckdb_extensions_impl().unwrap_or_else(|e| {
panic!("{}", e);
});
iter::TableIterator::new(rows)
}

#[inline]
fn duckdb_extensions_impl() -> Result<Vec<DuckdbExtensionsRow>> {
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<String>>(0)?,
row.get::<_, Option<bool>>(1)?,
row.get::<_, Option<bool>>(2)?,
row.get::<_, Option<String>>(3)?,
row.get::<_, Option<String>>(4)?,
row.get::<_, Option<String>>(5)?,
row.get::<_, Option<String>>(6)?,
row.get::<_, Option<String>>(7)?,
row.get::<_, Option<String>>(8)?,
))
})?
.map(|row| row.unwrap())
.collect::<Vec<DuckdbExtensionsRow>>())
}
10 changes: 10 additions & 0 deletions tests/tests/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ async fn test_duckdb_settings(mut conn: PgConnection) -> Result<()> {

Ok(())
}

#[rstest]
async fn test_duckdb_extensions(mut conn: PgConnection) -> Result<()> {
let azure_extension: (Option<String>,) =
"SELECT extension_name FROM duckdb_extensions() WHERE extension_name = 'azure'"
.fetch_one(&mut conn);
assert!(azure_extension.0.is_some());

Ok(())
}

0 comments on commit eb8b205

Please sign in to comment.