chore(g3fo-db-ops): sync SKILL.md and db_ops implementation from project
Made-with: Cursor
This commit is contained in:
@@ -19,6 +19,8 @@ import json
|
||||
import os
|
||||
import sys
|
||||
from typing import Any, Optional
|
||||
from datetime import date, datetime, time
|
||||
from decimal import Decimal
|
||||
|
||||
try:
|
||||
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:
|
||||
"""执行 SELECT 查询。"""
|
||||
conn = get_connection(config)
|
||||
@@ -96,7 +108,10 @@ def cmd_query(args: argparse.Namespace, config: dict) -> dict:
|
||||
cursor.execute(args.sql, args.params or [])
|
||||
rows = cursor.fetchall()
|
||||
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:
|
||||
return {"success": False, "error": str(e)}
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user