refactor: 优化代码注释和文档字符串格式

style: 统一代码风格和格式

docs: 完善函数和方法的文档字符串

refactor(base_model): 移除冗余的表名和表参数生成方法

refactor(constant): 更新返回码注释格式

refactor(router_class): 添加路由处理器的详细文档

refactor(database): 完善数据库连接函数的文档

refactor(security): 添加认证类和方法的详细文档

refactor(validator): 更新验证器函数的文档格式

refactor(serialize): 优化序列化工具类的文档

refactor(response): 完善响应类的文档字符串

refactor(dependencies): 添加依赖函数的详细文档

refactor(initialize): 完善初始化脚本的文档

refactor(plugin): 添加生命周期和中间件注册的文档

refactor(service): 完善服务层方法的文档

refactor(controller): 添加控制器方法的详细文档

refactor(crud): 完善CRUD操作的文档字符串

refactor(schema): 简化模型类并移除冗余字段

refactor(param): 更新查询参数类的注释格式

refactor(template): 优化代码生成模板的格式

refactor(console): 添加控制台输出功能的实现

refactor(util): 完善工具函数的文档字符串
This commit is contained in:
zhangtao
2025-10-18 16:31:28 +08:00
parent 0ea88c3320
commit c2ca6d19ac
101 changed files with 6914 additions and 2295 deletions
@@ -27,6 +27,17 @@ async def gen_table_list_controller(
search: GenTableQueryParam = Depends(),
auth: AuthSchema = Depends(AuthPermission(["generator:gencode:query"]))
) -> JSONResponse:
"""
查询代码生成业务表列表
参数:
- page (PaginationQueryParam): 分页查询参数
- search (GenTableQueryParam): 搜索参数
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含查询结果和分页信息的JSON响应
"""
result_dict_list = await GenTableService.get_gen_table_list_service(auth=auth, search=search)
result_dict = await PaginationService.paginate(data_list=result_dict_list, page_no=page.page_no, page_size=page.page_size)
logger.info('获取代码生成业务表列表成功')
@@ -39,6 +50,17 @@ async def get_gen_db_table_list_controller(
search: GenTableQueryParam = Depends(),
auth: AuthSchema = Depends(AuthPermission(["generator:dblist:query"]))
) -> JSONResponse:
"""
查询数据库表列表
参数:
- page (PaginationQueryParam): 分页查询参数
- search (GenTableQueryParam): 搜索参数
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含查询结果和分页信息的JSON响应
"""
result_dict_list = await GenTableService.get_gen_db_table_list_service(auth=auth, search=search, order_by=page.order_by)
result_dict = await PaginationService.paginate(data_list=result_dict_list, page_no=page.page_no, page_size=page.page_size)
logger.info('获取数据库表列表成功')
@@ -50,6 +72,16 @@ async def import_gen_table_controller(
table_names: List[str] = Body(..., description="表名列表"),
auth: AuthSchema = Depends(AuthPermission(["generator:gencode:import"])),
) -> JSONResponse:
"""
导入表结构
参数:
- table_names (List[str]): 表名列表
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含导入结果和导入的表结构列表的JSON响应
"""
add_gen_table_list = await GenTableService.get_gen_db_table_list_by_name_service(auth, table_names)
result = await GenTableService.import_gen_table_service(auth, add_gen_table_list)
logger.info('导入表结构成功')
@@ -61,6 +93,16 @@ async def gen_table_detail_controller(
table_id: int = Path(..., description="业务表ID"),
auth: AuthSchema = Depends(AuthPermission(["generator:gencode:query"]))
) -> JSONResponse:
"""
获取业务表详细信息
参数:
- table_id (int): 业务表ID
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含业务表详细信息的JSON响应
"""
gen_table = await GenTableService.get_gen_table_by_id_service(auth, table_id)
gen_tables = await GenTableService.get_gen_table_all_service(auth)
gen_table_detail_result = dict(info=gen_table.model_dump(), rows=gen_table.model_dump()['columns'], tables=[gen_table.model_dump() for gen_table in gen_tables])
@@ -73,6 +115,16 @@ async def create_table_controller(
sql: str = Body(..., description="SQL语句:CREATE TABLE user_demo (\n id INTEGER NOT NULL PRIMARY KEY,\n username VARCHAR(64) NOT NULL UNIQUE,\n);"),
auth: AuthSchema = Depends(AuthPermission(["generator:gencode:create"])),
) -> JSONResponse:
"""
创建表结构
参数:
- sql (str): SQL语句,用于创建表结构
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含创建结果的JSON响应
"""
result = await GenTableService.create_table_service(auth, sql)
logger.info('创建表结构成功')
return SuccessResponse(msg="创建表结构成功", data=result)
@@ -84,6 +136,17 @@ async def update_gen_table_controller(
data: GenTableSchema = Body(..., description="业务表信息"),
auth: AuthSchema = Depends(AuthPermission(["generator:gencode:update"])),
) -> JSONResponse:
"""
编辑业务表信息
参数:
- table_id (int): 业务表ID
- data (GenTableSchema): 业务表信息模型
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含编辑结果的JSON响应
"""
await GenTableService.validate_edit(data)
result_dict = await GenTableService.update_gen_table_service(auth, data, table_id)
logger.info('编辑业务表信息成功')
@@ -95,6 +158,16 @@ async def delete_gen_table_controller(
ids: List[int] = Body(..., description="业务表ID列表"),
auth: AuthSchema = Depends(AuthPermission(["generator:gencode:delete"]))
) -> JSONResponse:
"""
删除业务表信息
参数:
- ids (List[int]): 业务表ID列表
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含删除结果的JSON响应
"""
result = await GenTableService.delete_gen_table_service(auth, ids)
logger.info('删除业务表信息成功')
return SuccessResponse(msg="删除业务表信息成功", data=result)
@@ -105,6 +178,16 @@ async def batch_gen_code_controller(
table_names: List[str] = Body(..., description="表名列表"),
auth: AuthSchema = Depends(AuthPermission(["generator:gencode:operate"]))
) -> StreamResponse:
"""
批量生成代码
参数:
- table_names (List[str]): 表名列表
- auth (AuthSchema): 认证信息模型
返回:
- StreamResponse: 包含批量生成代码的ZIP文件流响应
"""
# 检查table_names是否为空
if not table_names:
logger.error('表名列表不能为空')
@@ -129,6 +212,16 @@ async def gen_code_local_controller(
table_name: str = Path(..., description="表名"),
auth: AuthSchema = Depends(AuthPermission(["generator:gencode:code"]))
) -> JSONResponse:
"""
生成代码到指定路径
参数:
- table_name (str): 表名
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含生成结果的JSON响应
"""
from app.config.setting import settings
if not settings.allow_overwrite:
logger.error('【系统预设】不允许生成文件覆盖到本地')
@@ -143,6 +236,16 @@ async def preview_code_controller(
table_id: int = Path(..., description="业务表ID"),
auth: AuthSchema = Depends(AuthPermission(["generator:gencode:query"]))
) -> JSONResponse:
"""
预览代码
参数:
- table_id (int): 业务表ID
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含预览代码的JSON响应
"""
preview_code_result = await GenTableService.preview_code_service(auth, table_id)
logger.info('预览代码成功')
return SuccessResponse(data=preview_code_result, msg="预览代码成功")
@@ -153,6 +256,16 @@ async def sync_db_controller(
table_name: str = Path(..., description="表名"),
auth: AuthSchema = Depends(AuthPermission(["generator:db:sync"]))
) -> JSONResponse:
"""
同步数据库
参数:
- table_name (str): 表名
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含同步数据库结果的JSON响应
"""
result = await GenTableService.sync_db_service(auth, table_name)
logger.info('同步数据库成功')
return SuccessResponse(msg="同步数据库成功", data=result)
@@ -26,15 +26,23 @@ class GenTableCRUD(CRUDBase[GenTableModel, GenTableSchema, GenTableSchema]):
"""代码生成业务表模块数据库操作层"""
def __init__(self, auth: AuthSchema) -> None:
"""初始化CRUD"""
"""
初始化CRUD操作层
参数:
- auth (AuthSchema): 认证信息模型
"""
super().__init__(model=GenTableModel, auth=auth)
async def get_gen_table_by_id(self, table_id: int) -> Optional[GenTableModel]:
"""
根据业务表id获取需要生成的业务表信息
根据业务表ID获取需要生成的业务表信息
:param table_id: 业务表id
:return: 需要生成的业务表信息对象
参数:
- table_id (int): 业务表ID。
返回:
- GenTableModel | None: 业务表信息对象。
"""
gen_table = (
(
@@ -52,10 +60,13 @@ class GenTableCRUD(CRUDBase[GenTableModel, GenTableSchema, GenTableSchema]):
async def get_gen_table_by_name(self, table_name: str) -> Optional[GenTableModel]:
"""
根据业务表名称获取需要生成的业务表信息
根据业务表名称获取需要生成的业务表信息
:param table_name: 业务表名称
:return: 需要生成的业务表信息对象
参数:
- table_name (str): 业务表名称。
返回:
- GenTableModel | None: 业务表信息对象。
"""
gen_table = (
(
@@ -73,9 +84,10 @@ class GenTableCRUD(CRUDBase[GenTableModel, GenTableSchema, GenTableSchema]):
async def get_gen_table_all(self) -> Sequence[GenTableModel]:
"""
获取所有业务表信息
获取所有业务表信息
:return: 所有业务表信息列表
返回:
- Sequence[GenTableModel]: 所有业务表信息列表。
"""
gen_table_all = (
await self.db.execute(
@@ -88,10 +100,13 @@ class GenTableCRUD(CRUDBase[GenTableModel, GenTableSchema, GenTableSchema]):
async def get_gen_table_list(self, search: Optional[GenTableQueryParam] = None) -> Sequence[GenTableModel]:
"""
根据查询参数获取代码生成业务表列表信息
根据查询参数获取代码生成业务表列表信息
:param search: 查询参数对象
:return: 代码生成业务表列表信息对象
参数:
- search (GenTableQueryParam | None): 查询参数对象
返回:
- Sequence[GenTableModel]: 业务表列表信息。
"""
# 获取所有数据
result = await self.db.execute(
@@ -110,7 +125,13 @@ class GenTableCRUD(CRUDBase[GenTableModel, GenTableSchema, GenTableSchema]):
async def add_gen_table(self, add_model: GenTableSchema) -> GenTableModel:
"""
增加
新增业务表信息。
参数:
- add_model (GenTableSchema): 新增业务表信息模型。
返回:
- GenTableModel: 新增的业务表信息对象。
"""
gen_table = GenTableModel(
**add_model.model_dump(exclude_unset=True, exclude={"sub", "tree", "crud"})
@@ -121,7 +142,14 @@ class GenTableCRUD(CRUDBase[GenTableModel, GenTableSchema, GenTableSchema]):
async def edit_gen_table(self, table_id: int, edit_model: GenTableSchema) -> GenTableSchema:
"""
修改
修改业务表信息。
参数:
- table_id (int): 业务表ID。
- edit_model (GenTableSchema): 修改业务表信息模型。
返回:
- GenTableSchema: 修改后的业务表信息模型。
"""
edit_dict_data = edit_model.model_dump(exclude_unset=True)
await self.db.execute(
@@ -135,7 +163,10 @@ class GenTableCRUD(CRUDBase[GenTableModel, GenTableSchema, GenTableSchema]):
async def delete_gen_table(self, ids: List[int]) -> None:
"""
删除
删除业务表信息。除了系统表。
参数:
- ids (List[int]): 业务表ID列表。
"""
await self.db.execute(
delete(GenTableModel)
@@ -145,10 +176,13 @@ class GenTableCRUD(CRUDBase[GenTableModel, GenTableSchema, GenTableSchema]):
async def get_db_table_list(self, search: Optional[GenTableQueryParam] = None) -> list[Dict]:
"""
根据查询参数获取数据库列表信息
根据查询参数获取数据库列表信息
:param search: 查询参数对象
:return: 数据库列表信息对象
参数:
- search (GenTableQueryParam | None): 查询参数对象
返回:
- list[Dict]: 数据库表列表信息(已转为可序列化字典)。
"""
# 使用更健壮的方式检测数据库方言
@@ -241,10 +275,13 @@ class GenTableCRUD(CRUDBase[GenTableModel, GenTableSchema, GenTableSchema]):
async def get_db_table_list_by_names(self, table_names: List[str]) -> list[GenDBTableSchema]:
"""
根据业务表名称获取数据库表信息
根据业务表名称列表获取数据库表信息
:param table_names: 业务表名称组
:return: 数据库列表信息对象
参数:
- table_names (List[str]): 业务表名称列表。
返回:
- list[GenDBTableSchema]: 数据库表信息对象列表。
"""
# 使用更健壮的方式检测数据库方言
if settings.DATABASE_TYPE == "postgresql":
@@ -330,11 +367,13 @@ class GenTableCRUD(CRUDBase[GenTableModel, GenTableSchema, GenTableSchema]):
async def create_table_by_sql(self, sql: str) -> bool:
"""
根据sql语句创建表结构
根据SQL语句创建表结构
:param db: orm对象
:param sql: sql语句
:return:
参数:
- sql (str): 创建表的SQL语句
返回:
- bool: 是否创建成功。
"""
try:
await self.db.execute(text(sql))
@@ -353,27 +392,57 @@ class GenTableColumnCRUD(CRUDBase[GenTableColumnModel, GenTableColumnSchema, Gen
"""代码生成业务表字段模块数据库操作层"""
def __init__(self, auth: AuthSchema) -> None:
"""初始化CRUD"""
"""
初始化CRUD操作层
参数:
- auth (AuthSchema): 认证信息模型
"""
super().__init__(model=GenTableColumnModel, auth=auth)
async def get_gen_table_column_by_id(self, id: int) -> Optional[GenTableColumnModel]:
"""根据业务表字段ID获取业务表字段信息"""
"""根据业务表字段ID获取业务表字段信息
参数:
- id (int): 业务表字段ID。
返回:
- Optional[GenTableColumnModel]: 业务表字段信息对象。
"""
return await self.get(id=id)
async def get_gen_table_column_list_by_table_id(self, table_id: int) -> Optional[GenTableColumnModel]:
"""根据业务表ID获取业务表字段列表信息"""
"""根据业务表ID获取业务表字段列表信息
参数:
- table_id (int): 业务表ID。
返回:
- Optional[GenTableColumnModel]: 业务表字段列表信息对象。
"""
return await self.get(table_id=table_id)
async def list_gen_table_column_crud_by_table_id(self, table_id: int, order_by: Optional[List[Dict[str, str]]] = None) -> Sequence[GenTableColumnModel]:
"""根据业务表ID查询业务表字段列表"""
"""根据业务表ID查询业务表字段列表
参数:
- table_id (int): 业务表ID。
- order_by (Optional[List[Dict[str, str]]]): 排序字段列表,每个元素为{"field": "字段名", "order": "asc" | "desc"}。
返回:
- Sequence[GenTableColumnModel]: 业务表字段列表信息对象序列。
"""
return await self.list(search={"table_id": table_id}, order_by=order_by)
async def get_gen_db_table_columns_by_name(self, table_name: str | None) -> List[GenTableColumnOutSchema]:
"""
根据业务表名称获取业务表字段列表信息
根据业务表名称获取业务表字段列表信息
:param table_name: 业务表名称
:return: 业务表字段列表信息对象
参数:
- table_name (str | None): 业务表名称。
返回:
- List[GenTableColumnOutSchema]: 业务表字段列表信息对象。
"""
# 检查表名是否为空
if not table_name:
@@ -448,19 +517,49 @@ class GenTableColumnCRUD(CRUDBase[GenTableColumnModel, GenTableColumnSchema, Gen
return result
async def list_gen_table_column_crud(self, search: Optional[Dict] = None, order_by: Optional[List[Dict[str, str]]] = None) -> Sequence[GenTableColumnModel]:
"""根据业务表ID查询业务表字段列表"""
"""根据业务表字段查询业务表字段列表
参数:
- search (Optional[Dict]): 查询参数,例如{"table_id": 1}。
- order_by (Optional[List[Dict[str, str]]]): 排序字段列表,每个元素为{"field": "字段名", "order": "asc" | "desc"}。
返回:
- Sequence[GenTableColumnModel]: 业务表字段列表信息对象序列。
"""
return await self.list(search=search, order_by=order_by)
async def create_gen_table_column_crud(self, data: GenTableColumnSchema) -> Optional[GenTableColumnModel]:
"""创建业务表字段"""
"""创建业务表字段
参数:
- data (GenTableColumnSchema): 业务表字段模型。
返回:
- Optional[GenTableColumnModel]: 业务表字段列表信息对象。
"""
return await self.create(data=data)
async def update_gen_table_column_crud(self, id: int, data: GenTableColumnSchema) -> Optional[GenTableColumnModel]:
"""更新业务表字段"""
"""更新业务表字段
参数:
- id (int): 业务表字段ID。
- data (GenTableColumnSchema): 业务表字段模型。
返回:
- Optional[GenTableColumnModel]: 业务表字段列表信息对象。
"""
return await self.update(id=id, data=data)
async def delete_gen_table_column_by_table_id_dao(self, table_ids: List[int]) -> None:
"""根据业务表ID批量删除"""
"""根据业务表ID批量删除业务表字段。
参数:
- table_ids (List[int]): 业务表ID列表。
返回:
- None
"""
# 先查询出这些表ID对应的所有字段ID
query = select(GenTableColumnModel.id).where(GenTableColumnModel.table_id.in_(table_ids))
result = await self.db.execute(query)
@@ -471,5 +570,12 @@ class GenTableColumnCRUD(CRUDBase[GenTableColumnModel, GenTableColumnSchema, Gen
await self.delete(ids=column_ids)
async def delete_gen_table_column_by_column_id_dao(self, data: GenTableColumnDeleteSchema) -> None:
"""根据业务表字段ID批量删除"""
"""根据业务表字段ID批量删除业务表字段。
参数:
- data (GenTableColumnDeleteSchema): 业务表字段删除模型。
返回:
- None
"""
return await self.delete(ids=data.column_ids)
@@ -3,8 +3,6 @@
from typing import Optional
from fastapi import Query
from app.core.validator import DateTimeStr
class GenTableQueryParam:
"""代码生成业务表查询参数"""
@@ -8,7 +8,6 @@ from typing import Any, List, Dict, Literal, Optional
from sqlglot.expressions import Add, Alter, Create, Delete, Drop, Expression, Insert, Table, TruncateTable, Update
from sqlglot import parse as sqlglot_parse
from app.api.v1.module_generator.gencode.model import GenTableModel
from app.config.setting import settings
from app.core.exceptions import CustomException
from app.common.constant import GenConstant
@@ -30,7 +29,15 @@ class GenTableService:
@classmethod
async def get_gen_table_detail_service(cls, auth: AuthSchema, table_id: int) -> Dict:
"""获取业务表详细信息"""
"""获取业务表详细信息
参数:
- auth (AuthSchema): 认证信息。
- table_id (int): 业务表ID。
返回:
- Dict: 包含业务表详细信息、字段列表和所有业务表的字典。
"""
gen_table = await cls.get_gen_table_by_id_service(auth, table_id)
gen_tables = await cls.get_gen_table_all_service(auth)
gen_columns = await GenTableColumnService.get_gen_table_column_list_by_table_id_service(auth, table_id)
@@ -41,23 +48,47 @@ class GenTableService:
return dict(info=gen_table, rows=gen_columns, tables=gen_tables)
@classmethod
async def get_gen_table_list_service(
cls, auth: AuthSchema, search: GenTableQueryParam
) -> List[Dict]:
"""获取代码生成业务表列表信息"""
async def get_gen_table_list_service(cls, auth: AuthSchema, search: GenTableQueryParam) -> List[Dict]:
"""
获取代码生成业务表列表信息。
参数:
- auth (AuthSchema): 认证信息。
- search (GenTableQueryParam): 查询参数模型。
返回:
- List[Dict]: 包含业务表列表信息的字典列表。
"""
gen_table_list_result = await GenTableCRUD(auth=auth).get_gen_table_list(search)
return [GenTableOutSchema.model_validate(obj).model_dump() for obj in gen_table_list_result]
@classmethod
async def get_gen_db_table_list_service(cls, auth: AuthSchema, search: GenTableQueryParam, order_by: Optional[List[Dict[str, str]]] = None) -> list[Any]:
"""获取数据库列表信息"""
"""获取数据库列表信息
参数:
- auth (AuthSchema): 认证信息。
- search (GenTableQueryParam): 查询参数模型。
- order_by (Optional[List[Dict[str, str]]]): 排序参数列表,默认值为None。
返回:
- list[Any]: 包含数据库列表信息的任意类型列表。
"""
# 确保db是AsyncSession类型
gen_db_table_list_result = await GenTableCRUD(auth=auth).get_db_table_list(search)
return gen_db_table_list_result
@classmethod
async def get_gen_db_table_list_by_name_service(cls, auth: AuthSchema, table_names: List[str]) -> List[GenTableOutSchema]:
"""根据表名称组获取数据库列表信息"""
"""根据表名称组获取数据库列表信息
参数:
- auth (AuthSchema): 认证信息。
- table_names (List[str]): 业务表名称列表。
返回:
- List[GenTableOutSchema]: 包含业务表列表信息的模型列表。
"""
gen_db_table_list_result = await GenTableCRUD(auth=auth).get_db_table_list_by_names(table_names)
# 检查是否有未找到的表
@@ -74,10 +105,19 @@ class GenTableService:
return result
@classmethod
async def import_gen_table_service(
cls, auth: AuthSchema, gen_table_list: List[GenTableOutSchema]
) -> Literal[True] | None:
"""导入表结构"""
async def import_gen_table_service(cls, auth: AuthSchema, gen_table_list: List[GenTableOutSchema]) -> Literal[True] | None:
"""导入表结构
参数:
- auth (AuthSchema): 认证对象。
- gen_table_list (List[GenTableOutSchema]): 要导入的业务表列表。
返回:
- Literal[True] | None: 导入成功返回True,否则返回None。
异常:
- CustomException: 当没有可导入的表结构、表已存在或导入过程中发生错误时抛出。
"""
# 检查是否有表需要导入
if not gen_table_list:
raise CustomException(msg="没有可导入的表结构")
@@ -131,7 +171,18 @@ class GenTableService:
@classmethod
async def create_table_service(cls, auth: AuthSchema, sql: str) -> Literal[True] | None:
"""创建表结构"""
"""创建表结构
参数:
- auth (AuthSchema): 认证信息。
- sql (str): 包含建表SQL语句的字符串。
返回:
- Literal[True] | None: 创建成功返回True,否则返回None。
异常:
- CustomException: 当SQL语句不是合法的建表语句、创建表失败或导入表结构失败时抛出。
"""
try:
sql_statements = sqlglot_parse(sql, dialect=settings.DATABASE_TYPE)
# 校验sql语句是否为合法的建表语句
@@ -139,7 +190,9 @@ class GenTableService:
raise CustomException(msg='sql语句不是合法的建表语句')
table_names = cls.__get_table_names(sql_statements)
# 执行SQL语句创建表
await GenTableCRUD(auth=auth).create_table_by_sql(sql)
result = await GenTableCRUD(auth=auth).create_table_by_sql(sql)
if not result:
raise CustomException(msg='创建表失败,请检查SQL语句,请确保语法是否符合标准,并检查后端日志')
gen_table_list = await cls.get_gen_db_table_list_by_name_service(auth, table_names)
import_result = await cls.import_gen_table_service(auth, gen_table_list)
return import_result
@@ -149,10 +202,13 @@ class GenTableService:
@classmethod
def __is_valid_create_table(cls, sql_statements: List[Expression | None]) -> bool:
"""
校验sql语句是否为合法的建表语句
:param sql_statements: sql语句的ast列表
:return: 校验结果
校验SQL语句是否为合法的建表语句
参数:
- sql_statements (List[Expression | None]): SQL的AST列表。
返回:
- bool: 校验结果。
"""
validate_create = [isinstance(sql_statement, Create) for sql_statement in sql_statements]
validate_forbidden_keywords = [
@@ -169,10 +225,13 @@ class GenTableService:
@classmethod
def __get_table_names(cls, sql_statements: List[Expression | None]) -> List[str]:
"""
获取sql语句中所有的建表表名
:param sql_statements: sql语句的ast列表
:return: 建表表名列表
获取SQL语句中所有的建表表名
参数:
- sql_statements (List[Expression | None]): SQL的AST列表
返回:
- List[str]: 建表表名列表。
"""
table_names = []
for sql_statement in sql_statements:
@@ -184,7 +243,19 @@ class GenTableService:
@classmethod
async def update_gen_table_service(cls, auth: AuthSchema, data: GenTableSchema, table_id: int) -> Dict[str, Any]:
"""编辑业务表信息"""
"""编辑业务表信息
参数:
- auth (AuthSchema): 认证信息。
- data (GenTableSchema): 包含业务表更新信息的模型。
- table_id (int): 业务表ID。
返回:
- Dict[str, Any]: 更新后的业务表信息字典。
异常:
- CustomException: 当业务表不存在、更新失败或处理字段参数时抛出。
"""
edit_gen_table = data.model_dump(exclude_unset=True, by_alias=True)
gen_table_info = await cls.get_gen_table_by_id_service(auth, table_id)
if gen_table_info.id:
@@ -210,7 +281,18 @@ class GenTableService:
@classmethod
async def delete_gen_table_service(cls, auth: AuthSchema, ids: List[int]) -> None:
"""删除业务表信息"""
"""删除业务表信息
参数:
- auth (AuthSchema): 认证信息。
- ids (List[int]): 业务表ID列表。
返回:
- None
异常:
- CustomException: 当删除失败时抛出。
"""
try:
# 先删除相关的字段信息
await GenTableColumnCRUD(auth=auth).delete_gen_table_column_by_table_id_dao(ids)
@@ -221,7 +303,18 @@ class GenTableService:
@classmethod
async def get_gen_table_by_id_service(cls, auth: AuthSchema, table_id: int) -> GenTableOutSchema:
"""获取需要生成的业务表详细信息"""
"""获取需要生成代码的业务表详细信息
参数:
- auth (AuthSchema): 认证信息。
- table_id (int): 业务表ID。
返回:
- GenTableOutSchema: 包含业务表详细信息的模型。
异常:
- CustomException: 当业务表不存在时抛出。
"""
gen_table = await GenTableCRUD(auth=auth).get_gen_table_by_id(table_id)
if gen_table:
# 使用更直接的转换方式
@@ -257,7 +350,14 @@ class GenTableService:
@classmethod
async def get_gen_table_all_service(cls, auth: AuthSchema) -> List[GenTableOutSchema]:
"""获取所有业务表信息"""
"""获取所有业务表信息
参数:
- auth (AuthSchema): 认证信息。
返回:
- List[GenTableOutSchema]: 包含所有业务表详细信息的模型列表。
"""
gen_table_all = await GenTableCRUD(auth=auth).get_gen_table_all()
gen_table_all_dict = [GenTableOutSchema.model_validate(gen_table).model_dump() for gen_table in gen_table_all]
result = [GenTableOutSchema(**gen_table) for gen_table in gen_table_all_dict]
@@ -266,11 +366,14 @@ class GenTableService:
@classmethod
async def preview_code_service(cls, auth: AuthSchema, table_id: int) -> Dict[Any, Any]:
"""
预览代码service
:param auth: 认证对象
:param table_id: 业务表id
:return: 预览数据列表
预览代码
参数:
- auth (AuthSchema): 认证对象。
- table_id (int): 业务表ID。
返回:
- Dict[Any, Any]: 模版文件名到渲染内容的映射。
"""
gen_table = GenTableOutSchema.model_validate(
await GenTableCRUD(auth).get_gen_table_by_id(table_id)
@@ -285,19 +388,29 @@ class GenTableService:
template_list = Jinja2TemplateUtil.get_template_list(tpl_category, tpl_web_type)
preview_code_result = {}
for template in template_list:
render_content = env.get_template(template).render(**context)
render_content = await env.get_template(template).render_async(**context)
preview_code_result[template] = render_content
return preview_code_result
@classmethod
async def generate_code_service(cls, auth: AuthSchema, table_name: str) -> SuccessResponse:
"""生成代码至指定路径"""
"""生成代码至指定路径
参数:
- auth (AuthSchema): 认证对象。
- table_name (str): 业务表名称。
返回:
- SuccessResponse: 成功响应模型。
异常:
- CustomException: 当渲染模板失败时抛出。
"""
env = Jinja2TemplateInitializerUtil.init_jinja2()
render_info = await cls.__get_gen_render_info(auth, table_name)
for template in render_info[0]:
try:
render_content = env.get_template(template).render(**render_info[2])
render_content = await env.get_template(template).render_async(**render_info[2])
gen_path = cls.__get_gen_path(render_info[3], template)
if gen_path:
os.makedirs(os.path.dirname(gen_path), exist_ok=True)
@@ -311,11 +424,14 @@ class GenTableService:
@classmethod
async def batch_gen_code_service(cls, auth: AuthSchema, table_names: List[str]) -> bytes:
"""
批量生成代码service
:param auth: 认证对象
:param table_names: 业务表名称组
:return: 下载代码结果
批量生成代码并打包为ZIP。
参数:
- auth (AuthSchema): 认证对象。
- table_names (List[str]): 业务表名称组。
返回:
- bytes: 下载代码的ZIP二进制数据。
"""
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
@@ -323,7 +439,7 @@ class GenTableService:
env = Jinja2TemplateInitializerUtil.init_jinja2()
render_info = await cls.__get_gen_render_info(auth, table_name)
for template_file, output_file in zip(render_info[0], render_info[1]):
render_content = env.get_template(template_file).render(**render_info[2])
render_content = await env.get_template(template_file).render_async(**render_info[2])
zip_file.writestr(output_file, render_content)
zip_data = zip_buffer.getvalue()
@@ -332,67 +448,106 @@ class GenTableService:
@classmethod
async def sync_db_service(cls, auth: AuthSchema, table_name: str) -> None:
"""同步数据库"""
"""同步数据库表结构。
参数:
- auth (AuthSchema): 认证对象。
- table_name (str): 业务表名称。
返回:
- None
异常:
- CustomException: 当业务表不存在时抛出。
"""
gen_table = await GenTableCRUD(auth).get_gen_table_by_name(table_name)
if not gen_table:
raise CustomException(msg='业务表不存在')
table = GenTableSchema.model_validate(gen_table)
# 处理table.columns为None的情况
# 关键修复:确保 table.table_id 正确设置为持久化的表ID,否则列无法关联到该表
if getattr(table, 'table_id', None) is None:
table.table_id = getattr(gen_table, 'id', None)
table_columns = table.columns or []
table_column_map = {column.column_name: column for column in table_columns}
query_db_table_columns = await GenTableColumnCRUD(auth).get_gen_db_table_columns_by_name(table_name)
# 直接使用查询结果,因为get_gen_db_table_columns_by_name已经返回GenTableColumnOutSchema对象列表
db_table_columns = query_db_table_columns
if not db_table_columns:
raise CustomException(msg='同步数据失败,原表结构不存在')
db_table_columns = await GenTableColumnCRUD(auth).get_gen_db_table_columns_by_name(table_name)
db_table_column_names = [column.column_name for column in db_table_columns]
try:
for column in db_table_columns:
# 仅在缺省时初始化默认属性(包含 table_id 关联)
GenUtils.init_column_field(column, table)
if column.column_name in table_column_map:
prev_column = table_column_map[column.column_name]
# 处理column.id为None的情况
# 复用旧记录ID,确保执行更新
if hasattr(prev_column, 'id') and prev_column.id:
column.id = prev_column.id
if column.list:
# 保留用户配置的显示与查询属性
if getattr(prev_column, 'dict_type', None):
column.dict_type = prev_column.dict_type
if getattr(prev_column, 'query_type', None):
column.query_type = prev_column.query_type
if (
hasattr(prev_column, 'is_required') and prev_column.is_required != ''
and not column.pk
and (column.insert or column.edit)
and (column.usable_column or column.super_column)
):
column.is_required = prev_column.is_required
if getattr(prev_column, 'html_type', None):
column.html_type = prev_column.html_type
# 处理column.id为None的情况
# 保留 is_* 标志(旧值非空则保留),主键不设置必填
def keep_str(orig, current):
return orig if (orig is not None and orig != '') else current
is_pk_bool = bool(getattr(prev_column, 'pk', False)) or (prev_column.is_pk == '1')
if not is_pk_bool:
column.is_required = keep_str(prev_column.is_required, column.is_required)
column.is_unique = keep_str(prev_column.is_unique, column.is_unique)
column.is_insert = keep_str(prev_column.is_insert, column.is_insert)
column.is_edit = keep_str(prev_column.is_edit, column.is_edit)
column.is_list = keep_str(prev_column.is_list, column.is_list)
column.is_query = keep_str(prev_column.is_query, column.is_query)
if hasattr(column, 'id') and column.id:
await GenTableColumnCRUD(auth).update_gen_table_column_crud(column.id, column)
else:
await GenTableColumnCRUD(auth).create_gen_table_column_crud(column)
else:
await GenTableColumnCRUD(auth).create_gen_table_column_crud(column)
del_columns = [column for column in table_columns if column.column_name not in db_table_column_names]
if del_columns:
for column in del_columns:
# 处理column.id为None的情况
if hasattr(column, 'id') and column.id:
await GenTableColumnCRUD(auth).delete_gen_table_column_by_column_id_dao(
GenTableColumnDeleteSchema(column_ids=[column.id])
)
except Exception as e:
raise e
raise CustomException(msg=f'同步失败: {str(e)}')
@classmethod
async def set_sub_table(cls, auth: AuthSchema, gen_table: GenTableOutSchema) -> None:
"""设置主子表信息"""
"""设置主子表信息
参数:
- auth (AuthSchema): 认证对象。
- gen_table (GenTableOutSchema): 业务表详细信息模型。
返回:
- None
异常:
- CustomException: 当子表不存在时抛出。
"""
if gen_table.sub_table_name:
gen_table_dao = GenTableCRUD(auth=auth)
sub_table = await gen_table_dao.get_gen_table_by_name(gen_table.sub_table_name)
if sub_table:
gen_table.sub_table = GenTableOutSchema.model_validate(sub_table)
@classmethod
async def set_pk_column(cls, gen_table: GenTableOutSchema) -> None:
"""设置主键列信息"""
"""设置主键列信息
参数:
- gen_table (GenTableOutSchema): 业务表详细信息模型。
返回:
- None
"""
if gen_table.columns:
for column in gen_table.columns:
if column.pk:
@@ -411,7 +566,14 @@ class GenTableService:
@classmethod
async def set_table_from_options(cls, gen_table: GenTableOutSchema) -> GenTableOutSchema:
"""设置代码生成其他选项值"""
"""设置代码生成其他选项值
参数:
- gen_table (GenTableOutSchema): 业务表详细信息模型。
返回:
- GenTableOutSchema: 更新后的业务表详细信息模型。
"""
# 处理gen_table.options为None的情况
if gen_table.options:
try:
@@ -432,7 +594,17 @@ class GenTableService:
@classmethod
async def validate_edit(cls, edit_gen_table: GenTableSchema) -> None:
"""编辑保存参数校验"""
"""编辑保存参数校验
参数:
- edit_gen_table (GenTableSchema): 编辑后的业务表模型。
返回:
- None
异常:
- CustomException: 当参数校验失败时抛出。
"""
if edit_gen_table.tpl_category == GenConstant.TPL_TREE:
# 从options字段获取参数,而不是params
if not edit_gen_table.options:
@@ -459,11 +631,17 @@ class GenTableService:
@classmethod
async def __get_gen_render_info(cls, auth: AuthSchema, table_name: str) -> List[Any]:
"""
获取生成代码渲染模板相关信息
:param auth: 认证对象
:param table_name: 业务表名称
:return: 生成代码渲染模板相关信息
获取生成代码渲染模板相关信息
参数:
- auth (AuthSchema): 认证对象。
- table_name (str): 业务表名称。
返回:
- List[Any]: [模板列表, 输出文件名列表, 渲染上下文, 业务表对象]。
异常:
- CustomException: 当业务表不存在或数据转换失败时抛出。
"""
gen_table = await GenTableCRUD(auth=auth).get_gen_table_by_name(table_name)
# 检查表是否存在
@@ -491,11 +669,18 @@ class GenTableService:
output_files.append(file_name)
return [template_list, output_files, context, gen_table_schema]
@classmethod
def __get_gen_path(cls, gen_table: GenTableOutSchema, template: str) -> Optional[str]:
"""根据GenTableModel对象和模板名称生成路径"""
"""根据GenTableOutSchema对象和模板名称生成路径
参数:
- gen_table (GenTableOutSchema): 业务表详细信息模型。
- template (str): 模板名称。
返回:
- Optional[str]: 生成的文件路径,若失败则返回None。
"""
try:
gen_path = gen_table.gen_path or ""
file_name = Jinja2TemplateUtil.get_file_name([template], gen_table)
@@ -515,7 +700,15 @@ class GenTableColumnService:
@classmethod
async def get_gen_table_column_list_by_table_id_service(cls, auth: AuthSchema, table_id: int) -> List[GenTableColumnOutSchema]:
"""获取业务表字段列表信息"""
"""获取业务表字段列表信息
参数:
- auth (AuthSchema): 认证对象。
- table_id (int): 业务表ID。
返回:
- List[GenTableColumnOutSchema]: 业务表字段详细信息模型列表。
"""
gen_table_column_list_result = await GenTableColumnCRUD(auth).list_gen_table_column_crud({"table_id": table_id})
return [
GenTableColumnOutSchema.model_validate(gen_table_column)