From af807b5becd19d4f4e5b10b8b4d53289fb7c8856 Mon Sep 17 00:00:00 2001 From: levina <82658782+levina-lab@users.noreply.github.com> Date: Sun, 14 Nov 2021 07:15:13 +0700 Subject: [PATCH] added data_sql for saving data created data_sql for user data --- driver/database/data_sql.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 driver/database/data_sql.py diff --git a/driver/database/data_sql.py b/driver/database/data_sql.py new file mode 100644 index 0000000..fae5704 --- /dev/null +++ b/driver/database/data_sql.py @@ -0,0 +1,37 @@ +import threading +from driver.database import BASE, SESSION +from sqlalchemy import Column, String, UnicodeText + + +class Chats(BASE): + __tablename__ = "chats" + chat_id = Column(String(14), primary_key=True) + chat_name = Column(UnicodeText) + + def __init__(self, chat_id, chat_name=None): + self.chat_id = chat_id + self.chat_name = chat_name + + +Chats.__table__.create(checkfirst=True) + + +CHATS_LOCK = threading.RLock() +CHATS_DATA = set() + + +def del_chat(chat_id): + with CHATS_LOCK: + chat = SESSION.query(Chats).get(str(chat_id)) + if chat: + SESSION.delete(chat) + SESSION.commit() + + +def chatlists(): + global CHAT_ID + try: + CHAT_ID = {int(x.chat_id) for x in SESSION.query(Chats).all()} + return CHAT_ID + finally: + SESSION.close()