From f090ff3360415cf4000c34bd02a340ed2bb6a3d3 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Fri, 19 Jan 2024 17:35:50 +0000 Subject: [PATCH] Ensure advanced `DEFINE PARAM` parameters are computed correctly (#3358) --- lib/src/sql/param.rs | 4 ++-- lib/src/sql/statements/define/param.rs | 11 ++++++++--- tests/cli_integration.rs | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/src/sql/param.rs b/lib/src/sql/param.rs index 94661b9f..5cd7d3eb 100644 --- a/lib/src/sql/param.rs +++ b/lib/src/sql/param.rs @@ -100,8 +100,8 @@ impl Param { } } } - // Return the value - Ok(val.value.to_owned()) + // Return the computed value + val.value.compute(ctx, opt, txn, doc).await } // The param has not been set globally Err(_) => Ok(Value::None), diff --git a/lib/src/sql/statements/define/param.rs b/lib/src/sql/statements/define/param.rs index 676632ca..f2be9707 100644 --- a/lib/src/sql/statements/define/param.rs +++ b/lib/src/sql/statements/define/param.rs @@ -24,10 +24,10 @@ impl DefineParamStatement { /// Process this type returning a computed simple Value pub(crate) async fn compute( &self, - _ctx: &Context<'_>, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, - _doc: Option<&CursorDoc<'_>>, + doc: Option<&CursorDoc<'_>>, ) -> Result { // Allowed to run? opt.is_allowed(Action::Edit, ResourceKind::Parameter, &Base::Db)?; @@ -35,11 +35,16 @@ impl DefineParamStatement { let mut run = txn.lock().await; // Clear the cache run.clear_cache(); + // Compute the param + let val = DefineParamStatement { + value: self.value.compute(ctx, opt, txn, doc).await?, + ..self.clone() + }; // Process the statement let key = crate::key::database::pa::new(opt.ns(), opt.db(), &self.name); run.add_ns(opt.ns(), 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(Value::None) } diff --git a/tests/cli_integration.rs b/tests/cli_integration.rs index 1ed106b0..68726c8f 100644 --- a/tests/cli_integration.rs +++ b/tests/cli_integration.rs @@ -112,6 +112,24 @@ mod cli_integration { 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 [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"); { let args = format!(