2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::comment::shouldbespace;
|
|
|
|
use crate::sql::common::commas;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2022-10-04 21:51:18 +00:00
|
|
|
use crate::sql::fmt::Fmt;
|
2022-03-23 11:51:36 +00:00
|
|
|
use crate::sql::idiom::{basic, Idiom};
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::bytes::complete::tag_no_case;
|
2023-08-29 10:50:00 +00:00
|
|
|
use nom::combinator::{cut, opt};
|
2021-03-29 15:43:37 +00:00
|
|
|
use nom::multi::separated_list1;
|
2023-08-29 10:50:00 +00:00
|
|
|
use nom::sequence::terminated;
|
2023-08-17 18:03:46 +00:00
|
|
|
use revision::revisioned;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-10-04 21:51:18 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2022-03-25 18:43:22 +00:00
|
|
|
use std::ops::Deref;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-10-27 12:23:24 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2022-01-13 17:36:41 +00:00
|
|
|
pub struct Splits(pub Vec<Split>);
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-03-25 18:43:22 +00:00
|
|
|
impl Deref for Splits {
|
|
|
|
type Target = Vec<Split>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoIterator for Splits {
|
|
|
|
type Item = Split;
|
|
|
|
type IntoIter = std::vec::IntoIter<Self::Item>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
impl fmt::Display for Splits {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2022-10-04 21:51:18 +00:00
|
|
|
write!(f, "SPLIT ON {}", Fmt::comma_separated(&self.0))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-27 12:23:24 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2022-05-04 21:02:26 +00:00
|
|
|
pub struct Split(pub Idiom);
|
|
|
|
|
|
|
|
impl Deref for Split {
|
|
|
|
type Target = Idiom;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 21:51:18 +00:00
|
|
|
impl Display for Split {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
Display::fmt(&self.0, f)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn split(i: &str) -> IResult<&str, Splits> {
|
|
|
|
let (i, _) = tag_no_case("SPLIT")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
2023-09-08 11:28:36 +00:00
|
|
|
let (i, _) = opt(terminated(tag_no_case("ON"), shouldbespace))(i)?;
|
|
|
|
let (i, v) = cut(separated_list1(commas, split_raw))(i)?;
|
|
|
|
Ok((i, Splits(v)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn split_raw(i: &str) -> IResult<&str, Split> {
|
2022-03-23 11:51:36 +00:00
|
|
|
let (i, v) = basic(i)?;
|
2022-05-04 21:02:26 +00:00
|
|
|
Ok((i, Split(v)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::test::Parse;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_statement() {
|
|
|
|
let sql = "SPLIT field";
|
|
|
|
let res = split(sql);
|
|
|
|
let out = res.unwrap().1;
|
2022-05-04 21:02:26 +00:00
|
|
|
assert_eq!(out, Splits(vec![Split(Idiom::parse("field"))]),);
|
2020-06-29 15:36:01 +00:00
|
|
|
assert_eq!("SPLIT ON field", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_statement_on() {
|
|
|
|
let sql = "SPLIT ON field";
|
|
|
|
let res = split(sql);
|
|
|
|
let out = res.unwrap().1;
|
2022-05-04 21:02:26 +00:00
|
|
|
assert_eq!(out, Splits(vec![Split(Idiom::parse("field"))]),);
|
2020-06-29 15:36:01 +00:00
|
|
|
assert_eq!("SPLIT ON field", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_statement_multiple() {
|
|
|
|
let sql = "SPLIT field, other.field";
|
|
|
|
let res = split(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
2022-05-04 21:02:26 +00:00
|
|
|
Splits(vec![Split(Idiom::parse("field")), Split(Idiom::parse("other.field")),])
|
2020-06-29 15:36:01 +00:00
|
|
|
);
|
|
|
|
assert_eq!("SPLIT ON field, other.field", format!("{}", out));
|
|
|
|
}
|
|
|
|
}
|