From 67d6289d0edb5074941efa01539bdcc9a6c57801 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Fri, 14 Jan 2022 08:31:14 +0000 Subject: [PATCH] Attach request variables to query executor context --- src/dbs/dbs.rs | 10 ++++++---- src/dbs/mod.rs | 2 ++ src/dbs/variables.rs | 25 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/dbs/variables.rs diff --git a/src/dbs/dbs.rs b/src/dbs/dbs.rs index 0fe21d0f..2dbfabfa 100644 --- a/src/dbs/dbs.rs +++ b/src/dbs/dbs.rs @@ -1,19 +1,19 @@ use crate::dbs::executor::Executor; use crate::dbs::response::Responses; use crate::dbs::session::Session; +use crate::dbs::variables::Attach; +use crate::dbs::variables::Variables; use crate::err::Error; use crate::sql; use crate::sql::query::Query; -use crate::sql::value::Value; -use std::collections::HashMap; - -pub type Variables = Option>; pub async fn execute(txt: &str, session: Session, vars: Variables) -> Result { // Create a new query executor let mut exe = Executor::new(); // Create a new execution context let ctx = session.context(); + // Attach the defined variables + let ctx = vars.attach(ctx); // Parse the SQL query text let ast = sql::parse(txt)?; // Process all statements @@ -27,6 +27,8 @@ pub async fn process(ast: Query, session: Session, vars: Variables) -> Result>; + +pub(crate) trait Attach { + fn attach(self, ctx: Arc) -> Arc; +} + +impl Attach for Variables { + fn attach(self, ctx: Arc) -> Arc { + match self { + Some(m) => { + let mut ctx = Context::new(&ctx); + for (key, val) in m { + ctx.add_value(key, val); + } + ctx.freeze() + } + None => ctx, + } + } +}