-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrop_tablespace.rs
107 lines (97 loc) · 3.21 KB
/
drop_tablespace.rs
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
use core::fmt;
use std::fmt::Formatter;
use std::str;
use nom::bytes::complete::tag_no_case;
use nom::character::complete::multispace0;
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, String::from),
multispace0,
opt(|x| CommonParser::parse_string_value_with_key(x, "ENGINE".to_string())),
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])
}
}
}