Files
2026-10-20 21:53:26 +08:00

98 lines
3.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 10.4 表结构解析引擎
:::tip 概述
解析引擎负责把数据库表翻译为生成元数据:表注释(MySQL information_schema / 非 MySQL 降级)、字段类型映射、注释智能解析(枚举/标签/图片/富文本识别)、字段分桶(列表/表单/筛选)。
:::
## 元数据来源
```text
MySQL:查 information_schema.TABLES
(表注释/引擎/行数/自增/排序规则/创建时间)
非 MySQL:gdb 元数据降级(仅表名,注释留空,模块名回退表名)
字段元数据:g.DB().TableFields(tableName)
按元数据 Index 还原物理列序
```
::: tip 生成建议
非 MySQL 库内省不到注释与枚举——**建议在 MySQL 上生成**,或生成后人工补注释再重新生成。
:::
## 类型映射(buildField,正则按 MySQL 类型串推断)
```text
tinyint(1) → BooleanField
int 族 → IntegerField
decimal/numeric → DecimalField
float/double → FloatField
date → DateField
datetime/timestamp → DateTimeField
blob/binary → BinaryField
text 族 → TextField
varchar/char/enum → CharField
Go 类型:bigint→int64;decimal/float→float64;时间列→*gtime.Time;
int 族→int;其余 string
行取值方法:.Int() / .Float64() / .String()
```
## 注释智能解析
```text
枚举:"状态:1-正常 2-禁用"(reChoice 支持 - — - = 分隔)
→ Choices 集合,标签类型 1→success 2→danger 3→warning 其余 info
表单标签:cleanLabel 去掉枚举尾巴保留中文名("状态")
图片字段:注释含 图片/头像/封面
或列名含 avatar/cover/image/icon/photo
富文本: 注释含 富文本
或 TextField 且列名含 content/body/editor
```
图片/富文本识别驱动 FileFields/ContentFields 声明与前端组件选型。
## 字段分桶(buildVars)
```text
审计列排除:
id 进列表(树形/分页需要 Id)
create_user/create_time 仅标记 HasCreateUser/HasCreateTime
update_user/update_time/del_flag/tenant_id 不进前后端字段
salt 不进表单
列表字段:排除富文本/password/salt/BinaryField
表单字段:全部业务列(BinaryField 除外)
筛选字段:CharField 非图片非 URL 列(website/site/url/link 不作模糊条件);
带枚举的 Integer/Boolean → Select;*_id 外键 Integer → InputNumber
显示字段(模糊搜索列)优先级:
name/title/label/username/realname/nickname/category_name/
job_name/dept_name/role_name/position_name/level_name
未命中取列表首列
```
## 命名推导
```text
moduleName = 表名去 goframe_ 前缀(config_item)
Pascal = 结构体类名(ConfigItem)
camelLower = 前端模块名(configItem)
权限前缀 = sys:{camelLower}(sys:configItem)
路由前缀 = moduleName(snake)
域归属 = genDomainMap(未匹配默认 tool)
模板类型 = 有 parent_id/pid 列 → ui2 树形,否则 ui 普通
```
## 建表侧的配合
解析质量取决于建表规范:
```text
1. 列注释按枚举规范写("状态:1-正常 2-禁用")→ 字典自动创建
2. 图片列注释含"图片"或用 avatar/cover 等列名 → FileFields 识别
3. 富文本列用 content 列名或注释"富文本" → ContentFields 识别
4. 索引 uk_* 规范命名 → 唯一性语义清晰
5. 审计列齐全(解析器自动排除)
```