surrealdb_sql/statements/define/
namespace.rsuse crate::ctx::Context;
use crate::dbs::{Options, Transaction};
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::iam::{Action, ResourceKind};
use crate::{Base, Ident, Strand, Value};
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[revisioned(revision = 1)]
pub struct DefineNamespaceStatement {
pub id: Option<u32>,
pub name: Ident,
pub comment: Option<Strand>,
}
impl DefineNamespaceStatement {
pub(crate) async fn compute(
&self,
_ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
_doc: Option<&CursorDoc<'_>>,
) -> Result<Value, Error> {
opt.is_allowed(Action::Edit, ResourceKind::Namespace, &Base::Root)?;
let key = crate::key::root::ns::new(&self.name);
let mut run = txn.lock().await;
run.clear_cache();
if self.id.is_none() {
let mut ns = self.clone();
ns.id = Some(run.get_next_ns_id().await?);
run.set(key, ns).await?;
} else {
run.set(key, self).await?;
}
Ok(Value::None)
}
}
impl Display for DefineNamespaceStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DEFINE NAMESPACE {}", self.name)?;
if let Some(ref v) = self.comment {
write!(f, " COMMENT {v}")?
}
Ok(())
}
}