cql3_parser/
alter_type.rs

1use crate::alter_column::AlterColumnType;
2use crate::common::{ColumnDefinition, FQName, Identifier};
3use itertools::Itertools;
4use std::fmt::{Display, Formatter};
5
6/// data for an `AlterType` statement
7#[derive(PartialEq, Debug, Clone)]
8pub struct AlterType {
9    /// the name of the type to alter
10    pub name: FQName,
11    /// the operation to perform on the type.
12    pub operation: AlterTypeOperation,
13}
14
15impl Display for AlterType {
16    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
17        write!(f, "ALTER TYPE {} {}", self.name, self.operation)
18    }
19}
20
21/// the alter type operations.
22#[derive(PartialEq, Debug, Clone)]
23pub enum AlterTypeOperation {
24    /// Alter the column type
25    AlterColumnType(AlterColumnType),
26    /// Add a columm
27    Add(Vec<ColumnDefinition>),
28    /// rename a column
29    Rename(Vec<(Identifier, Identifier)>),
30}
31
32impl Display for AlterTypeOperation {
33    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34        match self {
35            AlterTypeOperation::AlterColumnType(column_type) => write!(f, "{}", column_type),
36            AlterTypeOperation::Add(columns) => write!(
37                f,
38                "ADD {}",
39                columns.iter().map(|x| x.to_string()).join(", ")
40            ),
41            AlterTypeOperation::Rename(pairs) => write!(
42                f,
43                "RENAME {}",
44                pairs
45                    .iter()
46                    .map(|(x, y)| format!("{} TO {}", x, y))
47                    .join(" AND ")
48            ),
49        }
50    }
51}