Ensure CONTENT and REPLACE clauses accept only an object

This commit is contained in:
Tobie Morgan Hitchcock 2022-01-20 23:09:42 +00:00
parent 9390ae9821
commit 3ffd194a05

View file

@ -19,8 +19,8 @@ pub enum Data {
SetExpression(Vec<(Idiom, Operator, Value)>), SetExpression(Vec<(Idiom, Operator, Value)>),
DiffExpression(Array), DiffExpression(Array),
MergeExpression(Object), MergeExpression(Object),
ReplaceExpression(Value), ReplaceExpression(Object),
ContentExpression(Value), ContentExpression(Object),
SingleExpression(Value), SingleExpression(Value),
ValuesExpression(Vec<Vec<(Idiom, Value)>>), ValuesExpression(Vec<Vec<(Idiom, Value)>>),
UpdateExpression(Vec<(Idiom, Operator, Value)>), UpdateExpression(Vec<(Idiom, Operator, Value)>),
@ -113,14 +113,14 @@ fn merge(i: &str) -> IResult<&str, Data> {
fn replace(i: &str) -> IResult<&str, Data> { fn replace(i: &str) -> IResult<&str, Data> {
let (i, _) = tag_no_case("REPLACE")(i)?; let (i, _) = tag_no_case("REPLACE")(i)?;
let (i, _) = shouldbespace(i)?; let (i, _) = shouldbespace(i)?;
let (i, v) = value(i)?; let (i, v) = object(i)?;
Ok((i, Data::ReplaceExpression(v))) Ok((i, Data::ReplaceExpression(v)))
} }
fn content(i: &str) -> IResult<&str, Data> { fn content(i: &str) -> IResult<&str, Data> {
let (i, _) = tag_no_case("CONTENT")(i)?; let (i, _) = tag_no_case("CONTENT")(i)?;
let (i, _) = shouldbespace(i)?; let (i, _) = shouldbespace(i)?;
let (i, v) = value(i)?; let (i, v) = object(i)?;
Ok((i, Data::ContentExpression(v))) Ok((i, Data::ContentExpression(v)))
} }
@ -217,6 +217,15 @@ mod tests {
assert_eq!("CONTENT { field: true }", format!("{}", out)); assert_eq!("CONTENT { field: true }", format!("{}", out));
} }
#[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));
}
#[test] #[test]
fn values_statement() { fn values_statement() {
let sql = "(one, two, three) VALUES ($param, true, [1, 2, 3]), ($param, false, [4, 5, 6])"; let sql = "(one, two, three) VALUES ($param, true, [1, 2, 3]), ($param, false, [4, 5, 6])";