Attach request variables to query executor context
This commit is contained in:
parent
3126251a65
commit
67d6289d0e
3 changed files with 33 additions and 4 deletions
|
@ -1,19 +1,19 @@
|
||||||
use crate::dbs::executor::Executor;
|
use crate::dbs::executor::Executor;
|
||||||
use crate::dbs::response::Responses;
|
use crate::dbs::response::Responses;
|
||||||
use crate::dbs::session::Session;
|
use crate::dbs::session::Session;
|
||||||
|
use crate::dbs::variables::Attach;
|
||||||
|
use crate::dbs::variables::Variables;
|
||||||
use crate::err::Error;
|
use crate::err::Error;
|
||||||
use crate::sql;
|
use crate::sql;
|
||||||
use crate::sql::query::Query;
|
use crate::sql::query::Query;
|
||||||
use crate::sql::value::Value;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
pub type Variables = Option<HashMap<String, Value>>;
|
|
||||||
|
|
||||||
pub async fn execute(txt: &str, session: Session, vars: Variables) -> Result<Responses, Error> {
|
pub async fn execute(txt: &str, session: Session, vars: Variables) -> Result<Responses, Error> {
|
||||||
// Create a new query executor
|
// Create a new query executor
|
||||||
let mut exe = Executor::new();
|
let mut exe = Executor::new();
|
||||||
// Create a new execution context
|
// Create a new execution context
|
||||||
let ctx = session.context();
|
let ctx = session.context();
|
||||||
|
// Attach the defined variables
|
||||||
|
let ctx = vars.attach(ctx);
|
||||||
// Parse the SQL query text
|
// Parse the SQL query text
|
||||||
let ast = sql::parse(txt)?;
|
let ast = sql::parse(txt)?;
|
||||||
// Process all statements
|
// Process all statements
|
||||||
|
@ -27,6 +27,8 @@ pub async fn process(ast: Query, session: Session, vars: Variables) -> Result<Re
|
||||||
let mut exe = Executor::new();
|
let mut exe = Executor::new();
|
||||||
// Store session info on context
|
// Store session info on context
|
||||||
let ctx = session.context();
|
let ctx = session.context();
|
||||||
|
// Attach the defined variables
|
||||||
|
let ctx = vars.attach(ctx);
|
||||||
// Process all statements
|
// Process all statements
|
||||||
exe.ns = session.ns;
|
exe.ns = session.ns;
|
||||||
exe.db = session.db;
|
exe.db = session.db;
|
||||||
|
|
|
@ -6,6 +6,7 @@ mod options;
|
||||||
mod response;
|
mod response;
|
||||||
mod runtime;
|
mod runtime;
|
||||||
mod session;
|
mod session;
|
||||||
|
mod variables;
|
||||||
|
|
||||||
pub use self::auth::*;
|
pub use self::auth::*;
|
||||||
pub use self::dbs::*;
|
pub use self::dbs::*;
|
||||||
|
@ -15,6 +16,7 @@ pub use self::options::*;
|
||||||
pub use self::response::*;
|
pub use self::response::*;
|
||||||
pub use self::runtime::*;
|
pub use self::runtime::*;
|
||||||
pub use self::session::*;
|
pub use self::session::*;
|
||||||
|
pub use self::variables::*;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub(crate) mod test;
|
pub(crate) mod test;
|
||||||
|
|
25
src/dbs/variables.rs
Normal file
25
src/dbs/variables.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
use crate::ctx::Context;
|
||||||
|
use crate::sql::value::Value;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub type Variables = Option<HashMap<String, Value>>;
|
||||||
|
|
||||||
|
pub(crate) trait Attach {
|
||||||
|
fn attach(self, ctx: Arc<Context>) -> Arc<Context>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Attach for Variables {
|
||||||
|
fn attach(self, ctx: Arc<Context>) -> Arc<Context> {
|
||||||
|
match self {
|
||||||
|
Some(m) => {
|
||||||
|
let mut ctx = Context::new(&ctx);
|
||||||
|
for (key, val) in m {
|
||||||
|
ctx.add_value(key, val);
|
||||||
|
}
|
||||||
|
ctx.freeze()
|
||||||
|
}
|
||||||
|
None => ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue