2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::comment::mightbespace;
|
|
|
|
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};
|
|
|
|
use crate::sql::operator::{assigner, Operator};
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::value::{value, Value};
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
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;
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub enum Data {
|
2022-01-13 17:36:41 +00:00
|
|
|
EmptyExpression,
|
|
|
|
SetExpression(Vec<(Idiom, Operator, Value)>),
|
2022-07-07 09:58:35 +00:00
|
|
|
PatchExpression(Value),
|
2022-02-13 23:39:10 +00:00
|
|
|
MergeExpression(Value),
|
|
|
|
ReplaceExpression(Value),
|
|
|
|
ContentExpression(Value),
|
2022-01-13 17:36:41 +00:00
|
|
|
SingleExpression(Value),
|
|
|
|
ValuesExpression(Vec<Vec<(Idiom, Value)>>),
|
|
|
|
UpdateExpression(Vec<(Idiom, Operator, Value)>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Data {
|
|
|
|
fn default() -> Data {
|
|
|
|
Data::EmptyExpression
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Data {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2022-01-13 17:36:41 +00:00
|
|
|
Data::EmptyExpression => write!(f, ""),
|
2020-06-29 15:36:01 +00:00
|
|
|
Data::SetExpression(v) => write!(
|
|
|
|
f,
|
|
|
|
"SET {}",
|
|
|
|
v.iter()
|
|
|
|
.map(|(l, o, r)| format!("{} {} {}", l, o, r))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")
|
|
|
|
),
|
2022-01-21 13:23:28 +00:00
|
|
|
Data::PatchExpression(v) => write!(f, "PATCH {}", v),
|
2020-06-29 15:36:01 +00:00
|
|
|
Data::MergeExpression(v) => write!(f, "MERGE {}", v),
|
2022-01-13 17:36:41 +00:00
|
|
|
Data::ReplaceExpression(v) => write!(f, "REPLACE {}", v),
|
2020-06-29 15:36:01 +00:00
|
|
|
Data::ContentExpression(v) => write!(f, "CONTENT {}", v),
|
2022-01-13 17:36:41 +00:00
|
|
|
Data::SingleExpression(v) => write!(f, "{}", v),
|
|
|
|
Data::ValuesExpression(v) => write!(
|
|
|
|
f,
|
|
|
|
"({}) VALUES {}",
|
|
|
|
v.first()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|v| format!("{}", v.0))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "),
|
|
|
|
v.iter()
|
|
|
|
.map(|v| format!(
|
|
|
|
"({})",
|
|
|
|
v.iter().map(|v| format!("{}", v.1)).collect::<Vec<_>>().join(", ")
|
|
|
|
))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")
|
|
|
|
),
|
|
|
|
Data::UpdateExpression(v) => write!(
|
|
|
|
f,
|
|
|
|
"ON DUPLICATE KEY UPDATE {}",
|
|
|
|
v.iter()
|
|
|
|
.map(|(l, o, r)| format!("{} {} {}", l, o, r))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")
|
|
|
|
),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn data(i: &str) -> IResult<&str, Data> {
|
2022-01-21 13:23:28 +00:00
|
|
|
alt((set, patch, merge, replace, content))(i)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set(i: &str) -> IResult<&str, Data> {
|
|
|
|
let (i, _) = tag_no_case("SET")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
2021-03-29 15:43:37 +00:00
|
|
|
let (i, v) = separated_list1(commas, |i| {
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, l) = idiom(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, o) = assigner(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, r) = value(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, (l, o, r)))
|
|
|
|
})(i)?;
|
|
|
|
Ok((i, Data::SetExpression(v)))
|
|
|
|
}
|
|
|
|
|
2022-01-21 13:23:28 +00:00
|
|
|
fn patch(i: &str) -> IResult<&str, Data> {
|
|
|
|
let (i, _) = tag_no_case("PATCH")(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = shouldbespace(i)?;
|
2022-07-07 09:58:35 +00:00
|
|
|
let (i, v) = value(i)?;
|
2022-01-21 13:23:28 +00:00
|
|
|
Ok((i, Data::PatchExpression(v)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn merge(i: &str) -> IResult<&str, Data> {
|
|
|
|
let (i, _) = tag_no_case("MERGE")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
2022-02-13 23:39:10 +00:00
|
|
|
let (i, v) = value(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, Data::MergeExpression(v)))
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
fn replace(i: &str) -> IResult<&str, Data> {
|
|
|
|
let (i, _) = tag_no_case("REPLACE")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
2022-02-13 23:39:10 +00:00
|
|
|
let (i, v) = value(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, Data::ReplaceExpression(v)))
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
fn content(i: &str) -> IResult<&str, Data> {
|
|
|
|
let (i, _) = tag_no_case("CONTENT")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
2022-02-13 23:39:10 +00:00
|
|
|
let (i, v) = value(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, Data::ContentExpression(v)))
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
pub fn single(i: &str) -> IResult<&str, Data> {
|
|
|
|
let (i, v) = value(i)?;
|
|
|
|
Ok((i, Data::SingleExpression(v)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn values(i: &str) -> IResult<&str, Data> {
|
|
|
|
let (i, _) = tag_no_case("(")(i)?;
|
|
|
|
let (i, fields) = separated_list1(commas, idiom)(i)?;
|
|
|
|
let (i, _) = tag_no_case(")")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
|
|
|
let (i, _) = tag_no_case("VALUES")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
|
|
|
let (i, values) = separated_list1(commas, |i| {
|
|
|
|
let (i, _) = tag_no_case("(")(i)?;
|
|
|
|
let (i, v) = separated_list1(commas, value)(i)?;
|
|
|
|
let (i, _) = tag_no_case(")")(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
})(i)?;
|
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
Data::ValuesExpression(
|
|
|
|
values
|
|
|
|
.into_iter()
|
|
|
|
.map(|row| fields.iter().cloned().zip(row.into_iter()).collect())
|
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update(i: &str) -> IResult<&str, Data> {
|
|
|
|
let (i, _) = tag_no_case("ON DUPLICATE KEY UPDATE")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
|
|
|
let (i, v) = separated_list1(commas, |i| {
|
|
|
|
let (i, l) = idiom(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, o) = assigner(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, r) = value(i)?;
|
|
|
|
Ok((i, (l, o, r)))
|
|
|
|
})(i)?;
|
|
|
|
Ok((i, Data::UpdateExpression(v)))
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_statement() {
|
|
|
|
let sql = "SET field = true";
|
|
|
|
let res = data(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("SET field = true", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_statement_multiple() {
|
|
|
|
let sql = "SET field = true, other.field = false";
|
|
|
|
let res = data(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("SET field = true, other.field = false", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-01-21 13:23:28 +00:00
|
|
|
fn patch_statement() {
|
|
|
|
let sql = "PATCH [{ field: true }]";
|
|
|
|
let res = patch(sql);
|
2020-06-29 15:36:01 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
2022-01-21 13:23:28 +00:00
|
|
|
assert_eq!("PATCH [{ field: true }]", format!("{}", out));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_statement() {
|
|
|
|
let sql = "MERGE { field: true }";
|
|
|
|
let res = data(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("MERGE { field: true }", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn content_statement() {
|
|
|
|
let sql = "CONTENT { field: true }";
|
|
|
|
let res = data(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("CONTENT { field: true }", format!("{}", out));
|
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
|
2022-01-20 23:09:42 +00:00
|
|
|
#[test]
|
|
|
|
fn replace_statement() {
|
|
|
|
let sql = "REPLACE { field: true }";
|
|
|
|
let res = data(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("REPLACE { field: true }", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
#[test]
|
|
|
|
fn values_statement() {
|
|
|
|
let sql = "(one, two, three) VALUES ($param, true, [1, 2, 3]), ($param, false, [4, 5, 6])";
|
|
|
|
let res = values(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(
|
|
|
|
"(one, two, three) VALUES ($param, true, [1, 2, 3]), ($param, false, [4, 5, 6])",
|
|
|
|
format!("{}", out)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_statement() {
|
|
|
|
let sql = "ON DUPLICATE KEY UPDATE field = true, other.field = false";
|
|
|
|
let res = update(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("ON DUPLICATE KEY UPDATE field = true, other.field = false", format!("{}", out));
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|