Skip to content

Commit 65c7fae

Browse files
committed
Optimize generator
1 parent f846f40 commit 65c7fae

File tree

13 files changed

+277
-115
lines changed

13 files changed

+277
-115
lines changed

backend/app/generator/crud/crud_gen.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@
99

1010

1111
class CRUDGen:
12+
"""代码生成 CRUD 类"""
13+
1214
@staticmethod
13-
async def get_all_tables(db: AsyncSession, table_schema: str) -> Sequence[str]:
15+
async def get_all_tables(db: AsyncSession, table_schema: str) -> list[str]:
16+
"""
17+
获取所有表名
18+
19+
:param db: 数据库会话
20+
:param table_schema: 数据库 schema 名称
21+
:return:
22+
"""
1423
if settings.DATABASE_TYPE == 'mysql':
1524
sql = """
1625
SELECT table_name AS table_name FROM information_schema.tables
@@ -30,6 +39,13 @@ async def get_all_tables(db: AsyncSession, table_schema: str) -> Sequence[str]:
3039

3140
@staticmethod
3241
async def get_table(db: AsyncSession, table_name: str) -> Row[tuple]:
42+
"""
43+
获取表信息
44+
45+
:param db: 数据库会话
46+
:param table_name: 表名
47+
:return:
48+
"""
3349
if settings.DATABASE_TYPE == 'mysql':
3450
sql = """
3551
SELECT table_name AS table_name, table_comment AS table_comment FROM information_schema.tables
@@ -51,6 +67,14 @@ async def get_table(db: AsyncSession, table_name: str) -> Row[tuple]:
5167

