2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::comment::shouldbespace;
|
|
|
|
use crate::sql::common::commas;
|
|
|
|
use crate::sql::idiom::{idiom, Idiom};
|
|
|
|
use nom::bytes::complete::tag_no_case;
|
|
|
|
use nom::combinator::opt;
|
2021-03-29 15:43:37 +00:00
|
|
|
use nom::multi::separated_list1;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::sequence::tuple;
|
|
|
|
use nom::IResult;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub struct Splits(Vec<Split>);
|
|
|
|
|
|
|
|
impl fmt::Display for Splits {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"SPLIT ON {}",
|
2021-03-29 15:43:37 +00:00
|
|
|
self.0.iter().map(|ref v| format!("{}", v)).collect::<Vec<_>>().join(", ")
|
2020-06-29 15:36:01 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub struct Split {
|
|
|
|
pub split: Idiom,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Split {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.split)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn split(i: &str) -> IResult<&str, Splits> {
|
|
|
|
let (i, _) = tag_no_case("SPLIT")(i)?;
|
|
|
|
let (i, _) = opt(tuple((shouldbespace, tag_no_case("ON"))))(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
2021-03-29 15:43:37 +00:00
|
|
|
let (i, v) = separated_list1(commas, split_raw)(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, Splits(v)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn split_raw(i: &str) -> IResult<&str, Split> {
|
|
|
|
let (i, v) = idiom(i)?;
|
2021-03-29 15:43:37 +00:00
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
Split {
|
|
|
|
split: v,
|
|
|
|
},
|
|
|
|
))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_statement() {
|
|
|
|
let sql = "SPLIT field";
|
|
|
|
let res = split(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Splits(vec![Split {
|
|
|
|
split: Idiom::from("field")
|
|
|
|
}])
|
|
|
|
);
|
|
|
|
assert_eq!("SPLIT ON field", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_statement_on() {
|
|
|
|
let sql = "SPLIT ON field";
|
|
|
|
let res = split(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Splits(vec![Split {
|
|
|
|
split: Idiom::from("field")
|
|
|
|
}])
|
|
|
|
);
|
|
|
|
assert_eq!("SPLIT ON field", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_statement_multiple() {
|
|
|
|
let sql = "SPLIT field, other.field";
|
|
|
|
let res = split(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Splits(vec![
|
|
|
|
Split {
|
|
|
|
split: Idiom::from("field")
|
|
|
|
},
|
|
|
|
Split {
|
|
|
|
split: Idiom::from("other.field")
|
|
|
|
},
|
|
|
|
])
|
|
|
|
);
|
|
|
|
assert_eq!("SPLIT ON field, other.field", format!("{}", out));
|
|
|
|
}
|
|
|
|
}
|