2021-03-29 15:43:37 +00:00
|
|
|
use crate::dbs;
|
|
|
|
use crate::dbs::Executor;
|
2021-03-31 12:10:13 +00:00
|
|
|
use crate::dbs::Runtime;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::doc::Document;
|
|
|
|
use crate::err::Error;
|
2021-03-31 11:54:20 +00:00
|
|
|
use crate::sql::idiom::{idiom, Idiom};
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::sql::literal::Literal;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::IResult;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
use std::str;
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub struct Param {
|
2021-03-31 11:54:20 +00:00
|
|
|
pub name: Idiom,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Idiom> for Param {
|
|
|
|
fn from(p: Idiom) -> Param {
|
|
|
|
Param {
|
|
|
|
name: p,
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a str> for Param {
|
|
|
|
fn from(p: &str) -> Param {
|
|
|
|
Param {
|
2021-03-31 11:54:20 +00:00
|
|
|
name: Idiom::from(p),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Param {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "${}", &self.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
impl dbs::Process for Param {
|
|
|
|
fn process(
|
|
|
|
&self,
|
2021-03-31 12:10:13 +00:00
|
|
|
ctx: &Runtime,
|
2021-03-29 15:43:37 +00:00
|
|
|
exe: &Executor,
|
|
|
|
doc: Option<&Document>,
|
|
|
|
) -> Result<Literal, Error> {
|
|
|
|
// 1. Loop through the context variables
|
|
|
|
// 2. Find a variable with the right name
|
|
|
|
// 3. Process the variable value
|
|
|
|
// 4. Return the processed value
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
pub fn param(i: &str) -> IResult<&str, Param> {
|
|
|
|
let (i, _) = tag("$")(i)?;
|
2021-03-31 11:54:20 +00:00
|
|
|
let (i, v) = idiom(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, Param::from(v)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn param_normal() {
|
|
|
|
let sql = "$test";
|
|
|
|
let res = param(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("$test", format!("{}", out));
|
|
|
|
assert_eq!(out, Param::from("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn param_longer() {
|
|
|
|
let sql = "$test_and_deliver";
|
|
|
|
let res = param(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("$test_and_deliver", format!("{}", out));
|
|
|
|
assert_eq!(out, Param::from("test_and_deliver"));
|
|
|
|
}
|
2021-03-31 11:54:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn param_embedded() {
|
|
|
|
let sql = "$test.temporary[0].embedded";
|
|
|
|
let res = param(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("$test.temporary[0].embedded", format!("{}", out));
|
|
|
|
assert_eq!(out, Param::from("test.temporary[0].embedded"));
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|