use anyhow::Result;
use surrealdb_strand::Strand;
use crate::catalog::providers::DatabaseProvider;
use crate::ctx::FrozenContext;
use crate::dbs::Options;
use crate::err::Error;
use crate::expr::{Base, Value};
use crate::iam::{Action, ResourceKind};
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)]
pub(crate) struct RemoveParamStatement {
pub name: Strand,
pub if_exists: bool,
}
impl RemoveParamStatement {
pub(crate) async fn compute(&self, ctx: &FrozenContext, opt: &Options) -> Result<Value> {
ctx.is_allowed(opt, Action::Edit, ResourceKind::Parameter, Base::Db)?;
let txn = ctx.tx();
let (ns, db) = ctx.expect_ns_db_ids(opt).await?;
let pa = match txn.get_db_param(ns, db, &self.name, None).await {
Ok(x) => x,
Err(e) => {
if self.if_exists && matches!(e.downcast_ref(), Some(Error::PaNotFound { .. })) {
return Ok(Value::None);
} else {
return Err(e);
}
}
};
let key = crate::key::database::pa::new(ns, db, &pa.name);
txn.del(&key).await?;
txn.clear_cache();
Ok(Value::None)
}
}