cql3_parser/
alter_table.rs

1use crate::common::{ColumnDefinition, FQName, Identifier, WithItem};
2use itertools::Itertools;
3use std::fmt::{Display, Formatter};
4
5/// data for the `AlterTable` command
6#[derive(PartialEq, Debug, Clone)]
7pub struct AlterTable {
8    /// the name of the table.
9    pub name: FQName,
10    /// the table alteration operation.
11    pub operation: AlterTableOperation,
12}
13
14/// table alteration operations
15#[derive(PartialEq, Debug, Clone)]
16pub enum AlterTableOperation {
17    /// add columns to the table.
18    Add(Vec<ColumnDefinition>),
19    /// drop columns from the table.
20    DropColumns(Vec<Identifier>),
21    /// drop the "compact storage"
22    DropCompactStorage,
23    /// rename columns `(from, to)`
24    Rename((Identifier, Identifier)),
25    /// add with element options.
26    With(Vec<WithItem>),
27}
28
29impl Display for AlterTableOperation {
30    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
31        match self {
32            AlterTableOperation::Add(columns) => write!(
33                f,
34                "ADD {}",
35                columns.iter().map(|x| x.to_string()).join(", ")
36            ),
37            AlterTableOperation::DropColumns(columns) => write!(
38                f,
39                "DROP {}",
40                columns.iter().map(|c| c.to_string()).join(", ")
41            ),
42            AlterTableOperation::DropCompactStorage => write!(f, "DROP COMPACT STORAGE"),
43            AlterTableOperation::Rename((from, to)) => write!(f, "RENAME {} TO {}", from, to),
44            AlterTableOperation::With(with_element) => write!(
45                f,
46                "WITH {}",
47                with_element.iter().map(|x| x.to_string()).join(" AND ")
48            ),
49        }
50    }
51}