2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::common::commas;
|
|
|
|
use crate::sql::common::escape;
|
|
|
|
use crate::sql::common::val_char;
|
|
|
|
use crate::sql::ident::ident_raw;
|
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;
|
|
|
|
use std::str;
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
|
|
|
pub struct Tables(pub Vec<Table>);
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
impl fmt::Display for Tables {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0.iter().map(|ref v| format!("{}", v)).collect::<Vec<_>>().join(", "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tables(i: &str) -> IResult<&str, Tables> {
|
2021-03-29 15:43:37 +00:00
|
|
|
let (i, v) = separated_list1(commas, table)(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, Tables(v)))
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub struct Table {
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Table {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", escape(&self.name, &val_char, "`"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn table(i: &str) -> IResult<&str, Table> {
|
|
|
|
let (i, v) = ident_raw(i)?;
|
2022-01-15 22:13:32 +00:00
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
Table {
|
|
|
|
name: v,
|
|
|
|
},
|
|
|
|
))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn table_normal() {
|
|
|
|
let sql = "test";
|
|
|
|
let res = table(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test", format!("{}", out));
|
2022-01-15 22:13:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Table {
|
|
|
|
name: String::from("test"),
|
|
|
|
}
|
|
|
|
);
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn table_quoted_backtick() {
|
|
|
|
let sql = "`test`";
|
|
|
|
let res = table(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test", format!("{}", out));
|
2022-01-15 22:13:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Table {
|
|
|
|
name: String::from("test"),
|
|
|
|
}
|
|
|
|
);
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn table_quoted_brackets() {
|
|
|
|
let sql = "⟨test⟩";
|
|
|
|
let res = table(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test", format!("{}", out));
|
2022-01-15 22:13:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Table {
|
|
|
|
name: String::from("test"),
|
|
|
|
}
|
|
|
|
);
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|