dbschema/provider/postgres/generator/
text_search.rs1use std::fmt;
2
3use super::ident;
4
5#[derive(Debug, Clone)]
6pub struct TextSearchDictionary {
7 pub schema: String,
8 pub name: String,
9 pub template: String,
10 pub options: Vec<String>,
11}
12
13impl From<&crate::ir::TextSearchDictionarySpec> for TextSearchDictionary {
14 fn from(d: &crate::ir::TextSearchDictionarySpec) -> Self {
15 Self {
16 schema: d.schema.clone().unwrap_or_else(|| "public".to_string()),
17 name: d.alt_name.clone().unwrap_or_else(|| d.name.clone()),
18 template: d.template.clone(),
19 options: d.options.clone(),
20 }
21 }
22}
23
24impl fmt::Display for TextSearchDictionary {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 write!(
27 f,
28 "CREATE TEXT SEARCH DICTIONARY {}.{} (TEMPLATE = {}",
29 ident(&self.schema),
30 ident(&self.name),
31 self.template
32 )?;
33 for opt in &self.options {
34 write!(f, ", {opt}")?;
35 }
36 write!(f, ");")
37 }
38}
39
40#[derive(Debug, Clone)]
41pub struct TextSearchConfigurationMapping {
42 pub tokens: Vec<String>,
43 pub dictionaries: Vec<String>,
44}
45
46#[derive(Debug, Clone)]
47pub struct TextSearchConfiguration {
48 pub schema: String,
49 pub name: String,
50 pub parser: String,
51 pub mappings: Vec<TextSearchConfigurationMapping>,
52}
53
54impl From<&crate::ir::TextSearchConfigurationSpec> for TextSearchConfiguration {
55 fn from(c: &crate::ir::TextSearchConfigurationSpec) -> Self {
56 Self {
57 schema: c.schema.clone().unwrap_or_else(|| "public".to_string()),
58 name: c.alt_name.clone().unwrap_or_else(|| c.name.clone()),
59 parser: c.parser.clone(),
60 mappings: c
61 .mappings
62 .iter()
63 .map(|m| TextSearchConfigurationMapping {
64 tokens: m.tokens.clone(),
65 dictionaries: m.dictionaries.clone(),
66 })
67 .collect(),
68 }
69 }
70}
71
72impl fmt::Display for TextSearchConfiguration {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 write!(
75 f,
76 "CREATE TEXT SEARCH CONFIGURATION {}.{} (PARSER = {});",
77 ident(&self.schema),
78 ident(&self.name),
79 self.parser
80 )?;
81 for m in &self.mappings {
82 write!(
83 f,
84 "\nALTER TEXT SEARCH CONFIGURATION {}.{} ADD MAPPING FOR {} WITH {};",
85 ident(&self.schema),
86 ident(&self.name),
87 m.tokens.join(", "),
88 m.dictionaries.join(", ")
89 )?;
90 }
91 Ok(())
92 }
93}
94
95#[derive(Debug, Clone)]
96pub struct TextSearchTemplate {
97 pub schema: String,
98 pub name: String,
99 pub init: Option<String>,
100 pub lexize: String,
101}
102
103impl From<&crate::ir::TextSearchTemplateSpec> for TextSearchTemplate {
104 fn from(t: &crate::ir::TextSearchTemplateSpec) -> Self {
105 Self {
106 schema: t.schema.clone().unwrap_or_else(|| "public".to_string()),
107 name: t.alt_name.clone().unwrap_or_else(|| t.name.clone()),
108 init: t.init.clone(),
109 lexize: t.lexize.clone(),
110 }
111 }
112}
113
114impl fmt::Display for TextSearchTemplate {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 write!(
117 f,
118 "CREATE TEXT SEARCH TEMPLATE {}.{} (LEXIZE = {}",
119 ident(&self.schema),
120 ident(&self.name),
121 self.lexize
122 )?;
123 if let Some(init) = &self.init {
124 write!(f, ", INIT = {init}")?;
125 }
126 write!(f, ");")
127 }
128}
129
130#[derive(Debug, Clone)]
131pub struct TextSearchParser {
132 pub schema: String,
133 pub name: String,
134 pub start: String,
135 pub gettoken: String,
136 pub end: String,
137 pub headline: Option<String>,
138 pub lextypes: String,
139}
140
141impl From<&crate::ir::TextSearchParserSpec> for TextSearchParser {
142 fn from(p: &crate::ir::TextSearchParserSpec) -> Self {
143 Self {
144 schema: p.schema.clone().unwrap_or_else(|| "public".to_string()),
145 name: p.alt_name.clone().unwrap_or_else(|| p.name.clone()),
146 start: p.start.clone(),
147 gettoken: p.gettoken.clone(),
148 end: p.end.clone(),
149 headline: p.headline.clone(),
150 lextypes: p.lextypes.clone(),
151 }
152 }
153}
154
155impl fmt::Display for TextSearchParser {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 write!(
158 f,
159 "CREATE TEXT SEARCH PARSER {}.{} (START = {}, GETTOKEN = {}, END = {}, LEXTYPES = {}",
160 ident(&self.schema),
161 ident(&self.name),
162 self.start,
163 self.gettoken,
164 self.end,
165 self.lextypes
166 )?;
167 if let Some(headline) = &self.headline {
168 write!(f, ", HEADLINE = {headline}")?;
169 }
170 write!(f, ");")
171 }
172}
173
174#[cfg(test)]
175mod tests {
176 use super::*;
177
178 #[test]
179 fn dictionary_sql_basic() {
180 let spec = crate::ir::TextSearchDictionarySpec {
181 name: "d".into(),
182 alt_name: None,
183 schema: None,
184 template: "simple".into(),
185 options: vec!["dict = 'simple'".into()],
186 comment: None,
187 };
188 let dict = TextSearchDictionary::from(&spec);
189 assert_eq!(
190 dict.to_string(),
191 "CREATE TEXT SEARCH DICTIONARY \"public\".\"d\" (TEMPLATE = simple, dict = 'simple');"
192 );
193 }
194}