自定义方言
启用相应 feature 时,SqliteDialect 和 PostgresDialect 使用 afastorm 的 SQLx 原生连接池。
自定义 Dialect 可配合 Orm::connect_with_dialect 使用,连接经由 SQLx Any;目标数据库
必须受 SQLx Any 支持。
use afastorm::{Dialect, FieldType, Orm, Result, SqliteDialect};
#[derive(Clone, Copy)]
struct CustomSqliteDialect;
impl Dialect for CustomSqliteDialect {
fn name(&self) -> &'static str { "sqlite" }
fn quote_identifier(&self, identifier: &str) -> Result<String> {
SqliteDialect.quote_identifier(identifier)
}
fn placeholder(&self, index: usize) -> String {
SqliteDialect.placeholder(index)
}
fn column_type(&self, field_type: FieldType) -> &'static str {
SqliteDialect.column_type(field_type)
}
fn auto_increment_primary_key(&self) -> &'static str {
SqliteDialect.auto_increment_primary_key()
}
fn boolean_default(&self, value: bool) -> &'static str {
SqliteDialect.boolean_default(value)
}
fn current_value(&self, field_type: FieldType) -> &'static str {
SqliteDialect.current_value(field_type)
}
}
let orm = Orm::connect_with_dialect("sqlite::memory:", CustomSqliteDialect, 1).await?;
示例委托 SQLite SQL 语法并通过 SQLx Any 连接;它用于展示扩展入口,并未新增数据库后端。
该 trait 控制标识符引用、占位符、列类型、自增主键、布尔默认值和当前值表达式。默认字段映射
使用 column_type;PostgreSQL 数组/范围的字段级 schema 元数据由内置 PostgreSQL 方言处理。
SQLx Any 存在驱动和类型编码限制。请确认 URL scheme、SQL 语法、RETURNING、事务行为和
所有模型类型均受目标 SQLx driver 支持。如果模型使用 auto_create、auto_update 或软删除,
current_value 必须返回类型兼容的表达式。自定义方言不会自动增加自定义模型字段编解码器,
详见模型。