surrealdb_sql/statements/define/
analyzer.rs1use common::fmt::Fmt;
2use surrealdb_strand::Strand;
3use surrealdb_types::{SqlFormat, ToSql, write_sql};
4
5use super::DefineKind;
6use crate::analyzer_function::fmt_analyzer_function;
7use crate::filter::Filter;
8use crate::tokenizer::{Tokenizer, write_tokenizers_sql};
9use crate::{CoverStmts, Expr, Literal};
10
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct DefineAnalyzerStatement {
13 pub kind: DefineKind,
14 pub name: Expr,
15 pub function: Option<Strand>,
16 pub tokenizers: Option<Vec<Tokenizer>>,
17 pub filters: Option<Vec<Filter>>,
18 pub comment: Expr,
19}
20
21impl Default for DefineAnalyzerStatement {
22 fn default() -> Self {
23 Self {
24 kind: DefineKind::Default,
25 name: Expr::Literal(Literal::None),
26 function: None,
27 tokenizers: None,
28 filters: None,
29 comment: Expr::Literal(Literal::None),
30 }
31 }
32}
33
34impl ToSql for DefineAnalyzerStatement {
35 fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
36 write_sql!(f, sql_fmt, "DEFINE ANALYZER");
37 match self.kind {
38 DefineKind::Default => {}
39 DefineKind::Overwrite => write_sql!(f, sql_fmt, " OVERWRITE"),
40 DefineKind::IfNotExists => write_sql!(f, sql_fmt, " IF NOT EXISTS"),
41 }
42 write_sql!(f, sql_fmt, " {}", CoverStmts(&self.name));
43 if let Some(ref i) = self.function {
44 fmt_analyzer_function(f, sql_fmt, i.as_str());
45 }
46 if let Some(v) = &self.tokenizers {
47 write_sql!(f, sql_fmt, " TOKENIZERS ");
48 write_tokenizers_sql(f, sql_fmt, v.iter().copied());
49 }
50 if let Some(v) = &self.filters {
51 write_sql!(f, sql_fmt, " FILTERS {}", Fmt::comma_separated(v.iter()));
52 }
53 if !matches!(self.comment, Expr::Literal(Literal::None)) {
54 write_sql!(f, sql_fmt, " COMMENT {}", CoverStmts(&self.comment));
55 }
56 }
57}