Files
MyGoNavi/internal/app/app.go
杨国锋 af91c916c3 feat(frontend): 升级 DataGrid 组件并引入高性能拖拽交互
- 实现基于原生 DOM 事件的零渲染列宽拖拽,彻底解决卡顿与误触排序问题
- 查询编辑器集成 DataGrid,支持 SQL 结果直接编辑与事务提交
- 侧边栏新增上下文感知的 "新建查询" 快捷入口
- 优化 TabManager 渲染逻辑与全局布局,消除不必要的滚动条
2026-02-02 11:32:49 +08:00

73 lines
1.5 KiB
Go

package app
import (
"context"
"fmt"
"sync"
"GoNavi-Wails/internal/connection"
"GoNavi-Wails/internal/db"
)
// App struct
type App struct {
ctx context.Context
dbCache map[string]db.Database // Cache for DB connections
mu sync.Mutex // Mutex for cache access
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
dbCache: make(map[string]db.Database),
}
}
// Startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
}
// Shutdown is called when the app terminates
func (a *App) Shutdown(ctx context.Context) {
a.mu.Lock()
defer a.mu.Unlock()
for _, dbInst := range a.dbCache {
dbInst.Close()
}
}
// Helper: Generate a unique key for the connection config
func getCacheKey(config connection.ConnectionConfig) string {
return fmt.Sprintf("%s|%s|%s:%d|%s|%s|%v", config.Type, config.User, config.Host, config.Port, config.Database, config.SSH.Host, config.UseSSH)
}
// Helper: Get or create a database connection
func (a *App) getDatabase(config connection.ConnectionConfig) (db.Database, error) {
key := getCacheKey(config)
a.mu.Lock()
defer a.mu.Unlock()
if dbInst, ok := a.dbCache[key]; ok {
if err := dbInst.Ping(); err == nil {
return dbInst, nil
}
dbInst.Close()
delete(a.dbCache, key)
}
dbInst, err := db.NewDatabase(config.Type)
if err != nil {
return nil, err
}
if err := dbInst.Connect(config); err != nil {
return nil, err
}
a.dbCache[key] = dbInst
return dbInst, nil
}