surrealdb_sql/statements/
kill.rsuse crate::ctx::Context;
use crate::dbs::{Options, Transaction};
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::Uuid;
use crate::Value;
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[revisioned(revision = 1)]
pub struct KillStatement {
pub id: Value,
}
impl KillStatement {
pub(crate) async fn compute(
&self,
ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
_doc: Option<&CursorDoc<'_>>,
) -> Result<Value, Error> {
opt.realtime()?;
opt.valid_for_db()?;
let live_query_id = match &self.id {
Value::Uuid(id) => *id,
Value::Param(param) => match param.compute(ctx, opt, txn, None).await? {
Value::Uuid(id) => id,
Value::Strand(id) => match uuid::Uuid::try_parse(&id) {
Ok(id) => Uuid(id),
_ => {
return Err(Error::KillStatement {
value: self.id.to_string(),
})
}
},
_ => {
return Err(Error::KillStatement {
value: self.id.to_string(),
})
}
},
_ => {
return Err(Error::KillStatement {
value: self.id.to_string(),
})
}
};
let mut run = txn.lock().await;
let key = crate::key::node::lq::new(opt.id()?, live_query_id.0, opt.ns(), opt.db());
match run.get(key).await? {
Some(val) => match std::str::from_utf8(&val) {
Ok(tb) => {
let key =
crate::key::node::lq::new(opt.id()?, live_query_id.0, opt.ns(), opt.db());
run.del(key).await?;
let key = crate::key::table::lq::new(opt.ns(), opt.db(), tb, live_query_id.0);
run.del(key).await?;
}
_ => {
return Err(Error::KillStatement {
value: self.id.to_string(),
})
}
},
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)
}
}