1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::common::DataType;
use crate::common::{ColumnDefinition, FQName};
use itertools::Itertools;
use std::fmt::{Display, Formatter};

/// Data for the create function statement
#[derive(PartialEq, Debug, Clone)]
pub struct CreateFunction {
    /// if specified the 'OR REPLACE' clause will be added.
    pub or_replace: bool,
    /// if specified the 'NOT EXISTS' clause will be added.
    pub not_exists: bool,
    /// the name of the function.
    pub name: FQName,
    /// the parameters for the function.
    pub params: Vec<ColumnDefinition>,
    /// if set the function should return `NULL`` when called with `NULL`` otherwise
    /// the function should process the input.
    pub return_null: bool,
    /// the data type the function returns.
    pub return_type: DataType,
    /// the language the function is written in.
    pub language: String,
    /// the code block containing the function
    pub code_block: String,
}

impl Display for CreateFunction {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "CREATE {}FUNCTION {}{} ({}) {} ON NULL INPUT RETURNS {} LANGUAGE {} AS {}",
            if self.or_replace { "OR REPLACE " } else { "" },
            if self.not_exists {
                "IF NOT EXISTS "
            } else {
                ""
            },
            self.name,
            self.params.iter().map(|x| x.to_string()).join(", "),
            if self.return_null {
                "RETURNS NULL"
            } else {
                "CALLED"
            },
            self.return_type,
            self.language,
            self.code_block
        )
    }
}