duckdb_extension_framework/table_functions/
replacement_scan.rs

1/// A replacement scan is a way to pretend that a table exists in DuckDB
2/// For example, you can do the following:
3/// ```sql
4/// SELECT * from "hello.csv"
5/// ```
6/// and DuckDB will realise that you're referring to a CSV file, and read that instead
7use crate::{
8    duckly::{
9        duckdb_replacement_scan_add_parameter, duckdb_replacement_scan_info,
10        duckdb_replacement_scan_set_error, duckdb_replacement_scan_set_function_name,
11    },
12    Value,
13};
14use std::ffi::CString;
15pub struct ReplacementScanInfo(pub(crate) duckdb_replacement_scan_info);
16
17impl ReplacementScanInfo {
18    /// Sets the replacement function name to use. If this function is called in the replacement callback, the replacement scan is performed. If it is not called, the replacement callback is not performed.
19    pub fn set_function_name(&mut self, function_name: &str) {
20        unsafe {
21            let function_name = CString::new(function_name).unwrap();
22            duckdb_replacement_scan_set_function_name(self.0, function_name.as_ptr());
23        }
24    }
25    /// Adds a parameter to the replacement scan function.
26    pub fn add_parameter(&mut self, parameter: Value) {
27        unsafe {
28            duckdb_replacement_scan_add_parameter(self.0, parameter.0);
29        }
30    }
31    /// Report that an error has occurred while executing the replacement scan.
32    pub fn set_error(&mut self, error: &str) {
33        unsafe {
34            let error = CString::new(error).unwrap();
35            duckdb_replacement_scan_set_error(self.0, error.as_ptr());
36        }
37    }
38}
39
40impl From<duckdb_replacement_scan_info> for ReplacementScanInfo {
41    fn from(value: duckdb_replacement_scan_info) -> Self {
42        Self(value)
43    }
44}