5268
@staticmethod
5369
async def get_all_columns(db: AsyncSession, table_schema: str, table_name: str) -> Sequence[Row[tuple]]:
70+
"""
71+
获取所有列信息
72+
73+
:param db: 数据库会话
74+
:param table_schema: 数据库 schema 名称
75+
:param table_name: 表名
76+
:return:
77+
"""
5478
if settings.DATABASE_TYPE == 'mysql':
5579
sql = """
5680
SELECT column_name AS column_name,

backend/app/generator/crud/crud_gen_business.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111

1212
class CRUDGenBusiness(CRUDPlus[GenBusiness]):
13+
"""代码生成业务 CRUD 类"""
14+
1315
async def get(self, db: AsyncSession, pk: int) -> GenBusiness | None:
1416
"""
1517
获取代码生成业务表
1618
17-
:param db:
18-
:param pk:
19+
:param db: 数据库会话
20+
:param pk: 主键 ID
1921
:return:
2022
"""
2123
return await self.select_model(db, pk)
@@ -24,8 +26,8 @@ async def get_by_name(self, db: AsyncSession, name: str) -> GenBusiness | None:
2426
"""
2527
通过 name 获取代码生成业务表
2628
27-
:param db:
28-
:param name:
29+
:param db: 数据库会话
30+
:param name: 表名
2931
:return:
3032
"""
3133
return await self.select_model_by_column(db, table_name_en=name)
@@ -34,6 +36,7 @@ async def get_all(self, db: AsyncSession) -> Sequence[GenBusiness]:
3436
"""
3537
获取所有代码生成业务表
3638
39+
:param db: 数据库会话
3740
:return:
3841
"""
3942
return await self.select_models(db)
@@ -42,8 +45,8 @@ async def create(self, db: AsyncSession, obj_in: CreateGenBusinessParam) -> None
4245
"""
4346
创建代码生成业务表
4447
45-
:param db:
46-
:param obj_in:
48+
:param db: 数据库会话
49+
:param obj_in: 创建参数
4750
:return:
4851
"""
4952
await self.create_model(db, obj_in)
@@ -52,9 +55,8 @@ async def update(self, db: AsyncSession, pk: int, obj_in: UpdateGenBusinessParam
5255
"""
5356
更新代码生成业务表
5457
55-
:param db:
56-
:param pk:
57-
:param obj_in:
58+
:param db: 数据库会话
59+
:param obj_in: 更新参数
5860
:return:
5961
"""
6062
return await self.update_model(db, pk, obj_in)
@@ -63,8 +65,8 @@ async def delete(self, db: AsyncSession, pk: int) -> int:
6365
"""
6466
删除代码生成业务表
6567
66-
:param db:
67-
:param pk:
68+
:param db: 数据库会话
69+
:param pk: 主键 ID
6870
:return:
6971
"""
7072
return await self.delete_model(db, pk)

backend/app/generator/crud/crud_gen_model.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010

1111

1212
class CRUDGenModel(CRUDPlus[GenModel]):
13+
"""代码生成模型 CRUD 类"""
14+
1315
async def get(self, db: AsyncSession, pk: int) -> GenModel | None:
1416
"""
1517
获取代码生成模型列
1618
19+
:param db: 数据库会话
20+
:param pk: 主键 ID
1721
:return:
1822
"""
1923
return await self.select_model(db, pk)
@@ -22,8 +26,8 @@ async def get_all_by_business_id(self, db: AsyncSession, business_id: int) -> Se
2226
"""
2327
获取所有代码生成模型列
2428
25-
:param db:
26-
:param business_id:
29+
:param db: 数据库会话
30+
:param business_id: 业务 ID
2731
:return:
2832
"""
2933
return await self.select_models_order(db, sort_columns='sort', gen_business_id=business_id)
@@ -32,21 +36,21 @@ async def create(self, db: AsyncSession, obj_in: CreateGenModelParam, pd_type: s
3236
"""
3337
创建代码生成模型表
3438
35-
:param db:
36-
:param obj_in:
37-
:param pd_type:
39+
:param db: 数据库会话
40+
:param obj_in: 创建参数
41+
:param pd_type: Pydantic 类型
3842
:return:
3943
"""
4044
await self.create_model(db, obj_in, pd_type=pd_type)
4145

4246
async def update(self, db: AsyncSession, pk: int, obj_in: UpdateGenModelParam, pd_type: str | None = None) -> int:
4347
"""
44-
更细代码生成模型表
48+
更新代码生成模型表
4549
46-
:param db:
47-
:param pk:
48-
:param obj_in:
49-
:param pd_type:
50+
:param db: 数据库会话
51+
:param pk: 主键 ID
52+
:param obj_in: 更新参数
53+
:param pd_type: Pydantic 类型
5054
:return:
5155
"""
5256
return await self.update_model(db, pk, obj_in, pd_type=pd_type)
@@ -55,8 +59,8 @@ async def delete(self, db: AsyncSession, pk: int) -> int:
5559
"""
5660
删除代码生成模型表
5761
58-
:param db:
59-
:param pk:
62+
:param db: 数据库会话
63+
:param pk: 主键 ID
6064
:return:
6165
"""
6266
return await self.delete_model(db, pk)

backend/app/generator/model/gen_business.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
from typing import TYPE_CHECKING
4+
35
from sqlalchemy import String
46
from sqlalchemy.dialects.mysql import LONGTEXT
57
from sqlalchemy.dialects.postgresql import TEXT
68
from sqlalchemy.orm import Mapped, mapped_column, relationship
79

810
from backend.common.model import Base, id_key
911

12+
if TYPE_CHECKING:
13+
from backend.app.generator.model.gen_model import GenModel
14+
1015

1116
class GenBusiness(Base):
1217
"""代码生成业务表"""
@@ -28,4 +33,4 @@ class GenBusiness(Base):
2833
LONGTEXT().with_variant(TEXT, 'postgresql'), default=None, comment='备注'
2934
)
3035
# 代码生成业务模型一对多
31-
gen_model: Mapped[list['GenModel']] = relationship(init=False, back_populates='gen_business') # noqa: F821
36+
gen_model: Mapped[list['GenModel']] = relationship(init=False, back_populates='gen_business')

backend/app/generator/model/gen_model.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
from typing import Union
3+
from typing import TYPE_CHECKING, Union
44

55
from sqlalchemy import ForeignKey, String
66
from sqlalchemy.dialects.mysql import LONGTEXT
@@ -9,6 +9,9 @@
99

1010
from backend.common.model import DataClassBase, id_key
1111

12+
if TYPE_CHECKING:
13+
from backend.app.generator.model.gen_business import GenBusiness
14+
1215

1316
class GenModel(DataClassBase):
1417
"""代码生成模型表"""
@@ -32,4 +35,4 @@ class GenModel(DataClassBase):
3235
gen_business_id: Mapped[int] = mapped_column(
3336
ForeignKey('sys_gen_business.id', ondelete='CASCADE'), default=0, comment='代码生成业务ID'
3437
)
35-
gen_business: Mapped[Union['GenBusiness', None]] = relationship(init=False, back_populates='gen_model') # noqa: F821
38+
gen_business: Mapped[Union['GenBusiness', None]] = relationship(init=False, back_populates='gen_model')

backend/app/generator/schema/gen.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77

88
class ImportParam(SchemaBase):
9+
"""导入参数"""
10+
911
app: str = Field(description='应用名称,用于代码生成到指定 app')
1012
table_name: str = Field(description='数据库表名')
1113
table_schema: str = Field(description='数据库名')

backend/app/generator/schema/gen_business.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,40 @@
99

1010

1111
class GenBusinessSchemaBase(SchemaBase):
12-
app_name: str
13-
table_name_en: str
14-
table_name_zh: str
15-
table_simple_name_zh: str
16-
table_comment: str | None = None
17-
schema_name: str | None = None
18-
default_datetime_column: bool = Field(default=True)
19-
api_version: str = Field(default='v1')
20-
gen_path: str | None = None
21-
remark: str | None = None
12+
"""代码生成业务基类"""
13+
14+
app_name: str = Field(description='应用名称(英文)')
15+
table_name_en: str = Field(description='表名称(英文)')
16+
table_name_zh: str = Field(description='表名称(中文)')
17+
table_simple_name_zh: str = Field(description='表名称(中文简称)')
18+
table_comment: str | None = Field(default=None, description='表描述')
19+
schema_name: str | None = Field(default=None, description='Schema 名称 (默认为英文表名称)')
20+
default_datetime_column: bool = Field(default=True, description='是否存在默认时间列')
21+
api_version: str = Field(default='v1', description='代码生成 api 版本,默认为 v1')
22+
gen_path: str | None = Field(default=None, description='代码生成路径(默认为 app 根路径)')
23+
remark: str | None = Field(default=None, description='备注')
2224

2325
@model_validator(mode='after')
2426
def check_schema_name(self) -> Self:
27+
"""检查并设置 schema 名称"""
2528
if self.schema_name is None:
2629
self.schema_name = self.table_name_en
2730
return self
2831

2932

3033
class CreateGenBusinessParam(GenBusinessSchemaBase):
31-
pass
34+
"""创建代码生成业务"""
3235

3336

3437
class UpdateGenBusinessParam(GenBusinessSchemaBase):
35-
pass
38+
"""更新代码生成业务"""
3639

3740

3841
class GetGenBusinessDetail(GenBusinessSchemaBase):
42+
"""获取代码生成业务详情"""
43+
3944
model_config = ConfigDict(from_attributes=True)
4045

41-
id: int
42-
created_time: datetime
43-
updated_time: datetime | None = None
46+
id: int = Field(description='主键 ID')
47+
created_time: datetime = Field(description='创建时间')
48+
updated_time: datetime | None = Field(default=None, description='更新时间')

backend/app/generator/schema/gen_model.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,37 @@
77

88

99
class GenModelSchemaBase(SchemaBase):
10-
name: str
11-
comment: str | None = None
12-
type: str
13-
default: str | None = None
14-
sort: int
15-
length: int
16-
is_pk: bool = Field(default=False)
17-
is_nullable: bool = Field(default=False)
18-
gen_business_id: int | None = Field(ge=1)
10+
"""代码生成模型基类"""
11+
12+
name: str = Field(description='列名称')
13+
comment: str | None = Field(default=None, description='列描述')
14+
type: str = Field(description='SQLA 模型列类型')
15+
default: str | None = Field(default=None, description='列默认值')
16+
sort: int = Field(description='列排序')
17+
length: int = Field(description='列长度')
18+
is_pk: bool = Field(default=False, description='是否主键')
19+
is_nullable: bool = Field(default=False, description='是否可为空')
20+
gen_business_id: int | None = Field(ge=1, description='代码生成业务ID')
1921

2022
@field_validator('type')
2123
@classmethod
22-
def type_update(cls, v):
24+
def type_update(cls, v: str) -> str:
25+
"""更新列类型"""
2326
return sql_type_to_sqlalchemy(v)
2427

2528

2629
class CreateGenModelParam(GenModelSchemaBase):
27-
pass
30+
"""创建代码生成模型"""
2831

2932

3033
class UpdateGenModelParam(GenModelSchemaBase):
31-
pass
34+
"""更新代码生成模型"""
3235

3336

3437
class GetGenModelDetail(GenModelSchemaBase):
38+
"""获取代码生成模型详情"""
39+
3540
model_config = ConfigDict(from_attributes=True)
3641

37-
id: int
38-
pd_type: str
42+
id: int = Field(description='主键 ID')
43+
pd_type: str = Field(description='列类型对应的 pydantic 类型')

0 commit comments

Comments
 (0)