endpoint_libs/model/
pg_func.rs1use crate::model::{Field, Type};
2use convert_case::Case;
3use convert_case::Casing;
4use itertools::Itertools;
5
6#[derive(Clone, Debug)]
8pub struct ProceduralFunction {
9 pub name: String,
11
12 pub parameters: Vec<Field>,
14
15 pub return_row_type: Type,
17
18 pub body: String,
20}
21
22fn sort_parameters(parameters: Vec<Field>) -> Vec<Field> {
24 parameters
25 .into_iter()
26 .sorted_by_cached_key(|x| matches!(x.ty, Type::Optional(_)))
27 .collect()
28}
29
30impl ProceduralFunction {
31 pub fn new(name: impl Into<String>, parameters: Vec<Field>, returns: Vec<Field>, body: impl Into<String>) -> Self {
33 let name = name.into();
34 Self {
35 name: name.clone(),
36 parameters: sort_parameters(parameters),
37 return_row_type: Type::struct_(format!("{}RespRow", name.to_case(Case::Pascal)), returns),
38 body: body.into(),
39 }
40 }
41
42 pub fn new_with_row_type(
44 name: impl Into<String>,
45 parameters: Vec<Field>,
46 return_row_type: Type,
47 body: impl Into<String>,
48 ) -> Self {
49 Self {
50 name: name.into(),
51 parameters: sort_parameters(parameters),
52 return_row_type,
53 body: body.into(),
54 }
55 }
56}