cql3_parser/
aggregate.rs

1use crate::common::{DataType, FQName};
2use itertools::Itertools;
3use std::fmt::{Display, Formatter};
4
5#[derive(PartialEq, Debug, Clone)]
6pub struct Aggregate {
7    pub or_replace: bool,
8    pub not_exists: bool,
9    pub name: FQName,
10    pub data_type: DataType,
11    pub sfunc: FQName,
12    pub stype: DataType,
13    pub finalfunc: FQName,
14    pub init_cond: InitCondition,
15}
16
17impl Display for Aggregate {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        write!(
20            f,
21            "CREATE {}AGGREGATE {}{} ({}) SFUNC {} STYPE {} FINALFUNC {} INITCOND {}",
22            if self.or_replace { "OR REPLACE " } else { "" },
23            if self.not_exists {
24                "IF NOT EXISTS "
25            } else {
26                ""
27            },
28            self.name,
29            self.data_type,
30            self.sfunc,
31            self.stype,
32            self.finalfunc,
33            self.init_cond
34        )
35    }
36}
37
38#[derive(PartialEq, Debug, Clone)]
39pub enum InitCondition {
40    Constant(String),
41    List(Vec<InitCondition>),
42    Map(Vec<(String, InitCondition)>),
43}
44
45impl Display for InitCondition {
46    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
47        match self {
48            InitCondition::Constant(name) => write!(f, "{}", name),
49            InitCondition::List(lst) => {
50                write!(f, "({})", lst.iter().map(|x| x.to_string()).join(", "))
51            }
52            InitCondition::Map(entries) => write!(
53                f,
54                "({})",
55                entries
56                    .iter()
57                    .map(|(k, v)| format!("{}:{}", k, v))
58                    .join(", ")
59            ),
60        }
61    }
62}