@@ -23,6 +23,20 @@ mod excel;
2323pub use function:: { BindInfo , InitInfo , TableFunction , TableFunctionInfo } ;
2424pub use value:: Value ;
2525
26+ /// Options for registering a table function.
27+ pub struct TableFunctionOptions < E > {
28+ /// Extra info passed to the function at runtime.
29+ /// Accessible via `BindInfo::get_extra_info`, `InitInfo::get_extra_info`,
30+ /// or `TableFunctionInfo::get_extra_info`.
31+ pub extra_info : Option < E > ,
32+ }
33+
34+ impl < E > Default for TableFunctionOptions < E > {
35+ fn default ( ) -> Self {
36+ Self { extra_info : None }
37+ }
38+ }
39+
2640use crate :: core:: { DataChunkHandle , LogicalTypeHandle } ;
2741use ffi:: { duckdb_bind_info, duckdb_data_chunk, duckdb_function_info, duckdb_init_info} ;
2842
@@ -151,23 +165,26 @@ impl Connection {
151165 self . db . borrow_mut ( ) . register_table_function ( table_function)
152166 }
153167
154- /// Register the given TableFunction with custom extra info.
155- ///
156- /// This allows you to pass extra info that can be accessed during bind, init, and execution
157- /// via `BindInfo::get_extra_info`, `InitInfo::get_extra_info`, or `TableFunctionInfo::get_extra_info`.
168+ /// Register the given TableFunction with options.
158169 #[ inline]
159- pub fn register_table_function_with_extra_info < T : VTab , E > ( & self , name : & str , extra_info : & E ) -> Result < ( ) >
170+ pub fn register_table_function_with_options < T : VTab , E > (
171+ & self ,
172+ name : & str ,
173+ options : TableFunctionOptions < E > ,
174+ ) -> Result < ( ) >
160175 where
161- E : Clone + Send + Sync + ' static ,
176+ E : Send + Sync + ' static ,
162177 {
163178 let table_function = TableFunction :: default ( ) ;
164179 table_function
165180 . set_name ( name)
166181 . supports_pushdown ( T :: supports_pushdown ( ) )
167182 . set_bind ( Some ( bind :: < T > ) )
168183 . set_init ( Some ( init :: < T > ) )
169- . set_function ( Some ( func :: < T > ) )
170- . with_extra_info ( extra_info. clone ( ) ) ;
184+ . set_function ( Some ( func :: < T > ) ) ;
185+ if let Some ( extra_info) = options. extra_info {
186+ table_function. with_extra_info ( extra_info) ;
187+ }
171188 for ty in T :: parameters ( ) . unwrap_or_default ( ) {
172189 table_function. add_parameter ( & ty) ;
173190 }
@@ -354,9 +371,14 @@ mod test {
354371 }
355372
356373 #[ test]
357- fn test_table_function_with_extra_info ( ) -> Result < ( ) , Box < dyn Error > > {
374+ fn test_table_function_with_options ( ) -> Result < ( ) , Box < dyn Error > > {
358375 let conn = Connection :: open_in_memory ( ) ?;
359- conn. register_table_function_with_extra_info :: < PrefixVTab , _ > ( "greet" , & "Howdy" . to_string ( ) ) ?;
376+ conn. register_table_function_with_options :: < PrefixVTab , _ > (
377+ "greet" ,
378+ TableFunctionOptions {
379+ extra_info : Some ( "Howdy" . to_string ( ) ) ,
380+ } ,
381+ ) ?;
360382
361383 let val = conn. query_row ( "select * from greet('partner')" , [ ] , |row| <( String , ) >:: try_from ( row) ) ?;
362384 assert_eq ! ( val, ( "Howdy partner" . to_string( ) , ) ) ;
0 commit comments