Spaces:
Sleeping
Sleeping
File size: 5,027 Bytes
13ba451 |
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
from sqlalchemy.orm import sessionmaker
from models import Database_Entity
from repository import ConfigDatabase as cf
detail_chat = Database_Entity.DetailChat
chat_history = Database_Entity.ChatHistory
def getListDetailChatByChatId(chat_id: int) -> detail_chat:
try:
engine = cf.get_db_engine1()
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with Session() as session:
chat_record= session.query(detail_chat).filter(detail_chat.chat_id == chat_id)
session.commit()
if chat_record:
session.close()
return chat_record
else:
session.close()
return None
except:
engine = cf.get_db_engine()
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with Session() as session:
chat_record= session.query(detail_chat).filter(detail_chat.chat_id == chat_id)
session.commit()
if chat_record:
session.close()
return chat_record
else:
session.close()
return None
def addDetailChat(chat_id: int, YouMessage: str, AiMessage: str, data_relevant: str, source_file: str) -> None:
try:
engine = cf.get_db_engine()
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with Session() as session:
new_user = detail_chat(
chat_id = chat_id,
YouMessage = YouMessage,
AiMessage = AiMessage,
data_relevant = data_relevant,
source_file = source_file
)
session.add(new_user)
session.commit()
return new_user.id
session.close()
except:
engine = cf.get_db_engine1()
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with Session() as session:
new_user = detail_chat(
chat_id=chat_id,
YouMessage=YouMessage,
AiMessage=AiMessage,
data_relevant=data_relevant,
source_file=source_file
)
session.add(new_user)
session.commit()
return new_user.id
session.close()
def getDetailChatByChatId(id: int) -> detail_chat:
try:
engine = cf.get_db_engine()
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with Session() as session:
try:
chat = session.query(detail_chat).filter(detail_chat.id == id).one_or_none()
return chat
except:
session.close()
return False
except:
engine = cf.get_db_engine1()
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with Session() as session:
try:
chat = session.query(detail_chat.id,detail_chat.data_relevant,detail_chat.source_file).filter(detail_chat.id == id).one_or_none()
session.commit()
session.close()
return chat
except:
session.close()
return False
def delete_chat_detail(chat_name: str) -> bool:
try:
engine = cf.get_db_engine()
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with Session() as session:
try:
detail_chat2 = session.query(detail_chat).filter(detail_chat.chat_id == chat_history.id).filter(chat_history.name_chat == chat_name)
session.query(detail_chat).filter(detail_chat.chat_id == chat_history.id).filter(chat_history.name_chat == chat_name).delete(synchronize_session=False)
session.commit()
session.close()
return True
except:
session.close()
return False
except:
engine = cf.get_db_engine1()
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with Session() as session:
try:
session.query(detail_chat).filter(detail_chat.chat_id == chat_history.id).filter(chat_history.name_chat == chat_name).delete(synchronize_session=False)
session.commit()
session.close()
return True
except:
session.close()
return False
def delete_chat_detail_by_id(id_chat_detail: int) -> bool:
try:
engine = cf.get_db_engine()
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with Session() as session:
try:
session.query(detail_chat).filter(detail_chat.id == id_chat_detail).delete(synchronize_session=False)
session.commit()
session.close()
return True
except:
session.close()
return False
except:
engine = cf.get_db_engine1()
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with Session() as session:
try:
session.query(detail_chat).filter(detail_chat.chat_id == id_chat_detail).delete(synchronize_session=False)
session.commit()
session.close()
return True
except:
session.close()
return False |