surrealpatch/src/sql/statements/option.rs

99 lines
2.2 KiB
Rust
Raw Normal View History

2021-03-29 15:43:37 +00:00
use crate::dbs::Executor;
use crate::dbs::Level;
use crate::dbs::Options;
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-16 20:31:50 +00:00
use crate::sql::error::IResult;
2020-06-29 15:36:01 +00:00
use crate::sql::ident::{ident, Ident};
use crate::sql::value::Value;
2020-06-29 15:36:01 +00:00
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::tag_no_case;
use nom::combinator::{map, opt};
use nom::sequence::tuple;
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 OptionStatement {
pub name: Ident,
pub what: bool,
}
2022-01-14 08:12:56 +00:00
impl OptionStatement {
pub async fn compute(
2021-03-29 15:43:37 +00:00
&self,
_ctx: &Runtime,
2022-01-14 08:12:56 +00:00
opt: &Options<'_>,
exe: &mut Executor,
_doc: Option<&Value>,
) -> Result<Value, Error> {
// Allowed to run?
exe.check(opt, Level::Db)?;
// Return nothing
Ok(Value::None)
2021-03-29 15:43:37 +00:00
}
}
2022-01-14 08:12:56 +00:00
impl fmt::Display for OptionStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.what {
write!(f, "OPTION {}", self.name)
} else {
write!(f, "OPTION {} = FALSE", self.name)
}
}
}
2020-06-29 15:36:01 +00:00
pub fn option(i: &str) -> IResult<&str, OptionStatement> {
let (i, _) = tag_no_case("OPTION")(i)?;
let (i, _) = shouldbespace(i)?;
let (i, n) = ident(i)?;
let (i, v) = opt(alt((
2021-03-29 15:43:37 +00:00
map(tuple((mightbespace, tag("="), mightbespace, tag_no_case("TRUE"))), |_| true),
map(tuple((mightbespace, tag("="), mightbespace, tag_no_case("FALSE"))), |_| false),
2020-06-29 15:36:01 +00:00
)))(i)?;
Ok((
i,
OptionStatement {
name: n,
what: v.unwrap_or(true),
},
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn option_statement() {
let sql = "OPTION IMPORT";
let res = option(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("OPTION IMPORT", format!("{}", out));
}
#[test]
fn option_statement_true() {
let sql = "OPTION IMPORT = TRUE";
let res = option(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("OPTION IMPORT", format!("{}", out));
}
#[test]
fn option_statement_false() {
let sql = "OPTION IMPORT = FALSE";
let res = option(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("OPTION IMPORT = FALSE", format!("{}", out));
}
}