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    Duckdb,
103    Mysql,
104    Postgres,
105    Redshift,
106    Snowflake,
107    Sparksql,
108    Sqlite,
109    Trino,
110    Tsql,
111}
112
113impl DialectKind {
114    /// Returns the human-readable name of the dialect.
115    pub fn name(&self) -> &'static str {
116        match self {
117            DialectKind::Ansi => "ansi",
118            DialectKind::Athena => "athena",
119            DialectKind::Bigquery => "bigquery",
120            DialectKind::Clickhouse => "clickhouse",
121            DialectKind::Databricks => "databricks",
122            DialectKind::Duckdb => "duckdb",
123            DialectKind::Mysql => "mysql",
124            DialectKind::Postgres => "postgres",
125            DialectKind::Redshift => "redshift",
126            DialectKind::Snowflake => "snowflake",
127            DialectKind::Sparksql => "sparksql",
128            DialectKind::Sqlite => "sqlite",
129            DialectKind::Trino => "trino",
130            DialectKind::Tsql => "tsql",
131        }
132    }
133
134    /// Returns a human-readable description of the dialect.
135    pub fn description(&self) -> &'static str {
136        match self {
137            DialectKind::Ansi => {
138                "Standard SQL syntax. The default dialect and base for all others."
139            }
140            DialectKind::Athena => "Amazon Athena SQL dialect for querying data in S3.",
141            DialectKind::Bigquery => {
142                "Google BigQuery SQL dialect for analytics and data warehousing."
143            }
144            DialectKind::Clickhouse => "ClickHouse SQL dialect for real-time analytics.",
145            DialectKind::Databricks => "Databricks SQL dialect for lakehouse analytics.",
146            DialectKind::Duckdb => "DuckDB SQL dialect for in-process analytical database.",
147            DialectKind::Mysql => "MySQL SQL dialect for the popular open-source database.",
148            DialectKind::Postgres => {
149                "PostgreSQL SQL dialect for the advanced open-source database."
150            }
151            DialectKind::Redshift => "Amazon Redshift SQL dialect for cloud data warehousing.",
152            DialectKind::Snowflake => "Snowflake SQL dialect for cloud data platform.",
153            DialectKind::Sparksql => "Apache Spark SQL dialect for big data processing.",
154            DialectKind::Sqlite => "SQLite SQL dialect for embedded database.",
155            DialectKind::Trino => "Trino (formerly PrestoSQL) dialect for distributed SQL queries.",
156            DialectKind::Tsql => "T-SQL dialect for Microsoft SQL Server and Azure SQL.",
157        }
158    }
159
160    /// Returns the configuration section header for this dialect.
161    /// Format: `[sqruff:dialect:{dialect_name}]`
162    pub fn config_section(&self) -> String {
163        format!("[sqruff:dialect:{}]", self.name())
164    }
165
166    /// Returns an optional URL to the official documentation for the dialect.
167    pub fn doc_url(&self) -> Option<&'static str> {
168        match self {
169            DialectKind::Ansi => None,
170            DialectKind::Athena => {
171                Some("https://docs.aws.amazon.com/athena/latest/ug/ddl-sql-reference.html")
172            }
173            DialectKind::Bigquery => {
174                Some("https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax")
175            }
176            DialectKind::Clickhouse => Some("https://clickhouse.com/docs/en/sql-reference/"),
177            DialectKind::Databricks => {
178                Some("https://docs.databricks.com/en/sql/language-manual/index.html")
179            }
180            DialectKind::Duckdb => Some("https://duckdb.org/docs/sql/introduction"),
181            DialectKind::Mysql => Some("https://dev.mysql.com/doc/"),
182            DialectKind::Postgres => Some("https://www.postgresql.org/docs/current/sql.html"),
183            DialectKind::Redshift => {
184                Some("https://docs.aws.amazon.com/redshift/latest/dg/cm_chap_SQLCommandRef.html")
185            }
186            DialectKind::Snowflake => Some("https://docs.snowflake.com/en/sql-reference.html"),
187            DialectKind::Sparksql => Some("https://spark.apache.org/sql/"),
188            DialectKind::Sqlite => Some("https://www.sqlite.org/lang.html"),
189            DialectKind::Trino => Some("https://trino.io/docs/current/sql.html"),
190            DialectKind::Tsql => {
191                Some("https://learn.microsoft.com/en-us/sql/t-sql/language-reference")
192            }
193        }
194    }
195}
196
197/// Generate a readout of available dialects.
198pub fn dialect_readout() -> Vec<String> {
199    DialectKind::iter()
200        .map(|x| x.as_ref().to_string())
201        .collect()
202}
203
204#[cfg(test)]
205mod tests {
206    use super::DialectKind;
207
208    #[test]
209    fn dialect_readout_is_alphabetically_sorted() {
210        let readout = super::dialect_readout();
211
212        let mut sorted = readout.clone();
213        sorted.sort();
214
215        assert_eq!(readout, sorted);
216    }
217
218    #[test]
219    fn config_section_format() {
220        assert_eq!(
221            DialectKind::Snowflake.config_section(),
222            "[sqruff:dialect:snowflake]"
223        );
224        assert_eq!(
225            DialectKind::Bigquery.config_section(),
226            "[sqruff:dialect:bigquery]"
227        );
228        assert_eq!(DialectKind::Ansi.config_section(), "[sqruff:dialect:ansi]");
229    }
230}