surrealpatch/src/sql/fetch.rs

91 lines
1.8 KiB
Rust
Raw Normal View History

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;
2021-03-29 15:43:37 +00:00
use nom::multi::separated_list1;
2020-06-29 15:36:01 +00:00
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 Fetchs(Vec<Fetch>);
impl fmt::Display for Fetchs {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"FETCH {}",
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 Fetch {
pub fetch: Idiom,
}
impl fmt::Display for Fetch {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.fetch)
}
}
pub fn fetch(i: &str) -> IResult<&str, Fetchs> {
let (i, _) = tag_no_case("FETCH")(i)?;
let (i, _) = shouldbespace(i)?;
2021-03-29 15:43:37 +00:00
let (i, v) = separated_list1(commas, fetch_raw)(i)?;
2020-06-29 15:36:01 +00:00
Ok((i, Fetchs(v)))
}
fn fetch_raw(i: &str) -> IResult<&str, Fetch> {
let (i, v) = idiom(i)?;
2021-03-29 15:43:37 +00:00
Ok((
i,
Fetch {
fetch: v,
},
))
2020-06-29 15:36:01 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fetch_statement() {
let sql = "FETCH field";
let res = fetch(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!(
out,
Fetchs(vec![Fetch {
fetch: Idiom::from("field")
}])
);
assert_eq!("FETCH field", format!("{}", out));
}
#[test]
fn fetch_statement_multiple() {
let sql = "FETCH field, other.field";
let res = fetch(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!(
out,
Fetchs(vec![
Fetch {
fetch: Idiom::from("field")
},
Fetch {
fetch: Idiom::from("other.field")
},
])
);
assert_eq!("FETCH field, other.field", format!("{}", out));
}
}