Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #63 若mysql服务关闭、给出对应提示 #65

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading