surrealdb/sql/statements/remove/
table.rs

1use 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	/// Process this type returning a computed simple Value
20	pub(crate) async fn compute(
21		&self,
22		_ctx: &Context<'_>,
23		opt: &Options,
24		txn: &Transaction,
25	) -> Result<Value, Error> {
26		// Allowed to run?
27		opt.is_allowed(Action::Edit, ResourceKind::Table, &Base::Db)?;
28		// Claim transaction
29		let mut run = txn.lock().await;
30		// Clear the cache
31		run.clear_cache();
32		// Get the defined table
33		let tb = run.get_tb(opt.ns(), opt.db(), &self.name).await?;
34		// Delete the definition
35		let key = crate::key::database::tb::new(opt.ns(), opt.db(), &self.name);
36		run.del(key).await?;
37		// Remove the resource data
38		let key = crate::key::table::all::new(opt.ns(), opt.db(), &self.name);
39		run.delp(key, u32::MAX).await?;
40		// Check if this is a foreign table
41		if let Some(view) = &tb.view {
42			// Process each foreign table
43			for v in view.what.0.iter() {
44				// Save the view config
45				let key = crate::key::table::ft::new(opt.ns(), opt.db(), v, &self.name);
46				run.del(key).await?;
47			}
48		}
49		// Ok all good
50		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}