surrealdb_core/sql/statements/remove/
database.rsuse crate::ctx::Context;
use crate::dbs::Options;
use crate::err::Error;
use crate::iam::{Action, ResourceKind};
use crate::sql::{Base, Ident, Value};
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
#[revisioned(revision = 3)]
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub struct RemoveDatabaseStatement {
pub name: Ident,
#[revision(start = 2)]
pub if_exists: bool,
#[revision(start = 3)]
pub expunge: bool,
}
impl RemoveDatabaseStatement {
pub(crate) async fn compute(&self, ctx: &Context, opt: &Options) -> Result<Value, Error> {
let future = async {
opt.is_allowed(Action::Edit, ResourceKind::Database, &Base::Ns)?;
let txn = ctx.tx();
ctx.get_index_stores().database_removed(&txn, opt.ns()?, &self.name).await?;
let db = txn.get_db(opt.ns()?, &self.name).await?;
let key = crate::key::namespace::db::new(opt.ns()?, &db.name);
match self.expunge {
true => txn.clr(key).await?,
false => txn.del(key).await?,
};
let key = crate::key::database::all::new(opt.ns()?, &db.name);
match self.expunge {
true => txn.clrp(key).await?,
false => txn.delp(key).await?,
};
txn.clear();
Ok(Value::None)
}
.await;
match future {
Err(Error::DbNotFound {
..
}) if self.if_exists => Ok(Value::None),
v => v,
}
}
}
impl Display for RemoveDatabaseStatement {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "REMOVE DATABASE")?;
if self.if_exists {
write!(f, " IF EXISTS")?
}
write!(f, " {}", self.name)?;
Ok(())
}
}