duckdb_extension_framework/table_functions/
table_function.rs1use crate::duckly::{
2 duckdb_create_table_function, duckdb_delete_callback_t, duckdb_destroy_table_function,
3 duckdb_table_function, duckdb_table_function_add_parameter, duckdb_table_function_init_t,
4 duckdb_table_function_set_bind, duckdb_table_function_set_extra_info,
5 duckdb_table_function_set_function, duckdb_table_function_set_init,
6 duckdb_table_function_set_local_init, duckdb_table_function_set_name,
7 duckdb_table_function_supports_projection_pushdown,
8};
9use crate::logical_type::LogicalType;
10#[allow(unused)]
11use crate::table_functions::InitInfo;
12use std::ffi::{c_void, CString};
13
14#[derive(Debug)]
16pub struct TableFunction {
17 pub(crate) ptr: duckdb_table_function,
18}
19
20impl Drop for TableFunction {
21 fn drop(&mut self) {
22 unsafe {
23 duckdb_destroy_table_function(&mut self.ptr);
24 }
25 }
26}
27
28impl TableFunction {
29 pub fn supports_pushdown(&self, supports: bool) -> &Self {
38 unsafe {
39 duckdb_table_function_supports_projection_pushdown(self.ptr, supports);
40 }
41 self
42 }
43
44 pub fn add_parameter(&self, logical_type: &LogicalType) -> &Self {
49 unsafe {
50 duckdb_table_function_add_parameter(self.ptr, logical_type.typ);
51 }
52 self
53 }
54
55 pub fn set_function(
60 &self,
61 func: Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>,
62 ) -> &Self {
63 unsafe {
64 duckdb_table_function_set_function(self.ptr, func);
65 }
66 self
67 }
68
69 pub fn set_init(&self, init_func: Option<unsafe extern "C" fn(*mut c_void)>) -> &Self {
74 unsafe {
75 duckdb_table_function_set_init(self.ptr, init_func);
76 }
77 self
78 }
79
80 pub fn set_bind(&self, bind_func: Option<unsafe extern "C" fn(*mut c_void)>) -> &Self {
85 unsafe {
86 duckdb_table_function_set_bind(self.ptr, bind_func);
87 }
88 self
89 }
90
91 pub fn new() -> Self {
93 Self {
94 ptr: unsafe { duckdb_create_table_function() },
95 }
96 }
97
98 pub fn set_name(&self, name: &str) -> &TableFunction {
103 unsafe {
104 let string = CString::from_vec_unchecked(name.as_bytes().into());
105 duckdb_table_function_set_name(self.ptr, string.as_ptr());
106 }
107 self
108 }
109
110 pub unsafe fn set_extra_info(
118 &self,
119 extra_info: *mut c_void,
120 destroy: duckdb_delete_callback_t,
121 ) {
122 duckdb_table_function_set_extra_info(self.ptr, extra_info, destroy);
123 }
124
125 pub fn set_local_init(&self, init: duckdb_table_function_init_t) {
130 unsafe { duckdb_table_function_set_local_init(self.ptr, init) };
131 }
132}
133impl Default for TableFunction {
134 fn default() -> Self {
135 Self::new()
136 }
137}