cql3_parser/
create_function.rs

1use crate::common::DataType;
2use crate::common::{ColumnDefinition, FQName};
3use itertools::Itertools;
4use std::fmt::{Display, Formatter};
5
6/// Data for the create function statement
7#[derive(PartialEq, Debug, Clone)]
8pub struct CreateFunction {
9    /// if specified the 'OR REPLACE' clause will be added.
10    pub or_replace: bool,
11    /// if specified the 'NOT EXISTS' clause will be added.
12    pub not_exists: bool,
13    /// the name of the function.
14    pub name: FQName,
15    /// the parameters for the function.
16    pub params: Vec<ColumnDefinition>,
17    /// if set the function should return `NULL`` when called with `NULL`` otherwise
18    /// the function should process the input.
19    pub return_null: bool,
20    /// the data type the function returns.
21    pub return_type: DataType,
22    /// the language the function is written in.
23    pub language: String,
24    /// the code block containing the function
25    pub code_block: String,
26}
27
28impl Display for CreateFunction {
29    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30        write!(
31            f,
32            "CREATE {}FUNCTION {}{} ({}) {} ON NULL INPUT RETURNS {} LANGUAGE {} AS {}",
33            if self.or_replace { "OR REPLACE " } else { "" },
34            if self.not_exists {
35                "IF NOT EXISTS "
36            } else {
37                ""
38            },
39            self.name,
40            self.params.iter().map(|x| x.to_string()).join(", "),
41            if self.return_null {
42                "RETURNS NULL"
43            } else {
44                "CALLED"
45            },
46            self.return_type,
47            self.language,
48            self.code_block
49        )
50    }
51}