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)]
|
2022-01-13 17:36:41 +00:00
|
|
|
pub struct Fetchs(pub Vec<Fetch>);
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
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::*;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::test::Parse;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
#[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 {
|
2022-01-13 17:36:41 +00:00
|
|
|
fetch: Idiom::parse("field")
|
2020-06-29 15:36:01 +00:00
|
|
|
}])
|
|
|
|
);
|
|
|
|
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 {
|
2022-01-13 17:36:41 +00:00
|
|
|
fetch: Idiom::parse("field")
|
2020-06-29 15:36:01 +00:00
|
|
|
},
|
|
|
|
Fetch {
|
2022-01-13 17:36:41 +00:00
|
|
|
fetch: Idiom::parse("other.field")
|
2020-06-29 15:36:01 +00:00
|
|
|
},
|
|
|
|
])
|
|
|
|
);
|
|
|
|
assert_eq!("FETCH field, other.field", format!("{}", out));
|
|
|
|
}
|
|
|
|
}
|