-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisplay_util.rs
35 lines (30 loc) · 1000 Bytes
/
display_util.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
use base::CommonParser;
pub struct DisplayUtil;
impl DisplayUtil {
/// add `` to string if string is a MySQL keyword
pub fn escape_if_keyword(s: &str) -> String {
if CommonParser::sql_keyword(s).is_ok() {
format!("`{}`", s)
} else {
s.to_owned()
}
}
}
#[cfg(test)]
mod tests {
use crate::{ParseConfig, Parser};
#[test]
fn escaped_keyword() {
let str0 = "delete from articles where `key`='aaa'";
let str1 = "delete from `where` where user=?";
let expected0 = "DELETE FROM articles WHERE `key` = 'aaa'";
let expected1 = "DELETE FROM `where` WHERE user = ?";
let config = ParseConfig::default();
let res0 = Parser::parse(&config, str0);
let res1 = Parser::parse(&config, str1);
assert!(res0.is_ok());
assert!(res1.is_ok());
assert_eq!(expected0, format!("{}", res0.unwrap()));
assert_eq!(expected1, format!("{}", res1.unwrap()));
}
}