surrealdb_core/sql/statements/define/
function.rsuse crate::ctx::Context;
use crate::dbs::Options;
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::iam::{Action, ResourceKind};
use crate::sql::statements::info::InfoStructure;
use crate::sql::{
fmt::{is_pretty, pretty_indent},
Base, Block, Ident, Kind, Object, Permission, Strand, Value,
};
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Write};
#[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 DefineFunctionStatement {
pub name: Ident,
pub args: Vec<(Ident, Kind)>,
pub block: Block,
pub comment: Option<Strand>,
pub permissions: Permission,
#[revision(start = 2)]
pub if_not_exists: bool,
}
impl DefineFunctionStatement {
pub(crate) async fn compute(
&self,
ctx: &Context<'_>,
opt: &Options,
_doc: Option<&CursorDoc<'_>>,
) -> Result<Value, Error> {
opt.is_allowed(Action::Edit, ResourceKind::Function, &Base::Db)?;
let mut run = ctx.tx_lock().await;
run.clear_cache();
if run.get_db_function(opt.ns()?, opt.db()?, &self.name).await.is_ok() {
if self.if_not_exists {
return Ok(Value::None);
} else {
return Err(Error::FcAlreadyExists {
value: self.name.to_string(),
});
}
}
let key = crate::key::database::fc::new(opt.ns()?, opt.db()?, &self.name);
run.add_ns(opt.ns()?, opt.strict).await?;
run.add_db(opt.ns()?, opt.db()?, opt.strict).await?;
run.set(
key,
DefineFunctionStatement {
if_not_exists: false,
..self.clone()
},
)
.await?;
Ok(Value::None)
}
}
impl fmt::Display for DefineFunctionStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DEFINE FUNCTION")?;
if self.if_not_exists {
write!(f, " IF NOT EXISTS")?
}
write!(f, " fn::{}(", self.name.0)?;
for (i, (name, kind)) in self.args.iter().enumerate() {
if i > 0 {
f.write_str(", ")?;
}
write!(f, "${name}: {kind}")?;
}
f.write_str(") ")?;
Display::fmt(&self.block, f)?;
if let Some(ref v) = self.comment {
write!(f, " COMMENT {v}")?
}
let _indent = if is_pretty() {
Some(pretty_indent())
} else {
f.write_char(' ')?;
None
};
write!(f, "PERMISSIONS {}", self.permissions)?;
Ok(())
}
}
impl InfoStructure for DefineFunctionStatement {
fn structure(self) -> Value {
let Self {
name,
args,
block,
comment,
permissions,
..
} = self;
let mut acc = Object::default();
acc.insert("name".to_string(), name.structure());
acc.insert(
"args".to_string(),
Value::Array(
args.into_iter()
.map(|(n, k)| Value::Array(vec![n.structure(), k.structure()].into()))
.collect::<Vec<Value>>()
.into(),
),
);
acc.insert("block".to_string(), block.structure());
acc.insert("permissions".to_string(), permissions.structure());
if let Some(comment) = comment {
acc.insert("comment".to_string(), comment.into());
}
Value::Object(acc)
}
}