From 2e809d7751b76baade839884ea9a312ffc7e25b7 Mon Sep 17 00:00:00 2001 From: thsrite Date: Sun, 9 Jun 2024 18:53:47 +0800 Subject: [PATCH] =?UTF-8?q?fix=20v1.2=E8=B0=83=E6=95=B4=E4=BA=A4=E4=BA=92?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=BF=94=E5=9B=9E=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- package.json | 3 +- plugins/sqlexecute/__init__.py | 52 ++++++++++++++++++++++++++-------- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 87bc3c5..c497467 100644 --- a/README.md +++ b/README.md @@ -40,5 +40,5 @@ MoviePilot三方插件市场:https://github.com/thsrite/MoviePilot-Plugins/ - 热门媒体订阅 v1.7 - [HomePage v1.2](docs%2FHomePage.md) - 目录监控(统一入库消息增强版) v1.0 -- Sql执行器 v1.1 +- Sql执行器 v1.2 - 命令执行器 v1.2 \ No newline at end of file diff --git a/package.json b/package.json index 001b679..61337e4 100644 --- a/package.json +++ b/package.json @@ -484,11 +484,12 @@ "name": "Sql执行器", "description": "自定义MoviePilot数据库Sql执行。", "labels": "工具", - "version": "1.1", + "version": "1.2", "icon": "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/sqlite.png", "author": "thsrite", "level": 1, "history": { + "v1.2": "调整交互命令返回信息", "v1.1": "支持交互命令/sql [command]执行,需主程序1.9.4+", "v1.0": "自定义MoviePilot数据库Sql执行" } diff --git a/plugins/sqlexecute/__init__.py b/plugins/sqlexecute/__init__.py index cdcc550..3aff415 100644 --- a/plugins/sqlexecute/__init__.py +++ b/plugins/sqlexecute/__init__.py @@ -4,7 +4,7 @@ from app.core.event import eventmanager, Event from app.plugins import _PluginBase from typing import Any, List, Dict, Tuple from app.log import logger -from app.schemas.types import EventType +from app.schemas.types import EventType, MessageChannel class SqlExecute(_PluginBase): @@ -15,7 +15,7 @@ class SqlExecute(_PluginBase): # 插件图标 plugin_icon = "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/sqlite.png" # 插件版本 - plugin_version = "1.1" + plugin_version = "1.2" # 插件作者 plugin_author = "thsrite" # 作者主页 @@ -54,7 +54,21 @@ class SqlExecute(_PluginBase): # 执行SQL语句 cursor.execute(sql) - logger.info(cursor.fetchall()) + rows = cursor.fetchall() + if 'select' in sql.lower(): + # 获取列名 + columns = [desc[0] for desc in cursor.description] + # 将查询结果转换为key-value对的列表 + results = [] + for row in rows: + result = dict(zip(columns, row)) + results.append(result) + result = "\n".join([str(i) for i in results]) + else: + result = "\n".join([str(i) for i in rows]) + + result = str(result).replace("'", "\"") + logger.info(result) except Exception as e: logger.error(f"SQL语句执行失败 {str(e)}") return @@ -92,16 +106,30 @@ class SqlExecute(_PluginBase): # 执行SQL语句 try: - for sql in self._sql.split("\n"): - logger.info(f"开始执行SQL语句 {sql}") - # 执行SQL语句 - cursor.execute(sql) + # 执行SQL语句 + cursor.execute(args) + rows = cursor.fetchall() + if 'select' in args.lower(): + # 获取列名 + columns = [desc[0] for desc in cursor.description] + # 将查询结果转换为key-value对的列表 + results = [] + for row in rows: + result = dict(zip(columns, row)) + results.append(result) + result = "\n".join([str(i) for i in results]) + else: + result = "\n".join([str(i) for i in rows]) - logger.info(cursor.fetchall()) - self.post_message(channel=event.event_data.get("channel"), - title="SQL执行结果", - text='\n'.join(cursor.fetchall()), - userid=event.event_data.get("user")) + result = str(result).replace("'", "\"") + logger.info(result) + + if event.event_data.get("channel") == MessageChannel.Telegram: + result = f"```plaintext\n{result}\n```" + self.post_message(channel=event.event_data.get("channel"), + title="SQL执行结果", + text=result, + userid=event.event_data.get("user")) except Exception as e: logger.error(f"SQL语句执行失败 {str(e)}") return