5
5
import zipfile
6
6
7
7
from pathlib import Path
8
+ from typing import Sequence
8
9
9
10
import aiofiles
10
11
13
14
from backend .common .exception import errors
14
15
from backend .core .path_conf import BASE_PATH
15
16
from backend .database .db import async_db_session
17
+ from backend .plugin .code_generator .crud .crud_business import gen_business_dao
18
+ from backend .plugin .code_generator .crud .crud_column import gen_model_dao
16
19
from backend .plugin .code_generator .crud .crud_gen import gen_dao
17
- from backend .plugin .code_generator .crud .crud_gen_business import gen_business_dao
18
- from backend .plugin .code_generator .crud .crud_gen_model import gen_model_dao
19
20
from backend .plugin .code_generator .model import GenBusiness
21
+ from backend .plugin .code_generator .schema .business import CreateGenBusinessParam
22
+ from backend .plugin .code_generator .schema .column import CreateGenModelParam
20
23
from backend .plugin .code_generator .schema .gen import ImportParam
21
- from backend .plugin .code_generator .schema .gen_business import CreateGenBusinessParam
22
- from backend .plugin .code_generator .schema .gen_model import CreateGenModelParam
23
- from backend .plugin .code_generator .service .gen_model_service import gen_model_service
24
+ from backend .plugin .code_generator .service .column_service import gen_model_service
24
25
from backend .plugin .code_generator .utils .gen_template import gen_template
25
26
from backend .plugin .code_generator .utils .type_conversion import sql_type_to_pydantic
26
27
@@ -29,7 +30,7 @@ class GenService:
29
30
"""代码生成服务类"""
30
31
31
32
@staticmethod
32
- async def get_tables (* , table_schema : str ) -> list [str ]:
33
+ async def get_tables (* , table_schema : str ) -> Sequence [str ]:
33
34
"""
34
35
获取指定 schema 下的所有表名
35
36
@@ -57,32 +58,38 @@ async def import_business_and_model(*, obj: ImportParam) -> None:
57
58
raise errors .ForbiddenError (msg = '已存在相同数据库表业务' )
58
59
59
60
table_name = table_info [0 ]
60
- business_data = {
61
- 'app_name' : obj .app ,
62
- 'table_name_en' : table_name ,
63
- 'table_name_zh' : table_info [1 ] or ' ' .join (table_name .split ('_' )),
64
- 'table_simple_name_zh' : table_info [1 ] or table_name .split ('_' )[- 1 ],
65
- 'table_comment' : table_info [1 ],
66
- }
67
- new_business = GenBusiness (** CreateGenBusinessParam (** business_data ).model_dump ())
61
+ new_business = GenBusiness (
62
+ ** CreateGenBusinessParam (
63
+ app_name = obj .app ,
64
+ table_name_en = table_name ,
65
+ table_name_zh = table_info [1 ] or ' ' .join (table_name .split ('_' )),
66
+ table_simple_name_zh = table_info [1 ] or table_name .split ('_' )[- 1 ],
67
+ table_comment = table_info [1 ],
68
+ schema_name = table_name ,
69
+ filename = table_name ,
70
+ ).model_dump ()
71
+ )
68
72
db .add (new_business )
69
73
await db .flush ()
70
74
71
75
column_info = await gen_dao .get_all_columns (db , obj .table_schema , table_name )
72
76
for column in column_info :
73
77
column_type = column [- 1 ].split ('(' )[0 ].upper ()
74
78
pd_type = sql_type_to_pydantic (column_type )
75
- model_data = {
76
- 'name' : column [0 ],
77
- 'comment' : column [- 2 ],
78
- 'type' : column_type ,
79
- 'sort' : column [- 3 ],
80
- 'length' : column [- 1 ].split ('(' )[1 ][:- 1 ] if pd_type == 'str' and '(' in column [- 1 ] else 0 ,
81
- 'is_pk' : column [1 ],
82
- 'is_nullable' : column [2 ],
83
- 'gen_business_id' : new_business .id ,
84
- }
85
- await gen_model_dao .create (db , CreateGenModelParam (** model_data ), pd_type = pd_type )
79
+ await gen_model_dao .create (
80
+ db ,
81
+ CreateGenModelParam (
82
+ name = column [0 ],
83
+ comment = column [- 2 ],
84
+ type = column_type ,
85
+ sort = column [- 3 ],
86
+ length = column [- 1 ].split ('(' )[1 ][:- 1 ] if pd_type == 'str' and '(' in column [- 1 ] else 0 ,
87
+ is_pk = column [1 ],
88
+ is_nullable = column [2 ],
89
+ gen_business_id = new_business .id ,
90
+ ),
91
+ pd_type = pd_type ,
92
+ )
86
93
87
94
@staticmethod
88
95
async def render_tpl_code (* , business : GenBusiness ) -> dict [str , str ]:
0 commit comments