Skip to content

Commit

Permalink
fix #63 若mysql服务关闭、给出对应提示
Browse files Browse the repository at this point in the history
  • Loading branch information
zy7y committed May 16, 2024
1 parent 24a4dba commit 972e306
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
38 changes: 21 additions & 17 deletions dfs_generate/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

app = bottle.Bottle()

CACHE: Dict[str, MySQLHelper] = {}
CACHE: Dict[str, MySQLConf] = {}

# 解决打包桌面程序static找不到的问题
static_file_abspath = os.path.join(
Expand Down Expand Up @@ -51,40 +51,44 @@ def index():
def connect():
payload = bottle.request.json
try:
conf = MySQLConf(**payload)
CACHE["connect"] = MySQLHelper(conf)
with MySQLHelper(MySQLConf(**payload)) as obj:
CACHE["conf"] = MySQLConf(**payload)
return {"code": 20000, "msg": "ok", "data": None}
except Exception as e:
return {"code": 40000, "msg": str(e), "data": None}


@app.get("/tables")
def tables():
if obj := CACHE.get("connect"):
like = bottle.request.query.get("tableName")
data = [
{"tableName": table, "key": table}
for table in obj.get_tables()
if like in table
]
return {"code": 20000, "msg": "ok", "data": data}
return {"code": 40000, "msg": "error", "data": None}
like = bottle.request.query.get("tableName")
try:
with MySQLHelper(CACHE.get("conf")) as obj:
data = [
{"tableName": table, "key": table}
for table in obj.get_tables()
if like in table
]
return {"code": 20000, "msg": "ok", "data": data}
except Exception as e:
return {"code": 40000, "msg": str(e), "data": None}


@app.get("/codegen")
def codegen():
obj = CACHE.get("connect")
table = bottle.request.query.get("tableName")
mode = bottle.request.query.get("mode")
results = []
if mode == "sqlmodel":
_instance = SQLModelConversion
else:
_instance = TortoiseConversion

data = _instance(
table, obj.get_table_columns(table), obj.conf.get_db_uri()
).gencode()
try:
with MySQLHelper(CACHE.get("conf")) as obj:
data = _instance(
table, obj.get_table_columns(table), obj.conf.get_db_uri()
).gencode()
except Exception as e:
return {"code": 40000, "msg": str(e), "data": None}

for k, v in data.items():
if k.endswith("py"):
Expand Down
6 changes: 6 additions & 0 deletions dfs_generate/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,9 @@ def get_table_columns(self, table_name):
self.cursor.execute(self.GET_TABLE_COLUMNS, [self.conf.db, table_name])
rows = self.cursor.fetchall()
return sorted(rows, key=lambda x: x["ORDINAL_POSITION"])

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
11 changes: 9 additions & 2 deletions web/src/compoents/content.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,20 @@ const DFSContent = () => {
body: localStorage.getItem("dbConf"),
});
const resData = await resConf.json();
if (resData.code === 40000) {
message.error(resData.msg);
return;
}
console.log(resData, resConf);
};
useEffect(() => {
if (localStorage.getItem("dbConf")) {
resetContentDB();
getTables();
} else {
message.info("welcome to use dfs-generate");
setModalOpen(true)
}
getTables();

}, []);

// 搜索
Expand Down

0 comments on commit 972e306

Please sign in to comment.