Skip to main content

ClientContext

Struct ClientContext 

Source
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

Source

pub unsafe fn from_connection( con: duckdb_connection, ) -> Result<Self, ExtensionError>

Obtain a client context from a duckdb_connection.

§Errors

Returns ExtensionError if the context cannot be obtained.

§Safety

con must be a valid, open duckdb_connection.

Source

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.

Source

pub const fn as_raw(&self) -> duckdb_client_context

Returns the raw handle.

Source

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:

  • name is empty. duckdb_client_context_get_catalog rejects that outright (strlen(name) == 0 is an explicit early return); it is not a way to ask for “the default”. The catalog of an in-memory database is named memory; a file database’s is the file’s stem.
  • No transaction is active. DuckDB checks transaction.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.

Source

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';
Source

pub fn connection_id(&self) -> u64

Returns the connection ID associated with this client context.

Trait Implementations§

Source§

impl Debug for ClientContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for ClientContext

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.