pub struct ClientContext { /* private fields */ }Expand description
RAII wrapper for a duckdb_client_context.
Provides access to the connection’s catalog, configuration, and file system. Automatically destroyed when dropped.
Implementations§
Source§impl ClientContext
impl ClientContext
Sourcepub unsafe fn from_connection(
con: duckdb_connection,
) -> Result<Self, ExtensionError>
pub unsafe fn from_connection( con: duckdb_connection, ) -> Result<Self, ExtensionError>
Sourcepub const unsafe fn from_raw(ctx: duckdb_client_context) -> Self
pub const unsafe fn from_raw(ctx: duckdb_client_context) -> Self
Wrap a raw duckdb_client_context handle.
§Safety
ctx must be a valid, non-null duckdb_client_context.
Sourcepub const fn as_raw(&self) -> duckdb_client_context
pub const fn as_raw(&self) -> duckdb_client_context
Returns the raw handle.
Sourcepub unsafe fn catalog(&self, name: &CStr) -> Option<Catalog>
pub unsafe fn catalog(&self, name: &CStr) -> Option<Catalog>
Retrieves a database catalog by name.
Returns None when there is no such catalog — and in two cases that are
easy to mistake for one:
nameis empty.duckdb_client_context_get_catalogrejects that outright (strlen(name) == 0is an explicit early return); it is not a way to ask for “the default”. The catalog of an in-memory database is namedmemory; a file database’s is the file’s stem.- No transaction is active.
DuckDBcheckstransaction.HasActiveTransaction()and returns null otherwise, so this works inside a function callback but not on an idle auto-commit connection.
§Safety
Must be called from within an active transaction context.
Sourcepub fn config_option(&self, name: &CStr) -> Option<String>
pub fn config_option(&self, name: &CStr) -> Option<String>
Retrieves a configuration option value by name.
Returns the value as a string, or None if the option does not exist.
§Do not use this to probe for a setting that may not exist
DuckDB 1.5.5’s duckdb_client_context_get_config_option reads the
lookup’s scope before checking the lookup succeeded:
// src/main/capi/config_options-c.cpp
switch (ctx.TryGetCurrentSetting(option_name, result).GetScope()) {
// src/include/duckdb/main/setting_info.hpp
SettingScope GetScope() {
D_ASSERT(scope != SettingScope::INVALID);A missing setting yields SettingScope::INVALID, so GetScope() trips
that assertion. In a release DuckDB — what users run — D_ASSERT
compiles out, the function’s own default: branch handles INVALID,
and this returns None as documented. Against a DuckDB built with
debug assertions, the process aborts. Verified against 1.5.5.
So this is safe for a setting you registered or know exists, and unsafe as an existence check. To ask whether a setting exists, use SQL, which has no such path:
SELECT count(*) FROM duckdb_settings() WHERE name = 'my_setting';Sourcepub fn connection_id(&self) -> u64
pub fn connection_id(&self) -> u64
Returns the connection ID associated with this client context.