surrealdb-sql 1.1.0

Full type definitions for the SurrealQL query language
Documentation
use crate::ctx::Context;
use crate::dbs::Statement;
use crate::dbs::{Options, Transaction};
use crate::doc::Document;
use crate::err::Error;
use crate::key::key_req::KeyRequirements;

impl<'a> Document<'a> {
	pub async fn store(
		&self,
		_ctx: &Context<'_>,
		opt: &Options,
		txn: &Transaction,
		stm: &Statement<'_>,
	) -> Result<(), Error> {
		// Check if forced
		if !opt.force && !self.changed() {
			return Ok(());
		}
		// Check if the table is a view
		if self.tb(opt, txn).await?.drop {
			return Ok(());
		}
		// Claim transaction
		let mut run = txn.lock().await;
		// Get the record id
		let rid = self.id.as_ref().unwrap();
		// Store the record data
		let key = crate::key::thing::new(opt.ns(), opt.db(), &rid.tb, &rid.id);
		//
		match stm {
			// This is a CREATE statement so try to insert the key
			Statement::Create(_) => match run.put(key.key_category(), key, self).await {
				// The key already exists, so return an error
				Err(Error::TxKeyAlreadyExistsCategory(_)) => Err(Error::RecordExists {
					thing: rid.to_string(),
				}),
				// Return any other received error
				Err(e) => Err(e),
				// Record creation worked fine
				Ok(v) => Ok(v),
			},
			// This is not a CREATE statement, so update the key
			_ => run.set(key, self).await,
		}?;
		// Carry on
		Ok(())
	}
}