Ensure advanced DEFINE PARAM parameters are computed correctly (#3358)

This commit is contained in:
Tobie Morgan Hitchcock 2024-01-19 17:35:50 +00:00 committed by GitHub
parent 650eb8ed5d
commit f090ff3360
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 5 deletions

View file

@ -100,8 +100,8 @@ impl Param {
} }
} }
} }
// Return the value // Return the computed value
Ok(val.value.to_owned()) val.value.compute(ctx, opt, txn, doc).await
} }
// The param has not been set globally // The param has not been set globally
Err(_) => Ok(Value::None), Err(_) => Ok(Value::None),

View file

@ -24,10 +24,10 @@ impl DefineParamStatement {
/// Process this type returning a computed simple Value /// Process this type returning a computed simple Value
pub(crate) async fn compute( pub(crate) async fn compute(
&self, &self,
_ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,
txn: &Transaction, txn: &Transaction,
_doc: Option<&CursorDoc<'_>>, doc: Option<&CursorDoc<'_>>,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
// Allowed to run? // Allowed to run?
opt.is_allowed(Action::Edit, ResourceKind::Parameter, &Base::Db)?; opt.is_allowed(Action::Edit, ResourceKind::Parameter, &Base::Db)?;
@ -35,11 +35,16 @@ impl DefineParamStatement {
let mut run = txn.lock().await; let mut run = txn.lock().await;
// Clear the cache // Clear the cache
run.clear_cache(); run.clear_cache();
// Compute the param
let val = DefineParamStatement {
value: self.value.compute(ctx, opt, txn, doc).await?,
..self.clone()
};
// Process the statement // Process the statement
let key = crate::key::database::pa::new(opt.ns(), opt.db(), &self.name); let key = crate::key::database::pa::new(opt.ns(), opt.db(), &self.name);
run.add_ns(opt.ns(), opt.strict).await?; run.add_ns(opt.ns(), opt.strict).await?;
run.add_db(opt.ns(), opt.db(), opt.strict).await?; run.add_db(opt.ns(), opt.db(), opt.strict).await?;
run.set(key, self).await?; run.set(key, val).await?;
// Ok all good // Ok all good
Ok(Value::None) Ok(Value::None)
} }

View file

@ -112,6 +112,24 @@ mod cli_integration {
assert_eq!(fs::read_to_string(file).unwrap(), "Save"); assert_eq!(fs::read_to_string(file).unwrap(), "Save");
} }
info!("* Advanced uncomputed variable to be computed before saving");
{
let args = format!(
"sql --conn ws://{addr} {creds} --ns {throwaway} --db {throwaway} --multi",
throwaway = Ulid::new()
);
let output = common::run(&args)
.input(
"DEFINE PARAM $something VALUE <set>[1, 2, 3]; \
$something;
",
)
.output()
.unwrap();
assert!(output.contains("[1, 2, 3]"), "missing success in {output}");
}
info!("* Multi-statement (and multi-line) query including error(s) over WS"); info!("* Multi-statement (and multi-line) query including error(s) over WS");
{ {
let args = format!( let args = format!(