cql3_parser/
create_table.rs

1use crate::common::{ColumnDefinition, FQName, PrimaryKey, WithItem};
2use itertools::Itertools;
3use std::fmt::{Display, Formatter};
4
5/// The data for a `Create table` statement
6#[derive(PartialEq, Debug, Clone)]
7pub struct CreateTable {
8    /// only create if the table does not exist
9    pub if_not_exists: bool,
10    /// the name of the table
11    pub name: FQName,
12    /// the column definitions.
13    pub columns: Vec<ColumnDefinition>,
14    /// the primary key if not specified in the column definitions.
15    pub key: Option<PrimaryKey>,
16    /// the list of `WITH` options.
17    pub with_clause: Vec<WithItem>,
18}
19
20impl Display for CreateTable {
21    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22        let mut v: Vec<String> = self.columns.iter().map(|x| x.to_string()).collect();
23        if let Some(key) = &self.key {
24            v.push(key.to_string());
25        }
26        write!(
27            f,
28            "{}{} ({}){}",
29            if self.if_not_exists {
30                "IF NOT EXISTS ".to_string()
31            } else {
32                "".to_string()
33            },
34            self.name,
35            v.join(", "),
36            if !self.with_clause.is_empty() {
37                format!(
38                    " WITH {}",
39                    self.with_clause.iter().map(|x| x.to_string()).join(" AND ")
40                )
41            } else {
42                "".to_string()
43            }
44        )
45    }
46}