forked from msooner/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolService.py
More file actions
90 lines (73 loc) · 3.14 KB
/
ToolService.py
File metadata and controls
90 lines (73 loc) · 3.14 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
#-*-coding:utf-8-*-
from .BaseService import BaseService
from dao.ToolDAO import ToolDAO
from library.Result import Result
class ToolService(BaseService):
""" 工具相关的功能 """
def __init__(self):
self.toolDAO = ToolDAO.getInstance()
super().__init__()
def get_all_db(self):
""" 查询所有数据库 """
res = self.toolDAO.get_all_db()
return Result(code=0, data=res)
def get_all_tables(self, dbname=""):
""" 指定数据库的所有表 """
res = self.toolDAO.get_all_tables(dbname)
return Result(code=0, data=res)
def get_schema(self, dbname='', table='', superclass='', prefix="yes"):
""" 指定数据库和表的schema """
res = self.toolDAO.get_schema(dbname, table)
schema = []
columns = []
for x in res:
schema.append({
'table_name': x['TABLE_NAME'],
'data_type': x['DATA_TYPE'],
'is_nullable': x['IS_NULLABLE'].lower(),
'column_name': x['COLUMN_NAME'],
'default_value': x['COLUMN_DEFAULT'],
'extra': x['EXTRA'],
'column_key': x['COLUMN_KEY'],
})
columns.append(x['COLUMN_NAME'])
data = []
data.append("#!/usr/bin/env python")
data.append("\n")
data.append("from .BaseDO import {superclass}".format(superclass=superclass))
data.append("from sqlalchemy import Column")
data.append("from sqlalchemy.types import *")
data.append("\n")
data.append("\n")
data.append("class {name}DO({superclass}):".format(name=self.toolDAO.get_schema_name(table, prefix),
superclass=superclass))
data.append(" __tablename__='{table}'".format(table=table))
data.append("\n")
fields = []
if schema:
for x in schema:
condition = []
condition.append(self.toolDAO.get_datatype_mapper(x['data_type']))
if x['column_key'] == 'PRI':
condition.append("primary_key=True")
if x['extra'] == 'auto_increment':
condition.append("autoincrement=True")
if x['is_nullable'] == 'yes':
condition.append("nullable=True")
else:
condition.append("nullable=False")
if x['default_value']:
value = x["default_value"]
if x['data_type'] in ('varchar', 'text'):
value = "'{value}'".format(value=value)
condition.append("default={value}".format(value=value))
fields.append({
"key": x["column_name"],
"val": " {column_name} = Column({condition})".format(column_name=x["column_name"],
condition=", ".join(condition))
})
return Result(code=0, data={
"data": data,
"fields": fields,
'columns': columns,
})