Skip to main content

alun_db/
dialect.rs

1/// 数据库方言枚举
2///
3/// 封装不同数据库的 SQL 语法差异,如参数占位符风格和标识符引号。
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Dialect {
6    /// PostgreSQL(参数:`$1`,引号:`""`)
7    Postgres,
8    /// MySQL(参数:`?`,引号:`` ` ` ``)
9    Mysql,
10    /// SQLite(参数:`?`,引号:`""`)
11    Sqlite,
12}
13
14impl Dialect {
15    /// 参数占位符风格
16    pub fn placeholder(&self, index: usize) -> String {
17        match self {
18            Dialect::Postgres => format!("${}", index),
19            Dialect::Mysql => "?".to_string(),
20            Dialect::Sqlite => "?".to_string(),
21        }
22    }
23
24    /// 引用标识符(表名/列名)
25    pub fn quote(&self, ident: &str) -> String {
26        match self {
27            Dialect::Postgres => format!("\"{}\"", ident),
28            Dialect::Mysql => format!("`{}`", ident),
29            Dialect::Sqlite => format!("\"{}\"", ident),
30        }
31    }
32}