2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::dbs::Statement;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::doc::Document;
|
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::value::Value;
|
2022-01-13 07:00:50 +00:00
|
|
|
|
2022-02-13 19:03:00 +00:00
|
|
|
impl<'a> Document<'a> {
|
2022-02-06 01:14:56 +00:00
|
|
|
pub async fn insert(
|
|
|
|
&mut self,
|
2022-05-30 15:32:26 +00:00
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
2023-07-06 14:57:42 +00:00
|
|
|
txn: &Transaction,
|
2022-05-30 15:32:26 +00:00
|
|
|
stm: &Statement<'_>,
|
2022-02-06 01:14:56 +00:00
|
|
|
) -> Result<Value, Error> {
|
2023-08-29 08:46:48 +00:00
|
|
|
// Check whether current record exists
|
2023-07-06 14:57:42 +00:00
|
|
|
match self.current.doc.is_some() {
|
2023-08-29 08:46:48 +00:00
|
|
|
// 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.
|
|
|
|
true => self.insert_update(ctx, opt, txn, 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 => {
|
2023-08-29 08:46:48 +00:00
|
|
|
// First of all let's try to create the record
|
|
|
|
match self.insert_create(ctx, opt, txn, 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
|
|
|
}
|
|
|
|
}
|
2022-02-06 01:14:56 +00:00
|
|
|
}
|
2023-08-29 08:46:48 +00:00
|
|
|
// Attempt to run an INSERT clause
|
|
|
|
async fn insert_create(
|
|
|
|
&mut self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
|
|
|
txn: &Transaction,
|
|
|
|
stm: &Statement<'_>,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
// Merge record data
|
|
|
|
self.merge(ctx, opt, txn, stm).await?;
|
|
|
|
// Merge fields data
|
|
|
|
self.field(ctx, opt, txn, stm).await?;
|
|
|
|
// Reset fields data
|
|
|
|
self.reset(ctx, opt, txn, stm).await?;
|
|
|
|
// Clean fields data
|
|
|
|
self.clean(ctx, opt, txn, stm).await?;
|
|
|
|
// Check if allowed
|
|
|
|
self.allow(ctx, opt, txn, stm).await?;
|
|
|
|
// Store record data
|
|
|
|
self.store(ctx, opt, txn, stm).await?;
|
2023-11-20 18:13:34 +00:00
|
|
|
// Store index data
|
|
|
|
self.index(ctx, opt, txn, stm).await?;
|
2023-08-29 08:46:48 +00:00
|
|
|
// Run table queries
|
|
|
|
self.table(ctx, opt, txn, stm).await?;
|
|
|
|
// Run lives queries
|
|
|
|
self.lives(ctx, opt, txn, stm).await?;
|
|
|
|
// Run change feeds queries
|
|
|
|
self.changefeeds(ctx, opt, txn, stm).await?;
|
|
|
|
// Run event queries
|
|
|
|
self.event(ctx, opt, txn, stm).await?;
|
|
|
|
// Yield document
|
|
|
|
self.pluck(ctx, opt, txn, stm).await
|
|
|
|
}
|
|
|
|
// Attempt to run an UPDATE clause
|
|
|
|
async fn insert_update(
|
|
|
|
&mut self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
|
|
|
txn: &Transaction,
|
|
|
|
stm: &Statement<'_>,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
// Check if allowed
|
|
|
|
self.allow(ctx, opt, txn, stm).await?;
|
|
|
|
// Alter record data
|
|
|
|
self.alter(ctx, opt, txn, stm).await?;
|
|
|
|
// Merge fields data
|
|
|
|
self.field(ctx, opt, txn, stm).await?;
|
|
|
|
// Reset fields data
|
|
|
|
self.reset(ctx, opt, txn, stm).await?;
|
|
|
|
// Clean fields data
|
|
|
|
self.clean(ctx, opt, txn, stm).await?;
|
|
|
|
// Check if allowed
|
|
|
|
self.allow(ctx, opt, txn, stm).await?;
|
|
|
|
// Store record data
|
|
|
|
self.store(ctx, opt, txn, stm).await?;
|
2023-11-20 18:13:34 +00:00
|
|
|
// Store index data
|
|
|
|
self.index(ctx, opt, txn, stm).await?;
|
2023-08-29 08:46:48 +00:00
|
|
|
// Run table queries
|
|
|
|
self.table(ctx, opt, txn, stm).await?;
|
|
|
|
// Run lives queries
|
|
|
|
self.lives(ctx, opt, txn, stm).await?;
|
|
|
|
// Run change feeds queries
|
|
|
|
self.changefeeds(ctx, opt, txn, stm).await?;
|
|
|
|
// Run event queries
|
|
|
|
self.event(ctx, opt, txn, stm).await?;
|
|
|
|
// Yield document
|
|
|
|
self.pluck(ctx, opt, txn, stm).await
|
|
|
|
}
|
2022-02-06 01:14:56 +00:00
|
|
|
}
|