surrealdb_core/sql/statements/remove/
index.rs1use crate::ctx::Context;
2use crate::dbs::Options;
3use crate::err::Error;
4use crate::iam::{Action, ResourceKind};
5use crate::sql::statements::define::DefineTableStatement;
6use crate::sql::{Base, Ident, Value};
7
8use revision::revisioned;
9use serde::{Deserialize, Serialize};
10use std::fmt::{self, Display, Formatter};
11use uuid::Uuid;
12
13#[revisioned(revision = 2)]
14#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
15#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
16#[non_exhaustive]
17pub struct RemoveIndexStatement {
18 pub name: Ident,
19 pub what: Ident,
20 #[revision(start = 2)]
21 pub if_exists: bool,
22}
23
24impl RemoveIndexStatement {
25 pub(crate) async fn compute(&self, ctx: &Context, opt: &Options) -> Result<Value, Error> {
27 let future = async {
28 opt.is_allowed(Action::Edit, ResourceKind::Index, &Base::Db)?;
30 let (ns, db) = opt.ns_db()?;
32 let txn = ctx.tx();
34 #[cfg(not(target_family = "wasm"))]
36 ctx.get_index_stores()
37 .index_removed(ctx.get_index_builder(), &txn, ns, db, &self.what, &self.name)
38 .await?;
39 #[cfg(target_family = "wasm")]
40 ctx.get_index_stores().index_removed(&txn, ns, db, &self.what, &self.name).await?;
41 let key = crate::key::table::ix::new(ns, db, &self.what, &self.name);
43 txn.del(key).await?;
44 let key = crate::key::index::all::new(ns, db, &self.what, &self.name);
46 txn.delp(key).await?;
47 let key = crate::key::database::tb::new(ns, db, &self.what);
49 let tb = txn.get_tb(ns, db, &self.what).await?;
50 txn.set(
51 key,
52 revision::to_vec(&DefineTableStatement {
53 cache_indexes_ts: Uuid::now_v7(),
54 ..tb.as_ref().clone()
55 })?,
56 None,
57 )
58 .await?;
59 if let Some(cache) = ctx.get_cache() {
61 cache.clear_tb(ns, db, &self.what);
62 }
63 txn.clear();
65 Ok(Value::None)
67 }
68 .await;
69 match future {
70 Err(Error::IxNotFound {
71 ..
72 }) if self.if_exists => Ok(Value::None),
73 v => v,
74 }
75 }
76}
77
78impl Display for RemoveIndexStatement {
79 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
80 write!(f, "REMOVE INDEX")?;
81 if self.if_exists {
82 write!(f, " IF EXISTS")?
83 }
84 write!(f, " {} ON {}", self.name, self.what)?;
85 Ok(())
86 }
87}