surrealdb/sql/statements/define/
database.rsuse crate::ctx::Context;
use crate::dbs::{Options, Transaction};
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::iam::{Action, ResourceKind};
use crate::sql::{changefeed::ChangeFeed, 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 DefineDatabaseStatement {
pub id: Option<u32>,
pub name: Ident,
pub comment: Option<Strand>,
pub changefeed: Option<ChangeFeed>,
}
impl DefineDatabaseStatement {
pub(crate) async fn compute(
&self,
_ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
_doc: Option<&CursorDoc<'_>>,
) -> Result<Value, Error> {
opt.is_allowed(Action::Edit, ResourceKind::Database, &Base::Ns)?;
let mut run = txn.lock().await;
run.clear_cache();
let key = crate::key::namespace::db::new(opt.ns(), &self.name);
let ns = run.add_ns(opt.ns(), opt.strict).await?;
if self.id.is_none() && ns.id.is_some() {
let mut db = self.clone();
db.id = Some(run.get_next_db_id(ns.id.unwrap()).await?);
run.set(key, db).await?;
} else {
run.set(key, self).await?;
}
Ok(Value::None)
}
}
impl Display for DefineDatabaseStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DEFINE DATABASE {}", self.name)?;
if let Some(ref v) = self.comment {
write!(f, " COMMENT {v}")?
}
if let Some(ref v) = self.changefeed {
write!(f, " {v}")?;
}
Ok(())
}
}