Become a sponsor

本章概要
表结构解析引擎的工作原理,从数据库读取表元数据并智能推断字段用途的过程。
generator.py 中的 TableSchemaParser 类是代码生成器的解析层,负责从数据库 information_schema 读取表元数据,输出标准化的配置字典。
from generator import TableSchemaParser
parser = TableSchemaParser("django_example")
config = parser.parse()| 参数 | 说明 |
|---|---|
table_name | 数据库表名,可带前缀(django_example)或不带前缀(example) |
构造时自动处理:
django_ 前缀(前缀由 config/env.py 中的 DATABASE_PREFIX 配置)id / create_user / create_time / update_user / update_time / is_delete)parse() 方法执行 9 步:
1. 获取表信息(TABLE_COMMENT、ENGINE 等)
2. 获取字段列表(排除系统字段)
3. 获取索引信息
4. 清理表名(去掉 sys_/tb_/django_/auth_/captcha_ 前缀)
5. 构建命名(app_name / module_comment / model_class_name / route_prefix)
6. 构建字段配置列表
7. 判断功能标识(has_sort / has_status / has_unique_code)
8. 获取显示字段(优先 name -> title -> 第一个字符串字段)
9. 组装完整配置字典字段名包含以下关键词时,is_image 标记为 True:
avatar / cover / logo / image / photo / picture / img / icon / thumbnail / banner效果:
filterable=False、searchable=FalseTextField 类型字段且注释包含以下关键词时,is_rich_text 标记为 True:
内容 / 介绍 / 说明 / 描述 / 详情 / 正文效果:
字段名包含以下关键词时,识别为状态字段:
status / state / is_active / is_enabled / is_deleted效果:
/status)has_status=True字段名包含以下关键词时,识别为排序字段:
sort / order / seq / sequence / priority效果:
has_sort=True字段注释包含 数字-文本 格式(如 1-正常 2-停用)时,自动解析为选项列表:
# 注释:"案例类型:1-类型1 2-类型2 3-类型3 4-类型4"
# 解析结果:
choices = [(1, '类型1'), (2, '类型2'), (3, '类型3'), (4, '类型4')]
min_value = 1
max_value = 4效果:
filter_type='精确匹配'字段满足以下任一条件时,filterable 标记为 True:
name / title / code / mobile / phone / email / username / type / category / status / dept_id / role_id 等查询类型判断:
模糊查询(LIKE)精确匹配(=)精确匹配(=)精确匹配(=)| 数据库类型 | Django 字段类型 |
|---|---|
| varchar / char | CharField |
| text / longtext | TextField |
| int / tinyint / smallint | IntegerField |
| bigint | IntegerField |
| datetime / timestamp | DateTimeField |
| date | DateField |
| float / double | FloatField |
| decimal | DecimalField |
| boolean | BooleanField |
| Django 类型 | 中文 |
|---|---|
| CharField | 字符串 |
| TextField | 长文本 |
| IntegerField | 整数 |
| DateTimeField | 日期时间 |
| DateField | 日期 |
| FloatField | 浮点数 |
| DecimalField | 小数 |
| BooleanField | 布尔值 |
parse() 返回的配置字典结构:
{
"app_name": "example", # 模块名(去前缀小写)
"module_comment": "案例", # 模块中文名(表注释去掉"表"字)
"model_name": "example", # 模型名
"model_class_name": "Example", # 模型类名(帕斯卡命名)
"route_prefix": "example", # 路由前缀(下划线转斜杠)
"permission_prefix": "sys:example", # 权限前缀
"primary_key": "id", # 主键字段
"display_field": "name", # 显示字段
"display_field_camel": "name", # 显示字段驼峰
"table_name": "example", # 表名(去前缀)
"table_prefix": ["django_"], # 表前缀列表
"remove_prefix": True, # 是否移除前缀
"menu_group": "tool", # 菜单分组
"parent_menu_id": 322, # 父级菜单ID
"menu_sort": 5, # 菜单排序
"has_sort": True, # 有排序字段
"has_status": True, # 有状态字段
"has_unique_code": False, # 有唯一编码字段
"has_export": False, # 有导出功能
"has_image_field": True, # 有图片字段
"has_rich_text_field": False, # 有富文本字段
"fields": [ # 字段配置列表
{
"name": "name",
"comment": "案例名称",
"purpose": "存储案例名称信息",
"type": "CharField",
"type_display": "字符串",
"max_length": 100,
"nullable": False,
"required": True,
"is_unique": False,
"db_index": True,
"default": None,
"in_form": True,
"in_list": True,
"filterable": True,
"filter_type": "模糊查询",
"editable": True,
"searchable": True,
"is_image": False,
"is_rich_text": False,
},
# ... 更多字段
]
}generator.py generator_config.py
┌─────────────────────┐ ┌─────────────────────┐
│ TableSchemaParser │ │ CodeGenerator │
│ │ │ │
│ information_schema │ config │ Jinja2 模板渲染 │
│ ─────────────────> │ ────> │ ─────────────────> │
│ 解析表结构 │ 字典 │ 生成文件 │
└─────────────────────┘ └─────────────────────┘generator.py 负责"读",generator_config.py 负责"写",通过配置字典解耦。
表结构解析引擎从数据库读取表和字段的元信息(类型、长度、注释、索引等),智能推断字段用途(图片、富文本、状态、排序、枚举、查询条件等),为代码生成器提供结构化的表描述数据。