use crate::ctx::Context; use crate::dbs::Auth; use crate::sql::value::Value; use std::sync::Arc; /// Specifies the current session information when processing a query. #[derive(Clone, Debug, Default, Eq, PartialEq)] pub struct Session { /// The current [`Auth`] information pub au: Arc, /// The current connection IP address pub ip: Option, /// The current connection origin pub or: Option, /// The current connection ID pub id: Option, /// THe currently selected namespace pub ns: Option, /// THe currently selected database pub db: Option, /// The currently selected authentication scope pub sc: Option, /// The current scope authentication data pub sd: Option, } impl Session { // Retrieves the selected namespace pub(crate) fn ns(&self) -> Option> { self.ns.to_owned().map(Arc::new) } // Retrieves the selected database pub(crate) fn db(&self) -> Option> { self.db.to_owned().map(Arc::new) } // Convert a session into a runtime pub(crate) fn context(&self, mut ctx: Context) -> Context { // Add session value let key = String::from("session"); let val: Value = self.into(); ctx.add_value(key, val); // Add scope value let key = String::from("scope"); let val: Value = self.sc.to_owned().into(); ctx.add_value(key, val); // Add auth data let key = String::from("auth"); let val: Value = self.sd.to_owned().into(); ctx.add_value(key, val); // Output context ctx } } impl From<&Session> for Value { fn from(val: &Session) -> Value { Value::from(map! { "ip".to_string() => val.ip.to_owned().into(), "or".to_string() => val.or.to_owned().into(), "id".to_string() => val.id.to_owned().into(), "ns".to_string() => val.ns.to_owned().into(), "db".to_string() => val.db.to_owned().into(), "sc".to_string() => val.sc.to_owned().into(), }) } }