cql3_parser/
create_materialized_view.rs

1use crate::common::{FQName, Identifier, PrimaryKey};
2use crate::common::{RelationElement, WithItem};
3use itertools::Itertools;
4use std::fmt::{Display, Formatter};
5
6/// the data to create a materialized view
7#[derive(PartialEq, Debug, Clone)]
8pub struct CreateMaterializedView {
9    /// only create if it does not exist.
10    pub if_not_exists: bool,
11    /// the name of the materialized view.
12    pub name: FQName,
13    /// the columns in the view.
14    pub columns: Vec<Identifier>,
15    /// the table to extract the view from.
16    pub table: FQName,
17    /// the where clause to select.  Note: all elements of the primary key must be listed
18    /// in the where clause as `column ISNOT NULL`
19    pub where_clause: Vec<RelationElement>,
20    /// the primary key for the view
21    pub key: PrimaryKey,
22    /// the with options.
23    pub with_clause: Vec<WithItem>,
24}
25
26impl Display for CreateMaterializedView {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        write!(
29            f,
30            "CREATE MATERIALIZED VIEW {}{} AS SELECT {} FROM {} WHERE {} {}{}",
31            if self.if_not_exists {
32                "IF NOT EXISTS "
33            } else {
34                ""
35            },
36            self.name,
37            self.columns.iter().map(|c| c.to_string()).join(", "),
38            self.table,
39            self.where_clause.iter().join(" AND "),
40            self.key,
41            if self.with_clause.is_empty() {
42                "".to_string()
43            } else {
44                format!(
45                    " WITH {}",
46                    self.with_clause.iter().map(|x| x.to_string()).join(" AND ")
47                )
48            }
49        )
50    }
51}