Skip to main content

rigsql_dialects/
lib.rs

1use rigsql_lexer::LexerConfig;
2use rigsql_parser::{AnsiGrammar, Parser, TsqlGrammar};
3use strum::{Display, EnumString};
4
5/// Supported SQL dialects.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display, EnumString)]
7#[strum(ascii_case_insensitive)]
8#[derive(Default)]
9pub enum DialectKind {
10    #[strum(serialize = "ansi")]
11    #[default]
12    Ansi,
13    #[strum(serialize = "postgres", serialize = "postgresql")]
14    Postgres,
15    #[strum(serialize = "tsql", serialize = "sqlserver")]
16    Tsql,
17}
18
19impl DialectKind {
20    /// Create a parser configured for this dialect.
21    pub fn parser(self) -> Parser {
22        match self {
23            DialectKind::Ansi | DialectKind::Postgres => {
24                Parser::new(self.lexer_config(), Box::new(AnsiGrammar))
25            }
26            DialectKind::Tsql => Parser::new(self.lexer_config(), Box::new(TsqlGrammar)),
27        }
28    }
29
30    /// Get the lexer configuration for this dialect.
31    pub fn lexer_config(self) -> LexerConfig {
32        match self {
33            DialectKind::Ansi => LexerConfig::ansi(),
34            DialectKind::Postgres => LexerConfig::postgres(),
35            DialectKind::Tsql => LexerConfig::tsql(),
36        }
37    }
38
39    /// Returns the dialect name as a static string slice.
40    pub fn as_str(self) -> &'static str {
41        match self {
42            DialectKind::Ansi => "ansi",
43            DialectKind::Postgres => "postgres",
44            DialectKind::Tsql => "tsql",
45        }
46    }
47}