From bcaea757c2253007ee996ea18bef267d2fb67b11 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 17 Aug 2022 15:09:08 +0100 Subject: [PATCH] Add SQL session functions --- lib/src/fnc/mod.rs | 8 ++++++++ lib/src/fnc/session.rs | 33 +++++++++++++++++++++++++++++++++ lib/src/sql/function.rs | 12 ++++++++++++ lib/src/sql/paths.rs | 10 ++++++++++ 4 files changed, 63 insertions(+) create mode 100644 lib/src/fnc/session.rs diff --git a/lib/src/fnc/mod.rs b/lib/src/fnc/mod.rs index a79c56d5..54ffdb76 100644 --- a/lib/src/fnc/mod.rs +++ b/lib/src/fnc/mod.rs @@ -17,6 +17,7 @@ pub mod operate; pub mod parse; pub mod rand; pub mod script; +pub mod session; pub mod string; pub mod time; pub mod r#type; @@ -126,6 +127,13 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec) -> Result args::check(ctx, name, args, Args::None, rand::uuid), "rand" => args::check(ctx, name, args, Args::None, rand::rand), // + "session::db" => args::check(ctx, name, args, Args::None, session::db), + "session::id" => args::check(ctx, name, args, Args::None, session::id), + "session::ip" => args::check(ctx, name, args, Args::None, session::ip), + "session::ns" => args::check(ctx, name, args, Args::None, session::ns), + "session::origin" => args::check(ctx, name, args, Args::None, session::origin), + "session::sc" => args::check(ctx, name, args, Args::None, session::sc), + // "string::concat" => args::check(ctx, name, args, Args::Any, string::concat), "string::endsWith" => args::check(ctx, name, args, Args::Two, string::ends_with), "string::join" => args::check(ctx, name, args, Args::Any, string::join), diff --git a/lib/src/fnc/session.rs b/lib/src/fnc/session.rs new file mode 100644 index 00000000..8343b295 --- /dev/null +++ b/lib/src/fnc/session.rs @@ -0,0 +1,33 @@ +use crate::ctx::Context; +use crate::err::Error; +use crate::sql::paths::DB; +use crate::sql::paths::ID; +use crate::sql::paths::IP; +use crate::sql::paths::NS; +use crate::sql::paths::OR; +use crate::sql::paths::SC; +use crate::sql::value::Value; + +pub fn db(ctx: &Context, _: Vec) -> Result { + ctx.value("session").unwrap_or(&Value::None).pick(DB.as_ref()).ok() +} + +pub fn id(ctx: &Context, _: Vec) -> Result { + ctx.value("session").unwrap_or(&Value::None).pick(ID.as_ref()).ok() +} + +pub fn ip(ctx: &Context, _: Vec) -> Result { + ctx.value("session").unwrap_or(&Value::None).pick(IP.as_ref()).ok() +} + +pub fn ns(ctx: &Context, _: Vec) -> Result { + ctx.value("session").unwrap_or(&Value::None).pick(NS.as_ref()).ok() +} + +pub fn origin(ctx: &Context, _: Vec) -> Result { + ctx.value("session").unwrap_or(&Value::None).pick(OR.as_ref()).ok() +} + +pub fn sc(ctx: &Context, _: Vec) -> Result { + ctx.value("session").unwrap_or(&Value::None).pick(SC.as_ref()).ok() +} diff --git a/lib/src/sql/function.rs b/lib/src/sql/function.rs index 182e4388..2f6ada45 100644 --- a/lib/src/sql/function.rs +++ b/lib/src/sql/function.rs @@ -245,6 +245,7 @@ fn function_names(i: &str) -> IResult<&str, &str> { function_math, function_parse, function_rand, + function_session, function_string, function_time, function_type, @@ -384,6 +385,17 @@ fn function_rand(i: &str) -> IResult<&str, &str> { ))(i) } +fn function_session(i: &str) -> IResult<&str, &str> { + alt(( + tag("session::db"), + tag("session::id"), + tag("session::ip"), + tag("session::ns"), + tag("session::origin"), + tag("session::sc"), + ))(i) +} + fn function_string(i: &str) -> IResult<&str, &str> { alt(( tag("string::concat"), diff --git a/lib/src/sql/paths.rs b/lib/src/sql/paths.rs index b64536fd..0d833425 100644 --- a/lib/src/sql/paths.rs +++ b/lib/src/sql/paths.rs @@ -3,6 +3,16 @@ use once_cell::sync::Lazy; pub static ID: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("id")]); +pub static IP: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("ip")]); + +pub static NS: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("ns")]); + +pub static DB: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("db")]); + +pub static SC: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("sc")]); + +pub static OR: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("or")]); + pub static IN: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("in")]); pub static OUT: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("out")]);