Allow Record IDs defined within parameters to be used in CREATE and RELATE statements

Closes #1383
This commit is contained in:
Tobie Morgan Hitchcock 2022-10-19 19:16:31 +01:00
parent 1becd3ab55
commit 2ceb10f4cb
3 changed files with 31 additions and 8 deletions

View file

@ -1,3 +1,6 @@
use crate::ctx::Context;
use crate::dbs::Options;
use crate::dbs::Transaction;
use crate::err::Error; use crate::err::Error;
use crate::sql::comment::mightbespace; use crate::sql::comment::mightbespace;
use crate::sql::comment::shouldbespace; use crate::sql::comment::shouldbespace;
@ -35,16 +38,36 @@ impl Default for Data {
} }
impl Data { impl Data {
// Fetch /// Fetch the 'id' field if one has been specified
pub(crate) fn rid(&self, tb: &Table) -> Result<Thing, Error> { pub(crate) async fn rid(
&self,
ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
tb: &Table,
) -> Result<Thing, Error> {
match self { match self {
Self::MergeExpression(v) => v.rid().generate(tb, false), Self::MergeExpression(v) => {
Self::ReplaceExpression(v) => v.rid().generate(tb, false), // This MERGE expression has an 'id' field
Self::ContentExpression(v) => v.rid().generate(tb, false), 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()) { 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()), _ => Ok(tb.generate()),
}, },
// Generate a random id for all other data clauses
_ => Ok(tb.generate()), _ => Ok(tb.generate()),
} }
} }

View file

@ -54,7 +54,7 @@ impl CreateStatement {
match v { match v {
Value::Table(v) => match &self.data { Value::Table(v) => match &self.data {
// There is a data clause so check for a record id // 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 // There was a problem creating the record id
Err(e) => return Err(e), Err(e) => return Err(e),
// There is an id field so use the record id // There is an id field so use the record id

View file

@ -150,7 +150,7 @@ impl RelateStatement {
let w = w.clone(); let w = w.clone();
match &self.data { match &self.data {
// There is a data clause so check for a record id // 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 // There was a problem creating the record id
Err(e) => return Err(e), Err(e) => return Err(e),
// There is an id field so use the record id // There is an id field so use the record id