pub struct SqlMacro { /* private fields */ }Expand description
A SQL macro definition ready to be registered with DuckDB.
Use SqlMacro::scalar or SqlMacro::table to construct, then call
SqlMacro::register to install. Use SqlMacro::to_sql to inspect
the generated CREATE MACRO statement without a live connection.
§Example
use quack_rs::sql_macro::SqlMacro;
let m = SqlMacro::scalar("add", &["a", "b"], "a + b").unwrap();
assert_eq!(m.to_sql(), "CREATE OR REPLACE MACRO add(a, b) AS (a + b)");Implementations§
Source§impl SqlMacro
impl SqlMacro
Sourcepub fn scalar(
name: &str,
params: &[&str],
expression: impl Into<String>,
) -> Result<Self, ExtensionError>
pub fn scalar( name: &str, params: &[&str], expression: impl Into<String>, ) -> Result<Self, ExtensionError>
Creates a scalar SQL macro definition.
Registers as:
CREATE OR REPLACE MACRO name(params) AS (expression)§Errors
Returns ExtensionError if name or any parameter name is invalid.
See validate_function_name
for naming rules.
§Example
use quack_rs::sql_macro::SqlMacro;
let m = SqlMacro::scalar("clamp", &["x", "lo", "hi"], "greatest(lo, least(hi, x))")?;Sourcepub fn table(
name: &str,
params: &[&str],
query: impl Into<String>,
) -> Result<Self, ExtensionError>
pub fn table( name: &str, params: &[&str], query: impl Into<String>, ) -> Result<Self, ExtensionError>
Creates a table SQL macro definition.
Registers as:
CREATE OR REPLACE MACRO name(params) AS TABLE query§Errors
Returns ExtensionError if name or any parameter name is invalid.
§Example
use quack_rs::sql_macro::SqlMacro;
let m = SqlMacro::table(
"active_rows",
&["tbl"],
"SELECT * FROM tbl WHERE active = true",
)?;Sourcepub fn to_sql(&self) -> String
pub fn to_sql(&self) -> String
Returns the CREATE OR REPLACE MACRO SQL statement for this definition.
Useful for logging, testing, and inspection without a live connection.
§Example
use quack_rs::sql_macro::SqlMacro;
let m = SqlMacro::scalar("add", &["a", "b"], "a + b").unwrap();
assert_eq!(m.to_sql(), "CREATE OR REPLACE MACRO add(a, b) AS (a + b)");
let t = SqlMacro::table("active_rows", &["tbl"], "SELECT * FROM tbl WHERE active = true").unwrap();
assert_eq!(
t.to_sql(),
"CREATE OR REPLACE MACRO active_rows(tbl) AS TABLE SELECT * FROM tbl WHERE active = true"
);Sourcepub unsafe fn register(
self,
con: duckdb_connection,
) -> Result<(), ExtensionError>
pub unsafe fn register( self, con: duckdb_connection, ) -> Result<(), ExtensionError>
Registers this macro on the given connection.
Executes the CREATE OR REPLACE MACRO statement via duckdb_query.
§Errors
Returns ExtensionError if DuckDB rejects the SQL statement.
The error message is extracted from duckdb_result_error.
§Safety
con must be a valid, open duckdb_connection.