Add support for constants, and add math constants (#1263)
This commit is contained in:
parent
7cd8bab75c
commit
176c9f692c
3 changed files with 71 additions and 0 deletions
lib/src/sql
66
lib/src/sql/constant.rs
Normal file
66
lib/src/sql/constant.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
use crate::sql::error::IResult;
|
||||
use crate::sql::value::Value;
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag_no_case;
|
||||
use nom::combinator::map;
|
||||
|
||||
pub fn constant(i: &str) -> IResult<&str, Value> {
|
||||
alt((constant_math,))(i)
|
||||
}
|
||||
|
||||
fn constant_math(i: &str) -> IResult<&str, Value> {
|
||||
alt((
|
||||
map(tag_no_case("math::E"), |_| std::f64::consts::E.into()),
|
||||
map(tag_no_case("math::FRAC_1_PI"), |_| std::f64::consts::FRAC_1_PI.into()),
|
||||
map(tag_no_case("math::FRAC_1_SQRT_2"), |_| std::f64::consts::FRAC_1_SQRT_2.into()),
|
||||
map(tag_no_case("math::FRAC_2_PI"), |_| std::f64::consts::FRAC_2_PI.into()),
|
||||
map(tag_no_case("math::FRAC_2_SQRT_PI"), |_| std::f64::consts::FRAC_2_SQRT_PI.into()),
|
||||
map(tag_no_case("math::FRAC_PI_2"), |_| std::f64::consts::FRAC_PI_2.into()),
|
||||
map(tag_no_case("math::FRAC_PI_3"), |_| std::f64::consts::FRAC_PI_3.into()),
|
||||
map(tag_no_case("math::FRAC_PI_4"), |_| std::f64::consts::FRAC_PI_4.into()),
|
||||
map(tag_no_case("math::FRAC_PI_6"), |_| std::f64::consts::FRAC_PI_6.into()),
|
||||
map(tag_no_case("math::FRAC_PI_8"), |_| std::f64::consts::FRAC_PI_8.into()),
|
||||
map(tag_no_case("math::LN_10"), |_| std::f64::consts::LN_10.into()),
|
||||
map(tag_no_case("math::LN_2"), |_| std::f64::consts::LN_2.into()),
|
||||
map(tag_no_case("math::LOG10_2"), |_| std::f64::consts::LOG10_2.into()),
|
||||
map(tag_no_case("math::LOG10_E"), |_| std::f64::consts::LOG10_E.into()),
|
||||
map(tag_no_case("math::LOG2_10"), |_| std::f64::consts::LOG2_10.into()),
|
||||
map(tag_no_case("math::LOG2_E"), |_| std::f64::consts::LOG2_E.into()),
|
||||
map(tag_no_case("math::PI"), |_| std::f64::consts::PI.into()),
|
||||
map(tag_no_case("math::SQRT_2"), |_| std::f64::consts::SQRT_2.into()),
|
||||
map(tag_no_case("math::TAU"), |_| std::f64::consts::TAU.into()),
|
||||
))(i)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn constant_lowercase() {
|
||||
let sql = "math::pi";
|
||||
let res = constant(sql);
|
||||
assert!(res.is_ok());
|
||||
let out = res.unwrap().1;
|
||||
assert_eq!(out, Value::from(std::f64::consts::PI));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constant_uppercase() {
|
||||
let sql = "MATH::PI";
|
||||
let res = constant(sql);
|
||||
assert!(res.is_ok());
|
||||
let out = res.unwrap().1;
|
||||
assert_eq!(out, Value::from(std::f64::consts::PI));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constant_mixedcase() {
|
||||
let sql = "math::PI";
|
||||
let res = constant(sql);
|
||||
assert!(res.is_ok());
|
||||
let out = res.unwrap().1;
|
||||
assert_eq!(out, Value::from(std::f64::consts::PI));
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ pub(crate) mod base;
|
|||
pub(crate) mod comment;
|
||||
pub(crate) mod common;
|
||||
pub(crate) mod cond;
|
||||
pub(crate) mod constant;
|
||||
pub(crate) mod data;
|
||||
pub(crate) mod datetime;
|
||||
pub(crate) mod dir;
|
||||
|
|
|
@ -7,6 +7,7 @@ use crate::dbs::Transaction;
|
|||
use crate::err::Error;
|
||||
use crate::sql::array::{array, Array};
|
||||
use crate::sql::common::commas;
|
||||
use crate::sql::constant::constant;
|
||||
use crate::sql::datetime::{datetime, Datetime};
|
||||
use crate::sql::duration::{duration, Duration};
|
||||
use crate::sql::edges::{edges, Edges};
|
||||
|
@ -1261,6 +1262,7 @@ pub fn single(i: &str) -> IResult<&str, Value> {
|
|||
alt((
|
||||
map(subquery, Value::from),
|
||||
map(function, Value::from),
|
||||
map(constant, Value::from),
|
||||
map(datetime, Value::from),
|
||||
map(duration, Value::from),
|
||||
map(geometry, Value::from),
|
||||
|
@ -1290,6 +1292,7 @@ pub fn select(i: &str) -> IResult<&str, Value> {
|
|||
map(expression, Value::from),
|
||||
map(subquery, Value::from),
|
||||
map(function, Value::from),
|
||||
map(constant, Value::from),
|
||||
map(datetime, Value::from),
|
||||
map(duration, Value::from),
|
||||
map(geometry, Value::from),
|
||||
|
@ -1313,6 +1316,7 @@ pub fn what(i: &str) -> IResult<&str, Value> {
|
|||
alt((
|
||||
map(subquery, Value::from),
|
||||
map(function, Value::from),
|
||||
map(constant, Value::from),
|
||||
map(param, Value::from),
|
||||
map(model, Value::from),
|
||||
map(edges, Value::from),
|
||||
|
|
Loading…
Reference in a new issue