Add SQL session functions

This commit is contained in:
Tobie Morgan Hitchcock 2022-08-17 15:09:08 +01:00
parent b8c793f7a4
commit bcaea757c2
4 changed files with 63 additions and 0 deletions

View file

@ -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<Value>) -> Result<Va
"rand::uuid" => 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),

33
lib/src/fnc/session.rs Normal file
View file

@ -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<Value>) -> Result<Value, Error> {
ctx.value("session").unwrap_or(&Value::None).pick(DB.as_ref()).ok()
}
pub fn id(ctx: &Context, _: Vec<Value>) -> Result<Value, Error> {
ctx.value("session").unwrap_or(&Value::None).pick(ID.as_ref()).ok()
}
pub fn ip(ctx: &Context, _: Vec<Value>) -> Result<Value, Error> {
ctx.value("session").unwrap_or(&Value::None).pick(IP.as_ref()).ok()
}
pub fn ns(ctx: &Context, _: Vec<Value>) -> Result<Value, Error> {
ctx.value("session").unwrap_or(&Value::None).pick(NS.as_ref()).ok()
}
pub fn origin(ctx: &Context, _: Vec<Value>) -> Result<Value, Error> {
ctx.value("session").unwrap_or(&Value::None).pick(OR.as_ref()).ok()
}
pub fn sc(ctx: &Context, _: Vec<Value>) -> Result<Value, Error> {
ctx.value("session").unwrap_or(&Value::None).pick(SC.as_ref()).ok()
}

View file

@ -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"),

View file

@ -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")]);