Add $before
and $after
params to RETURN
clauses (#2665)
This commit is contained in:
parent
c3acab4614
commit
592a1b7c7e
3 changed files with 90 additions and 5 deletions
|
@ -172,10 +172,8 @@ impl<'a> Document<'a> {
|
|||
if opt.check_perms(stm.into()) {
|
||||
// Get the table
|
||||
let tb = self.tb(opt, txn).await?;
|
||||
// Get the permission clause
|
||||
let perms = &tb.permissions.select;
|
||||
// Process the table permissions
|
||||
match perms {
|
||||
match &tb.permissions.select {
|
||||
Permission::None => return Err(Error::Ignore),
|
||||
Permission::Full => return Ok(()),
|
||||
Permission::Specific(e) => {
|
||||
|
|
|
@ -26,13 +26,25 @@ impl<'a> Document<'a> {
|
|||
Output::None => Err(Error::Ignore),
|
||||
Output::Null => Ok(Value::Null),
|
||||
Output::Diff => {
|
||||
// Output a DIFF of any changes applied to the document
|
||||
Ok(self.initial.doc.diff(self.current.doc.as_ref(), Idiom::default()).into())
|
||||
}
|
||||
Output::After => self.current.doc.compute(ctx, opt, txn, Some(&self.current)).await,
|
||||
Output::After => {
|
||||
// Output the full document after all changes were applied
|
||||
self.current.doc.compute(ctx, opt, txn, Some(&self.current)).await
|
||||
}
|
||||
Output::Before => {
|
||||
// Output the full document before any changes were applied
|
||||
self.initial.doc.compute(ctx, opt, txn, Some(&self.initial)).await
|
||||
}
|
||||
Output::Fields(v) => v.compute(ctx, opt, txn, Some(&self.current), false).await,
|
||||
Output::Fields(v) => {
|
||||
// Configure the context
|
||||
let mut ctx = Context::new(ctx);
|
||||
ctx.add_value("after", self.current.doc.as_ref());
|
||||
ctx.add_value("before", self.initial.doc.as_ref());
|
||||
// Output the specified fields
|
||||
v.compute(&ctx, opt, txn, Some(&self.current), false).await
|
||||
}
|
||||
},
|
||||
None => match stm {
|
||||
Statement::Live(s) => match s.expr.len() {
|
||||
|
|
|
@ -206,6 +206,81 @@ async fn update_complex_with_input() -> Result<(), Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn update_with_return_clause() -> Result<(), Error> {
|
||||
let sql = "
|
||||
CREATE person:test SET age = 18, name = 'John';
|
||||
UPDATE person:test SET age = 25 RETURN VALUE $before;
|
||||
UPDATE person:test SET age = 30 RETURN VALUE { old_age: $before.age, new_age: $after.age };
|
||||
UPDATE person:test SET age = 35 RETURN age, name;
|
||||
DELETE person:test RETURN VALUE $before;
|
||||
";
|
||||
let dbs = new_ds().await?;
|
||||
let ses = Session::owner().with_ns("test").with_db("test");
|
||||
let res = &mut dbs.execute(sql, &ses, None).await?;
|
||||
assert_eq!(res.len(), 5);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::parse(
|
||||
"[
|
||||
{
|
||||
age: 18,
|
||||
id: person:test,
|
||||
name: 'John'
|
||||
}
|
||||
]",
|
||||
);
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::parse(
|
||||
"[
|
||||
{
|
||||
age: 18,
|
||||
id: person:test,
|
||||
name: 'John'
|
||||
}
|
||||
]",
|
||||
);
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::parse(
|
||||
"[
|
||||
{
|
||||
new_age: 30,
|
||||
old_age: 25
|
||||
}
|
||||
]",
|
||||
);
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::parse(
|
||||
"[
|
||||
{
|
||||
age: 35,
|
||||
name: 'John'
|
||||
}
|
||||
]",
|
||||
);
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
let tmp = res.remove(0).result?;
|
||||
let val = Value::parse(
|
||||
"[
|
||||
{
|
||||
age: 35,
|
||||
id: person:test,
|
||||
name: 'John'
|
||||
}
|
||||
]",
|
||||
);
|
||||
assert_eq!(tmp, val);
|
||||
//
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//
|
||||
// Permissions
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue