ignis_db/macros/
procedure.rs1#[macro_export]
42macro_rules! function {
43 (
45 $func_name:ident($($param_name:ident: $param_type:ident),* $(,)?) -> $return_type:ident {
46 $(language: $language:ident,)?
47 $(volatility: $volatility:ident,)?
48 $(security: $security:ident,)?
49 $(strict: $strict:ident,)?
50 body: $body:expr
51 }
52 ) => {
53 pub mod $func_name {
54 use $crate::schema::{StoredFunction, ProcedureParameter, ParameterMode, ProcedureLanguage, FunctionVolatility, FunctionSecurity};
55
56 pub fn function() -> StoredFunction {
58 let mut func = StoredFunction::new(stringify!($func_name));
59
60 $(
62 func = func.with_parameter(
63 ProcedureParameter::new(stringify!($param_name), $crate::function!(@pg_type $param_type))
64 );
65 )*
66
67 func = func.with_return_type($crate::function!(@pg_type $return_type));
69
70 func = func.with_language($crate::function!(@language $($language)?));
72
73 func = func.with_volatility($crate::function!(@volatility $($volatility)?));
75
76 func = func.with_security($crate::function!(@security $($security)?));
78
79 func = func.with_strict($crate::function!(@strict $($strict)?));
81
82 func = func.with_body($body);
84
85 func
86 }
87
88 pub fn name() -> &'static str {
90 stringify!($func_name)
91 }
92 }
93
94 inventory::submit! {
96 $crate::RegisteredObject($crate::SchemaObject {
97 kind: "function",
98 name: stringify!($func_name),
99 ddl: "", schema: None,
101 })
102 }
103 };
104
105 (
107 $func_name:ident($($param_name:ident: $param_type:ident),* $(,)?) -> Record {
108 out: {
109 $($out_param_name:ident: $out_param_type:ident),* $(,)?
110 },
111 $(language: $language:ident,)?
112 $(volatility: $volatility:ident,)?
113 $(security: $security:ident,)?
114 $(strict: $strict:ident,)?
115 body: $body:expr
116 }
117 ) => {
118 pub mod $func_name {
119 use $crate::schema::{StoredFunction, ProcedureParameter, ParameterMode, ProcedureLanguage, FunctionVolatility, FunctionSecurity};
120
121 pub fn function() -> StoredFunction {
123 let mut func = StoredFunction::new(stringify!($func_name));
124
125 $(
127 func = func.with_parameter(
128 ProcedureParameter::new(stringify!($param_name), $crate::function!(@pg_type $param_type))
129 .with_mode(ParameterMode::In)
130 );
131 )*
132
133 $(
135 func = func.with_parameter(
136 ProcedureParameter::new(stringify!($out_param_name), $crate::function!(@pg_type $out_param_type))
137 .with_mode(ParameterMode::Out)
138 );
139 )*
140
141 func = func.with_language($crate::function!(@language $($language)?));
143
144 func = func.with_volatility($crate::function!(@volatility $($volatility)?));
146
147 func = func.with_security($crate::function!(@security $($security)?));
149
150 func = func.with_strict($crate::function!(@strict $($strict)?));
152
153 func = func.with_body($body);
155
156 func
157 }
158
159 pub fn name() -> &'static str {
161 stringify!($func_name)
162 }
163 }
164
165 inventory::submit! {
167 $crate::RegisteredObject($crate::SchemaObject {
168 kind: "function",
169 name: stringify!($func_name),
170 ddl: "", schema: None,
172 })
173 }
174 };
175
176 (@pg_type Integer) => { "INTEGER" };
178 (@pg_type Int4) => { "INTEGER" };
179 (@pg_type Int8) => { "BIGINT" };
180 (@pg_type Int2) => { "SMALLINT" };
181 (@pg_type Text) => { "TEXT" };
182 (@pg_type Varchar) => { "VARCHAR" };
183 (@pg_type Bool) => { "BOOLEAN" };
184 (@pg_type Date) => { "DATE" };
185 (@pg_type Timestamptz) => { "TIMESTAMPTZ" };
186 (@pg_type Timestamp) => { "TIMESTAMP" };
187 (@pg_type Numeric) => { "NUMERIC" };
188 (@pg_type Real) => { "REAL" };
189 (@pg_type DoublePrecision) => { "DOUBLE PRECISION" };
190 (@pg_type Uuid) => { "UUID" };
191 (@pg_type Json) => { "JSON" };
192 (@pg_type Jsonb) => { "JSONB" };
193 (@pg_type Record) => { "RECORD" };
194
195 (@language) => { ProcedureLanguage::PlPgSql };
197 (@language plpgsql) => { ProcedureLanguage::PlPgSql };
198 (@language sql) => { ProcedureLanguage::Sql };
199 (@language python) => { ProcedureLanguage::PlPython3 };
200 (@language c) => { ProcedureLanguage::C };
201
202 (@volatility) => { FunctionVolatility::Volatile };
204 (@volatility volatile) => { FunctionVolatility::Volatile };
205 (@volatility stable) => { FunctionVolatility::Stable };
206 (@volatility immutable) => { FunctionVolatility::Immutable };
207
208 (@security) => { FunctionSecurity::Invoker };
210 (@security invoker) => { FunctionSecurity::Invoker };
211 (@security definer) => { FunctionSecurity::Definer };
212
213 (@strict) => { false };
215 (@strict true) => { true };
216 (@strict false) => { false };
217}
218
219#[macro_export]
261macro_rules! procedure {
262 (
264 $proc_name:ident($($param_name:ident: $param_type:ident),* $(,)?) {
265 $(language: $language:ident,)?
266 $(security: $security:ident,)?
267 body: $body:expr
268 }
269 ) => {
270 pub mod $proc_name {
271 use $crate::schema::{StoredProcedure, ProcedureParameter, ParameterMode, ProcedureLanguage, FunctionSecurity};
272
273 pub fn procedure() -> StoredProcedure {
275 let mut proc = StoredProcedure::new(stringify!($proc_name));
276
277 $(
279 proc = proc.with_parameter(
280 ProcedureParameter::new(stringify!($param_name), $crate::procedure!(@pg_type $param_type))
281 );
282 )*
283
284 proc = proc.with_language($crate::procedure!(@language $($language)?));
286
287 proc = proc.with_security($crate::procedure!(@security $($security)?));
289
290 proc = proc.with_body($body);
292
293 proc
294 }
295
296 pub fn name() -> &'static str {
298 stringify!($proc_name)
299 }
300 }
301
302 inventory::submit! {
304 $crate::RegisteredObject($crate::SchemaObject {
305 kind: "procedure",
306 name: stringify!($proc_name),
307 ddl: "", schema: None,
309 })
310 }
311 };
312
313 (
315 $proc_name:ident($($param_name:ident: $param_type:ident),* $(,)?) {
316 inout: {
317 $($inout_param_name:ident: $inout_param_type:ident),* $(,)?
318 },
319 $(language: $language:ident,)?
320 $(security: $security:ident,)?
321 body: $body:expr
322 }
323 ) => {
324 pub mod $proc_name {
325 use $crate::schema::{StoredProcedure, ProcedureParameter, ParameterMode, ProcedureLanguage, FunctionSecurity};
326
327 pub fn procedure() -> StoredProcedure {
329 let mut proc = StoredProcedure::new(stringify!($proc_name));
330
331 $(
333 proc = proc.with_parameter(
334 ProcedureParameter::new(stringify!($param_name), $crate::procedure!(@pg_type $param_type))
335 .with_mode(ParameterMode::In)
336 );
337 )*
338
339 $(
341 proc = proc.with_parameter(
342 ProcedureParameter::new(stringify!($inout_param_name), $crate::procedure!(@pg_type $inout_param_type))
343 .with_mode(ParameterMode::InOut)
344 );
345 )*
346
347 proc = proc.with_language($crate::procedure!(@language $($language)?));
349
350 proc = proc.with_security($crate::procedure!(@security $($security)?));
352
353 proc = proc.with_body($body);
355
356 proc
357 }
358
359 pub fn name() -> &'static str {
361 stringify!($proc_name)
362 }
363 }
364
365 inventory::submit! {
367 $crate::RegisteredObject($crate::SchemaObject {
368 kind: "procedure",
369 name: stringify!($proc_name),
370 ddl: "", schema: None,
372 })
373 }
374 };
375
376 (@pg_type Integer) => { "INTEGER" };
378 (@pg_type Int4) => { "INTEGER" };
379 (@pg_type Int8) => { "BIGINT" };
380 (@pg_type Int2) => { "SMALLINT" };
381 (@pg_type Text) => { "TEXT" };
382 (@pg_type Varchar) => { "VARCHAR" };
383 (@pg_type Bool) => { "BOOLEAN" };
384 (@pg_type Date) => { "DATE" };
385 (@pg_type Timestamptz) => { "TIMESTAMPTZ" };
386 (@pg_type Timestamp) => { "TIMESTAMP" };
387 (@pg_type Numeric) => { "NUMERIC" };
388 (@pg_type Real) => { "REAL" };
389 (@pg_type DoublePrecision) => { "DOUBLE PRECISION" };
390 (@pg_type Uuid) => { "UUID" };
391 (@pg_type Json) => { "JSON" };
392 (@pg_type Jsonb) => { "JSONB" };
393
394 (@language) => { ProcedureLanguage::PlPgSql };
396 (@language plpgsql) => { ProcedureLanguage::PlPgSql };
397 (@language sql) => { ProcedureLanguage::Sql };
398 (@language python) => { ProcedureLanguage::PlPython3 };
399 (@language c) => { ProcedureLanguage::C };
400
401 (@security) => { FunctionSecurity::Invoker };
403 (@security invoker) => { FunctionSecurity::Invoker };
404 (@security definer) => { FunctionSecurity::Definer };
405}
406
407#[cfg(test)]
408mod tests {
409 use super::*;
410 use crate::schema::*;
411
412 function! {
414 test_calculate_age(birth_date: Date) -> Integer {
415 language: plpgsql,
416 volatility: immutable,
417 body: r#"
418 BEGIN
419 RETURN EXTRACT(YEAR FROM AGE(birth_date));
420 END;
421 "#
422 }
423 }
424
425 procedure! {
427 test_update_status(user_id: Integer, status: Text) {
428 language: plpgsql,
429 body: r#"
430 BEGIN
431 UPDATE users SET status = status WHERE id = user_id;
432 COMMIT;
433 END;
434 "#
435 }
436 }
437
438 #[test]
439 fn test_function_macro() {
440 let func = test_calculate_age::function();
441 assert_eq!(func.name, "test_calculate_age");
442 assert_eq!(func.parameters.len(), 1);
443 assert_eq!(func.parameters[0].name, "birth_date");
444 assert_eq!(func.parameters[0].data_type, "DATE");
445 assert_eq!(func.return_type, Some("INTEGER".to_string()));
446 assert_eq!(func.volatility, FunctionVolatility::Immutable);
447 assert_eq!(test_calculate_age::name(), "test_calculate_age");
448 }
449
450 #[test]
451 fn test_procedure_macro() {
452 let proc = test_update_status::procedure();
453 assert_eq!(proc.name, "test_update_status");
454 assert_eq!(proc.parameters.len(), 2);
455 assert_eq!(proc.parameters[0].name, "user_id");
456 assert_eq!(proc.parameters[0].data_type, "INTEGER");
457 assert_eq!(proc.parameters[1].name, "status");
458 assert_eq!(proc.parameters[1].data_type, "TEXT");
459 assert_eq!(test_update_status::name(), "test_update_status");
460 }
461
462 #[test]
463 fn test_function_ddl_generation() {
464 let func = test_calculate_age::function();
465 let ddl = func.generate_create_ddl();
466
467 assert!(ddl.contains("CREATE OR REPLACE FUNCTION \"test_calculate_age\""));
468 assert!(ddl.contains("birth_date DATE"));
469 assert!(ddl.contains("RETURNS INTEGER"));
470 assert!(ddl.contains("LANGUAGE plpgsql"));
471 assert!(ddl.contains("IMMUTABLE"));
472 assert!(ddl.contains("EXTRACT(YEAR FROM AGE(birth_date))"));
473 }
474
475 #[test]
476 fn test_procedure_ddl_generation() {
477 let proc = test_update_status::procedure();
478 let ddl = proc.generate_create_ddl();
479
480 assert!(ddl.contains("CREATE OR REPLACE PROCEDURE \"test_update_status\""));
481 assert!(ddl.contains("user_id INTEGER"));
482 assert!(ddl.contains("status TEXT"));
483 assert!(!ddl.contains("RETURNS")); assert!(ddl.contains("LANGUAGE plpgsql"));
485 assert!(ddl.contains("UPDATE users SET"));
486 }
487}