surrealdb_core/sql/statements/
kill.rsuse crate::ctx::Context;
use crate::dbs::Options;
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::kvs::Live;
use crate::sql::Value;
use derive::Store;
use reblessive::tree::Stk;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt;
#[revisioned(revision = 1)]
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub struct KillStatement {
pub id: Value,
}
impl KillStatement {
pub(crate) async fn compute(
&self,
stk: &mut Stk,
ctx: &Context<'_>,
opt: &Options,
_doc: Option<&CursorDoc<'_>>,
) -> Result<Value, Error> {
opt.realtime()?;
opt.valid_for_db()?;
let lid = match self.id.compute(stk, ctx, opt, None).await?.convert_to_uuid() {
Err(_) => {
return Err(Error::KillStatement {
value: self.id.to_string(),
})
}
Ok(id) => id,
};
let nid = opt.id()?;
let lid = lid.0;
let txn = ctx.tx();
let mut txn = txn.lock().await;
let key = crate::key::node::lq::new(nid, lid);
match txn.get(key).await? {
Some(val) => {
let val: Live = val.into();
let key = crate::key::node::lq::new(nid, lid);
txn.del(key).await?;
let key = crate::key::table::lq::new(&val.ns, &val.db, &val.tb, lid);
txn.del(key).await?;
}
None => {
return Err(Error::KillStatement {
value: self.id.to_string(),
});
}
}
Ok(Value::None)
}
}
impl fmt::Display for KillStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "KILL {}", self.id)
}
}