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, Array, Base, Ident, Strand, Value};
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
#[revisioned(revision = 4)]
#[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,
#[revision(start = 4)]
pub overwrite: 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 txn = ctx.tx();
let (ns, db) = (opt.ns()?, opt.db()?);
if txn.get_db_analyzer(ns, db, &self.name).await.is_ok() {
if self.if_not_exists {
return Ok(Value::None);
} else if !self.overwrite {
return Err(Error::AzAlreadyExists {
value: self.name.to_string(),
});
}
}
let key = crate::key::database::az::new(ns, db, &self.name);
txn.get_or_add_ns(ns, opt.strict).await?;
txn.get_or_add_db(ns, db, opt.strict).await?;
let az = DefineAnalyzerStatement {
if_not_exists: false,
overwrite: false,
..self.clone()
};
ctx.get_index_stores().mappers().load(&az).await?;
txn.set(key, az, None).await?;
txn.clear();
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")?
}
if self.overwrite {
write!(f, " OVERWRITE")?
}
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 {
Value::from(map! {
"name".to_string() => self.name.structure(),
"function".to_string(), if let Some(v) = self.function => v.structure(),
"tokenizers".to_string(), if let Some(v) = self.tokenizers =>
v.into_iter().map(|v| v.to_string().into()).collect::<Array>().into(),
"filters".to_string(), if let Some(v) = self.filters =>
v.into_iter().map(|v| v.to_string().into()).collect::<Array>().into(),
"comment".to_string(), if let Some(v) = self.comment => v.into(),
})
}
}