Skip to main content

rudb_parse/
dialect.rs

1//! SQL parser dialects installed in this build.
2//!
3//! The registry has one entry today because the vendored grammar is DuckDB's grammar. Keeping the
4//! name here rather than in the settings layer makes `current_dialect` a lookup whose behavior
5//! changes when another parser is registered, not a string special case that has to be replaced.
6
7/// One installed SQL parser dialect.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct Dialect {
10    name: &'static str,
11}
12
13impl Dialect {
14    /// The name accepted by `SET current_dialect` and listed by `duckdb_dialects()`.
15    #[must_use]
16    pub const fn name(self) -> &'static str {
17        self.name
18    }
19}
20
21/// Every SQL parser dialect installed in this build.
22pub const DIALECTS: &[Dialect] = &[Dialect { name: "duckdb" }];
23
24/// Finds an installed dialect without regard to identifier case.
25#[must_use]
26pub fn dialect_named(name: &str) -> Option<Dialect> {
27    DIALECTS.iter().copied().find(|dialect| dialect.name.eq_ignore_ascii_case(name))
28}
29
30#[cfg(test)]
31mod tests {
32    use super::{DIALECTS, dialect_named};
33
34    #[test]
35    fn the_vendored_grammar_is_the_one_registered_dialect() {
36        assert_eq!(DIALECTS.len(), 1);
37        assert_eq!(dialect_named("DUCKDB").map(|dialect| dialect.name()), Some("duckdb"));
38        assert_eq!(dialect_named("cypher"), None);
39    }
40}