From 82e347027dcbbe46ed529d3a94b9716688118043 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 4 Sep 2022 10:52:01 +0100 Subject: [PATCH] Enable support for dynamic expressions in record IDs --- lib/src/sql/thing.rs | 31 +++++++++++++++++++++++++++++++ lib/src/sql/value/value.rs | 1 + 2 files changed, 32 insertions(+) diff --git a/lib/src/sql/thing.rs b/lib/src/sql/thing.rs index 460afa13..b5f66510 100644 --- a/lib/src/sql/thing.rs +++ b/lib/src/sql/thing.rs @@ -1,8 +1,13 @@ +use crate::ctx::Context; +use crate::dbs::Options; +use crate::dbs::Transaction; +use crate::err::Error; use crate::sql::error::IResult; use crate::sql::escape::escape_id; use crate::sql::id::{id, Id}; use crate::sql::ident::ident_raw; use crate::sql::serde::is_internal_serialization; +use crate::sql::value::Value; use derive::Store; use nom::branch::alt; use nom::character::complete::char; @@ -58,6 +63,32 @@ impl fmt::Display for Thing { } } +impl Thing { + pub(crate) async fn compute( + &self, + ctx: &Context<'_>, + opt: &Options, + txn: &Transaction, + doc: Option<&Value>, + ) -> Result { + Ok(Value::Thing(Thing { + tb: self.tb.clone(), + id: match &self.id { + Id::Number(v) => Id::Number(*v), + Id::String(v) => Id::String(v.clone()), + Id::Object(v) => match v.compute(ctx, opt, txn, doc).await? { + Value::Object(v) => Id::Object(v), + _ => unreachable!(), + }, + Id::Array(v) => match v.compute(ctx, opt, txn, doc).await? { + Value::Array(v) => Id::Array(v), + _ => unreachable!(), + }, + }, + })) + } +} + impl Serialize for Thing { fn serialize(&self, serializer: S) -> Result where diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index 47cd9c5f..162c5e8d 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -1115,6 +1115,7 @@ impl Value { Value::Null => Ok(Value::Null), Value::True => Ok(Value::True), Value::False => Ok(Value::False), + Value::Thing(v) => v.compute(ctx, opt, txn, doc).await, Value::Param(v) => v.compute(ctx, opt, txn, doc).await, Value::Idiom(v) => v.compute(ctx, opt, txn, doc).await, Value::Array(v) => v.compute(ctx, opt, txn, doc).await,