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;
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::idiom::{idiom, Idiom};
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::value::{value, Value};
|
2021-03-29 15:43:37 +00:00
|
|
|
use nom::branch::alt;
|
2020-06-29 15:36:01 +00:00
|
|
|
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 serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
|
|
|
pub struct Fields(pub Vec<Field>);
|
|
|
|
|
|
|
|
impl Fields {
|
2022-02-06 01:14:56 +00:00
|
|
|
pub fn all(&self) -> bool {
|
2022-03-04 16:01:32 +00:00
|
|
|
self.0.iter().any(|v| matches!(v, Field::All))
|
2022-02-06 01:14:56 +00:00
|
|
|
}
|
|
|
|
pub fn other(&self) -> impl Iterator<Item = &Field> {
|
2022-03-04 16:01:32 +00:00
|
|
|
self.0.iter().filter(|v| !matches!(v, Field::All))
|
2022-02-06 01:14:56 +00:00
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
pub fn single(&self) -> Option<Idiom> {
|
|
|
|
match self.0.len() {
|
|
|
|
1 => match self.0.first() {
|
|
|
|
Some(Field::All) => None,
|
|
|
|
Some(Field::Alone(e)) => Some(e.to_idiom()),
|
|
|
|
Some(Field::Alias(_, i)) => Some(i.to_owned()),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
impl fmt::Display for Fields {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0.iter().map(|ref v| format!("{}", v)).collect::<Vec<_>>().join(", "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fields(i: &str) -> IResult<&str, Fields> {
|
2021-03-29 15:43:37 +00:00
|
|
|
let (i, v) = separated_list1(commas, field)(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, Fields(v)))
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
2021-03-29 15:43:37 +00:00
|
|
|
pub enum Field {
|
|
|
|
All,
|
2022-01-13 17:36:41 +00:00
|
|
|
Alone(Value),
|
|
|
|
Alias(Value, Idiom),
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Field {
|
|
|
|
fn default() -> Field {
|
|
|
|
Field::All
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Field {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2021-03-29 15:43:37 +00:00
|
|
|
match self {
|
|
|
|
Field::All => write!(f, "*"),
|
2022-01-13 17:36:41 +00:00
|
|
|
Field::Alone(e) => write!(f, "{}", e),
|
|
|
|
Field::Alias(e, a) => write!(f, "{} AS {}", e, a),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn field(i: &str) -> IResult<&str, Field> {
|
2021-03-29 15:43:37 +00:00
|
|
|
alt((all, alias, alone))(i)
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
pub fn all(i: &str) -> IResult<&str, Field> {
|
2021-03-29 15:43:37 +00:00
|
|
|
let (i, _) = tag_no_case("*")(i)?;
|
|
|
|
Ok((i, Field::All))
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
pub fn alone(i: &str) -> IResult<&str, Field> {
|
|
|
|
let (i, f) = value(i)?;
|
2021-03-29 15:43:37 +00:00
|
|
|
Ok((i, Field::Alone(f)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
pub fn alias(i: &str) -> IResult<&str, Field> {
|
|
|
|
let (i, f) = value(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = shouldbespace(i)?;
|
|
|
|
let (i, _) = tag_no_case("AS")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
2021-03-29 15:43:37 +00:00
|
|
|
let (i, a) = idiom(i)?;
|
|
|
|
Ok((i, Field::Alias(f, a)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[test]
|
|
|
|
fn field_all() {
|
|
|
|
let sql = "*";
|
|
|
|
let res = fields(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("*", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[test]
|
|
|
|
fn field_single() {
|
|
|
|
let sql = "field";
|
|
|
|
let res = fields(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("field", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn field_multiple() {
|
|
|
|
let sql = "field, other.field";
|
|
|
|
let res = fields(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("field, other.field", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn field_aliases() {
|
|
|
|
let sql = "field AS one, other.field AS two";
|
|
|
|
let res = fields(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("field AS one, other.field AS two", format!("{}", out));
|
|
|
|
}
|
|
|
|
}
|