Skip to main content

Dialect

Trait Dialect 

Source
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§

Source

fn db_type(&self) -> DbType

返回该方言对应的数据库类型

Source

fn quote(&self, identifier: &str) -> String

引用标识符(表名、列名等)

Source

fn escape_string(&self, s: &str) -> String

转义字符串字面量,确保可安全嵌入 SQL

Source

fn supports_returning(&self) -> bool

该方言是否支持 RETURNING 子句

Source

fn build_pagination(&self, sql: &str, page: u64, limit: u64) -> String

生成分页 SQL

Source

fn json_type(&self) -> &'static str

获取 JSON 类型的 SQL 类型名

Source

fn json_extract(&self, column: &str, path: &str) -> String

生成 JSON_EXTRACT 函数调用

生成全文检索 SQL

Source

fn bool_to_int(&self, expr: &str) -> String

将布尔表达式转换为整型存储

Source

fn concat(&self, parts: &[&str]) -> String

生成 CONCAT 函数调用

Source

fn supports_if_exists(&self) -> bool

该方言是否支持 IF EXISTS

Source

fn supports_if_not_exists(&self) -> bool

该方言是否支持 IF NOT EXISTS

Source

fn auto_increment_keyword(&self) -> &'static str

获取自增列关键字

Source

fn last_insert_id_sql(&self) -> Option<&'static str>

获取最近插入 ID 的 SQL(独立可执行语句)

返回 None 表示该方言不支持以独立 SQL 获取最后插入 ID(如 Oracle 只能通过 在 INSERT 语句末尾附加 RETURNING ... INTO :bind 子句的方式获取,无法独立执行)。 调用方在拿到 None 时必须改用 supports_returning() + 在 INSERT 后追加 RETURNING。

Source

fn build_create_table(&self, table: &str, columns: &[ColumnDef]) -> String

生成 CREATE TABLE 语句

Source

fn build_alter_table(&self, table: &str, changes: &[TableChange]) -> String

生成 ALTER TABLE 语句

Provided Methods§

Source

fn quote_checked(&self, identifier: &str) -> Result<String, DbError>

L-4 修复:带校验的引用标识符

quote() 不同,此方法会先校验标识符:

  • 非空
  • 长度 ≤ MAX_IDENTIFIER_LEN (63 chars)
  • 不含 SQL 元字符(引号、分号、空格、注释等)

校验失败时返回 DbError::InvalidInput

建议在调用方不可信的场景(如用户输入的表名/列名)使用此方法替代 quote()

Source

fn build_drop_table(&self, table: &str, if_exists: bool) -> String

生成 DROP TABLE 语句

默认实现生成标准 DROP TABLE [IF EXISTS] <table> 语法。 不支持 IF EXISTS 的方言(如 DB2)应覆盖此方法。

Source

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 KEY
  • update_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§

Source§

impl Display for dyn Dialect

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§