1use nom::bytes::complete::tag_no_case;
2use nom::character::complete::{multispace0, multispace1};
3use std::{fmt, str};
4
5use common::{literal, sql_identifier, statement_terminator, Literal};
6use nom::sequence::tuple;
7use nom::IResult;
8
9#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
10pub struct SetStatement {
11 pub variable: String,
12 pub value: Literal,
13}
14
15impl fmt::Display for SetStatement {
16 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17 write!(f, "SET ")?;
18 write!(f, "{} = {}", self.variable, self.value.to_string())?;
19 Ok(())
20 }
21}
22
23pub fn set(i: &[u8]) -> IResult<&[u8], SetStatement> {
24 let (remaining_input, (_, _, var, _, _, _, value, _)) = tuple((
25 tag_no_case("set"),
26 multispace1,
27 sql_identifier,
28 multispace0,
29 tag_no_case("="),
30 multispace0,
31 literal,
32 statement_terminator,
33 ))(i)?;
34 let variable = String::from(str::from_utf8(var).unwrap());
35 Ok((remaining_input, SetStatement { variable, value }))
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn simple_set() {
44 let qstring = "SET SQL_AUTO_IS_NULL = 0;";
45 let res = set(qstring.as_bytes());
46 assert_eq!(
47 res.unwrap().1,
48 SetStatement {
49 variable: "SQL_AUTO_IS_NULL".to_owned(),
50 value: 0.into(),
51 }
52 );
53 }
54
55 #[test]
56 fn user_defined_vars() {
57 let qstring = "SET @var = 123;";
58 let res = set(qstring.as_bytes());
59 assert_eq!(
60 res.unwrap().1,
61 SetStatement {
62 variable: "@var".to_owned(),
63 value: 123.into(),
64 }
65 );
66 }
67
68 #[test]
69 fn format_set() {
70 let qstring = "set autocommit=1";
71 let expected = "SET autocommit = 1";
72 let res = set(qstring.as_bytes());
73 assert_eq!(format!("{}", res.unwrap().1), expected);
74 }
75}