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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use super::*;
use std::fmt;
#[derive(PartialEq, Debug)]
pub enum Keyword {
    SELECT,
    FROM,
    WHERE,
    ORDER,
    BY,
    CREATE,
    TABLE,
    TRIGGER,
    DATABASE,
    IF,
    ELSE,
    END,
    DESC,
    ASC,
    AS,
    TOP,
    BOTTOM,
    DISTINCT,
    LIMIT,
    OFFSET,
    GROUP,
    HAVING,
    JOIN,
    LIKE,
    IN,
    AND,
    UNKNOWN,
}

impl Keyword {
    pub fn value_for_command(&self) -> i32 {
        match self {
            Keyword::SELECT => 1,
            _ => 10,
        }
    }

    pub fn needs_data(&self) -> bool {
        match self {
            Keyword::SELECT => true,
            Keyword::FROM => true,
            _ => false,
        }
    }

    pub fn bigger_than(&self, comparator: &Keyword) -> bool {
        if self.compare_command(comparator) != 1 {
            return false;
        }
        true
    }
    /// compare the assigned values to a command in order to check what command has a higher
    /// importance level.
    ///
    /// If the command value of the first element is bigger than the value of the
    /// parameter_keyword, the function will return 1;
    /// If they have the same value, it will return 0;
    /// If the value of the given element is smaller than the parameter_keyword, the function will
    /// return -1;
    pub fn compare_command(&self, comparator: &Keyword) -> i32 {
        if self.value_for_command() > comparator.value_for_command() {
            1
        } else if self.value_for_command() == comparator.value_for_command() {
            0
        } else {
            -1
        }
    }
}

/// convert a given string into a better processable enum-value that can be later used in order
/// to construct the finit expression with the data.
pub fn unwrap_string(mut convertable: String) -> Keyword {
    convertable = convertable.to_lowercase();
    match convertable.as_ref() {
        "select" => Keyword::SELECT,
        "where" => Keyword::WHERE,
        "from" => Keyword::FROM,
        "create" => Keyword::CREATE,
        "table" => Keyword::TABLE,
        "database" => Keyword::DATABASE,
        "order" => Keyword::ORDER,
        "if" => Keyword::IF,
        "else" => Keyword::ELSE,
        "distinct" => Keyword::DISTINCT,
        "top" => Keyword::TOP,
        "end" => Keyword::END,
        "limit" => Keyword::LIMIT,
        "having" => Keyword::HAVING,
        "like" => Keyword::LIKE,
        _ => Keyword::UNKNOWN,
    }
}

impl fmt::Display for Keyword {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

pub fn identifiy_data_after_keyword(_expression: expression::Expression) {}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn return_enum_value_for_keyword_string() {
        let keyword_strings = vec!["SELECT", "FROM", "WHERE"];
        let result_return = vec![Keyword::SELECT, Keyword::FROM, Keyword::WHERE];
        let mut returned_result_vec: Vec<Keyword> = Vec::new();
        for i in 0..keyword_strings.len() {
            returned_result_vec.push(grammar::unwrap_string(keyword_strings[i].to_string()));
        }
        assert_eq!(result_return, returned_result_vec);
    }

    #[test]
    fn get_wheiht_for_enum_value() {
        let enum_value = Keyword::SELECT;
        let expected_result = 1;
        assert_eq!(expected_result, enum_value.value_for_command());
    }

    #[test]
    fn keyword_needs_values() {
        assert_eq!(Keyword::SELECT.needs_data(), true);
        assert_eq!(Keyword::FROM.needs_data(), true);
        assert_eq!(Keyword::ORDER.needs_data(), false);
    }

}