Skip to content

Commit 9c50277

Browse files
committed
添加orm组件
1 parent 8039bff commit 9c50277

File tree

2 files changed

+57
-57
lines changed

2 files changed

+57
-57
lines changed

model/database_service.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
from sqlalchemy.inspection import inspect
2-
3-
from model.db_connection_pool import get_db_pool
4-
5-
db_pool = get_db_pool()
6-
7-
8-
class DatabaseService:
9-
"""
10-
数据库服务类
11-
"""
12-
13-
def __init__(self):
14-
self.engine = db_pool.get_engine()
15-
16-
def get_table_schema(self):
17-
"""
18-
获取数据中所有表结构
19-
:return: 表结构
20-
"""
21-
inspector = inspect(self.engine)
22-
table_info = {}
23-
24-
for table_name in inspector.get_table_names():
25-
columns = {
26-
col["name"]: {"type": str(col["type"]), "comment": str(col["comment"])}
27-
for col in inspector.get_columns(table_name)
28-
}
29-
foreign_keys = [
30-
f"{fk['constrained_columns'][0]} -> {fk['referred_table']}.{fk['referred_columns'][0]}"
31-
for fk in inspector.get_foreign_keys(table_name)
32-
]
33-
34-
table_info[table_name] = {"columns": columns, "foreign_keys": foreign_keys}
35-
36-
print(table_info)
1+
# from sqlalchemy.inspection import inspect
2+
#
3+
# from model.db_connection_pool import get_db_pool
4+
#
5+
# db_pool = get_db_pool()
6+
#
7+
#
8+
# class DatabaseService:
9+
# """
10+
# 数据库服务类
11+
# """
12+
#
13+
# def __init__(self):
14+
# self.engine = db_pool.get_engine()
15+
#
16+
# def get_table_schema(self):
17+
# """
18+
# 获取数据中所有表结构
19+
# :return: 表结构
20+
# """
21+
# inspector = inspect(self.engine)
22+
# table_info = {}
23+
#
24+
# for table_name in inspector.get_table_names():
25+
# columns = {
26+
# col["name"]: {"type": str(col["type"]), "comment": str(col["comment"])}
27+
# for col in inspector.get_columns(table_name)
28+
# }
29+
# foreign_keys = [
30+
# f"{fk['constrained_columns'][0]} -> {fk['referred_table']}.{fk['referred_columns'][0]}"
31+
# for fk in inspector.get_foreign_keys(table_name)
32+
# ]
33+
#
34+
# table_info[table_name] = {"columns": columns, "foreign_keys": foreign_keys}
35+
#
36+
# print(table_info)

services/user_service.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@
2020
logger = logging.getLogger(__name__)
2121

2222
mysql_client = MysqlUtil()
23-
pool = get_db_pool()
23+
# pool = get_db_pool()
2424

2525

2626
async def authenticate_user(username, password):
2727
"""验证用户凭据并返回用户信息或 None"""
28-
with pool.get_session() as session:
29-
session: Session = session
30-
user_dict = model_to_dict(
31-
session.query(TUser).filter(TUser.userName == username).filter(TUser.password == password).first()
32-
)
33-
if user_dict:
34-
return user_dict
35-
# sql = f"""select * from t_user where userName='{username}' and password='{password}'"""
36-
# report_dict = MysqlUtil().query_mysql_dict(sql)
37-
# if len(report_dict) > 0:
38-
# return report_dict[0]
39-
# else:
40-
# return False
28+
# with pool.get_session() as session:
29+
# session: Session = session
30+
# user_dict = model_to_dict(
31+
# session.query(TUser).filter(TUser.userName == username).filter(TUser.password == password).first()
32+
# )
33+
# if user_dict:
34+
# return user_dict
35+
sql = f"""select * from t_user where userName='{username}' and password='{password}'"""
36+
report_dict = MysqlUtil().query_mysql_dict(sql)
37+
if len(report_dict) > 0:
38+
return report_dict[0]
39+
else:
40+
return False
4141

4242

4343
async def generate_jwt_token(user_id, username):
@@ -265,13 +265,13 @@ def query_user_qa_record(chat_id):
265265
:param chat_id:
266266
:return:
267267
"""
268-
with pool.get_session() as session:
269-
records = (
270-
session.query(TUserQaRecord).filter(TUserQaRecord.chat_id == chat_id).order_by(TUserQaRecord.id.desc())
271-
)
272-
return model_to_dict(records)
273-
# sql = f"select * from t_user_qa_record where chat_id='{chat_id}' order by id desc limit 1"
274-
# return mysql_client.query_mysql_dict(sql)
268+
# with pool.get_session() as session:
269+
# records = (
270+
# session.query(TUserQaRecord).filter(TUserQaRecord.chat_id == chat_id).order_by(TUserQaRecord.id.desc())
271+
# )
272+
# return model_to_dict(records)
273+
sql = f"select * from t_user_qa_record where chat_id='{chat_id}' order by id desc limit 1"
274+
return mysql_client.query_mysql_dict(sql)
275275

276276

277277
async def send_dify_feedback(chat_id, rating):

0 commit comments

Comments
 (0)