forked from Boris-code/feapder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_item.py
More file actions
167 lines (136 loc) · 5.52 KB
/
create_item.py
File metadata and controls
167 lines (136 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- coding: utf-8 -*-
"""
Created on 2018-08-28 17:38:43
---------
@summary: 创建item
---------
@author: Boris
@email: boris_liu@foxmail.com
"""
import getpass
import os
import feapder.utils.tools as tools
from feapder import setting
from feapder.db.mysqldb import MysqlDB
from .create_init import CreateInit
def deal_file_info(file):
file = file.replace("{DATE}", tools.get_current_date())
file = file.replace("{USER}", getpass.getuser())
return file
class CreateItem:
def __init__(self):
self._db = MysqlDB()
self._create_init = CreateInit()
def select_columns(self, table_name):
# sql = 'SHOW COLUMNS FROM ' + table_name
sql = f"SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, EXTRA, COLUMN_KEY, COLUMN_COMMENT FROM INFORMATION_SCHEMA.Columns WHERE table_name = '{table_name}' and table_schema = '{setting.MYSQL_DB}'"
columns = self._db.find(sql)
return columns
def select_tables_name(self, tables_name):
"""
@summary:
---------
@param tables_name: 一类tables 如 qidian*
---------
@result:
"""
sql = f"select table_name from information_schema.tables where table_name like '{tables_name}' and table_schema = '{setting.MYSQL_DB}'"
tables_name = self._db.find(sql)
return tables_name
def convert_table_name_to_hump(self, table_name):
"""
@summary: 格式化表明为驼峰格式
---------
@param table:
---------
@result:
"""
table_hump_format = ""
words = table_name.split("_")
for word in words:
table_hump_format += word.capitalize() # 首字母大写
return table_hump_format
def get_item_template(self):
template_path = os.path.abspath(
os.path.join(__file__, "../../../templates/item_template.tmpl")
)
with open(template_path, "r", encoding="utf-8") as file:
item_template = file.read()
return item_template
def create_item(self, item_template, columns, table_name, support_dict):
table_name_hump_format = self.convert_table_name_to_hump(table_name)
# 组装 类名
item_template = item_template.replace("${item_name}", table_name_hump_format)
if support_dict:
item_template = item_template.replace("${command}", table_name + " 1")
else:
item_template = item_template.replace("${command}", table_name)
item_template = item_template.replace("${table_name}", table_name)
# 组装 属性
propertys = ""
for column in columns:
column_name = column[0]
column_type = column[1]
is_nullable = column[2]
column_default = column[3]
column_extra = column[4]
column_key = column[5]
column_comment = column[6]
try:
column_default = None if column_default == "NULL" else column_default
value = (
"kwargs.get('{column_name}')".format(column_name=column_name)
if support_dict
else (
column_default != "CURRENT_TIMESTAMP" and column_default or None
)
and eval(column_default)
)
except:
value = (
"kwargs.get('{column_name}')".format(column_name=column_name)
if support_dict
else (
column_default != "CURRENT_TIMESTAMP" and column_default or None
)
and column_default
)
if column_extra == "auto_increment" or column_default is not None:
propertys += f"# self.{column_name} = {value}"
else:
if value is None or isinstance(value, (float, int)) or support_dict:
propertys += f"self.{column_name} = {value}"
else:
propertys += f"self.{column_name} = '{value}'"
if column_comment:
propertys += f" # {column_comment}"
propertys += "\n" + " " * 8
item_template = item_template.replace("${propertys}", propertys.strip())
item_template = deal_file_info(item_template)
return item_template
def save_template_to_file(self, item_template, table_name):
item_file = table_name + "_item.py"
if os.path.exists(item_file):
confirm = input("%s 文件已存在 是否覆盖 (y/n). " % item_file)
if confirm != "y":
print("取消覆盖 退出")
return
with open(item_file, "w", encoding="utf-8") as file:
file.write(item_template)
print("\n%s 生成成功" % item_file)
self._create_init.create()
def create(self, tables_name, support_dict):
input_tables_name = tables_name
tables_name = self.select_tables_name(tables_name)
if not tables_name:
print(tables_name)
tip = "mysql数据库中无 %s 表 " % input_tables_name
raise KeyError(tip)
for table_name in tables_name:
table_name = table_name[0]
columns = self.select_columns(table_name)
item_template = self.get_item_template()
item_template = self.create_item(
item_template, columns, table_name, support_dict
)
self.save_template_to_file(item_template, table_name)