pub trait Dialect: Send + Sync {
Show 19 methods
// Required methods
fn db_type(&self) -> DbType;
fn quote(&self, identifier: &str) -> String;
fn escape_string(&self, s: &str) -> String;
fn supports_returning(&self) -> bool;
fn build_pagination(&self, sql: &str, page: u64, limit: u64) -> String;
fn json_type(&self) -> &'static str;
fn json_extract(&self, column: &str, path: &str) -> String;
fn full_text_search(&self, columns: &[&str], keyword: &str) -> String;
fn bool_to_int(&self, expr: &str) -> String;
fn concat(&self, parts: &[&str]) -> String;
fn supports_if_exists(&self) -> bool;
fn supports_if_not_exists(&self) -> bool;
fn auto_increment_keyword(&self) -> &'static str;
fn last_insert_id_sql(&self) -> Option<&'static str>;
fn build_create_table(&self, table: &str, columns: &[ColumnDef]) -> String;
fn build_alter_table(&self, table: &str, changes: &[TableChange]) -> String;
// Provided methods
fn quote_checked(&self, identifier: &str) -> Result<String, DbError> { ... }
fn build_drop_table(&self, table: &str, if_exists: bool) -> String { ... }
fn build_upsert_on_conflict(
&self,
conflict_columns: &[&str],
update_columns: &[&str],
all_columns: &[String],
) -> Option<String> { ... }
}Expand description
数据库方言 trait
实现者负责处理各数据库特有的 SQL 语法差异
Required Methods§
Sourcefn escape_string(&self, s: &str) -> String
fn escape_string(&self, s: &str) -> String
转义字符串字面量,确保可安全嵌入 SQL
Sourcefn supports_returning(&self) -> bool
fn supports_returning(&self) -> bool
该方言是否支持 RETURNING 子句
Sourcefn json_extract(&self, column: &str, path: &str) -> String
fn json_extract(&self, column: &str, path: &str) -> String
生成 JSON_EXTRACT 函数调用
Sourcefn full_text_search(&self, columns: &[&str], keyword: &str) -> String
fn full_text_search(&self, columns: &[&str], keyword: &str) -> String
生成全文检索 SQL
Sourcefn bool_to_int(&self, expr: &str) -> String
fn bool_to_int(&self, expr: &str) -> String
将布尔表达式转换为整型存储
Sourcefn supports_if_exists(&self) -> bool
fn supports_if_exists(&self) -> bool
该方言是否支持 IF EXISTS
Sourcefn supports_if_not_exists(&self) -> bool
fn supports_if_not_exists(&self) -> bool
该方言是否支持 IF NOT EXISTS
Sourcefn auto_increment_keyword(&self) -> &'static str
fn auto_increment_keyword(&self) -> &'static str
获取自增列关键字
Sourcefn last_insert_id_sql(&self) -> Option<&'static str>
fn last_insert_id_sql(&self) -> Option<&'static str>
获取最近插入 ID 的 SQL(独立可执行语句)
返回 None 表示该方言不支持以独立 SQL 获取最后插入 ID(如 Oracle 只能通过
在 INSERT 语句末尾附加 RETURNING ... INTO :bind 子句的方式获取,无法独立执行)。
调用方在拿到 None 时必须改用 supports_returning() + 在 INSERT 后追加 RETURNING。
Sourcefn build_create_table(&self, table: &str, columns: &[ColumnDef]) -> String
fn build_create_table(&self, table: &str, columns: &[ColumnDef]) -> String
生成 CREATE TABLE 语句
Sourcefn build_alter_table(&self, table: &str, changes: &[TableChange]) -> String
fn build_alter_table(&self, table: &str, changes: &[TableChange]) -> String
生成 ALTER TABLE 语句
Provided Methods§
Sourcefn quote_checked(&self, identifier: &str) -> Result<String, DbError>
fn quote_checked(&self, identifier: &str) -> Result<String, DbError>
L-4 修复:带校验的引用标识符
与 quote() 不同,此方法会先校验标识符:
- 非空
- 长度 ≤
MAX_IDENTIFIER_LEN(63 chars) - 不含 SQL 元字符(引号、分号、空格、注释等)
校验失败时返回 DbError::InvalidInput。
建议在调用方不可信的场景(如用户输入的表名/列名)使用此方法替代 quote()。
Sourcefn build_drop_table(&self, table: &str, if_exists: bool) -> String
fn build_drop_table(&self, table: &str, if_exists: bool) -> String
生成 DROP TABLE 语句
默认实现生成标准 DROP TABLE [IF EXISTS] <table> 语法。
不支持 IF EXISTS 的方言(如 DB2)应覆盖此方法。
Sourcefn build_upsert_on_conflict(
&self,
conflict_columns: &[&str],
update_columns: &[&str],
all_columns: &[String],
) -> Option<String>
fn build_upsert_on_conflict( &self, conflict_columns: &[&str], update_columns: &[&str], all_columns: &[String], ) -> Option<String>
P2-6:生成批量 upsert 的冲突处理子句(ON CONFLICT / ON DUPLICATE KEY UPDATE)
参数(均为原始列名,由各方言自行 quote):
conflict_columns: 冲突检测列(唯一键/主键),用于ON CONFLICT (cols)或ON DUPLICATE KEYupdate_columns: 冲突时需要更新的列;空切片表示更新所有all_columns中非冲突列all_columns: 本次 INSERT 的全部列名(原始未 quote),用于确定“更新所有非冲突列“的范围
返回 None 表示该方言不支持 upsert(如 ClickHouse、Db2)。
返回 Some(clause) 表示完整的冲突处理子句(不含前导空格),如:
- MySQL:
ON DUPLICATE KEY UPDATE \c1`=VALUES(`c1`), `c2`=VALUES(`c2`)` - PG/SQLite:
ON CONFLICT ("c1") DO UPDATE SET "c2"=EXCLUDED."c2"
L3 实现深度:返回的子句使用参数化占位符(VALUES(col) 或 EXCLUDED.col),
不拼接用户值,杜绝 SQL 注入。
Trait Implementations§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".