SQL 执行 MCP 工具开发(二)

Wails 客户端

bash 复制代码
{
  "$schema": "https://wails.io/schemas/config.v2.json",
  "name": "execute-sql",
  "outputfilename": "execute-sql",
  "frontend:dir": "E:/Workspace/MCP/tools/execute_sql/ui",
  "frontend:install": "npm install",
  "frontend:build": "npm run build",
  "frontend:dev:watcher": "npm run dev",
  "frontend:dev:serverUrl": "auto",
  "author": {
    "name": "jiudan",
    "email": "jiudan1021@foxmail.com"
  }
}

wails.json 配置信息

Go 复制代码
import (
	"context"
	"embed"
	
	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/options"
	"github.com/wailsapp/wails/v2/pkg/options/assetserver"
	"github.com/wailsapp/wails/v2/pkg/runtime"
)

//go:embed all:dist
var assets embed.FS

type App struct {
	ctx context.Context
}

func NewApp() *App {
	return &App{}
}

func (a *App) startup(ctx context.Context) {
	a.ctx = ctx
}

func (a *App) beforeClose(ctx context.Context) (prevent bool) {
	selection, err := runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
		Type:    runtime.QuestionDialog,
		Title:   "确认关闭",
		Message: "确定要退出应用吗?",
		Buttons: []string{"取消", "确定"},
	})
	if err != nil {
		return false
	}
	if selection == "取消" {
		return true
	}
	return false
}

func RunApp(title string, width, height, minWidth, minHeight int, onTop bool, disableResize bool, frameless bool, closeConfirm bool) {
	// 创建APP
	app := NewApp()
	// 配置
	option := &options.App{
		Title:         title,
		Width:         width,
		Height:        height,
		Frameless:     frameless,
		AlwaysOnTop:   onTop,
		DisableResize: disableResize,
		OnStartup:     app.startup,
		AssetServer: &assetserver.Options{
			Assets: assets,
		},
		BackgroundColour: &options.RGBA{R: 255, G: 255, B: 255, A: 1},
		Bind: []interface{}{
			app,
		},
	}
	// 配置最小宽高
	if minWidth > 0 {
		option.MinWidth = minWidth
	}
	if minHeight > 0 {
		option.MinHeight = minHeight
	}
	// 关闭确认
	if closeConfirm {
		option.OnBeforeClose = app.beforeClose
	}
	// 运行 APP
	err := wails.Run(option)
	if err != nil {
		panic(err)
	}
}

页面数据交互

Go 复制代码
var Data = make(map[string]interface{})

func (a *App) GetData() map[string]any {
	return Data
}

func (a *App) SetData(data map[string]any) bool {
	Data = make(map[string]any)
	for k, v := range data {
		Data[k] = v
	}
	return true
}

func (a *App) UpdateData(key string, value any) bool {
	Data[key] = value
	return true
}

与后端交互的API,这里只需要读取和更新数据即可

参数解析

Go 复制代码
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

func str2bool(s string) bool {
	return strings.ToLower(s) == "t" || strings.ToLower(s) == "true" ||
		strings.ToLower(s) == "1" || strings.ToLower(s) == "y"
}

func main() {
	title := flag.String("title", "RunSQL", "标题")
	width := flag.Int("width", 800, "页面宽度")
	height := flag.Int("height", 600, "页面高度")
	minWidth := flag.Int("min_width", 0, "页面最小宽度")
	minHeight := flag.Int("min_height", 0, "页面最小高度")
	onTop := flag.String("on_top", "false", "页面是否置顶")
	disableResize := flag.String("disable_resize", "false", "页面是否可缩放")
	frameless := flag.String("frameless", "true", "页面是否无边框")
	dataPath := flag.String("data_path", "./data.json", "数据文件路径")
	resultPath := flag.String("result_path", "./result.json", "结果文件路径")
	closeConfirm := flag.String("close_confirm", "false", "是否显示关闭确认对话框")
	flag.Parse()
	// 检查数据文件路径
	if *dataPath == "" {
		panic("数据文件路径不能为空")
	}
	if _, err := os.Stat(*dataPath); os.IsNotExist(err) {
		panic(fmt.Sprintf("数据文件不存在: %s", *dataPath))
	}
	// 处理结果文件路径
	if *resultPath == "" {
		filename := filepath.Base(*dataPath)
		*resultPath = strings.TrimSuffix(filename, filepath.Ext(filename)) + "-result.json"
	}
	// 读取并解析JSON数据
	jsonData, err := os.ReadFile(*dataPath)
	if err != nil {
		panic(err)
	}
	err = json.Unmarshal(jsonData, &Data)
	if err != nil {
		panic(err)
	}
	// 运行应用(不再传递assetsDir参数)
	RunApp(*title, *width, *height, *minWidth, *minHeight,
		str2bool(*onTop), str2bool(*disableResize), str2bool(*frameless), str2bool(*closeConfirm),
	)
	// 输出结果
	dataString, err := json.Marshal(Data)
	if err != nil {
		panic(err)
	}
	err = os.WriteFile(*resultPath, dataString, 0644)
	if err != nil {
		panic(err)
	}
}

编译运行

bash 复制代码
 wails build   

注意配置的 frontend:dir/wailsjs 会生成对应的 js 文件,前端调用 Go 的方法通过调用 Js 方法实现。

相关推荐
Micro麦可乐2 小时前
Spring Boot 实战:从零设计一个短链系统(含完整代码与数据库设计)
数据库·spring boot·后端·哈希算法·雪花算法·短链系统
码农阿豪2 小时前
从零到一:Spring Boot快速接入金仓数据库实战
数据库·spring boot·后端
鼎讯信通2 小时前
风电光缆运维提质增效:G-4000A 光缆故障追踪仪破解风场巡检难题
运维·网络·数据库
三十..3 小时前
MySQL 从入门到高可用架构实战精要
运维·数据库·mysql
cfm_29144 小时前
Redis五大基本数据结构底层了解
数据结构·数据库·redis
真实的菜4 小时前
Redis 从入门到精通(十二):典型业务场景实战 —— 排行榜、限流器、秒杀系统、Session 共享
数据库·redis·python
你想考研啊4 小时前
mysql数据库导出导入
数据库·mysql·oracle
十年编程老舅5 小时前
Linux DRM:底层逻辑与实践架构
数据库·mysql
The Sheep 20235 小时前
Vue复习
linux·服务器·数据库
云边有个稻草人6 小时前
深度解析:KingbaseES高可用架构落地原理与生产运维实战
数据库·读写分离·数据库运维·金仓数据库·国产数据库技术·数据备份恢复