This commit is contained in:
qsc
2025-12-05 11:31:04 +08:00
5 changed files with 64 additions and 3 deletions
+58
View File
@@ -0,0 +1,58 @@
import aiosqlite
class AsyncSQLite:
def __init__(self, db_path: str):
self.db_path = db_path
self.conn = None
async def init(self):
if self.conn is None:
self.conn = await aiosqlite.connect(self.db_path)
self.conn.row_factory = aiosqlite.Row
async def close(self):
if self.conn:
await self.conn.close()
self.conn = None
async def fetch_one(self, sql: str, params=None):
await self.init()
async with self.conn.execute(sql, params or ()) as cursor:
row = await cursor.fetchone()
return dict(row) if row else None
async def fetch_all(self, sql: str, params=None):
await self.init()
async with self.conn.execute(sql, params or ()) as cursor:
rows = await cursor.fetchall()
return [dict(r) for r in rows]
async def execute(self, sql: str, params=None):
await self.init()
async with self.conn.execute(sql, params or ()):
await self.conn.commit()
return True
async def executemany(self, sql: str, params_list):
await self.init()
await self.conn.executemany(sql, params_list)
await self.conn.commit()
return True
async def insert_record(self, table: str, data: dict):
keys = ", ".join(f"`{k}`" for k in data.keys())
placeholders = ", ".join(['?'] * len(data))
sql = f"INSERT INTO `{table}` ({keys}) VALUES ({placeholders})"
return await self.execute(sql, tuple(data.values()))
async def update_record(self, table: str, data: dict, where: dict):
set_clause = ", ".join(f"`{k}`=?" for k in data.keys())
where_clause = " AND ".join(f"`{k}`=?" for k in where.keys())
sql = f"UPDATE `{table}` SET {set_clause} WHERE {where_clause}"
params = tuple(data.values()) + tuple(where.values())
return await self.execute(sql, params)
async def delete_record(self, table: str, where: dict):
where_clause = " AND ".join(f"`{k}`=?" for k in where.keys())
sql = f"DELETE FROM `{table}` WHERE {where_clause}"
return await self.execute(sql, tuple(where.values()))
+3
View File
@@ -1,5 +1,6 @@
import aiomysql import aiomysql
from astrbot.api import logger
class AsyncMySQL: class AsyncMySQL:
def __init__(self, db_config: dict): def __init__(self, db_config: dict):
@@ -34,6 +35,7 @@ class AsyncMySQL:
await cursor.execute(sql, params or ()) await cursor.execute(sql, params or ())
return await cursor.fetchall() return await cursor.fetchall()
async def execute(self, sql: str, params=None): async def execute(self, sql: str, params=None):
"""执行 SQLinsert/update/delete""" """执行 SQLinsert/update/delete"""
await self.init_pool() await self.init_pool()
@@ -71,6 +73,7 @@ class AsyncMySQL:
keys = ", ".join(f"`{k}`" for k in data.keys()) keys = ", ".join(f"`{k}`" for k in data.keys())
placeholders = ", ".join(["%s"] * len(data)) placeholders = ", ".join(["%s"] * len(data))
sql = f"INSERT INTO `{table}` ({keys}) VALUES ({placeholders})" sql = f"INSERT INTO `{table}` ({keys}) VALUES ({placeholders})"
logger.info(f"Executing SQL: {sql}")
return await self.execute(sql, tuple(data.values())) return await self.execute(sql, tuple(data.values()))
async def update_record(self, table: str, data: dict, where: dict): async def update_record(self, table: str, data: dict, where: dict):
+1 -1
View File
@@ -4,7 +4,7 @@ from typing import Dict, Any, Optional, List, Union
from astrbot.api import logger from astrbot.api import logger
from .request import APIClient from .request import APIClient
from .AsyncMySQL import AsyncMySQL from .async_mysql import AsyncMySQL
from .function_basic import load_template,flatten_field,extract_fields,gold_to_string from .function_basic import load_template,flatten_field,extract_fields,gold_to_string
class JX3Service: class JX3Service:
+1 -1
View File
@@ -2,8 +2,8 @@
import json import json
import aiohttp import aiohttp
from typing import Optional, Dict, Any, Union, List from typing import Optional, Dict, Any, Union, List
from aiohttp import ClientTimeout, ClientSession from aiohttp import ClientTimeout, ClientSession
from astrbot.api import logger from astrbot.api import logger
class APIClient: class APIClient:
+1 -1
View File
@@ -25,7 +25,7 @@ class Jx3ApiPlugin(Star):
#获取配置 #获取配置
self.conf = config self.conf = config
# 本地数据存储路径 # 本地数据存储路径
self.local_data_dir = StarTools.get_data_dir("astrbot_plugin_jx3api") self.local_data_dir = StarTools.get_data_dir("astrbot_plugin_jx3")
# api数据文件 # api数据文件
self.api_file_path = Path(__file__).parent / "api_config.json" self.api_file_path = Path(__file__).parent / "api_config.json"
# 读取文件内容 # 读取文件内容