perf: 优化代码生成功能
This commit is contained in:
@@ -12,7 +12,13 @@ class GenUtils:
|
||||
|
||||
@classmethod
|
||||
def init_table(cls, gen_table: GenTableModel, oper_name: str) -> None:
|
||||
"""初始化表信息"""
|
||||
"""
|
||||
初始化表信息
|
||||
|
||||
param gen_table: 业务表对象
|
||||
param oper_name: 操作人
|
||||
:return:
|
||||
"""
|
||||
gen_table.class_name = cls.convert_class_name(gen_table.table_name)
|
||||
gen_table.package_name = GenConfig.package_name
|
||||
gen_table.module_name = cls.get_module_name(GenConfig.package_name)
|
||||
@@ -26,7 +32,13 @@ class GenUtils:
|
||||
|
||||
@classmethod
|
||||
def init_column_field(cls, column: GenTableColumnModel, table: GenTableModel) -> None:
|
||||
"""初始化列属性字段"""
|
||||
"""
|
||||
初始化列属性字段
|
||||
|
||||
param column: 业务表字段对象
|
||||
param table: 业务表对象
|
||||
:return:
|
||||
"""
|
||||
data_type = cls.get_db_type(column.column_type)
|
||||
column_name = column.column_name
|
||||
column.table_id = table.table_id
|
||||
@@ -34,7 +46,9 @@ class GenUtils:
|
||||
# 设置Python字段名
|
||||
column.python_field = cls.to_camel_case(column_name)
|
||||
# 设置默认类型
|
||||
column.python_type = StringUtil.get_mapping_value_by_key_ignore_case(GenConstant.MYSQL_TO_PYTHON_TYPE_MAPPING, data_type)
|
||||
column.python_type = StringUtil.get_mapping_value_by_key_ignore_case(
|
||||
GenConstant.MYSQL_TO_PYTHON_TYPE_MAPPING, data_type
|
||||
)
|
||||
column.query_type = GenConstant.QUERY_EQ
|
||||
|
||||
if cls.arrays_contains(GenConstant.COLUMNTYPE_STR, data_type) or cls.arrays_contains(
|
||||
@@ -87,22 +101,43 @@ class GenUtils:
|
||||
|
||||
@classmethod
|
||||
def arrays_contains(cls, arr: List[str], target_value: str) -> bool:
|
||||
"""校验数组是否包含指定值"""
|
||||
"""
|
||||
校验数组是否包含指定值
|
||||
|
||||
param arr: 数组
|
||||
param target_value: 需要校验的值
|
||||
:return: 校验结果
|
||||
"""
|
||||
return target_value in arr
|
||||
|
||||
@classmethod
|
||||
def get_module_name(cls, package_name: str) -> str:
|
||||
"""获取模块名"""
|
||||
"""
|
||||
获取模块名
|
||||
|
||||
param package_name: 包名
|
||||
:return: 模块名
|
||||
"""
|
||||
return package_name.split('.')[-1]
|
||||
|
||||
@classmethod
|
||||
def get_business_name(cls, table_name: str) -> str:
|
||||
"""获取业务名"""
|
||||
"""
|
||||
获取业务名
|
||||
|
||||
param table_name: 业务表名
|
||||
:return: 业务名
|
||||
"""
|
||||
return table_name.split('_')[-1]
|
||||
|
||||
@classmethod
|
||||
def convert_class_name(cls, table_name: str) -> str:
|
||||
"""表名转换成Python类名"""
|
||||
"""
|
||||
表名转换成Python类名
|
||||
|
||||
param table_name: 业务表名
|
||||
:return: Python类名
|
||||
"""
|
||||
auto_remove_pre = GenConfig.auto_remove_pre
|
||||
table_prefix = GenConfig.table_prefix
|
||||
if auto_remove_pre and table_prefix:
|
||||
@@ -112,7 +147,13 @@ class GenUtils:
|
||||
|
||||
@classmethod
|
||||
def replace_first(cls, replacement: str, search_list: List[str]) -> str:
|
||||
"""批量替换前缀"""
|
||||
"""
|
||||
批量替换前缀
|
||||
|
||||
param replacement: 需要被替换的字符串
|
||||
param search_list: 可替换的字符串列表
|
||||
:return: 替换后的字符串
|
||||
"""
|
||||
for search_string in search_list:
|
||||
if replacement.startswith(search_string):
|
||||
return replacement.replace(search_string, '', 1)
|
||||
@@ -120,19 +161,34 @@ class GenUtils:
|
||||
|
||||
@classmethod
|
||||
def replace_text(cls, text: str) -> str:
|
||||
"""关键字替换"""
|
||||
"""
|
||||
关键字替换
|
||||
|
||||
param text: 需要被替换的字符串
|
||||
:return: 替换后的字符串
|
||||
"""
|
||||
return re.sub(r'(?:表|若依)', '', text)
|
||||
|
||||
@classmethod
|
||||
def get_db_type(cls, column_type: str) -> str:
|
||||
"""获取数据库类型字段"""
|
||||
"""
|
||||
获取数据库类型字段
|
||||
|
||||
param column_type: 字段类型
|
||||
:return: 数据库类型
|
||||
"""
|
||||
if '(' in column_type:
|
||||
return column_type.split('(')[0]
|
||||
return column_type
|
||||
|
||||
@classmethod
|
||||
def get_column_length(cls, column_type: str) -> int:
|
||||
"""获取字段长度"""
|
||||
"""
|
||||
获取字段长度
|
||||
|
||||
param column_type: 字段类型
|
||||
:return: 字段长度
|
||||
"""
|
||||
if '(' in column_type:
|
||||
length = len(column_type.split('(')[1].split(')')[0])
|
||||
return length
|
||||
@@ -140,13 +196,23 @@ class GenUtils:
|
||||
|
||||
@classmethod
|
||||
def split_column_type(cls, column_type: str) -> List[str]:
|
||||
"""拆分列类型"""
|
||||
"""
|
||||
拆分列类型
|
||||
|
||||
param column_type: 字段类型
|
||||
:return: 拆分结果
|
||||
"""
|
||||
if '(' in column_type and ')' in column_type:
|
||||
return column_type.split('(')[1].split(')')[0].split(',')
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
def to_camel_case(cls, text: str) -> str:
|
||||
"""将字符串转换为驼峰命名"""
|
||||
"""
|
||||
将字符串转换为驼峰命名
|
||||
|
||||
param text: 需要转换的字符串
|
||||
:return: 驼峰命名
|
||||
"""
|
||||
parts = text.split('_')
|
||||
return parts[0] + ''.join(word.capitalize() for word in parts[1:])
|
||||
|
Reference in New Issue
Block a user