1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use crate::common::{FQName, Identifier};
use std::fmt::{Display, Formatter};

/// data to for the create index statement.
#[derive(PartialEq, Debug, Clone)]
pub struct CreateIndex {
    /// only if not exists.
    pub if_not_exists: bool,
    /// optional name of the index.
    pub name: Option<Identifier>,
    /// the table the index is on.
    pub table: FQName,
    /// the index column type.
    pub column: IndexColumnType,
}

impl Display for CreateIndex {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let name = if let Some(name) = &self.name {
            format!("{} ", name)
        } else {
            "".to_string()
        };
        let exists = if self.if_not_exists {
            "IF NOT EXISTS "
        } else {
            ""
        };

        write!(
            f,
            "CREATE INDEX {}{}ON {}( {} )",
            exists, name, self.table, self.column
        )
    }
}

/// The definition of an index column type
#[derive(PartialEq, Debug, Clone)]
pub enum IndexColumnType {
    /// column is a column
    Column(Identifier),
    /// use the keys from the column
    Keys(Identifier),
    /// use the entries from the column
    Entries(Identifier),
    /// use the full column entry.
    Full(Identifier),
}

impl Display for IndexColumnType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            IndexColumnType::Column(name) => write!(f, "{}", name),
            IndexColumnType::Keys(name) => write!(f, "KEYS( {} )", name),
            IndexColumnType::Entries(name) => write!(f, "ENTRIES( {} )", name),
            IndexColumnType::Full(name) => write!(f, "FULL( {} )", name),
        }
    }
}