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
use core::fmt;
use std::fmt::Formatter;
use std::str;

use nom::bytes::complete::{tag, tag_no_case};
use nom::character::complete::multispace0;
use nom::character::complete::multispace1;
use nom::combinator::{map, opt};
use nom::sequence::tuple;
use nom::IResult;

use base::error::ParseSQLError;
use base::CommonParser;

/// parse `DROP [UNDO] TABLESPACE tablespace_name
///     [ENGINE [=] engine_name]`
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct DropTablespaceStatement {
    pub undo: bool,
    pub tablespace_name: String,
    pub engine_name: Option<String>,
}

impl DropTablespaceStatement {
    pub fn parse(i: &str) -> IResult<&str, DropTablespaceStatement, ParseSQLError<&str>> {
        let mut parser = tuple((
            tag_no_case("DROP "),
            multispace0,
            opt(tag_no_case("UNDO")),
            multispace0,
            tag_no_case("TABLESPACE "),
            multispace0,
            map(CommonParser::sql_identifier, |tablespace_name| {
                String::from(tablespace_name)
            }),
            multispace0,
            opt(map(
                tuple((
                    tag_no_case("ENGINE"),
                    multispace0,
                    opt(tag("=")),
                    multispace0,
                    CommonParser::sql_identifier,
                    multispace0,
                )),
                |(_, _, _, _, engine, _)| String::from(engine),
            )),
            multispace0,
            CommonParser::statement_terminator,
        ));
        let (remaining_input, (_, _, opt_undo, _, _, _, tablespace_name, _, engine_name, _, _)) =
            parser(i)?;

        Ok((
            remaining_input,
            DropTablespaceStatement {
                undo: opt_undo.is_some(),
                tablespace_name,
                engine_name,
            },
        ))
    }
}

impl fmt::Display for DropTablespaceStatement {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "DROP")?;
        if self.undo {
            write!(f, " UNDO")?;
        }
        write!(f, " TABLESPACE")?;
        write!(f, " {}", self.tablespace_name)?;
        if let Some(ref engine_name) = self.engine_name {
            write!(f, " ENGINE = {}", engine_name)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use dds::drop_tablespace::DropTablespaceStatement;

    #[test]
    fn parse_drop_tablespace() {
        let sqls = [
            "DROP TABLESPACE tablespace_name;",
            "DROP UNDO TABLESPACE tablespace_name;",
            "DROP TABLESPACE tablespace_name ENGINE = demo;",
            "DROP UNDO TABLESPACE tablespace_name ENGINE = demo;",
        ];

        let exp_statements = [
            DropTablespaceStatement {
                undo: false,
                tablespace_name: "tablespace_name".to_string(),
                engine_name: None,
            },
            DropTablespaceStatement {
                undo: true,
                tablespace_name: "tablespace_name".to_string(),
                engine_name: None,
            },
            DropTablespaceStatement {
                undo: false,
                tablespace_name: "tablespace_name".to_string(),
                engine_name: Some("demo".to_string()),
            },
            DropTablespaceStatement {
                undo: true,
                tablespace_name: "tablespace_name".to_string(),
                engine_name: Some("demo".to_string()),
            },
        ];

        for i in 0..sqls.len() {
            let res = DropTablespaceStatement::parse(sqls[i]);
            assert!(res.is_ok());
            assert_eq!(res.unwrap().1, exp_statements[i])
        }
    }
}