Skip to main content

Module dialect

Module dialect 

Source
Expand description

Mongo 方言 → 关系型 SQL 翻译层(纯逻辑,无 IO)

背景:core 按约定继续产出 Mongo 命令 JSON(find/aggregate/countDocuments/…)。 本模块提供一条纯函数翻译通道,把这些命令落成 MySQL / PostgreSQL / SQLite 各后端的 参数化 SQL 语句(ir::SqlStmt),并负责把驱动返回的平铺 JOIN 行还原为嵌套 Mongo 文档(row::restore_rows)。

设计边界(对齐「用 MongoDB 方言,其他数据库适配」的要求):

  1. translate::translate 只做 JSON → SQL 中间表示,完全不触数据库;Host 拿到 SqlStmt 后自行绑定驱动。
  2. 关系模型采用严格关系范式:标量字段落在单表,object/array 展平为附属表, 关系用 SQL JOIN 解析。
  3. 无法安全翻译的组合输出 _unsupported 标志 + warning,绝不生成错误 SQL。
  4. 平铺行 → 嵌套文档的还原与 introspection 行 → schemaJSON 的映射都是纯逻辑,四侧对拍可复现。

Re-exports§

pub use introspect::introspect_to_schema_json;
pub use introspect::schema_def_from_rows;
pub use overlay::merge_schema;
pub use raw::compile_raw_stmt;
pub use raw::RawStmt;
pub use row::restore_rows_json;
pub use translate::translate;

Modules§

filter
Mongo filter → WHERE 子句(参数化)
introspect
introspection 行 JSON → schemaJSON(纯映射,可喂 Registry::register)
ir
SQL 中间表示(IR)
overlay
introspection 结果 ↔ 本地覆盖 合并(permission + compute)
raw
原生 SQL 语句编译:命名占位符(:name)→ 方言占位符 + 读写推断(纯逻辑,无 IO)。
row
平铺 JOIN 行 → 嵌套 Mongo 文档还原
select
SELECT 翻译:find / countDocuments / aggregate(含 $lookup → JOIN)
translate
翻译入口:Mongo 命令 → 各后端 SqlStmt 列表
write
写语句翻译:insertOne / insertMany / updateMany / findOneAndUpdate / deleteMany

Enums§

Backend
支持的数据库后端
ColumnRef
字段 → 列引用(见 [field_column_ref]):区分标量列 / JSON 整列 / JSON 点号路径。
JsonKind
JSON 整列的形态(JsonKind):决定 U1/U2 整值条件的翻译方式