2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::comment::shouldbespace;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::field::{fields, Fields};
|
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag_no_case;
|
|
|
|
use nom::combinator::map;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub enum Output {
|
|
|
|
None,
|
2022-01-27 11:18:41 +00:00
|
|
|
Null,
|
2020-06-29 15:36:01 +00:00
|
|
|
Diff,
|
|
|
|
After,
|
|
|
|
Before,
|
|
|
|
Fields(Fields),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Output {
|
|
|
|
fn default() -> Output {
|
2022-01-27 11:18:41 +00:00
|
|
|
Output::None
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Output {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "RETURN ")?;
|
|
|
|
match self {
|
|
|
|
Output::None => write!(f, "NONE"),
|
2022-01-27 11:18:41 +00:00
|
|
|
Output::Null => write!(f, "NULL"),
|
2020-06-29 15:36:01 +00:00
|
|
|
Output::Diff => write!(f, "DIFF"),
|
|
|
|
Output::After => write!(f, "AFTER"),
|
|
|
|
Output::Before => write!(f, "BEFORE"),
|
|
|
|
Output::Fields(v) => write!(f, "{}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn output(i: &str) -> IResult<&str, Output> {
|
|
|
|
let (i, _) = tag_no_case("RETURN")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
|
|
|
let (i, v) = alt((
|
|
|
|
map(tag_no_case("NONE"), |_| Output::None),
|
2022-01-27 11:18:41 +00:00
|
|
|
map(tag_no_case("NULL"), |_| Output::Null),
|
2020-06-29 15:36:01 +00:00
|
|
|
map(tag_no_case("DIFF"), |_| Output::Diff),
|
|
|
|
map(tag_no_case("AFTER"), |_| Output::After),
|
|
|
|
map(tag_no_case("BEFORE"), |_| Output::Before),
|
2022-03-04 16:01:32 +00:00
|
|
|
map(fields, Output::Fields),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn output_statement() {
|
|
|
|
let sql = "RETURN field, other.field";
|
|
|
|
let res = output(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("RETURN field, other.field", format!("{}", out));
|
|
|
|
}
|
|
|
|
}
|