olivebot/core/member_db.py
xszyou 87ed1c4425 Fay年翻更新
- 升级Agent(chat_module=agent切换):升级到langgraph react agent逻辑、集成到主分支fay中、基于自动决策工具调用机制、基于日程跟踪的主动沟通、支持外部观测数据传入;
- 修复因线程同步问题导致的配置文件读写不稳定
- 聊天采纳功能的bug修复
2024-11-20 23:44:47 +08:00

145 lines
3.8 KiB
Python

import sqlite3
import time
import threading
import functools
def synchronized(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
with self.lock:
return func(self, *args, **kwargs)
return wrapper
__member_db = None
def new_instance():
global __member_db
if __member_db is None:
__member_db = Member_Db()
__member_db.init_db()
return __member_db
class Member_Db:
def __init__(self) -> None:
self.lock = threading.Lock()
#初始化
def init_db(self):
conn = sqlite3.connect('user_profiles.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS T_Member
(id INTEGER PRIMARY KEY autoincrement,
username TEXT NOT NULL UNIQUE);''')
conn.commit()
conn.close()
# 添加新用户
@synchronized
def add_user(self, username):
if self.is_username_exist(username) == "notexists":
conn = sqlite3.connect('user_profiles.db')
c = conn.cursor()
c.execute('INSERT INTO T_Member (username) VALUES (?)', (username,))
conn.commit()
conn.close()
return "success"
else:
return f"Username '{username}' already exists."
# 修改用户名
@synchronized
def update_user(self, username, new_username):
if self.is_username_exist(new_username) == "notexists":
conn = sqlite3.connect('user_profiles.db')
c = conn.cursor()
c.execute('UPDATE T_Member SET username = ? WHERE username = ?', (new_username, username))
conn.commit()
conn.close()
return "success"
else:
return f"Username '{new_username}' already exists."
# 删除用户
@synchronized
def delete_user(self, username):
conn = sqlite3.connect('user_profiles.db')
c = conn.cursor()
c.execute('DELETE FROM T_Member WHERE username = ?', (username,))
conn.commit()
conn.close()
return "success"
# 检查用户名是否已存在
def is_username_exist(self, username):
conn = sqlite3.connect('user_profiles.db')
c = conn.cursor()
c.execute('SELECT COUNT(*) FROM T_Member WHERE username = ?', (username,))
result = c.fetchone()[0]
conn.close()
if result > 0:
return "exists"
else:
return "notexists"
#根据username查询uid
def find_user(self, username):
conn = sqlite3.connect('user_profiles.db')
c = conn.cursor()
c.execute('SELECT * FROM T_Member WHERE username = ?', (username,))
result = c.fetchone()
conn.close()
if result is None:
return 0
else:
return result[0]
#根据uid查询username
def find_username_by_uid(self, uid):
conn = sqlite3.connect('user_profiles.db')
c = conn.cursor()
c.execute('SELECT username FROM T_Member WHERE id = ?', (uid,))
result = c.fetchone()
conn.close()
if result is None:
return 0
else:
return result[0]
@synchronized
def query(self, sql):
try:
conn = sqlite3.connect('user_profiles.db')
c = conn.cursor()
c.execute(sql)
results = c.fetchall()
conn.commit()
conn.close()
return results
except Exception as e:
return f"执行时发生错误:{str(e)}"
# 获取所有用户
@synchronized
def get_all_users(self):
conn = sqlite3.connect('user_profiles.db')
c = conn.cursor()
c.execute('SELECT * FROM T_Member')
results = c.fetchall()
conn.close()
return results