|
| 1 | +extern crate duckdb; |
| 2 | +extern crate duckdb_loadable_macros; |
| 3 | +extern crate libduckdb_sys; |
| 4 | + |
| 5 | +use duckdb::{ |
| 6 | + vtab::{BindInfo, DataChunk, Free, FunctionInfo, InitInfo, Inserter, LogicalType, LogicalTypeId, VTab}, |
| 7 | + Connection, Result, |
| 8 | +}; |
| 9 | +use duckdb_loadable_macros::duckdb_entrypoint; |
| 10 | +use libduckdb_sys as ffi; |
| 11 | +use std::{ |
| 12 | + error::Error, |
| 13 | + ffi::{c_char, c_void, CString}, |
| 14 | +}; |
| 15 | + |
| 16 | +#[repr(C)] |
| 17 | +struct HelloBindData { |
| 18 | + name: *mut c_char, |
| 19 | +} |
| 20 | + |
| 21 | +impl Free for HelloBindData { |
| 22 | + fn free(&mut self) { |
| 23 | + unsafe { |
| 24 | + if self.name.is_null() { |
| 25 | + return; |
| 26 | + } |
| 27 | + drop(CString::from_raw(self.name)); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#[repr(C)] |
| 33 | +struct HelloInitData { |
| 34 | + done: bool, |
| 35 | +} |
| 36 | + |
| 37 | +struct HelloVTab; |
| 38 | + |
| 39 | +impl Free for HelloInitData {} |
| 40 | + |
| 41 | +impl VTab for HelloVTab { |
| 42 | + type InitData = HelloInitData; |
| 43 | + type BindData = HelloBindData; |
| 44 | + |
| 45 | + fn bind(bind: &BindInfo, data: *mut HelloBindData) -> Result<(), Box<dyn std::error::Error>> { |
| 46 | + bind.add_result_column("column0", LogicalType::new(LogicalTypeId::Varchar)); |
| 47 | + let param = bind.get_parameter(0).to_string(); |
| 48 | + unsafe { |
| 49 | + (*data).name = CString::new(param).unwrap().into_raw(); |
| 50 | + } |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | + |
| 54 | + fn init(_: &InitInfo, data: *mut HelloInitData) -> Result<(), Box<dyn std::error::Error>> { |
| 55 | + unsafe { |
| 56 | + (*data).done = false; |
| 57 | + } |
| 58 | + Ok(()) |
| 59 | + } |
| 60 | + |
| 61 | + fn func(func: &FunctionInfo, output: &mut DataChunk) -> Result<(), Box<dyn std::error::Error>> { |
| 62 | + let init_info = func.get_init_data::<HelloInitData>(); |
| 63 | + let bind_info = func.get_bind_data::<HelloBindData>(); |
| 64 | + |
| 65 | + unsafe { |
| 66 | + if (*init_info).done { |
| 67 | + output.set_len(0); |
| 68 | + } else { |
| 69 | + (*init_info).done = true; |
| 70 | + let vector = output.flat_vector(0); |
| 71 | + let name = CString::from_raw((*bind_info).name); |
| 72 | + let result = CString::new(format!("Hello {}", name.to_str()?))?; |
| 73 | + // Can't consume the CString |
| 74 | + (*bind_info).name = CString::into_raw(name); |
| 75 | + vector.insert(0, result); |
| 76 | + output.set_len(1); |
| 77 | + } |
| 78 | + } |
| 79 | + Ok(()) |
| 80 | + } |
| 81 | + |
| 82 | + fn parameters() -> Option<Vec<LogicalType>> { |
| 83 | + Some(vec![LogicalType::new(LogicalTypeId::Varchar)]) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Exposes a extern C function named "libhello_ext_init" in the compiled dynamic library, |
| 88 | +// the "entrypoint" that duckdb will use to load the extension. |
| 89 | +#[duckdb_entrypoint] |
| 90 | +pub fn libhello_ext_init(conn: Connection) -> Result<(), Box<dyn Error>> { |
| 91 | + conn.register_table_function::<HelloVTab>("hello")?; |
| 92 | + Ok(()) |
| 93 | +} |
0 commit comments