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::statements::define::DefineTableStatement;
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 key = crate::key::node::lq::new(nid, lid);
match txn.get(key, None).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?;
let key = crate::key::database::tb::new(&val.ns, &val.db, &val.tb);
let tb = txn.get_tb(&val.ns, &val.db, &val.tb).await?;
txn.set(
key,
DefineTableStatement {
cache_lives_ts: uuid::Uuid::now_v7(),
..tb.as_ref().clone()
},
None,
)
.await?;
txn.clear();
}
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)
}
}