surrealdb_core/sql/statements/define/
analyzer.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::{filter::Filter, tokenizer::Tokenizer, Base, Ident, Object, Strand, Value};
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
#[revisioned(revision = 3)]
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub struct DefineAnalyzerStatement {
pub name: Ident,
#[revision(start = 2)]
pub function: Option<Ident>,
pub tokenizers: Option<Vec<Tokenizer>>,
pub filters: Option<Vec<Filter>>,
pub comment: Option<Strand>,
#[revision(start = 3)]
pub if_not_exists: bool,
}
impl DefineAnalyzerStatement {
pub(crate) async fn compute(
&self,
ctx: &Context<'_>,
opt: &Options,
_doc: Option<&CursorDoc<'_>>,
) -> Result<Value, Error> {
opt.is_allowed(Action::Edit, ResourceKind::Analyzer, &Base::Db)?;
let mut run = ctx.tx_lock().await;
run.clear_cache();
if run.get_db_analyzer(opt.ns()?, opt.db()?, &self.name).await.is_ok() {
if self.if_not_exists {
return Ok(Value::None);
} else {
return Err(Error::AzAlreadyExists {
value: self.name.to_string(),
});
}
}
let key = crate::key::database::az::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,
DefineAnalyzerStatement {
if_not_exists: false,
..self.clone()
},
)
.await?;
drop(run); Ok(Value::None)
}
}
impl Display for DefineAnalyzerStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DEFINE ANALYZER")?;
if self.if_not_exists {
write!(f, " IF NOT EXISTS")?
}
write!(f, " {}", self.name)?;
if let Some(ref i) = self.function {
write!(f, " FUNCTION fn::{i}")?
}
if let Some(v) = &self.tokenizers {
let tokens: Vec<String> = v.iter().map(|f| f.to_string()).collect();
write!(f, " TOKENIZERS {}", tokens.join(","))?;
}
if let Some(v) = &self.filters {
let tokens: Vec<String> = v.iter().map(|f| f.to_string()).collect();
write!(f, " FILTERS {}", tokens.join(","))?;
}
if let Some(ref v) = self.comment {
write!(f, " COMMENT {v}")?
}
Ok(())
}
}
impl InfoStructure for DefineAnalyzerStatement {
fn structure(self) -> Value {
let Self {
name,
function,
tokenizers,
filters,
comment,
..
} = self;
let mut acc = Object::default();
acc.insert("name".to_string(), name.structure());
if let Some(function) = function {
acc.insert("function".to_string(), function.structure());
}
if let Some(tokenizers) = tokenizers {
acc.insert(
"tokenizers".to_string(),
Value::Array(tokenizers.into_iter().map(|t| t.to_string().into()).collect()),
);
}
if let Some(filters) = filters {
acc.insert(
"filters".to_string(),
Value::Array(filters.into_iter().map(|f| f.to_string().into()).collect()),
);
}
if let Some(comment) = comment {
acc.insert("comment".to_string(), comment.into());
}
Value::Object(acc)
}
}