surrealdb_core/sql/statements/remove/
model.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};
#[revisioned(revision = 2)]
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub struct RemoveModelStatement {
pub name: Ident,
pub version: String,
#[revision(start = 2)]
pub if_exists: bool,
}
impl RemoveModelStatement {
pub(crate) async fn compute(&self, ctx: &Context, opt: &Options) -> Result<Value, Error> {
let future = async {
opt.is_allowed(Action::Edit, ResourceKind::Model, &Base::Db)?;
let txn = ctx.tx();
let ml = txn.get_db_model(opt.ns()?, opt.db()?, &self.name, &self.version).await?;
let key = crate::key::database::ml::new(opt.ns()?, opt.db()?, &ml.name, &ml.version);
txn.del(key).await?;
txn.clear();
Ok(Value::None)
}
.await;
match future {
Err(Error::MlNotFound {
..
}) if self.if_exists => Ok(Value::None),
v => v,
}
}
}
impl Display for RemoveModelStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "REMOVE MODEL")?;
if self.if_exists {
write!(f, " IF EXISTS")?
}
write!(f, " ml::{}<{}>", self.name.0, self.version)?;
Ok(())
}
}