use anyhow::Result;
use crate::catalog::Error;
use crate::catalog::providers::DatabaseProvider;
use crate::ctx::FrozenContext;
use crate::dbs::Options;
use crate::expr::Base;
use crate::expr::statements::remove::function::RemoveFunctionStatement;
use crate::iam::{Action, ResourceKind};
use crate::key::schema::FunctionKey;
use crate::val::Value;
pub(crate) async fn remove_function_statement_compute(
this: &RemoveFunctionStatement,
ctx: &FrozenContext,
opt: &Options,
) -> Result<Value> {
ctx.is_allowed(opt, Action::Edit, ResourceKind::Function, Base::Db)?;
let txn = ctx.tx();
let (ns, db) = ctx.expect_ns_db_ids(opt).await?;
let fc = match txn.get_db_function(ns, db, &this.name, None).await {
Ok(x) => x,
Err(e) => {
if this.if_exists && matches!(e.downcast_ref(), Some(Error::FcNotFound { .. })) {
return Ok(Value::None);
} else {
return Err(e);
}
}
};
let key = FunctionKey {
ns,
db,
fc: std::borrow::Cow::Borrowed(&fc.name),
};
txn.del_key(&key).await?;
txn.clear_cache();
Ok(Value::None)
}