Enable access to input data using $input variable

Closes #1543
This commit is contained in:
Tobie Morgan Hitchcock 2023-03-31 19:46:13 +01:00
parent cfb27e0929
commit 78329abf97
2 changed files with 112 additions and 0 deletions

View file

@ -4,6 +4,7 @@ use crate::dbs::Statement;
use crate::dbs::Transaction; use crate::dbs::Transaction;
use crate::doc::Document; use crate::doc::Document;
use crate::err::Error; use crate::err::Error;
use crate::sql::data::Data;
use crate::sql::permission::Permission; use crate::sql::permission::Permission;
use crate::sql::value::Value; use crate::sql::value::Value;
@ -27,10 +28,22 @@ impl<'a> Document<'a> {
for (k, mut val) in self.current.walk(&fd.name).into_iter() { for (k, mut val) in self.current.walk(&fd.name).into_iter() {
// Get the initial value // Get the initial value
let old = self.initial.pick(&k); let old = self.initial.pick(&k);
// Get the input value
let inp = match stm.data() {
Some(Data::MergeExpression(v)) => v.pick(&k),
Some(Data::ContentExpression(v)) => v.pick(&k),
Some(Data::ReplaceExpression(v)) => v.pick(&k),
Some(Data::SetExpression(v)) => match v.iter().find(|f| f.0.eq(&k)) {
Some((_, _, v)) => v.to_owned(),
_ => Value::None,
},
_ => Value::None,
};
// Check for a VALUE clause // Check for a VALUE clause
if let Some(expr) = &fd.value { if let Some(expr) = &fd.value {
// Configure the context // Configure the context
let mut ctx = Context::new(ctx); let mut ctx = Context::new(ctx);
ctx.add_value("input".into(), &inp);
ctx.add_value("value".into(), &val); ctx.add_value("value".into(), &val);
ctx.add_value("after".into(), &val); ctx.add_value("after".into(), &val);
ctx.add_value("before".into(), &old); ctx.add_value("before".into(), &old);
@ -49,6 +62,7 @@ impl<'a> Document<'a> {
if let Some(expr) = &fd.assert { if let Some(expr) = &fd.assert {
// Configure the context // Configure the context
let mut ctx = Context::new(ctx); let mut ctx = Context::new(ctx);
ctx.add_value("input".into(), &inp);
ctx.add_value("value".into(), &val); ctx.add_value("value".into(), &val);
ctx.add_value("after".into(), &val); ctx.add_value("after".into(), &val);
ctx.add_value("before".into(), &old); ctx.add_value("before".into(), &old);
@ -81,6 +95,7 @@ impl<'a> Document<'a> {
let opt = &opt.perms(false); let opt = &opt.perms(false);
// Configure the context // Configure the context
let mut ctx = Context::new(ctx); let mut ctx = Context::new(ctx);
ctx.add_value("input".into(), &inp);
ctx.add_value("value".into(), &val); ctx.add_value("value".into(), &val);
ctx.add_value("after".into(), &val); ctx.add_value("after".into(), &val);
ctx.add_value("before".into(), &old); ctx.add_value("before".into(), &old);

97
lib/tests/update.rs Normal file
View file

@ -0,0 +1,97 @@
mod parse;
use parse::Parse;
use surrealdb::dbs::Session;
use surrealdb::err::Error;
use surrealdb::kvs::Datastore;
use surrealdb::sql::Value;
#[tokio::test]
async fn update_with_input() -> Result<(), Error> {
let sql = "
DEFINE FIELD name ON TABLE person
ASSERT
IF $input THEN
$input = /^[A-Z]{1}[a-z]+$/
ELSE
true
END
VALUE
IF $input THEN
'Name: ' + $input
ELSE
$value
END
;
UPDATE person:test CONTENT { name: 'Tobie' };
UPDATE person:test REPLACE { name: 'jaime' };
UPDATE person:test MERGE { name: 'Jaime' };
UPDATE person:test SET name = 'tobie';
UPDATE person:test SET name = 'Tobie';
SELECT * FROM person:test;
";
let dbs = Datastore::new("memory").await?;
let ses = Session::for_kv().with_ns("test").with_db("test");
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
assert_eq!(res.len(), 7);
//
let tmp = res.remove(0).result;
assert!(tmp.is_ok());
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: person:test,
name: 'Name: Tobie',
}
]",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(e) if e.to_string() == r#"Found 'Name: jaime' for field `name`, with record `person:test`, but field must conform to: IF $input THEN $input = /^[A-Z]{1}[a-z]+$/ ELSE true END"#
));
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: person:test,
name: 'Name: Jaime',
}
]",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(e) if e.to_string() == r#"Found 'Name: tobie' for field `name`, with record `person:test`, but field must conform to: IF $input THEN $input = /^[A-Z]{1}[a-z]+$/ ELSE true END"#
));
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: person:test,
name: 'Name: Tobie',
}
]",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: person:test,
name: 'Name: Tobie',
}
]",
);
assert_eq!(tmp, val);
//
Ok(())
}