新增:连接管理、数据查询等功能
This commit is contained in:
161
app.go
161
app.go
@@ -2,16 +2,24 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go-desk/internal/api"
|
||||
"go-desk/internal/database"
|
||||
"go-desk/internal/filesystem"
|
||||
"go-desk/internal/storage"
|
||||
"go-desk/internal/system"
|
||||
"os"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
// App 应用结构体
|
||||
type App struct {
|
||||
ctx context.Context
|
||||
db *database.DB
|
||||
ctx context.Context
|
||||
db *database.DB
|
||||
connectionAPI *api.ConnectionAPI
|
||||
sqlAPI *api.SqlAPI
|
||||
tabAPI *api.TabAPI
|
||||
}
|
||||
|
||||
// NewApp 创建新的应用实例
|
||||
@@ -23,13 +31,27 @@ func NewApp() *App {
|
||||
func (a *App) startup(ctx context.Context) {
|
||||
a.ctx = ctx
|
||||
|
||||
// 初始化数据库连接
|
||||
db, err := database.Init()
|
||||
// 初始化 SQLite 本地存储(核心依赖,必须成功)
|
||||
// 如果失败,应用无法正常工作,应该 panic
|
||||
_, err := storage.Init()
|
||||
if err != nil {
|
||||
println("数据库连接失败:", err.Error())
|
||||
return
|
||||
panic(fmt.Sprintf("SQLite 初始化失败,应用无法启动: %v", err))
|
||||
}
|
||||
|
||||
// 初始化数据库连接(可选,用于测试功能)
|
||||
// 失败不影响核心功能,只记录日志
|
||||
appDB, err := database.Init()
|
||||
if err != nil {
|
||||
println("数据库连接失败(可选功能):", err.Error())
|
||||
} else {
|
||||
a.db = appDB
|
||||
}
|
||||
|
||||
// 初始化 API 层(依赖 storage)
|
||||
// 如果失败,应用无法正常工作,应该 panic
|
||||
if err := a.initAPIs(); err != nil {
|
||||
panic(fmt.Sprintf("API 初始化失败,应用无法启动: %v", err))
|
||||
}
|
||||
a.db = db
|
||||
}
|
||||
|
||||
// QueryUsers 查询用户列表
|
||||
@@ -120,3 +142,128 @@ func splitEnv(env string) []string {
|
||||
}
|
||||
return []string{env}
|
||||
}
|
||||
|
||||
// ========== 数据库连接管理接口 ==========
|
||||
|
||||
// initAPIs 初始化所有API(在startup中调用)
|
||||
func (a *App) initAPIs() error {
|
||||
var err error
|
||||
a.connectionAPI, err = api.NewConnectionAPI()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.sqlAPI, err = api.NewSqlAPI()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.tabAPI, err = api.NewTabAPI()
|
||||
return err
|
||||
}
|
||||
|
||||
// SaveDbConnection 保存数据库连接配置
|
||||
func (a *App) SaveDbConnection(req api.SaveConnectionRequest) error {
|
||||
return a.connectionAPI.SaveDbConnection(req)
|
||||
}
|
||||
|
||||
// ListDbConnections 获取连接列表
|
||||
func (a *App) ListDbConnections() ([]map[string]interface{}, error) {
|
||||
return a.connectionAPI.ListDbConnections()
|
||||
}
|
||||
|
||||
// DeleteDbConnection 删除连接配置
|
||||
func (a *App) DeleteDbConnection(id uint) error {
|
||||
return a.connectionAPI.DeleteDbConnection(id)
|
||||
}
|
||||
|
||||
// TestDbConnection 测试连接(通过已保存的连接ID)
|
||||
func (a *App) TestDbConnection(id uint) error {
|
||||
return a.connectionAPI.TestDbConnection(id)
|
||||
}
|
||||
|
||||
// TestDbConnectionWithParams 测试数据库连接(直接传入参数,不保存数据)
|
||||
func (a *App) TestDbConnectionWithParams(req api.TestConnectionRequest) error {
|
||||
return a.connectionAPI.TestDbConnectionWithParams(req)
|
||||
}
|
||||
|
||||
// ExecuteSQL 执行 SQL 语句
|
||||
// 注意:SQL 语句应该已经包含分页信息(LIMIT 和 OFFSET),由客户端添加
|
||||
func (a *App) ExecuteSQL(connectionId uint, sqlStr string, database string) (map[string]interface{}, error) {
|
||||
return a.sqlAPI.ExecuteSQL(connectionId, sqlStr, database)
|
||||
}
|
||||
|
||||
// GetDatabases 获取数据库列表
|
||||
func (a *App) GetDatabases(connectionId uint) ([]string, error) {
|
||||
return a.sqlAPI.GetDatabases(connectionId)
|
||||
}
|
||||
|
||||
// GetTables 获取表列表
|
||||
func (a *App) GetTables(connectionId uint, database string) ([]string, error) {
|
||||
return a.sqlAPI.GetTables(connectionId, database)
|
||||
}
|
||||
|
||||
// GetTableStructure 获取表结构
|
||||
func (a *App) GetTableStructure(connectionId uint, database, tableName string) (map[string]interface{}, error) {
|
||||
return a.sqlAPI.GetTableStructure(connectionId, database, tableName)
|
||||
}
|
||||
|
||||
// GetIndexes 获取索引列表
|
||||
func (a *App) GetIndexes(connectionId uint, database, tableName string) ([]map[string]interface{}, error) {
|
||||
return a.sqlAPI.GetIndexes(connectionId, database, tableName)
|
||||
}
|
||||
|
||||
// PreviewTableStructure 预览表结构变更
|
||||
func (a *App) PreviewTableStructure(connectionId uint, database, tableName string, structure map[string]interface{}) ([]string, error) {
|
||||
return a.sqlAPI.PreviewTableStructure(connectionId, database, tableName, structure)
|
||||
}
|
||||
|
||||
// UpdateTableStructure 更新表结构
|
||||
func (a *App) UpdateTableStructure(connectionId uint, database, tableName string, structure map[string]interface{}) ([]string, error) {
|
||||
return a.sqlAPI.UpdateTableStructure(connectionId, database, tableName, structure)
|
||||
}
|
||||
|
||||
// SaveResult 手动保存执行结果
|
||||
func (a *App) SaveResult(connectionId uint, database, sql string, resultType string, data interface{}, columns []string, rowsAffected int, executionTime int64) (map[string]interface{}, error) {
|
||||
return a.sqlAPI.SaveResult(connectionId, database, sql, resultType, data, columns, rowsAffected, executionTime)
|
||||
}
|
||||
|
||||
// GetResultHistory 获取结果历史
|
||||
func (a *App) GetResultHistory(connectionId *uint, keyword string, limit, offset int) (map[string]interface{}, error) {
|
||||
return a.sqlAPI.GetResultHistory(connectionId, keyword, limit, offset)
|
||||
}
|
||||
|
||||
// GetResultHistoryByID 根据ID获取结果历史
|
||||
func (a *App) GetResultHistoryByID(id uint) (map[string]interface{}, error) {
|
||||
return a.sqlAPI.GetResultHistoryByID(id)
|
||||
}
|
||||
|
||||
// DeleteResultHistory 删除结果历史
|
||||
func (a *App) DeleteResultHistory(id uint) error {
|
||||
return a.sqlAPI.DeleteResultHistory(id)
|
||||
}
|
||||
|
||||
// Reload 重新加载窗口(用于菜单项)
|
||||
func (a *App) Reload() {
|
||||
if a.ctx != nil {
|
||||
runtime.WindowReload(a.ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// ClearCache 清理本地缓存(用于菜单项)
|
||||
func (a *App) ClearCache() {
|
||||
if a.ctx != nil {
|
||||
// 发送事件到前端,让前端清理 localStorage
|
||||
runtime.EventsEmit(a.ctx, "clear-cache")
|
||||
}
|
||||
}
|
||||
|
||||
// ========== SQL 标签页管理接口 ==========
|
||||
|
||||
// SaveSqlTabs 保存 SQL 标签页列表
|
||||
func (a *App) SaveSqlTabs(tabs []map[string]interface{}) error {
|
||||
return a.tabAPI.SaveSqlTabs(tabs)
|
||||
}
|
||||
|
||||
// ListSqlTabs 获取 SQL 标签页列表
|
||||
func (a *App) ListSqlTabs() ([]map[string]interface{}, error) {
|
||||
return a.tabAPI.ListSqlTabs()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user