cql3_parser/
create_index.rs

1use crate::common::{FQName, Identifier};
2use std::fmt::{Display, Formatter};
3
4/// data to for the create index statement.
5#[derive(PartialEq, Debug, Clone)]
6pub struct CreateIndex {
7    /// only if not exists.
8    pub if_not_exists: bool,
9    /// optional name of the index.
10    pub name: Option<Identifier>,
11    /// the table the index is on.
12    pub table: FQName,
13    /// the index column type.
14    pub column: IndexColumnType,
15}
16
17impl Display for CreateIndex {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        let name = if let Some(name) = &self.name {
20            format!("{} ", name)
21        } else {
22            "".to_string()
23        };
24        let exists = if self.if_not_exists {
25            "IF NOT EXISTS "
26        } else {
27            ""
28        };
29
30        write!(
31            f,
32            "CREATE INDEX {}{}ON {}( {} )",
33            exists, name, self.table, self.column
34        )
35    }
36}
37
38/// The definition of an index column type
39#[derive(PartialEq, Debug, Clone)]
40pub enum IndexColumnType {
41    /// column is a column
42    Column(Identifier),
43    /// use the keys from the column
44    Keys(Identifier),
45    /// use the entries from the column
46    Entries(Identifier),
47    /// use the full column entry.
48    Full(Identifier),
49}
50
51impl Display for IndexColumnType {
52    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53        match self {
54            IndexColumnType::Column(name) => write!(f, "{}", name),
55            IndexColumnType::Keys(name) => write!(f, "KEYS( {} )", name),
56            IndexColumnType::Entries(name) => write!(f, "ENTRIES( {} )", name),
57            IndexColumnType::Full(name) => write!(f, "FULL( {} )", name),
58        }
59    }
60}