mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 16:50:02 +08:00
feat✨: webui新增数据库api
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
from .database import *
|
||||
from .main import *
|
||||
from .manage import *
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
from os import name
|
||||
from typing import Optional
|
||||
|
||||
import nonebot
|
||||
from fastapi import APIRouter, Request
|
||||
from nonebot.drivers import Driver
|
||||
from tortoise import Tortoise
|
||||
from tortoise.exceptions import OperationalError
|
||||
|
||||
from configs.config import NICKNAME
|
||||
from services.db_context import TestSQL
|
||||
from utils.utils import get_matchers
|
||||
|
||||
from ....base_model import QueryModel, Result
|
||||
from ....config import QueryDateType
|
||||
from ....utils import authentication
|
||||
from .models.model import SqlModel, SqlText
|
||||
from .models.sql_log import SqlLog
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
|
||||
SQL_DICT = {}
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def _():
|
||||
for matcher in get_matchers(True):
|
||||
if _plugin := matcher.plugin:
|
||||
try:
|
||||
_module = _plugin.module
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
plugin_name = matcher.plugin_name
|
||||
if plugin_name in SQL_DICT:
|
||||
raise ValueError(f"{plugin_name} 常用SQL plugin_name 重复")
|
||||
SqlModel(
|
||||
name=getattr(_module, "__plugin_name__", None) or plugin_name or "",
|
||||
plugin_name=plugin_name or "",
|
||||
sql_list=getattr(_module, "sql_list", []),
|
||||
)
|
||||
SQL_DICT[plugin_name] = SqlModel
|
||||
|
||||
|
||||
@router.post("/exec_sql", dependencies=[authentication()], description="执行sql")
|
||||
async def _(sql: SqlText, request: Request) -> Result:
|
||||
ip = request.client.host if request.client else "unknown"
|
||||
try:
|
||||
if sql.sql.lower().startswith("select"):
|
||||
db = Tortoise.get_connection("default")
|
||||
res = await db.execute_query_dict(sql.sql)
|
||||
return Result.ok(res, "执行成功啦!")
|
||||
else:
|
||||
result = await TestSQL.raw(sql.sql)
|
||||
await SqlLog.add(ip or "0.0.0.0", sql.sql, str(result))
|
||||
return Result.ok(info="执行成功啦!")
|
||||
except OperationalError as e:
|
||||
await SqlLog.add(ip or "0.0.0.0", sql.sql, str(e), False)
|
||||
return Result.warning_(f"sql执行错误: {e}")
|
||||
|
||||
|
||||
@router.post("/get_sql_log", dependencies=[authentication()], description="sql日志列表")
|
||||
async def _(query: QueryModel) -> Result:
|
||||
data = await SqlLog.all().offset((query.index - 1) * query.size).limit(query.size)
|
||||
return Result.ok(data)
|
||||
|
||||
|
||||
@router.get("/get_sql", dependencies=[authentication()], description="常用sql")
|
||||
async def _(plugin_name: Optional[str] = None) -> Result:
|
||||
if plugin_name:
|
||||
return Result.ok(SQL_DICT.get(plugin_name))
|
||||
return Result.ok(SQL_DICT)
|
||||
@@ -0,0 +1,23 @@
|
||||
from typing import List
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from utils.models import CommonSql
|
||||
|
||||
|
||||
class SqlText(BaseModel):
|
||||
"""
|
||||
sql语句
|
||||
"""
|
||||
|
||||
sql: str
|
||||
|
||||
|
||||
class SqlModel(BaseModel):
|
||||
|
||||
name: str
|
||||
"""插件中文名称"""
|
||||
plugin_name: str
|
||||
"""插件名称"""
|
||||
sql_list: List[CommonSql]
|
||||
"""插件列表"""
|
||||
@@ -0,0 +1,40 @@
|
||||
from typing import Optional, Union
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from services.db_context import Model
|
||||
|
||||
|
||||
class SqlLog(Model):
|
||||
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
ip = fields.CharField(255)
|
||||
"""ip"""
|
||||
sql = fields.CharField(255)
|
||||
"""sql"""
|
||||
result = fields.CharField(255, null=True)
|
||||
"""结果"""
|
||||
is_suc = fields.BooleanField(default=True)
|
||||
"""是否成功"""
|
||||
create_time = fields.DatetimeField(auto_now_add=True)
|
||||
"""创建时间"""
|
||||
|
||||
class Meta:
|
||||
table = "sql_log"
|
||||
table_description = "sql执行日志"
|
||||
|
||||
@classmethod
|
||||
async def add(
|
||||
cls, ip: str, sql: str, result: Optional[str] = None, is_suc: bool = True
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
获取用户在群内的等级
|
||||
参数:
|
||||
:param ip: ip
|
||||
:param sql: sql
|
||||
:param result: 返回结果
|
||||
:param is_suc: 是否成功
|
||||
"""
|
||||
await cls.create(ip=ip, sql=sql, result=result, is_suc=is_suc)
|
||||
Reference in New Issue
Block a user