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(), r#"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"],
// Read a table parameter through `query_table`; a bare `FROM tbl`
// is resolved when the macro is created, and fails.
"SELECT * FROM query_table(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.
The macro name and parameter names are emitted as double-quoted
identifiers; see the module docs.
The body is emitted verbatim — except that a scalar body containing
-- is followed by a newline before the closing parenthesis, so a
trailing line comment cannot swallow it.
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(), r#"CREATE OR REPLACE MACRO "add"("a", "b") AS (a + b)"#);
// A table parameter is read through `query_table`: a bare `FROM tbl` would
// look for a table named `tbl` when the macro is created, and fail.
let t = SqlMacro::table("active_rows", &["tbl"], "SELECT * FROM query_table(tbl) WHERE active = true")
.unwrap();
assert_eq!(
t.to_sql(),
r#"CREATE OR REPLACE MACRO "active_rows"("tbl") AS TABLE SELECT * FROM query_table(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 —
after checking with duckdb_extract_statements, DuckDB’s own parser,
that it is exactly one statement. Read
where a macro lives
first: the macro is created in the user’s database, persists there,
and fails on a read-only database.
§Errors
Returns ExtensionError if the SQL does not parse, or the body makes it
more than one statement (nothing is executed in either case; the parse
error is DuckDB’s own), or if DuckDB rejects the SQL
statement — for example on a read-only database. The error message is
extracted from duckdb_result_error.
§Safety
con must be a valid, open [duckdb_connection].