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, + } + } +}