Skip to content

Commit c3269fd

Browse files
committed
完善create table 命令
1 parent f971948 commit c3269fd

1 file changed

Lines changed: 35 additions & 20 deletions

File tree

feapder/commands/create/create_table.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CreateTable:
2222
def __init__(self):
2323
self._db = MysqlDB()
2424

25-
def is_vaild_date(self, date):
25+
def is_valid_date(self, date):
2626
try:
2727
if ":" in date:
2828
time.strptime(date, "%Y-%m-%d %H:%M:%S")
@@ -44,15 +44,17 @@ def get_key_type(self, value):
4444
elif isinstance(value, float):
4545
key_type = "double"
4646
elif isinstance(value, str):
47-
if self.is_vaild_date(value):
47+
if self.is_valid_date(value):
4848
if ":" in value:
4949
key_type = "datetime"
5050
else:
5151
key_type = "date"
52-
elif len(value) > 255:
52+
elif len(value) > 50:
5353
key_type = "text"
5454
else:
5555
key_type = "varchar(255)"
56+
elif isinstance(value, (dict, list)):
57+
key_type = "longtext"
5658

5759
return key_type
5860

@@ -80,21 +82,26 @@ def create(self, table_name):
8082
# 拼接表结构
8183
sql = """
8284
CREATE TABLE `{db}`.`{table_name}` (
83-
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id 自动递增',
85+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id主键',
8486
{other_key}
85-
`gtime` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '抓取时间',
86-
PRIMARY KEY (`id`),
87+
`crawl_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '采集时间',
8788
{unique}
89+
PRIMARY KEY (`id`)
8890
) COMMENT='';
8991
"""
9092

91-
print("请设置注释 回车跳过")
93+
# print("请设置注释 回车跳过")
9294
other_key = ""
9395
for key, value in data.items():
9496
key = key2underline(key)
97+
comment = ""
98+
if key == "id":
99+
key = "data_id"
100+
comment = "原始数据id"
101+
95102
key_type = self.get_key_type(value)
96103

97-
comment = input("%s : %s -> comment:" % (key, key_type))
104+
# comment = input("%s : %s -> comment:" % (key, key_type))
98105

99106
other_key += (
100107
"`{key}` {key_type} COMMENT '{comment}',\n ".format(
@@ -105,34 +112,42 @@ def create(self, table_name):
105112
print("\n")
106113

107114
while True:
108-
is_need_batch_date = input("是否添加batch_date 字段 (y/n):")
109-
if is_need_batch_date == "y":
115+
yes = input("是否添加批次字段 batch_date(y/n):")
116+
if yes == "y":
110117
other_key += (
111118
"`{key}` {key_type} COMMENT '{comment}',\n ".format(
112119
key="batch_date", key_type="date", comment="批次时间"
113120
)
114121
)
115122
break
116-
elif is_need_batch_date == "n":
123+
elif yes == "n":
117124
break
118125

119126
print("\n")
120127

121128
while True:
122-
unique = input("请设置唯一索引, 多个逗号间隔\n等待输入:\n").replace(",", ",")
123-
if unique:
129+
yes = input("是否设置唯一索引(y/n):")
130+
if yes == "y":
131+
unique = input("请设置唯一索引, 多个逗号间隔\n等待输入:\n").replace(",", ",")
132+
if unique:
133+
unique = "UNIQUE `idx` USING BTREE (`%s`) comment ''," % "`,`".join(
134+
unique.split(",")
135+
)
136+
break
137+
elif yes == "n":
138+
unique = ""
124139
break
125-
unique = "UNIQUE `idx` USING BTREE (`%s`) comment ''" % "`,`".join(
126-
unique.split(",")
127-
)
128140

129141
sql = sql.format(
130142
db=setting.MYSQL_DB,
131143
table_name=table_name,
132-
other_key=other_key,
144+
other_key=other_key.strip(),
133145
unique=unique,
134146
)
135147
print(sql)
136-
self._db.execute(sql)
137-
print("\n%s 创建成功" % table_name)
138-
print("注意手动检查下字段类型,确保无误!!!")
148+
149+
if self._db.execute(sql):
150+
print("\n%s 创建成功" % table_name)
151+
print("注意手动检查下字段类型,确保无误!!!")
152+
else:
153+
print("\n%s 创建失败" % table_name)

0 commit comments

Comments
 (0)