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
use nom::multispace; use nom::types::CompleteByteSlice; use std::{fmt, str}; use common::{literal, opt_multispace, sql_identifier, statement_terminator, Literal}; #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct SetStatement { pub variable: String, pub value: Literal, } impl fmt::Display for SetStatement { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "SET ")?; write!(f, "{} = {}", self.variable, self.value.to_string())?; Ok(()) } } named!(pub set<CompleteByteSlice, SetStatement>, do_parse!( tag_no_case!("set") >> multispace >> var: sql_identifier >> opt_multispace >> tag_no_case!("=") >> opt_multispace >> val: literal >> statement_terminator >> (SetStatement { variable: String::from(str::from_utf8(*var).unwrap()), value: val, }) ) ); #[cfg(test)] mod tests { use super::*; #[test] fn simple_set() { let qstring = "SET SQL_AUTO_IS_NULL = 0;"; let res = set(CompleteByteSlice(qstring.as_bytes())); assert_eq!( res.unwrap().1, SetStatement { variable: "SQL_AUTO_IS_NULL".to_owned(), value: 0.into(), } ); } #[test] fn format_set() { let qstring = "set autocommit=1"; let expected = "SET autocommit = 1"; let res = set(CompleteByteSlice(qstring.as_bytes())); assert_eq!(format!("{}", res.unwrap().1), expected); } }