surrealpatch/core/src/doc/insert.rs

117 lines
3.4 KiB
Rust
Raw Normal View History

use crate::ctx::Context;
2024-05-28 10:43:45 +00:00
use crate::dbs::Options;
use crate::dbs::Statement;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::value::Value;
use reblessive::tree::Stk;
2022-01-13 07:00:50 +00:00
impl<'a> Document<'a> {
pub async fn insert(
&mut self,
stk: &mut Stk,
2022-05-30 15:32:26 +00:00
ctx: &Context<'_>,
opt: &Options,
stm: &Statement<'_>,
) -> Result<Value, Error> {
// Check whether current record exists
match self.current.doc.is_some() {
// We attempted to INSERT a document with an ID,
// and this ID already exists in the database,
// so we need to update the record instead.
2024-05-28 10:43:45 +00:00
true => self.insert_update(stk, ctx, opt, stm).await,
// We attempted to INSERT a document with an ID,
// which does not exist in the database, or we
// are creating a new record with a new ID.
2022-05-30 15:32:26 +00:00
false => {
// First of all let's try to create the record
2024-05-28 10:43:45 +00:00
match self.insert_create(stk, ctx, opt, stm).await {
// We received an index exists error, so we
// ignore the error, and attempt to update the
// record using the ON DUPLICATE KEY clause
// with the Record ID received in the error
Err(Error::IndexExists {
thing,
..
}) => Err(Error::RetryWithId(thing)),
// If any other error was received, then let's
// pass that error through and return an error
Err(e) => Err(e),
// Otherwise the record creation succeeded
Ok(v) => Ok(v),
}
2022-05-30 15:32:26 +00:00
}
}
}
// Attempt to run an INSERT clause
async fn insert_create(
&mut self,
stk: &mut Stk,
ctx: &Context<'_>,
opt: &Options,
stm: &Statement<'_>,
) -> Result<Value, Error> {
// Check if table has correct relation status
2024-05-28 10:43:45 +00:00
self.relation(ctx, opt, stm).await?;
// Merge record data
2024-05-28 10:43:45 +00:00
self.merge(stk, ctx, opt, stm).await?;
// Merge fields data
2024-05-28 10:43:45 +00:00
self.field(stk, ctx, opt, stm).await?;
// Reset fields data
2024-05-28 10:43:45 +00:00
self.reset(ctx, opt, stm).await?;
// Clean fields data
2024-05-28 10:43:45 +00:00
self.clean(stk, ctx, opt, stm).await?;
// Check if allowed
2024-05-28 10:43:45 +00:00
self.allow(stk, ctx, opt, stm).await?;
// Store index data
2024-05-28 10:43:45 +00:00
self.index(stk, ctx, opt, stm).await?;
// Store record data
2024-05-28 10:43:45 +00:00
self.store(ctx, opt, stm).await?;
// Run table queries
2024-05-28 10:43:45 +00:00
self.table(stk, ctx, opt, stm).await?;
// Run lives queries
2024-05-28 10:43:45 +00:00
self.lives(stk, ctx, opt, stm).await?;
// Run change feeds queries
2024-05-28 10:43:45 +00:00
self.changefeeds(ctx, opt, stm).await?;
// Run event queries
2024-05-28 10:43:45 +00:00
self.event(stk, ctx, opt, stm).await?;
// Yield document
2024-05-28 10:43:45 +00:00
self.pluck(stk, ctx, opt, stm).await
}
// Attempt to run an UPDATE clause
async fn insert_update(
&mut self,
stk: &mut Stk,
ctx: &Context<'_>,
opt: &Options,
stm: &Statement<'_>,
) -> Result<Value, Error> {
// Check if allowed
2024-05-28 10:43:45 +00:00
self.allow(stk, ctx, opt, stm).await?;
// Alter record data
2024-05-28 10:43:45 +00:00
self.alter(stk, ctx, opt, stm).await?;
// Merge fields data
2024-05-28 10:43:45 +00:00
self.field(stk, ctx, opt, stm).await?;
// Reset fields data
2024-05-28 10:43:45 +00:00
self.reset(ctx, opt, stm).await?;
// Clean fields data
2024-05-28 10:43:45 +00:00
self.clean(stk, ctx, opt, stm).await?;
// Check if allowed
2024-05-28 10:43:45 +00:00
self.allow(stk, ctx, opt, stm).await?;
// Store index data
2024-05-28 10:43:45 +00:00
self.index(stk, ctx, opt, stm).await?;
// Store record data
2024-05-28 10:43:45 +00:00
self.store(ctx, opt, stm).await?;
// Run table queries
2024-05-28 10:43:45 +00:00
self.table(stk, ctx, opt, stm).await?;
// Run lives queries
2024-05-28 10:43:45 +00:00
self.lives(stk, ctx, opt, stm).await?;
// Run change feeds queries
2024-05-28 10:43:45 +00:00
self.changefeeds(ctx, opt, stm).await?;
// Run event queries
2024-05-28 10:43:45 +00:00
self.event(stk, ctx, opt, stm).await?;
// Yield document
2024-05-28 10:43:45 +00:00
self.pluck(stk, ctx, opt, stm).await
}
}