polyglot_sql/dialects/
tableau.rs1use super::{DialectImpl, DialectType};
14use crate::error::Result;
15use crate::expressions::{Expression, Function};
16use crate::generator::GeneratorConfig;
17use crate::tokens::TokenizerConfig;
18
19pub struct TableauDialect;
21
22impl DialectImpl for TableauDialect {
23 fn dialect_type(&self) -> DialectType {
24 DialectType::Tableau
25 }
26
27 fn tokenizer_config(&self) -> TokenizerConfig {
28 let mut config = TokenizerConfig::default();
29 config.identifiers.insert('[', ']');
31 config
32 }
33
34 fn generator_config(&self) -> GeneratorConfig {
35 use crate::generator::IdentifierQuoteStyle;
36 GeneratorConfig {
37 identifier_quote: '[',
38 identifier_quote_style: IdentifierQuoteStyle::BRACKET,
39 dialect: Some(DialectType::Tableau),
40 ..Default::default()
41 }
42 }
43
44 fn transform_expr(&self, expr: Expression) -> Result<Expression> {
45 match expr {
46 Expression::Coalesce(f) => {
48 if f.expressions.len() == 2 {
49 Ok(Expression::Function(Box::new(Function::new(
50 "IFNULL".to_string(),
51 f.expressions,
52 ))))
53 } else {
54 Ok(Expression::Coalesce(f))
56 }
57 }
58
59 Expression::Nvl(f) => Ok(Expression::Function(Box::new(Function::new(
61 "IFNULL".to_string(),
62 vec![f.this, f.expression],
63 )))),
64
65 Expression::IfNull(f) => Ok(Expression::Function(Box::new(Function::new(
67 "IFNULL".to_string(),
68 vec![f.this, f.expression],
69 )))),
70
71 Expression::Function(f) => self.transform_function(*f),
73
74 Expression::AggregateFunction(f) => self.transform_aggregate_function(f),
76
77 _ => Ok(expr),
79 }
80 }
81}
82
83impl TableauDialect {
84 fn transform_function(&self, f: Function) -> Result<Expression> {
85 let name_upper = f.name.to_uppercase();
86 match name_upper.as_str() {
87 "COALESCE" if f.args.len() == 2 => Ok(Expression::Function(Box::new(Function::new(
89 "IFNULL".to_string(),
90 f.args,
91 )))),
92
93 "NVL" if f.args.len() == 2 => Ok(Expression::Function(Box::new(Function::new(
95 "IFNULL".to_string(),
96 f.args,
97 )))),
98
99 "ISNULL" if f.args.len() == 2 => Ok(Expression::Function(Box::new(Function::new(
101 "IFNULL".to_string(),
102 f.args,
103 )))),
104
105 "STRPOS" | "POSITION" | "INSTR" if f.args.len() >= 2 => {
107 Ok(Expression::Function(Box::new(Function::new(
108 "FIND".to_string(),
109 f.args,
110 ))))
111 }
112
113 "CHARINDEX" if f.args.len() >= 2 => Ok(Expression::Function(Box::new(Function::new(
115 "FIND".to_string(),
116 f.args,
117 )))),
118
119 _ => Ok(Expression::Function(Box::new(f))),
121 }
122 }
123
124 fn transform_aggregate_function(
125 &self,
126 f: Box<crate::expressions::AggregateFunction>,
127 ) -> Result<Expression> {
128 let name_upper = f.name.to_uppercase();
129 match name_upper.as_str() {
130 "COUNT" if f.distinct => {
132 Ok(Expression::Function(Box::new(Function::new(
133 "COUNTD".to_string(),
134 f.args,
135 ))))
136 }
137
138 _ => Ok(Expression::AggregateFunction(f)),
140 }
141 }
142}