Skip to main content

rigsql_dialects/
lib.rs

1use rigsql_lexer::LexerConfig;
2use rigsql_parser::{AnsiGrammar, Parser, PostgresGrammar, 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 => Parser::new(self.lexer_config(), Box::new(AnsiGrammar)),
24            DialectKind::Postgres => Parser::new(self.lexer_config(), Box::new(PostgresGrammar)),
25            DialectKind::Tsql => Parser::new(self.lexer_config(), Box::new(TsqlGrammar)),
26        }
27    }
28
29    /// Get the lexer configuration for this dialect.
30    pub fn lexer_config(self) -> LexerConfig {
31        match self {
32            DialectKind::Ansi => LexerConfig::ansi(),
33            DialectKind::Postgres => LexerConfig::postgres(),
34            DialectKind::Tsql => LexerConfig::tsql(),
35        }
36    }
37
38    /// Returns the dialect name as a static string slice.
39    pub fn as_str(self) -> &'static str {
40        match self {
41            DialectKind::Ansi => "ansi",
42            DialectKind::Postgres => "postgres",
43            DialectKind::Tsql => "tsql",
44        }
45    }
46}