surrealdb/sql/statements/remove/
table.rs1use crate::ctx::Context;
2use crate::dbs::Options;
3use crate::dbs::Transaction;
4use crate::err::Error;
5use crate::iam::{Action, ResourceKind};
6use crate::sql::{Base, Ident, Value};
7use derive::Store;
8use revision::revisioned;
9use serde::{Deserialize, Serialize};
10use std::fmt::{self, Display, Formatter};
11
12#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
13#[revisioned(revision = 1)]
14pub struct RemoveTableStatement {
15 pub name: Ident,
16}
17
18impl RemoveTableStatement {
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::Table, &Base::Db)?;
28 let mut run = txn.lock().await;
30 run.clear_cache();
32 let tb = run.get_tb(opt.ns(), opt.db(), &self.name).await?;
34 let key = crate::key::database::tb::new(opt.ns(), opt.db(), &self.name);
36 run.del(key).await?;
37 let key = crate::key::table::all::new(opt.ns(), opt.db(), &self.name);
39 run.delp(key, u32::MAX).await?;
40 if let Some(view) = &tb.view {
42 for v in view.what.0.iter() {
44 let key = crate::key::table::ft::new(opt.ns(), opt.db(), v, &self.name);
46 run.del(key).await?;
47 }
48 }
49 Ok(Value::None)
51 }
52}
53
54impl Display for RemoveTableStatement {
55 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
56 write!(f, "REMOVE TABLE {}", self.name)
57 }
58}