diff --git a/lib/src/sql/data.rs b/lib/src/sql/data.rs index 8d08e706..b1e8d75b 100644 --- a/lib/src/sql/data.rs +++ b/lib/src/sql/data.rs @@ -1,3 +1,6 @@ +use crate::ctx::Context; +use crate::dbs::Options; +use crate::dbs::Transaction; use crate::err::Error; use crate::sql::comment::mightbespace; use crate::sql::comment::shouldbespace; @@ -35,16 +38,36 @@ impl Default for Data { } impl Data { - // Fetch - pub(crate) fn rid(&self, tb: &Table) -> Result { + /// Fetch the 'id' field if one has been specified + pub(crate) async fn rid( + &self, + ctx: &Context<'_>, + opt: &Options, + txn: &Transaction, + tb: &Table, + ) -> Result { match self { - Self::MergeExpression(v) => v.rid().generate(tb, false), - Self::ReplaceExpression(v) => v.rid().generate(tb, false), - Self::ContentExpression(v) => v.rid().generate(tb, false), + Self::MergeExpression(v) => { + // This MERGE expression has an 'id' field + v.compute(ctx, opt, txn, None).await?.rid().generate(tb, false) + } + Self::ReplaceExpression(v) => { + // This REPLACE expression has an 'id' field + v.compute(ctx, opt, txn, None).await?.rid().generate(tb, false) + } + Self::ContentExpression(v) => { + // This CONTENT expression has an 'id' field + v.compute(ctx, opt, txn, None).await?.rid().generate(tb, false) + } Self::SetExpression(v) => match v.iter().find(|f| f.0.is_id()) { - Some((_, _, v)) => v.clone().generate(tb, false), + Some((_, _, v)) => { + // This SET expression has an 'id' field + v.compute(ctx, opt, txn, None).await?.generate(tb, false) + } + // This SET expression had no 'id' field _ => Ok(tb.generate()), }, + // Generate a random id for all other data clauses _ => Ok(tb.generate()), } } diff --git a/lib/src/sql/statements/create.rs b/lib/src/sql/statements/create.rs index aa56e030..844f23c8 100644 --- a/lib/src/sql/statements/create.rs +++ b/lib/src/sql/statements/create.rs @@ -54,7 +54,7 @@ impl CreateStatement { match v { Value::Table(v) => match &self.data { // There is a data clause so check for a record id - Some(data) => match data.rid(&v) { + Some(data) => match data.rid(ctx, opt, txn, &v).await { // There was a problem creating the record id Err(e) => return Err(e), // There is an id field so use the record id diff --git a/lib/src/sql/statements/relate.rs b/lib/src/sql/statements/relate.rs index 2564a35b..65146e11 100644 --- a/lib/src/sql/statements/relate.rs +++ b/lib/src/sql/statements/relate.rs @@ -150,7 +150,7 @@ impl RelateStatement { let w = w.clone(); match &self.data { // There is a data clause so check for a record id - Some(data) => match data.rid(&self.kind) { + Some(data) => match data.rid(ctx, opt, txn, &self.kind).await { // There was a problem creating the record id Err(e) => return Err(e), // There is an id field so use the record id