2022-09-04 09:52:01 +00:00
|
|
|
use crate::ctx::Context;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
|
|
|
use crate::doc::CursorDoc;
|
2022-09-04 09:52:01 +00:00
|
|
|
use crate::err::Error;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2022-10-16 23:04:07 +00:00
|
|
|
use crate::sql::escape::escape_rid;
|
2023-08-20 03:27:20 +00:00
|
|
|
use crate::sql::id::{id, Gen, Id};
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::ident::ident_raw;
|
2023-04-25 10:13:04 +00:00
|
|
|
use crate::sql::strand::Strand;
|
2022-09-04 09:52:01 +00:00
|
|
|
use crate::sql::value::Value;
|
2022-04-09 09:09:01 +00:00
|
|
|
use derive::Store;
|
2022-07-03 19:28:30 +00:00
|
|
|
use nom::branch::alt;
|
2023-01-17 09:53:30 +00:00
|
|
|
use nom::bytes::complete::tag;
|
2022-03-16 23:52:25 +00:00
|
|
|
use nom::character::complete::char;
|
2023-08-29 10:50:00 +00:00
|
|
|
use nom::combinator::value;
|
2022-10-16 20:35:50 +00:00
|
|
|
use nom::sequence::delimited;
|
2023-08-17 18:03:46 +00:00
|
|
|
use revision::revisioned;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
2023-04-25 10:13:04 +00:00
|
|
|
use std::str::FromStr;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2023-03-30 10:41:44 +00:00
|
|
|
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Thing";
|
|
|
|
|
2023-04-29 15:58:22 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Store, Hash)]
|
|
|
|
#[serde(rename = "$surrealdb::private::sql::Thing")]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub struct Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
pub tb: String,
|
2022-03-18 07:24:36 +00:00
|
|
|
pub id: Id,
|
|
|
|
}
|
|
|
|
|
2023-04-29 16:44:09 +00:00
|
|
|
impl From<(&str, Id)> for Thing {
|
|
|
|
fn from((tb, id): (&str, Id)) -> Self {
|
|
|
|
Self {
|
|
|
|
tb: tb.to_owned(),
|
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 07:24:36 +00:00
|
|
|
impl From<(String, Id)> for Thing {
|
2022-10-04 21:51:18 +00:00
|
|
|
fn from((tb, id): (String, Id)) -> Self {
|
|
|
|
Self {
|
|
|
|
tb,
|
|
|
|
id,
|
2022-03-18 07:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 18:11:44 +00:00
|
|
|
impl From<(String, String)> for Thing {
|
2022-10-04 21:51:18 +00:00
|
|
|
fn from((tb, id): (String, String)) -> Self {
|
|
|
|
Self::from((tb, Id::from(id)))
|
2022-03-18 07:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 08:18:08 +00:00
|
|
|
impl From<(&str, &str)> for Thing {
|
2022-10-04 21:51:18 +00:00
|
|
|
fn from((tb, id): (&str, &str)) -> Self {
|
|
|
|
Self::from((tb.to_owned(), Id::from(id)))
|
2022-06-09 08:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 10:13:04 +00:00
|
|
|
impl FromStr for Thing {
|
|
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Self::try_from(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<String> for Thing {
|
|
|
|
type Error = ();
|
|
|
|
fn try_from(v: String) -> Result<Self, Self::Error> {
|
|
|
|
Self::try_from(v.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Strand> for Thing {
|
|
|
|
type Error = ();
|
|
|
|
fn try_from(v: Strand) -> Result<Self, Self::Error> {
|
|
|
|
Self::try_from(v.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&str> for Thing {
|
|
|
|
type Error = ();
|
|
|
|
fn try_from(v: &str) -> Result<Self, Self::Error> {
|
|
|
|
match thing_raw(v) {
|
|
|
|
Ok((_, v)) => Ok(v),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 08:18:08 +00:00
|
|
|
impl Thing {
|
2022-10-19 09:55:19 +00:00
|
|
|
/// Convert the Thing to a raw String
|
2022-06-09 08:18:08 +00:00
|
|
|
pub fn to_raw(&self) -> String {
|
|
|
|
self.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
impl fmt::Display for Thing {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2022-10-16 23:04:07 +00:00
|
|
|
write!(f, "{}:{}", escape_rid(&self.tb), self.id)
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 09:52:01 +00:00
|
|
|
impl Thing {
|
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> {
|
2022-09-04 09:52:01 +00:00
|
|
|
Ok(Value::Thing(Thing {
|
|
|
|
tb: self.tb.clone(),
|
2023-07-06 14:57:42 +00:00
|
|
|
id: self.id.compute(ctx, opt, txn, doc).await?,
|
2022-09-04 09:52:01 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
pub fn thing(i: &str) -> IResult<&str, Thing> {
|
2022-10-16 20:35:50 +00:00
|
|
|
alt((thing_raw, thing_single, thing_double))(i)
|
2022-07-03 19:28:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 20:35:50 +00:00
|
|
|
fn thing_single(i: &str) -> IResult<&str, Thing> {
|
|
|
|
delimited(char('\''), thing_raw, char('\''))(i)
|
2022-07-03 19:28:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 20:35:50 +00:00
|
|
|
fn thing_double(i: &str) -> IResult<&str, Thing> {
|
|
|
|
delimited(char('\"'), thing_raw, char('\"'))(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thing_raw(i: &str) -> IResult<&str, Thing> {
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, t) = ident_raw(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(':')(i)?;
|
2023-01-17 09:53:30 +00:00
|
|
|
let (i, v) = alt((
|
2023-08-29 10:50:00 +00:00
|
|
|
value(Id::Generate(Gen::Rand), tag("rand()")),
|
|
|
|
value(Id::Generate(Gen::Ulid), tag("ulid()")),
|
|
|
|
value(Id::Generate(Gen::Uuid), tag("uuid()")),
|
2023-01-17 09:53:30 +00:00
|
|
|
id,
|
|
|
|
))(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
tb: t,
|
|
|
|
id: v,
|
2020-06-29 15:36:01 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
2022-08-26 23:33:52 +00:00
|
|
|
use crate::sql::array::Array;
|
|
|
|
use crate::sql::object::Object;
|
|
|
|
use crate::sql::value::Value;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thing_normal() {
|
|
|
|
let sql = "test:id";
|
|
|
|
let res = thing(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test:id", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
tb: String::from("test"),
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::from("id"),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:40:19 +00:00
|
|
|
#[test]
|
|
|
|
fn thing_integer() {
|
|
|
|
let sql = "test:001";
|
|
|
|
let res = thing(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test:1", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
|
|
|
tb: String::from("test"),
|
|
|
|
id: Id::from(1),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[test]
|
|
|
|
fn thing_quoted_backtick() {
|
|
|
|
let sql = "`test`:`id`";
|
|
|
|
let res = thing(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test:id", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
tb: String::from("test"),
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::from("id"),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thing_quoted_brackets() {
|
|
|
|
let sql = "⟨test⟩:⟨id⟩";
|
|
|
|
let res = thing(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test:id", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
tb: String::from("test"),
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::from("id"),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2022-08-26 23:33:52 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thing_object() {
|
|
|
|
let sql = "test:{ location: 'GBR', year: 2022 }";
|
|
|
|
let res = thing(sql);
|
|
|
|
let out = res.unwrap().1;
|
2022-10-19 14:48:50 +00:00
|
|
|
assert_eq!("test:{ location: 'GBR', year: 2022 }", format!("{}", out));
|
2022-08-26 23:33:52 +00:00
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
|
|
|
tb: String::from("test"),
|
|
|
|
id: Id::Object(Object::from(map! {
|
|
|
|
"location".to_string() => Value::from("GBR"),
|
|
|
|
"year".to_string() => Value::from(2022),
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thing_array() {
|
|
|
|
let sql = "test:['GBR', 2022]";
|
|
|
|
let res = thing(sql);
|
|
|
|
let out = res.unwrap().1;
|
2022-10-19 14:48:50 +00:00
|
|
|
assert_eq!("test:['GBR', 2022]", format!("{}", out));
|
2022-08-26 23:33:52 +00:00
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
|
|
|
tb: String::from("test"),
|
|
|
|
id: Id::Array(Array::from(vec![Value::from("GBR"), Value::from(2022)])),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|