surrealdb_sql/statements/remove/
token.rs1use crate::ctx::Context;
2use crate::dbs::{Options, Transaction};
3use crate::err::Error;
4use crate::iam::{Action, ResourceKind};
5use crate::{Base, Ident, Value};
6use derive::Store;
7use revision::revisioned;
8use serde::{Deserialize, Serialize};
9use std::fmt::{self, Display, Formatter};
10
11#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
12#[revisioned(revision = 1)]
13pub struct RemoveTokenStatement {
14 pub name: Ident,
15 pub base: Base,
16}
17
18impl RemoveTokenStatement {
19 pub(crate) async fn compute(
21 &self,
22 _ctx: &Context<'_>,
23 opt: &Options,
24 txn: &Transaction,
25 ) -> Result<Value, Error> {
26 opt.is_allowed(Action::Edit, ResourceKind::Actor, &self.base)?;
28
29 match &self.base {
30 Base::Ns => {
31 let mut run = txn.lock().await;
33 run.clear_cache();
35 let key = crate::key::namespace::tk::new(opt.ns(), &self.name);
37 run.del(key).await?;
38 Ok(Value::None)
40 }
41 Base::Db => {
42 let mut run = txn.lock().await;
44 run.clear_cache();
46 let key = crate::key::database::tk::new(opt.ns(), opt.db(), &self.name);
48 run.del(key).await?;
49 Ok(Value::None)
51 }
52 Base::Sc(sc) => {
53 let mut run = txn.lock().await;
55 run.clear_cache();
57 let key = crate::key::scope::tk::new(opt.ns(), opt.db(), sc, &self.name);
59 run.del(key).await?;
60 Ok(Value::None)
62 }
63 _ => Err(Error::InvalidLevel(self.base.to_string())),
64 }
65 }
66}
67
68impl Display for RemoveTokenStatement {
69 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
70 write!(f, "REMOVE TOKEN {} ON {}", self.name, self.base)
71 }
72}