2021-03-29 15:43:37 +00:00
|
|
|
use crate::dbs;
|
|
|
|
use crate::dbs::Executor;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::dbs::Options;
|
2021-03-31 12:10:13 +00:00
|
|
|
use crate::dbs::Runtime;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::err::Error;
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::comment::mightbespace;
|
|
|
|
use crate::sql::comment::shouldbespace;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::ident::ident_raw;
|
|
|
|
use crate::sql::value::{value, Value};
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::bytes::complete::tag_no_case;
|
2022-01-13 17:36:41 +00:00
|
|
|
use nom::sequence::preceded;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::IResult;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub struct SetStatement {
|
2022-01-13 17:36:41 +00:00
|
|
|
pub name: String,
|
|
|
|
pub what: Value,
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for SetStatement {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2022-01-13 17:36:41 +00:00
|
|
|
write!(f, "LET ${} = {}", self.name, self.what)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
impl dbs::Process for SetStatement {
|
|
|
|
fn process(
|
|
|
|
&self,
|
2021-03-31 12:10:13 +00:00
|
|
|
ctx: &Runtime,
|
2022-01-13 17:36:41 +00:00
|
|
|
opt: &Options,
|
|
|
|
exe: &mut Executor,
|
|
|
|
doc: Option<&Value>,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
self.what.process(ctx, opt, exe, doc)
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
pub fn set(i: &str) -> IResult<&str, SetStatement> {
|
|
|
|
let (i, _) = tag_no_case("LET")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, n) = preceded(tag("$"), ident_raw)(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, _) = tag("=")(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, w) = value(i)?;
|
2021-03-29 15:43:37 +00:00
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
SetStatement {
|
|
|
|
name: n,
|
|
|
|
what: w,
|
|
|
|
},
|
|
|
|
))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_statement() {
|
|
|
|
let sql = "LET $name = NULL";
|
|
|
|
let res = set(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("LET $name = NULL", format!("{}", out));
|
|
|
|
}
|
|
|
|
}
|