2022-10-01 14:55:48 +00:00
|
|
|
use crate::ctx::Context;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
|
|
|
use crate::doc::CursorDoc;
|
2022-10-01 14:55:48 +00:00
|
|
|
use crate::err::Error;
|
2022-09-30 21:22:00 +00:00
|
|
|
use crate::sql::error::IResult;
|
|
|
|
use crate::sql::value::Value;
|
2023-05-20 07:28:52 +00:00
|
|
|
use crate::sql::Datetime;
|
|
|
|
use chrono::TimeZone;
|
|
|
|
use chrono::Utc;
|
2022-10-01 14:55:48 +00:00
|
|
|
use derive::Store;
|
2022-09-30 21:22:00 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag_no_case;
|
2023-08-29 10:50:00 +00:00
|
|
|
use nom::combinator::value;
|
2023-04-17 12:09:56 +00:00
|
|
|
use nom::sequence::preceded;
|
2023-08-17 18:03:46 +00:00
|
|
|
use revision::revisioned;
|
2022-10-01 14:55:48 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
2022-09-30 21:22:00 +00:00
|
|
|
|
2023-03-30 10:41:44 +00:00
|
|
|
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Constant";
|
|
|
|
|
2023-04-29 20:50:25 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
|
|
|
|
#[serde(rename = "$surrealdb::private::sql::Constant")]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2022-10-01 14:55:48 +00:00
|
|
|
pub enum Constant {
|
|
|
|
MathE,
|
|
|
|
MathFrac1Pi,
|
|
|
|
MathFrac1Sqrt2,
|
|
|
|
MathFrac2Pi,
|
|
|
|
MathFrac2SqrtPi,
|
|
|
|
MathFracPi2,
|
|
|
|
MathFracPi3,
|
|
|
|
MathFracPi4,
|
|
|
|
MathFracPi6,
|
|
|
|
MathFracPi8,
|
2023-06-09 16:53:22 +00:00
|
|
|
MathInf,
|
2022-10-01 14:55:48 +00:00
|
|
|
MathLn10,
|
|
|
|
MathLn2,
|
|
|
|
MathLog102,
|
|
|
|
MathLog10E,
|
|
|
|
MathLog210,
|
|
|
|
MathLog2E,
|
|
|
|
MathPi,
|
|
|
|
MathSqrt2,
|
|
|
|
MathTau,
|
2023-05-20 07:28:52 +00:00
|
|
|
TimeEpoch,
|
2023-04-29 20:50:25 +00:00
|
|
|
// Add new variants here
|
2022-10-01 14:55:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 07:28:52 +00:00
|
|
|
/// A type of constant that may be converted to a value or JSON.
|
|
|
|
pub(crate) enum ConstantValue {
|
|
|
|
Float(f64),
|
|
|
|
Datetime(Datetime),
|
|
|
|
}
|
|
|
|
|
2022-10-01 14:55:48 +00:00
|
|
|
impl Constant {
|
2023-05-20 07:28:52 +00:00
|
|
|
pub(crate) fn value(&self) -> ConstantValue {
|
|
|
|
use std::f64::consts as f64c;
|
2023-04-29 08:06:02 +00:00
|
|
|
match self {
|
2023-05-20 07:28:52 +00:00
|
|
|
Self::MathE => ConstantValue::Float(f64c::E),
|
|
|
|
Self::MathFrac1Pi => ConstantValue::Float(f64c::FRAC_1_PI),
|
|
|
|
Self::MathFrac1Sqrt2 => ConstantValue::Float(f64c::FRAC_1_SQRT_2),
|
|
|
|
Self::MathFrac2Pi => ConstantValue::Float(f64c::FRAC_2_PI),
|
|
|
|
Self::MathFrac2SqrtPi => ConstantValue::Float(f64c::FRAC_2_SQRT_PI),
|
|
|
|
Self::MathFracPi2 => ConstantValue::Float(f64c::FRAC_PI_2),
|
|
|
|
Self::MathFracPi3 => ConstantValue::Float(f64c::FRAC_PI_3),
|
|
|
|
Self::MathFracPi4 => ConstantValue::Float(f64c::FRAC_PI_4),
|
|
|
|
Self::MathFracPi6 => ConstantValue::Float(f64c::FRAC_PI_6),
|
|
|
|
Self::MathFracPi8 => ConstantValue::Float(f64c::FRAC_PI_8),
|
2023-06-09 16:53:22 +00:00
|
|
|
Self::MathInf => ConstantValue::Float(f64::INFINITY),
|
2023-05-20 07:28:52 +00:00
|
|
|
Self::MathLn10 => ConstantValue::Float(f64c::LN_10),
|
|
|
|
Self::MathLn2 => ConstantValue::Float(f64c::LN_2),
|
|
|
|
Self::MathLog102 => ConstantValue::Float(f64c::LOG10_2),
|
|
|
|
Self::MathLog10E => ConstantValue::Float(f64c::LOG10_E),
|
|
|
|
Self::MathLog210 => ConstantValue::Float(f64c::LOG2_10),
|
|
|
|
Self::MathLog2E => ConstantValue::Float(f64c::LOG2_E),
|
|
|
|
Self::MathPi => ConstantValue::Float(f64c::PI),
|
|
|
|
Self::MathSqrt2 => ConstantValue::Float(f64c::SQRT_2),
|
|
|
|
Self::MathTau => ConstantValue::Float(f64c::TAU),
|
|
|
|
Self::TimeEpoch => ConstantValue::Datetime(Datetime(Utc.timestamp_nanos(0))),
|
2023-04-29 08:06:02 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-09 22:17:29 +00:00
|
|
|
/// Process this type returning a computed simple Value
|
2023-07-06 14:57:42 +00:00
|
|
|
pub(crate) async fn compute(
|
|
|
|
&self,
|
|
|
|
_ctx: &Context<'_>,
|
|
|
|
_opt: &Options,
|
|
|
|
_txn: &Transaction,
|
|
|
|
_doc: Option<&CursorDoc<'_>>,
|
|
|
|
) -> Result<Value, Error> {
|
2023-05-20 07:28:52 +00:00
|
|
|
Ok(match self.value() {
|
|
|
|
ConstantValue::Datetime(d) => d.into(),
|
|
|
|
ConstantValue::Float(f) => f.into(),
|
|
|
|
})
|
2022-10-01 14:55:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Constant {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.write_str(match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MathE => "math::E",
|
|
|
|
Self::MathFrac1Pi => "math::FRAC_1_PI",
|
|
|
|
Self::MathFrac1Sqrt2 => "math::FRAC_1_SQRT_2",
|
|
|
|
Self::MathFrac2Pi => "math::FRAC_2_PI",
|
|
|
|
Self::MathFrac2SqrtPi => "math::FRAC_2_SQRT_PI",
|
|
|
|
Self::MathFracPi2 => "math::FRAC_PI_2",
|
|
|
|
Self::MathFracPi3 => "math::FRAC_PI_3",
|
|
|
|
Self::MathFracPi4 => "math::FRAC_PI_4",
|
|
|
|
Self::MathFracPi6 => "math::FRAC_PI_6",
|
|
|
|
Self::MathFracPi8 => "math::FRAC_PI_8",
|
2023-06-09 16:53:22 +00:00
|
|
|
Self::MathInf => "math::INF",
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MathLn10 => "math::LN_10",
|
|
|
|
Self::MathLn2 => "math::LN_2",
|
|
|
|
Self::MathLog102 => "math::LOG10_2",
|
|
|
|
Self::MathLog10E => "math::LOG10_E",
|
|
|
|
Self::MathLog210 => "math::LOG2_10",
|
|
|
|
Self::MathLog2E => "math::LOG2_E",
|
|
|
|
Self::MathPi => "math::PI",
|
|
|
|
Self::MathSqrt2 => "math::SQRT_2",
|
|
|
|
Self::MathTau => "math::TAU",
|
2023-05-20 07:28:52 +00:00
|
|
|
Self::TimeEpoch => "time::EPOCH",
|
2022-10-01 14:55:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn constant(i: &str) -> IResult<&str, Constant> {
|
2023-05-20 07:28:52 +00:00
|
|
|
alt((constant_math, constant_time))(i)
|
2022-09-30 21:22:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 14:55:48 +00:00
|
|
|
fn constant_math(i: &str) -> IResult<&str, Constant> {
|
2023-04-17 12:09:56 +00:00
|
|
|
preceded(
|
|
|
|
tag_no_case("math::"),
|
|
|
|
alt((
|
2023-08-29 10:50:00 +00:00
|
|
|
value(Constant::MathE, tag_no_case("E")),
|
|
|
|
value(Constant::MathFrac1Pi, tag_no_case("FRAC_1_PI")),
|
|
|
|
value(Constant::MathFrac1Sqrt2, tag_no_case("FRAC_1_SQRT_2")),
|
|
|
|
value(Constant::MathFrac2Pi, tag_no_case("FRAC_2_PI")),
|
|
|
|
value(Constant::MathFrac2SqrtPi, tag_no_case("FRAC_2_SQRT_PI")),
|
|
|
|
value(Constant::MathFracPi2, tag_no_case("FRAC_PI_2")),
|
|
|
|
value(Constant::MathFracPi3, tag_no_case("FRAC_PI_3")),
|
|
|
|
value(Constant::MathFracPi4, tag_no_case("FRAC_PI_4")),
|
|
|
|
value(Constant::MathFracPi6, tag_no_case("FRAC_PI_6")),
|
|
|
|
value(Constant::MathFracPi8, tag_no_case("FRAC_PI_8")),
|
|
|
|
value(Constant::MathInf, tag_no_case("INF")),
|
|
|
|
value(Constant::MathLn10, tag_no_case("LN_10")),
|
|
|
|
value(Constant::MathLn2, tag_no_case("LN_2")),
|
|
|
|
value(Constant::MathLog102, tag_no_case("LOG10_2")),
|
|
|
|
value(Constant::MathLog10E, tag_no_case("LOG10_E")),
|
|
|
|
value(Constant::MathLog210, tag_no_case("LOG2_10")),
|
|
|
|
value(Constant::MathLog2E, tag_no_case("LOG2_E")),
|
|
|
|
value(Constant::MathPi, tag_no_case("PI")),
|
|
|
|
value(Constant::MathSqrt2, tag_no_case("SQRT_2")),
|
|
|
|
value(Constant::MathTau, tag_no_case("TAU")),
|
2023-04-17 12:09:56 +00:00
|
|
|
)),
|
|
|
|
)(i)
|
2022-09-30 21:22:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 07:28:52 +00:00
|
|
|
fn constant_time(i: &str) -> IResult<&str, Constant> {
|
2023-08-29 10:50:00 +00:00
|
|
|
preceded(tag_no_case("time::"), alt((value(Constant::TimeEpoch, tag_no_case("EPOCH")),)))(i)
|
2023-05-20 07:28:52 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 21:22:00 +00:00
|
|
|
#[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;
|
2022-10-01 14:55:48 +00:00
|
|
|
assert_eq!(out, Constant::MathPi);
|
2022-09-30 21:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn constant_uppercase() {
|
|
|
|
let sql = "MATH::PI";
|
|
|
|
let res = constant(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
2022-10-01 14:55:48 +00:00
|
|
|
assert_eq!(out, Constant::MathPi);
|
2022-09-30 21:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn constant_mixedcase() {
|
|
|
|
let sql = "math::PI";
|
|
|
|
let res = constant(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
2022-10-01 14:55:48 +00:00
|
|
|
assert_eq!(out, Constant::MathPi);
|
2022-09-30 21:22:00 +00:00
|
|
|
}
|
|
|
|
}
|