duckdb_extension_framework/table_functions/
function_info.rs

1use crate::as_string;
2use crate::duckly::{
3    duckdb_function_get_bind_data, duckdb_function_get_extra_info, duckdb_function_get_init_data,
4    duckdb_function_get_local_init_data, duckdb_function_info, duckdb_function_set_error,
5};
6#[allow(unused)]
7use crate::table_functions::{BindInfo, InitInfo, TableFunction};
8use std::os::raw::c_char;
9
10/// An interface to store and retrieve data during the function execution stage
11#[derive(Debug)]
12pub struct FunctionInfo(duckdb_function_info);
13
14impl FunctionInfo {
15    /// Report that an error has occurred while executing the function.
16    ///
17    /// # Arguments
18    ///  * `error`: The error message
19    pub fn set_error(&self, error: &str) {
20        unsafe {
21            duckdb_function_set_error(self.0, as_string!(error));
22        }
23    }
24    /// Gets the bind data set by [`BindInfo::set_bind_data`] during the bind.
25    ///
26    /// Note that the bind data should be considered as read-only.
27    /// For tracking state, use the init data instead.
28    ///
29    /// # Arguments
30    /// * `returns`: The bind data object
31    pub fn get_bind_data<T>(&self) -> *mut T {
32        unsafe { duckdb_function_get_bind_data(self.0).cast() }
33    }
34    /// Gets the init data set by [`InitInfo::set_init_data`] during the init.
35    ///
36    /// # Arguments
37    /// * `returns`: The init data object
38    pub fn get_init_data<T>(&self) -> *mut T {
39        unsafe { duckdb_function_get_init_data(self.0).cast() }
40    }
41    /// Retrieves the extra info of the function as set in [`TableFunction::set_extra_info`]
42    ///
43    /// # Arguments
44    /// * `returns`: The extra info
45    pub fn get_extra_info<T>(&self) -> *mut T {
46        unsafe { duckdb_function_get_extra_info(self.0).cast() }
47    }
48    /// Gets the thread-local init data set by [`InitInfo::set_init_data`] during the local_init.
49    ///
50    /// # Arguments
51    /// * `returns`: The init data object
52    pub fn get_local_init_data<T>(&self) -> *mut T {
53        unsafe { duckdb_function_get_local_init_data(self.0).cast() }
54    }
55}
56
57impl From<duckdb_function_info> for FunctionInfo {
58    fn from(ptr: duckdb_function_info) -> Self {
59        Self(ptr)
60    }
61}