2022-10-19 18:16:31 +00:00
|
|
|
use crate::ctx::Context;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
2022-09-24 01:57:27 +00:00
|
|
|
use crate::err::Error;
|
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;
|
2022-10-04 21:51:18 +00:00
|
|
|
use crate::sql::fmt::Fmt;
|
2023-03-31 15:42:29 +00:00
|
|
|
use crate::sql::idiom::{plain as idiom, Idiom};
|
2020-06-29 15:36:01 +00:00
|
|
|
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;
|
2023-08-17 18:03:46 +00:00
|
|
|
use revision::revisioned;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-10-04 21:51:18 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2023-08-18 22:51:56 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
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)>),
|
2023-05-18 08:04:40 +00:00
|
|
|
UnsetExpression(Vec<Idiom>),
|
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 {
|
2022-10-04 21:51:18 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::EmptyExpression
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-24 01:57:27 +00:00
|
|
|
impl Data {
|
2022-10-19 18:16:31 +00:00
|
|
|
/// Fetch the 'id' field if one has been specified
|
|
|
|
pub(crate) async fn rid(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
2023-07-06 14:57:42 +00:00
|
|
|
txn: &Transaction,
|
2023-08-21 22:47:35 +00:00
|
|
|
) -> Result<Option<Value>, Error> {
|
2022-09-24 01:57:27 +00:00
|
|
|
match self {
|
2022-10-19 18:16:31 +00:00
|
|
|
Self::MergeExpression(v) => {
|
|
|
|
// This MERGE expression has an 'id' field
|
2023-08-21 22:47:35 +00:00
|
|
|
Ok(v.compute(ctx, opt, txn, None).await?.rid().some())
|
2022-10-19 18:16:31 +00:00
|
|
|
}
|
|
|
|
Self::ReplaceExpression(v) => {
|
|
|
|
// This REPLACE expression has an 'id' field
|
2023-08-21 22:47:35 +00:00
|
|
|
Ok(v.compute(ctx, opt, txn, None).await?.rid().some())
|
2022-10-19 18:16:31 +00:00
|
|
|
}
|
|
|
|
Self::ContentExpression(v) => {
|
|
|
|
// This CONTENT expression has an 'id' field
|
2023-08-21 22:47:35 +00:00
|
|
|
Ok(v.compute(ctx, opt, txn, None).await?.rid().some())
|
2022-10-19 18:16:31 +00:00
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::SetExpression(v) => match v.iter().find(|f| f.0.is_id()) {
|
2022-10-19 18:16:31 +00:00
|
|
|
Some((_, _, v)) => {
|
|
|
|
// This SET expression has an 'id' field
|
2023-08-21 22:47:35 +00:00
|
|
|
Ok(v.compute(ctx, opt, txn, None).await?.some())
|
2022-10-19 18:16:31 +00:00
|
|
|
}
|
|
|
|
// This SET expression had no 'id' field
|
2023-08-21 22:47:35 +00:00
|
|
|
_ => Ok(None),
|
2022-09-24 01:57:27 +00:00
|
|
|
},
|
2022-10-19 18:16:31 +00:00
|
|
|
// Generate a random id for all other data clauses
|
2023-08-21 22:47:35 +00:00
|
|
|
_ => Ok(None),
|
2022-09-24 01:57:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 21:51:18 +00:00
|
|
|
impl Display for Data {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2020-06-29 15:36:01 +00:00
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::EmptyExpression => Ok(()),
|
|
|
|
Self::SetExpression(v) => write!(
|
2020-06-29 15:36:01 +00:00
|
|
|
f,
|
|
|
|
"SET {}",
|
2023-02-03 11:47:07 +00:00
|
|
|
Fmt::comma_separated(
|
|
|
|
v.iter().map(|args| Fmt::new(args, |(l, o, r), f| write!(f, "{l} {o} {r}",)))
|
|
|
|
)
|
2020-06-29 15:36:01 +00:00
|
|
|
),
|
2023-05-18 08:04:40 +00:00
|
|
|
Self::UnsetExpression(v) => write!(
|
|
|
|
f,
|
|
|
|
"UNSET {}",
|
|
|
|
Fmt::comma_separated(v.iter().map(|args| Fmt::new(args, |l, f| write!(f, "{l}",))))
|
|
|
|
),
|
2023-02-03 11:47:07 +00:00
|
|
|
Self::PatchExpression(v) => write!(f, "PATCH {v}"),
|
|
|
|
Self::MergeExpression(v) => write!(f, "MERGE {v}"),
|
|
|
|
Self::ReplaceExpression(v) => write!(f, "REPLACE {v}"),
|
|
|
|
Self::ContentExpression(v) => write!(f, "CONTENT {v}"),
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::SingleExpression(v) => Display::fmt(v, f),
|
|
|
|
Self::ValuesExpression(v) => write!(
|
2022-01-13 17:36:41 +00:00
|
|
|
f,
|
|
|
|
"({}) VALUES {}",
|
2022-10-04 21:51:18 +00:00
|
|
|
Fmt::comma_separated(v.first().unwrap().iter().map(|(v, _)| v)),
|
|
|
|
Fmt::comma_separated(v.iter().map(|v| Fmt::new(v, |v, f| write!(
|
|
|
|
f,
|
|
|
|
"({})",
|
|
|
|
Fmt::comma_separated(v.iter().map(|(_, v)| v))
|
|
|
|
))))
|
2022-01-13 17:36:41 +00:00
|
|
|
),
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::UpdateExpression(v) => write!(
|
2022-01-13 17:36:41 +00:00
|
|
|
f,
|
|
|
|
"ON DUPLICATE KEY UPDATE {}",
|
2023-02-03 11:47:07 +00:00
|
|
|
Fmt::comma_separated(
|
|
|
|
v.iter().map(|args| Fmt::new(args, |(l, o, r), f| write!(f, "{l} {o} {r}",)))
|
|
|
|
)
|
2022-01-13 17:36:41 +00:00
|
|
|
),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn data(i: &str) -> IResult<&str, Data> {
|
2023-05-18 08:04:40 +00:00
|
|
|
alt((set, unset, 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)))
|
|
|
|
}
|
|
|
|
|
2023-05-18 08:04:40 +00:00
|
|
|
fn unset(i: &str) -> IResult<&str, Data> {
|
|
|
|
let (i, _) = tag_no_case("UNSET")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
|
|
|
let (i, v) = separated_list1(commas, idiom)(i)?;
|
|
|
|
Ok((i, Data::UnsetExpression(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(
|
2023-08-24 19:02:44 +00:00
|
|
|
values.into_iter().map(|row| fields.iter().cloned().zip(row).collect()).collect(),
|
2022-01-13 17:36:41 +00:00
|
|
|
),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2023-05-18 08:04:40 +00:00
|
|
|
#[test]
|
|
|
|
fn unset_statement() {
|
|
|
|
let sql = "UNSET field";
|
|
|
|
let res = data(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("UNSET field", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unset_statement_multiple_fields() {
|
|
|
|
let sql = "UNSET field, other.field";
|
|
|
|
let res = data(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("UNSET field, other.field", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[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
|
|
|
}
|