2024-08-17 11:25:51 +08:00
|
|
|
from .cfg import config
|
|
|
|
import uuid
|
|
|
|
import aiomysql
|
|
|
|
import sys
|
|
|
|
|
|
|
|
HOST = config["db"]["host"]
|
|
|
|
PORT = config["db"]["port"]
|
|
|
|
USER = config["db"]["user"]
|
|
|
|
PASSWD = config["db"]["passwd"]
|
|
|
|
DB = config["db"]["db"]
|
|
|
|
|
|
|
|
MAXSIZE = config["db"]["maxsize"]
|
|
|
|
MINSIZE = config["db"]["minsize"]
|
|
|
|
|
|
|
|
|
|
|
|
async def connect_db():
|
|
|
|
global db
|
|
|
|
sys.stdout.write("connect db\n")
|
|
|
|
db = await aiomysql.create_pool(
|
|
|
|
host=HOST,
|
|
|
|
port=PORT,
|
|
|
|
user=USER,
|
|
|
|
password=PASSWD,
|
|
|
|
db=DB,
|
|
|
|
maxsize=MAXSIZE,
|
|
|
|
minsize=MINSIZE
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-08-30 17:43:49 +08:00
|
|
|
async def recreate():
|
|
|
|
global db
|
|
|
|
db.close()
|
|
|
|
db = await aiomysql.create_pool(
|
|
|
|
host=HOST,
|
|
|
|
port=PORT,
|
|
|
|
user=USER,
|
|
|
|
password=PASSWD,
|
|
|
|
db=DB,
|
|
|
|
maxsize=MAXSIZE,
|
|
|
|
minsize=MINSIZE
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-08-17 11:25:51 +08:00
|
|
|
async def create_db():
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
await (await conn.cursor()).execute(
|
|
|
|
"CREATE TABLE IF NOT EXISTS users (username VARCHAR(255) PRIMARY KEY, password VARCHAR(255), email VARCHAR(255), accoutpwd VARCHAR(255))"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def get_user(username: str):
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
await cur.execute("SELECT username, password FROM users WHERE username = %s", (username,))
|
|
|
|
result = await cur.fetchone()
|
|
|
|
if result:
|
|
|
|
return result[1]
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
async def get_user_passwd(username: str):
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
await cur.execute("SELECT accoutpwd FROM users WHERE username = %s", (username,))
|
|
|
|
result = await cur.fetchone()
|
|
|
|
if result:
|
|
|
|
return result[0]
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
async def create_user(username: str, password: str, email: str):
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
# 检查用户是否已存在
|
|
|
|
await cur.execute("SELECT username FROM users WHERE username = %s", (username,))
|
|
|
|
if await cur.fetchone():
|
|
|
|
return 1
|
|
|
|
# 创建新用户
|
|
|
|
await cur.execute("INSERT INTO users (username, password, email, accoutpwd) VALUES (%s, %s, %s, %s)", (username, password, email, uuid.uuid4().hex))
|
|
|
|
await conn.commit()
|
2024-08-30 17:43:49 +08:00
|
|
|
|
|
|
|
await recreate()
|
2024-08-17 11:25:51 +08:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
async def check_user(username: str):
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
# 检查用户是否已存在
|
|
|
|
await cur.execute("SELECT username FROM users WHERE username = %s", (username,))
|
|
|
|
if await cur.fetchone():
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
async def update_passwd(username: str, password: str):
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
await cur.execute("UPDATE users SET password = %s WHERE username = %s", (password, username))
|
|
|
|
await conn.commit()
|
2024-08-30 17:43:49 +08:00
|
|
|
await recreate()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
async def delete_user(username: str):
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
await cur.execute("DELETE FROM users WHERE username = %s;COMMIT;", (username))
|
|
|
|
await conn.commit()
|
|
|
|
await recreate()
|
2024-08-17 11:25:51 +08:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
async def get_email(username: str):
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
await cur.execute("SELECT email FROM users WHERE username = %s", (username,))
|
|
|
|
result = await cur.fetchone()
|
|
|
|
if result:
|
|
|
|
return result[0]
|
|
|
|
return None
|
2024-08-30 17:43:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
async def get_userscount():
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
await cur.execute("SELECT COUNT(*) FROM users")
|
|
|
|
result = await cur.fetchone()
|
|
|
|
if result:
|
|
|
|
return result[0]
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
async def search_user(page: int, username: str, PAGE_CNT):
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
username_search = ('WHERE username LIKE "%%'+username +
|
|
|
|
'%%"' if username != "" else "")
|
|
|
|
cmd = 'SELECT * FROM users '+username_search+' LIMIT ' + \
|
|
|
|
str(page*PAGE_CNT-PAGE_CNT)+','+str(PAGE_CNT)+';'
|
|
|
|
await cur.execute(cmd)
|
|
|
|
result = await cur.fetchall()
|
|
|
|
if result:
|
|
|
|
return [(i[0], i[2]) for i in result]
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
async def search_user_len(page: int, username: str, PAGE_CNT):
|
|
|
|
async with db.acquire() as conn:
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
username_search = ('WHERE username LIKE "%%'+username +
|
|
|
|
'%%"' if username != "" else "")
|
|
|
|
cmd = 'SELECT COUNT(*) FROM users '+username_search+';'
|
|
|
|
await cur.execute(cmd)
|
|
|
|
result = await cur.fetchone()
|
|
|
|
if result:
|
|
|
|
return result[0]
|
|
|
|
return None
|