style: modify code style
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
import re
|
||||
import pymysql
|
||||
import pymysql # pylint: disable=e0401 # type: ignore
|
||||
|
||||
|
||||
# 拆分sql对象
|
||||
def extract_tables(sql_statement):
|
||||
# 使用正则表达式匹配FROM关键字之后的表名以及逗号分隔的表名:
|
||||
pattern = r'FROM\s+([\w\s,]+)'
|
||||
pattern = r"FROM\s+([\w\s,]+)"
|
||||
matches = re.findall(pattern, sql_statement, re.IGNORECASE)
|
||||
# 提取逗号分隔的表名,并按逗号进行分割
|
||||
tabs = re.split(',', matches[0].strip())
|
||||
tabs = re.split(",", matches[0].strip())
|
||||
# 处理每个表名,去除空格和其他无关字符
|
||||
cleaned_tables = []
|
||||
for tab in tabs:
|
||||
cleaned_tab = tab.strip()
|
||||
if ' ' in cleaned_tab:
|
||||
if " " in cleaned_tab:
|
||||
cleaned_tab = cleaned_tab.split()[0]
|
||||
cleaned_tables.append(cleaned_tab)
|
||||
return cleaned_tables # 返回列表
|
||||
@@ -25,12 +25,12 @@ def divide_sql(sql):
|
||||
例如sql = "SELECT a FROM data1, data2, data3 WHERE a = b ORDER BY misc"
|
||||
拆分原语句
|
||||
"""
|
||||
parts = re.split(r'(?i)from\s', sql) # 拆分"from "【无论大小写】
|
||||
parts = re.split(r"(?i)from\s", sql) # 拆分"from "【无论大小写】
|
||||
head = parts[0] + "from " # SELECT a FROM
|
||||
divide_sqls = []
|
||||
|
||||
if re.search(r'where', parts[1], flags=re.IGNORECASE):
|
||||
data = re.split(r'(?i) where', parts[1]) # 拆分" where"【无论大小写】
|
||||
if re.search(r"where", parts[1], flags=re.IGNORECASE):
|
||||
data = re.split(r"(?i) where", parts[1]) # 拆分" where"【无论大小写】
|
||||
tail = " where" + data[1] # WHERE a = b ORDER BY misc
|
||||
# message_p = "涉及到的数据源有:"
|
||||
# print(message_p)
|
||||
@@ -64,7 +64,13 @@ class SelectDatabase: # 定义类
|
||||
|
||||
def ret_hospital(self): # 定义函数,返回字典——医院(HOS)这个表
|
||||
# 连接服务器Server的数据库:
|
||||
db = pymysql.connect(host='localhost', user='root', password='111111', db=f'{self.database}', charset='utf8')
|
||||
db = pymysql.connect(
|
||||
host="localhost",
|
||||
user="root",
|
||||
password="111111",
|
||||
db=f"{self.database}",
|
||||
charset="utf8",
|
||||
)
|
||||
# 使用操作游标:
|
||||
cursor = db.cursor()
|
||||
sql = """SELECT * FROM HOS"""
|
||||
@@ -108,7 +114,13 @@ class SelectDatabase: # 定义类
|
||||
|
||||
def ret_doctor(self): # 定义函数,返回字典——医生(DOC)这个表
|
||||
# 连接服务器Server的数据库:
|
||||
db = pymysql.connect(host='localhost', user='root', password='111111', db=f'{self.database}', charset='utf8')
|
||||
db = pymysql.connect(
|
||||
host="localhost",
|
||||
user="root",
|
||||
password="111111",
|
||||
db=f"{self.database}",
|
||||
charset="utf8",
|
||||
)
|
||||
# 使用操作游标:
|
||||
cursor = db.cursor()
|
||||
sql = """SELECT * FROM DOC"""
|
||||
@@ -147,7 +159,13 @@ class SelectDatabase: # 定义类
|
||||
|
||||
def ret_patient(self): # 定义函数,返回字典——病人(PAT)这个表
|
||||
# 连接服务器Server的数据库:
|
||||
db = pymysql.connect(host='localhost', user='root', password='111111', db=f'{self.database}', charset='utf8')
|
||||
db = pymysql.connect(
|
||||
host="localhost",
|
||||
user="root",
|
||||
password="111111",
|
||||
db=f"{self.database}",
|
||||
charset="utf8",
|
||||
)
|
||||
# 使用操作游标:
|
||||
cursor = db.cursor()
|
||||
sql = """SELECT * FROM PAT"""
|
||||
@@ -182,7 +200,13 @@ class SelectDatabase: # 定义类
|
||||
|
||||
def ret_diagnosis(self): # 定义函数,返回字典——诊断(DIA)这个表
|
||||
# 连接服务器Server的数据库:
|
||||
db = pymysql.connect(host='localhost', user='root', password='111111', db=f'{self.database}', charset='utf8')
|
||||
db = pymysql.connect(
|
||||
host="localhost",
|
||||
user="root",
|
||||
password="111111",
|
||||
db=f"{self.database}",
|
||||
charset="utf8",
|
||||
)
|
||||
# 使用操作游标:
|
||||
cursor = db.cursor()
|
||||
sql = """SELECT * FROM DIA"""
|
||||
@@ -228,7 +252,13 @@ class SelectDatabase: # 定义类
|
||||
|
||||
def ret_define(self, sql): # 定义函数,返回自定义信息的字典
|
||||
# 连接服务器Server的数据库:
|
||||
db = pymysql.connect(host='localhost', user='root', password='111111', db=f'{self.database}', charset='utf8')
|
||||
db = pymysql.connect(
|
||||
host="localhost",
|
||||
user="root",
|
||||
password="111111",
|
||||
db=f"{self.database}",
|
||||
charset="utf8",
|
||||
)
|
||||
try:
|
||||
# 使用操作游标:
|
||||
cursor = db.cursor()
|
||||
@@ -253,12 +283,12 @@ def main(hospital_name):
|
||||
PAT.PAT_ID = DIA.PAT_ID
|
||||
"""
|
||||
hospital_name = hospital_name.replace("\n", "")
|
||||
if hospital_name == 'xx大学附属医院':
|
||||
database = SelectDatabase('Hospital1')
|
||||
if hospital_name == "xx大学附属医院":
|
||||
database = SelectDatabase("Hospital1")
|
||||
database.ret_define(sql)
|
||||
|
||||
elif hospital_name == 'xx阳光社区附属医院':
|
||||
database = SelectDatabase('Hospital2')
|
||||
elif hospital_name == "xx阳光社区附属医院":
|
||||
database = SelectDatabase("Hospital2")
|
||||
database.ret_define(sql)
|
||||
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user