diff --git a/lib/src/doc/lives.rs b/lib/src/doc/lives.rs index 5f104b07..673407f7 100644 --- a/lib/src/doc/lives.rs +++ b/lib/src/doc/lives.rs @@ -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) => { diff --git a/lib/src/doc/pluck.rs b/lib/src/doc/pluck.rs index 8d5319b4..21db2e73 100644 --- a/lib/src/doc/pluck.rs +++ b/lib/src/doc/pluck.rs @@ -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() { diff --git a/lib/tests/update.rs b/lib/tests/update.rs index f6594557..3e5d654c 100644 --- a/lib/tests/update.rs +++ b/lib/tests/update.rs @@ -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 //