chore(g3fo-db-ops): sync SKILL.md and db_ops implementation from project
Made-with: Cursor
This commit is contained in:
@@ -13,8 +13,17 @@ Guide for performing database operations in the G3FO project, including automate
|
|||||||
|
|
||||||
**本技能不再使用 MCP MySQL**,改用 Python 程序 `scripts/db_ops.py` 执行所有数据库操作。
|
**本技能不再使用 MCP MySQL**,改用 Python 程序 `scripts/db_ops.py` 执行所有数据库操作。
|
||||||
|
|
||||||
### 安装依赖
|
### 安装依赖与运行前提
|
||||||
|
|
||||||
|
> **重要:本技能依赖本机可用的 Python 解释器。若未安装 Python 或 `python` 不在 PATH 中,在 Cursor 的 Shell 里直接执行脚本会出现 Windows `Exit code: 9009`(命令未找到)错误。**
|
||||||
|
|
||||||
|
1. 在本机安装 Python(3.8+),并确认命令行中可以直接运行:
|
||||||
|
```bash
|
||||||
|
python --version
|
||||||
|
```
|
||||||
|
如命令无效,请将 Python 安装目录加入系统 PATH,或使用 `py` / `python3` 等本机实际命令名。
|
||||||
|
|
||||||
|
2. 安装依赖:
|
||||||
```bash
|
```bash
|
||||||
pip install -r .claude/skills/g3fo-db-ops/scripts/requirements.txt
|
pip install -r .claude/skills/g3fo-db-ops/scripts/requirements.txt
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ import json
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
from datetime import date, datetime, time
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
@@ -88,6 +90,16 @@ def get_connection(config: dict):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_value(value: Any) -> Any:
|
||||||
|
"""将 MySQL 返回的值转换为可 JSON 序列化的类型。"""
|
||||||
|
if isinstance(value, (datetime, date, time)):
|
||||||
|
return value.isoformat()
|
||||||
|
if isinstance(value, Decimal):
|
||||||
|
# 大多数场景下用 float 即可,避免 JSON 不支持 Decimal
|
||||||
|
return float(value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def cmd_query(args: argparse.Namespace, config: dict) -> dict:
|
def cmd_query(args: argparse.Namespace, config: dict) -> dict:
|
||||||
"""执行 SELECT 查询。"""
|
"""执行 SELECT 查询。"""
|
||||||
conn = get_connection(config)
|
conn = get_connection(config)
|
||||||
@@ -96,7 +108,10 @@ def cmd_query(args: argparse.Namespace, config: dict) -> dict:
|
|||||||
cursor.execute(args.sql, args.params or [])
|
cursor.execute(args.sql, args.params or [])
|
||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
return {"success": True, "data": rows, "rowCount": len(rows)}
|
normalized_rows = [
|
||||||
|
{k: _normalize_value(v) for k, v in row.items()} for row in rows
|
||||||
|
]
|
||||||
|
return {"success": True, "data": normalized_rows, "rowCount": len(normalized_rows)}
|
||||||
except MySQLError as e:
|
except MySQLError as e:
|
||||||
return {"success": False, "error": str(e)}
|
return {"success": False, "error": str(e)}
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
Reference in New Issue
Block a user