Skip to main content

sqruff_lib_core/dialects/
init.rs

1use strum::IntoEnumIterator;
2use strum_macros::AsRefStr;
3
4use crate::value::Value;
5
6/// Trait for dialect-specific configuration.
7/// Each dialect implements this to parse and validate its configuration from raw config values.
8pub trait DialectConfig: Default + Clone + std::fmt::Debug {
9    /// Parse configuration from a Value (typically a Map from the config file's dialect section).
10    /// Returns the default configuration if parsing fails or if the input is None.
11    fn from_value(value: &Value) -> Self {
12        let _ = value;
13        Self::default()
14    }
15}
16
17/// Macro to generate a dialect config struct with `DialectConfig` impl and `config_options()`.
18///
19/// # Usage
20///
21/// ```ignore
22/// // Dialect with config options (all bool fields):
23/// sqruff_lib_core::dialect_config!(PostgresDialectConfig {
24///     /// Enable pg_trgm operators
25///     pg_trgm: "Enable parsing of pg_trgm trigram operators"
26/// });
27///
28/// // Dialect with no config options:
29/// sqruff_lib_core::dialect_config!(AnsiDialectConfig {});
30/// ```
31#[macro_export]
32macro_rules! dialect_config {
33    // With fields (all bool)
34    ($name:ident { $(
35        $(#[doc = $doc:expr])*
36        $field:ident : $desc:expr
37    ),* $(,)? }) => {
38        #[derive(Debug, Clone)]
39        pub struct $name {
40            $($(#[doc = $doc])* pub $field: bool,)*
41        }
42
43        impl Default for $name {
44            fn default() -> Self {
45                Self { $($field: false,)* }
46            }
47        }
48
49        impl $crate::dialects::init::DialectConfig for $name {
50            fn from_value(value: &$crate::value::Value) -> Self {
51                Self {
52                    $($field: value[stringify!($field)].to_bool(),)*
53                }
54            }
55        }
56
57        impl $name {
58            pub fn config_options() -> Vec<(&'static str, &'static str, &'static str)> {
59                vec![
60                    $((stringify!($field), $desc, "false"),)*
61                ]
62            }
63        }
64    };
65    // No fields
66    ($name:ident {}) => {
67        #[derive(Debug, Clone, Default)]
68        pub struct $name;
69
70        impl $crate::dialects::init::DialectConfig for $name {}
71
72        impl $name {
73            pub fn config_options() -> Vec<(&'static str, &'static str, &'static str)> {
74                vec![]
75            }
76        }
77    };
78}
79
80#[derive(
81    strum_macros::EnumString,
82    strum_macros::EnumIter,
83    AsRefStr,
84    Debug,
85    Clone,
86    Copy,
87    Default,
88    Ord,
89    PartialOrd,
90    Eq,
91    PartialEq,
92    Hash,
93)]
94#[strum(serialize_all = "snake_case")]
95pub enum DialectKind {
96    #[default]
97    Ansi,
98    Athena,
99    Bigquery,
100    Clickhouse,
101    Databricks,
102    Db2,
103    Duckdb,
104    Mysql,
105    Oracle,
106    Postgres,
107    Redshift,
108    Snowflake,
109    Sparksql,
110    Sqlite,
111    Trino,
112    Tsql,
113}
114
115impl DialectKind {
116    /// Returns the human-readable name of the dialect.
117    pub fn name(&self) -> &'static str {
118        match self {
119            DialectKind::Ansi => "ansi",
120            DialectKind::Athena => "athena",
121            DialectKind::Bigquery => "bigquery",
122            DialectKind::Clickhouse => "clickhouse",
123            DialectKind::Databricks => "databricks",
124            DialectKind::Db2 => "db2",
125            DialectKind::Duckdb => "duckdb",
126            DialectKind::Mysql => "mysql",
127            DialectKind::Oracle => "oracle",
128            DialectKind::Postgres => "postgres",
129            DialectKind::Redshift => "redshift",
130            DialectKind::Snowflake => "snowflake",
131            DialectKind::Sparksql => "sparksql",
132            DialectKind::Sqlite => "sqlite",
133            DialectKind::Trino => "trino",
134            DialectKind::Tsql => "tsql",
135        }
136    }
137
138    /// Returns a human-readable description of the dialect.
139    pub fn description(&self) -> &'static str {
140        match self {
141            DialectKind::Ansi => {
142                "Standard SQL syntax. The default dialect and base for all others."
143            }
144            DialectKind::Athena => "Amazon Athena SQL dialect for querying data in S3.",
145            DialectKind::Bigquery => {
146                "Google BigQuery SQL dialect for analytics and data warehousing."
147            }
148            DialectKind::Clickhouse => "ClickHouse SQL dialect for real-time analytics.",
149            DialectKind::Databricks => "Databricks SQL dialect for lakehouse analytics.",
150            DialectKind::Db2 => "IBM Db2 SQL dialect.",
151            DialectKind::Duckdb => "DuckDB SQL dialect for in-process analytical database.",
152            DialectKind::Mysql => "MySQL SQL dialect for the popular open-source database.",
153            DialectKind::Oracle => "Oracle SQL dialect for Oracle Database.",
154            DialectKind::Postgres => {
155                "PostgreSQL SQL dialect for the advanced open-source database."
156            }
157            DialectKind::Redshift => "Amazon Redshift SQL dialect for cloud data warehousing.",
158            DialectKind::Snowflake => "Snowflake SQL dialect for cloud data platform.",
159            DialectKind::Sparksql => "Apache Spark SQL dialect for big data processing.",
160            DialectKind::Sqlite => "SQLite SQL dialect for embedded database.",
161            DialectKind::Trino => "Trino (formerly PrestoSQL) dialect for distributed SQL queries.",
162            DialectKind::Tsql => "T-SQL dialect for Microsoft SQL Server and Azure SQL.",
163        }
164    }
165
166    /// Returns the configuration section header for this dialect.
167    /// Format: `[sqruff:dialect:{dialect_name}]`
168    pub fn config_section(&self) -> String {
169        format!("[sqruff:dialect:{}]", self.name())
170    }
171
172    /// Returns an optional URL to the official documentation for the dialect.
173    pub fn doc_url(&self) -> Option<&'static str> {
174        match self {
175            DialectKind::Ansi => None,
176            DialectKind::Athena => {
177                Some("https://docs.aws.amazon.com/athena/latest/ug/ddl-sql-reference.html")
178            }
179            DialectKind::Bigquery => {
180                Some("https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax")
181            }
182            DialectKind::Clickhouse => Some("https://clickhouse.com/docs/en/sql-reference/"),
183            DialectKind::Databricks => {
184                Some("https://docs.databricks.com/en/sql/language-manual/index.html")
185            }
186            DialectKind::Db2 => Some("https://www.ibm.com/docs/en/i/7.4?topic=overview-db2-i"),
187            DialectKind::Duckdb => Some("https://duckdb.org/docs/sql/introduction"),
188            DialectKind::Mysql => Some("https://dev.mysql.com/doc/"),
189            DialectKind::Oracle => {
190                Some("https://www.oracle.com/database/technologies/appdev/sql.html")
191            }
192            DialectKind::Postgres => Some("https://www.postgresql.org/docs/current/sql.html"),
193            DialectKind::Redshift => {
194                Some("https://docs.aws.amazon.com/redshift/latest/dg/cm_chap_SQLCommandRef.html")
195            }
196            DialectKind::Snowflake => Some("https://docs.snowflake.com/en/sql-reference.html"),
197            DialectKind::Sparksql => Some("https://spark.apache.org/sql/"),
198            DialectKind::Sqlite => Some("https://www.sqlite.org/lang.html"),
199            DialectKind::Trino => Some("https://trino.io/docs/current/sql.html"),
200            DialectKind::Tsql => {
201                Some("https://learn.microsoft.com/en-us/sql/t-sql/language-reference")
202            }
203        }
204    }
205}
206
207/// Generate a readout of available dialects.
208pub fn dialect_readout() -> Vec<String> {
209    DialectKind::iter()
210        .map(|x| x.as_ref().to_string())
211        .collect()
212}
213
214#[cfg(test)]
215mod tests {
216    use super::DialectKind;
217
218    #[test]
219    fn dialect_readout_is_alphabetically_sorted() {
220        let readout = super::dialect_readout();
221
222        let mut sorted = readout.clone();
223        sorted.sort();
224
225        assert_eq!(readout, sorted);
226    }
227
228    #[test]
229    fn config_section_format() {
230        assert_eq!(
231            DialectKind::Snowflake.config_section(),
232            "[sqruff:dialect:snowflake]"
233        );
234        assert_eq!(
235            DialectKind::Bigquery.config_section(),
236            "[sqruff:dialect:bigquery]"
237        );
238        assert_eq!(DialectKind::Ansi.config_section(), "[sqruff:dialect:ansi]");
239    }
240}