2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
|
|
|
use crate::doc::CursorDoc;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::err::Error;
|
2023-01-19 09:53:33 +00:00
|
|
|
use crate::sql::fmt::{fmt_separated_by, Fmt};
|
2023-11-18 13:56:13 +00:00
|
|
|
use crate::sql::part::Next;
|
|
|
|
use crate::sql::part::Part;
|
2023-04-16 17:17:44 +00:00
|
|
|
use crate::sql::paths::{ID, IN, META, OUT};
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::value::Value;
|
2022-07-16 22:23:13 +00:00
|
|
|
use md5::Digest;
|
|
|
|
use md5::Md5;
|
2023-08-17 18:03:46 +00:00
|
|
|
use revision::revisioned;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-10-04 21:51:18 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2022-02-22 19:05:58 +00:00
|
|
|
use std::ops::Deref;
|
2020-06-29 15:36:01 +00:00
|
|
|
use std::str;
|
|
|
|
|
2023-03-30 10:41:44 +00:00
|
|
|
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Idiom";
|
|
|
|
|
2022-10-27 12:23:24 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2024-01-09 15:34:52 +00:00
|
|
|
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
2022-01-13 17:36:41 +00:00
|
|
|
pub struct Idioms(pub Vec<Idiom>);
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-04-09 09:09:01 +00:00
|
|
|
impl Deref for Idioms {
|
|
|
|
type Target = Vec<Idiom>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 18:00:11 +00:00
|
|
|
impl IntoIterator for Idioms {
|
|
|
|
type Item = Idiom;
|
|
|
|
type IntoIter = std::vec::IntoIter<Self::Item>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 21:51:18 +00:00
|
|
|
impl Display for Idioms {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
Display::fmt(&Fmt::comma_separated(&self.0), f)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 15:58:22 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
|
|
|
#[serde(rename = "$surrealdb::private::sql::Idiom")]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2024-01-09 15:34:52 +00:00
|
|
|
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
2022-05-05 11:26:46 +00:00
|
|
|
pub struct Idiom(pub Vec<Part>);
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-02-22 19:05:58 +00:00
|
|
|
impl Deref for Idiom {
|
|
|
|
type Target = [Part];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2022-05-05 11:26:46 +00:00
|
|
|
self.0.as_slice()
|
2022-02-22 19:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 19:44:27 +00:00
|
|
|
impl From<String> for Idiom {
|
|
|
|
fn from(v: String) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self(vec![Part::from(v)])
|
2022-05-31 19:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
impl From<Vec<Part>> for Idiom {
|
|
|
|
fn from(v: Vec<Part>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self(v)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 09:24:14 +00:00
|
|
|
impl From<&[Part]> for Idiom {
|
|
|
|
fn from(v: &[Part]) -> Self {
|
|
|
|
Self(v.to_vec())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
impl Idiom {
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Appends a part to the end of this Idiom
|
2022-06-04 08:55:05 +00:00
|
|
|
pub(crate) fn push(mut self, n: Part) -> Idiom {
|
2022-05-05 11:26:46 +00:00
|
|
|
self.0.push(n);
|
2022-04-07 14:33:57 +00:00
|
|
|
self
|
2022-01-22 21:16:13 +00:00
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Convert this Idiom to a unique hash
|
2022-07-16 22:23:13 +00:00
|
|
|
pub(crate) fn to_hash(&self) -> String {
|
|
|
|
let mut hasher = Md5::new();
|
|
|
|
hasher.update(self.to_string().as_str());
|
|
|
|
format!("{:x}", hasher.finalize())
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Convert this Idiom to a JSON Path string
|
2022-06-04 08:55:05 +00:00
|
|
|
pub(crate) fn to_path(&self) -> String {
|
2023-02-03 11:47:07 +00:00
|
|
|
format!("/{self}").replace(']', "").replace(&['.', '['][..], "/")
|
2022-01-31 23:11:06 +00:00
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Simplifies this Idiom for use in object keys
|
2022-06-04 08:55:05 +00:00
|
|
|
pub(crate) fn simplify(&self) -> Idiom {
|
2022-05-31 19:44:27 +00:00
|
|
|
self.0
|
|
|
|
.iter()
|
2023-10-04 09:51:34 +00:00
|
|
|
.filter(|&p| {
|
2023-07-15 07:08:26 +00:00
|
|
|
matches!(p, Part::Field(_) | Part::Start(_) | Part::Value(_) | Part::Graph(_))
|
|
|
|
})
|
2023-10-04 09:51:34 +00:00
|
|
|
.cloned()
|
2022-05-31 19:44:27 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into()
|
|
|
|
}
|
2023-09-01 11:52:02 +00:00
|
|
|
/// Check if this Idiom is an 'id' field
|
2022-07-04 16:54:43 +00:00
|
|
|
pub(crate) fn is_id(&self) -> bool {
|
|
|
|
self.0.len() == 1 && self.0[0].eq(&ID[0])
|
|
|
|
}
|
2023-09-01 11:52:02 +00:00
|
|
|
/// Check if this Idiom is an 'in' field
|
2022-07-04 16:54:43 +00:00
|
|
|
pub(crate) fn is_in(&self) -> bool {
|
|
|
|
self.0.len() == 1 && self.0[0].eq(&IN[0])
|
|
|
|
}
|
2023-09-01 11:52:02 +00:00
|
|
|
/// Check if this Idiom is an 'out' field
|
2022-07-04 16:54:43 +00:00
|
|
|
pub(crate) fn is_out(&self) -> bool {
|
|
|
|
self.0.len() == 1 && self.0[0].eq(&OUT[0])
|
|
|
|
}
|
2023-09-01 11:52:02 +00:00
|
|
|
/// Check if this Idiom is a 'meta' field
|
2023-04-16 17:17:44 +00:00
|
|
|
pub(crate) fn is_meta(&self) -> bool {
|
|
|
|
self.0.len() == 1 && self.0[0].eq(&META[0])
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Check if this is an expression with multiple yields
|
2022-06-04 08:55:05 +00:00
|
|
|
pub(crate) fn is_multi_yield(&self) -> bool {
|
|
|
|
self.iter().any(Self::split_multi_yield)
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Check if the path part is a yield in a multi-yield expression
|
2022-06-04 08:55:05 +00:00
|
|
|
pub(crate) fn split_multi_yield(v: &Part) -> bool {
|
|
|
|
matches!(v, Part::Graph(g) if g.alias.is_some())
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
impl Idiom {
|
2023-05-09 22:17:29 +00:00
|
|
|
/// Check if we require a writeable transaction
|
|
|
|
pub(crate) fn writeable(&self) -> bool {
|
|
|
|
self.0.iter().any(|v| v.writeable())
|
|
|
|
}
|
|
|
|
/// 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-06-09 08:18:08 +00:00
|
|
|
match self.first() {
|
2023-03-31 15:42:29 +00:00
|
|
|
// The starting part is a value
|
2023-07-15 07:08:26 +00:00
|
|
|
Some(Part::Start(v)) => {
|
2023-07-06 14:57:42 +00:00
|
|
|
v.compute(ctx, opt, txn, doc)
|
2022-06-09 08:18:08 +00:00
|
|
|
.await?
|
2023-07-06 14:57:42 +00:00
|
|
|
.get(ctx, opt, txn, doc, self.as_ref().next())
|
2023-03-31 15:42:29 +00:00
|
|
|
.await?
|
2023-07-06 14:57:42 +00:00
|
|
|
.compute(ctx, opt, txn, doc)
|
2022-06-09 08:18:08 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
// Otherwise use the current document
|
2023-07-06 14:57:42 +00:00
|
|
|
_ => match doc {
|
2022-06-09 08:18:08 +00:00
|
|
|
// There is a current document
|
2023-07-06 14:57:42 +00:00
|
|
|
Some(v) => {
|
|
|
|
v.doc.get(ctx, opt, txn, doc, self).await?.compute(ctx, opt, txn, doc).await
|
|
|
|
}
|
2022-06-09 08:18:08 +00:00
|
|
|
// There isn't any document
|
|
|
|
None => Ok(Value::None),
|
|
|
|
},
|
2022-01-14 08:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 15:42:29 +00:00
|
|
|
impl Display for Idiom {
|
2020-06-29 15:36:01 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2023-01-19 09:53:33 +00:00
|
|
|
Display::fmt(
|
|
|
|
&Fmt::new(
|
|
|
|
self.0.iter().enumerate().map(|args| {
|
|
|
|
Fmt::new(args, |(i, p), f| match (i, p) {
|
|
|
|
(0, Part::Field(v)) => Display::fmt(v, f),
|
|
|
|
_ => Display::fmt(p, f),
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
fmt_separated_by(""),
|
|
|
|
),
|
2022-01-13 17:36:41 +00:00
|
|
|
f,
|
|
|
|
)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|