use crate::common::{FQName, Identifier, PrimaryKey};
use crate::common::{RelationElement, WithItem};
use itertools::Itertools;
use std::fmt::{Display, Formatter};
#[derive(PartialEq, Debug, Clone)]
pub struct CreateMaterializedView {
pub if_not_exists: bool,
pub name: FQName,
pub columns: Vec<Identifier>,
pub table: FQName,
pub where_clause: Vec<RelationElement>,
pub key: PrimaryKey,
pub with_clause: Vec<WithItem>,
}
impl Display for CreateMaterializedView {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"CREATE MATERIALIZED VIEW {}{} AS SELECT {} FROM {} WHERE {} {}{}",
if self.if_not_exists {
"IF NOT EXISTS "
} else {
""
},
self.name,
self.columns.iter().map(|c| c.to_string()).join(", "),
self.table,
self.where_clause.iter().join(" AND "),
self.key,
if self.with_clause.is_empty() {
"".to_string()
} else {
format!(
" WITH {}",
self.with_clause.iter().map(|x| x.to_string()).join(" AND ")
)
}
)
}
}