#[macro_export]
macro_rules! function {
(
$func_name:ident($($param_name:ident: $param_type:ident),* $(,)?) -> $return_type:ident {
$(language: $language:ident,)?
$(volatility: $volatility:ident,)?
$(security: $security:ident,)?
$(strict: $strict:ident,)?
body: $body:expr
}
) => {
pub mod $func_name {
use $crate::schema::{StoredFunction, ProcedureParameter, ParameterMode, ProcedureLanguage, FunctionVolatility, FunctionSecurity};
pub fn function() -> StoredFunction {
let mut func = StoredFunction::new(stringify!($func_name));
$(
func = func.with_parameter(
ProcedureParameter::new(stringify!($param_name), $crate::function!(@pg_type $param_type))
);
)*
func = func.with_return_type($crate::function!(@pg_type $return_type));
func = func.with_language($crate::function!(@language $($language)?));
func = func.with_volatility($crate::function!(@volatility $($volatility)?));
func = func.with_security($crate::function!(@security $($security)?));
func = func.with_strict($crate::function!(@strict $($strict)?));
func = func.with_body($body);
func
}
pub fn name() -> &'static str {
stringify!($func_name)
}
}
inventory::submit! {
$crate::RegisteredObject($crate::SchemaObject {
kind: "function",
name: stringify!($func_name),
ddl: "", schema: None,
})
}
};
(
$func_name:ident($($param_name:ident: $param_type:ident),* $(,)?) -> Record {
out: {
$($out_param_name:ident: $out_param_type:ident),* $(,)?
},
$(language: $language:ident,)?
$(volatility: $volatility:ident,)?
$(security: $security:ident,)?
$(strict: $strict:ident,)?
body: $body:expr
}
) => {
pub mod $func_name {
use $crate::schema::{StoredFunction, ProcedureParameter, ParameterMode, ProcedureLanguage, FunctionVolatility, FunctionSecurity};
pub fn function() -> StoredFunction {
let mut func = StoredFunction::new(stringify!($func_name));
$(
func = func.with_parameter(
ProcedureParameter::new(stringify!($param_name), $crate::function!(@pg_type $param_type))
.with_mode(ParameterMode::In)
);
)*
$(
func = func.with_parameter(
ProcedureParameter::new(stringify!($out_param_name), $crate::function!(@pg_type $out_param_type))
.with_mode(ParameterMode::Out)
);
)*
func = func.with_language($crate::function!(@language $($language)?));
func = func.with_volatility($crate::function!(@volatility $($volatility)?));
func = func.with_security($crate::function!(@security $($security)?));
func = func.with_strict($crate::function!(@strict $($strict)?));
func = func.with_body($body);
func
}
pub fn name() -> &'static str {
stringify!($func_name)
}
}
inventory::submit! {
$crate::RegisteredObject($crate::SchemaObject {
kind: "function",
name: stringify!($func_name),
ddl: "", schema: None,
})
}
};
(@pg_type Integer) => { "INTEGER" };
(@pg_type Int4) => { "INTEGER" };
(@pg_type Int8) => { "BIGINT" };
(@pg_type Int2) => { "SMALLINT" };
(@pg_type Text) => { "TEXT" };
(@pg_type Varchar) => { "VARCHAR" };
(@pg_type Bool) => { "BOOLEAN" };
(@pg_type Date) => { "DATE" };
(@pg_type Timestamptz) => { "TIMESTAMPTZ" };
(@pg_type Timestamp) => { "TIMESTAMP" };
(@pg_type Numeric) => { "NUMERIC" };
(@pg_type Real) => { "REAL" };
(@pg_type DoublePrecision) => { "DOUBLE PRECISION" };
(@pg_type Uuid) => { "UUID" };
(@pg_type Json) => { "JSON" };
(@pg_type Jsonb) => { "JSONB" };
(@pg_type Record) => { "RECORD" };
(@language) => { ProcedureLanguage::PlPgSql };
(@language plpgsql) => { ProcedureLanguage::PlPgSql };
(@language sql) => { ProcedureLanguage::Sql };
(@language python) => { ProcedureLanguage::PlPython3 };
(@language c) => { ProcedureLanguage::C };
(@volatility) => { FunctionVolatility::Volatile };
(@volatility volatile) => { FunctionVolatility::Volatile };
(@volatility stable) => { FunctionVolatility::Stable };
(@volatility immutable) => { FunctionVolatility::Immutable };
(@security) => { FunctionSecurity::Invoker };
(@security invoker) => { FunctionSecurity::Invoker };
(@security definer) => { FunctionSecurity::Definer };
(@strict) => { false };
(@strict true) => { true };
(@strict false) => { false };
}
#[macro_export]
macro_rules! procedure {
(
$proc_name:ident($($param_name:ident: $param_type:ident),* $(,)?) {
$(language: $language:ident,)?
$(security: $security:ident,)?
body: $body:expr
}
) => {
pub mod $proc_name {
use $crate::schema::{StoredProcedure, ProcedureParameter, ParameterMode, ProcedureLanguage, FunctionSecurity};
pub fn procedure() -> StoredProcedure {
let mut proc = StoredProcedure::new(stringify!($proc_name));
$(
proc = proc.with_parameter(
ProcedureParameter::new(stringify!($param_name), $crate::procedure!(@pg_type $param_type))
);
)*
proc = proc.with_language($crate::procedure!(@language $($language)?));
proc = proc.with_security($crate::procedure!(@security $($security)?));
proc = proc.with_body($body);
proc
}
pub fn name() -> &'static str {
stringify!($proc_name)
}
}
inventory::submit! {
$crate::RegisteredObject($crate::SchemaObject {
kind: "procedure",
name: stringify!($proc_name),
ddl: "", schema: None,
})
}
};
(
$proc_name:ident($($param_name:ident: $param_type:ident),* $(,)?) {
inout: {
$($inout_param_name:ident: $inout_param_type:ident),* $(,)?
},
$(language: $language:ident,)?
$(security: $security:ident,)?
body: $body:expr
}
) => {
pub mod $proc_name {
use $crate::schema::{StoredProcedure, ProcedureParameter, ParameterMode, ProcedureLanguage, FunctionSecurity};
pub fn procedure() -> StoredProcedure {
let mut proc = StoredProcedure::new(stringify!($proc_name));
$(
proc = proc.with_parameter(
ProcedureParameter::new(stringify!($param_name), $crate::procedure!(@pg_type $param_type))
.with_mode(ParameterMode::In)
);
)*
$(
proc = proc.with_parameter(
ProcedureParameter::new(stringify!($inout_param_name), $crate::procedure!(@pg_type $inout_param_type))
.with_mode(ParameterMode::InOut)
);
)*
proc = proc.with_language($crate::procedure!(@language $($language)?));
proc = proc.with_security($crate::procedure!(@security $($security)?));
proc = proc.with_body($body);
proc
}
pub fn name() -> &'static str {
stringify!($proc_name)
}
}
inventory::submit! {
$crate::RegisteredObject($crate::SchemaObject {
kind: "procedure",
name: stringify!($proc_name),
ddl: "", schema: None,
})
}
};
(@pg_type Integer) => { "INTEGER" };
(@pg_type Int4) => { "INTEGER" };
(@pg_type Int8) => { "BIGINT" };
(@pg_type Int2) => { "SMALLINT" };
(@pg_type Text) => { "TEXT" };
(@pg_type Varchar) => { "VARCHAR" };
(@pg_type Bool) => { "BOOLEAN" };
(@pg_type Date) => { "DATE" };
(@pg_type Timestamptz) => { "TIMESTAMPTZ" };
(@pg_type Timestamp) => { "TIMESTAMP" };
(@pg_type Numeric) => { "NUMERIC" };
(@pg_type Real) => { "REAL" };
(@pg_type DoublePrecision) => { "DOUBLE PRECISION" };
(@pg_type Uuid) => { "UUID" };
(@pg_type Json) => { "JSON" };
(@pg_type Jsonb) => { "JSONB" };
(@language) => { ProcedureLanguage::PlPgSql };
(@language plpgsql) => { ProcedureLanguage::PlPgSql };
(@language sql) => { ProcedureLanguage::Sql };
(@language python) => { ProcedureLanguage::PlPython3 };
(@language c) => { ProcedureLanguage::C };
(@security) => { FunctionSecurity::Invoker };
(@security invoker) => { FunctionSecurity::Invoker };
(@security definer) => { FunctionSecurity::Definer };
}
#[cfg(test)]
mod tests {
use super::*;
use crate::schema::*;
function! {
test_calculate_age(birth_date: Date) -> Integer {
language: plpgsql,
volatility: immutable,
body: r#"
BEGIN
RETURN EXTRACT(YEAR FROM AGE(birth_date));
END;
"#
}
}
procedure! {
test_update_status(user_id: Integer, status: Text) {
language: plpgsql,
body: r#"
BEGIN
UPDATE users SET status = status WHERE id = user_id;
COMMIT;
END;
"#
}
}
#[test]
fn test_function_macro() {
let func = test_calculate_age::function();
assert_eq!(func.name, "test_calculate_age");
assert_eq!(func.parameters.len(), 1);
assert_eq!(func.parameters[0].name, "birth_date");
assert_eq!(func.parameters[0].data_type, "DATE");
assert_eq!(func.return_type, Some("INTEGER".to_string()));
assert_eq!(func.volatility, FunctionVolatility::Immutable);
assert_eq!(test_calculate_age::name(), "test_calculate_age");
}
#[test]
fn test_procedure_macro() {
let proc = test_update_status::procedure();
assert_eq!(proc.name, "test_update_status");
assert_eq!(proc.parameters.len(), 2);
assert_eq!(proc.parameters[0].name, "user_id");
assert_eq!(proc.parameters[0].data_type, "INTEGER");
assert_eq!(proc.parameters[1].name, "status");
assert_eq!(proc.parameters[1].data_type, "TEXT");
assert_eq!(test_update_status::name(), "test_update_status");
}
#[test]
fn test_function_ddl_generation() {
let func = test_calculate_age::function();
let ddl = func.generate_create_ddl();
assert!(ddl.contains("CREATE OR REPLACE FUNCTION \"test_calculate_age\""));
assert!(ddl.contains("birth_date DATE"));
assert!(ddl.contains("RETURNS INTEGER"));
assert!(ddl.contains("LANGUAGE plpgsql"));
assert!(ddl.contains("IMMUTABLE"));
assert!(ddl.contains("EXTRACT(YEAR FROM AGE(birth_date))"));
}
#[test]
fn test_procedure_ddl_generation() {
let proc = test_update_status::procedure();
let ddl = proc.generate_create_ddl();
assert!(ddl.contains("CREATE OR REPLACE PROCEDURE \"test_update_status\""));
assert!(ddl.contains("user_id INTEGER"));
assert!(ddl.contains("status TEXT"));
assert!(!ddl.contains("RETURNS")); assert!(ddl.contains("LANGUAGE plpgsql"));
assert!(ddl.contains("UPDATE users SET"));
}
}