mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-11 23:39:44 +08:00
* 🐛 fix(data-viewer): 修复ClickHouse尾部分页异常并增强DuckDB复杂类型兼容 - DataViewer 新增 ClickHouse 反向分页策略,修复最后页与倒数页查询失败 - DuckDB 查询失败时按列类型生成安全 SELECT,复杂类型转 VARCHAR 重试 - 分页状态统一使用 currentPage 回填,避免页码与总数推导不一致 - 增强查询异常日志与重试路径,降低大表场景卡顿与误报 * ✨ feat(frontend-driver): 驱动管理支持快速搜索并优化信息展示 - 新增搜索框,支持按 DuckDB/ClickHouse 等关键字快速定位驱动 - 显示“匹配 x / y”统计与无结果提示 - 优化头部区域排版,提升透明/暗色场景下的视觉对齐 * 🔧 fix(connection-modal): 修复多数据源URI导入解析并校正Oracle服务名校验 - 新增单主机URI解析映射,兼容 postgres/postgresql、sqlserver、redis、tdengine、dameng(dm)、kingbase、highgo、vastbase、clickhouse、oracle - 抽取 parseSingleHostUri 复用逻辑,统一 host/port/user/password/database 回填行为 - Oracle 连接新增服务名必填校验,移除“服务名为空回退用户名”的隐式逻辑 - 连接弹窗补充 Oracle 服务名输入项与 URI 示例 * 🐛 fix(query-export): 修复查询结果导出卡住并统一按数据源能力控制导出路径 - 查询结果页导出增加稳定兜底,异常时确保 loading 关闭避免持续转圈 - DataGrid 导出逻辑按数据源能力分流,优先走后端 ExportQuery 并保留结果集导出降级 - QueryEditor 传递结果导出 SQL,保证查询结果导出范围与当前结果一致 - 后端补充 ExportData/ExportQuery 关键日志,提升导出链路可观测性 * 🐛 fix(precision): 修复查询链路与分页统计的大整数精度丢失 - 代理响应数据解码改为 UseNumber,避免默认 float64 吞精度 - 统一归一化 json.Number 与超界整数,超出 JS 安全范围转字符串 - 修复 DataViewer 总数解析,超大值不再误转 Number 参与分页 - refs #142 * 🐛 fix(driver-manager): 修复驱动管理网络告警重复并强化代理引导 - 新增下载链路域名探测,区分“GitHub可达但驱动下载链路不可达” - 网络不可达场景仅保留红色强提醒,移除重复二级告警 - 强提醒增加“打开全局代理设置”入口,优先引导使用 GoNavi 全局代理 - 统一网络检测与目录说明提示图标尺寸,修复加载期视觉不一致 - refs #141 * ♻️ refactor(frontend-interaction): 统一标签拖拽与暗色主题交互实现 - 重构Tab拖拽排序实现,统一为可配置拖拽引擎 - 规范拖拽与点击事件边界,提升交互一致性 - 统一多组件暗色透明样式策略,减少硬编码色值 - 提升Redis/表格/连接面板在透明模式下的观感一致性 - refs #144 * ♻️ refactor(update-state): 重构在线更新状态流并按版本统一进度展示 - 重构更新检查与下载状态同步流程,减少前后端状态分叉 - 进度展示严格绑定 latestVersion,避免跨版本状态串用 - 优化 about 打开场景的静默检查状态回填逻辑 - 统一下载弹窗关闭/后台隐藏行为 - 保持现有安装流程并补齐目录打开能力 * 🎨 style(sidebar-log): 将SQL执行日志入口调整为悬浮胶囊样式 - 移除侧栏底部整条日志入口容器 - 新增悬浮按钮阴影/边框/透明背景并适配明暗主题 - 为树区域预留底部空间避免入口遮挡内容 * ✨ feat(redis-cluster): 支持集群模式逻辑多库隔离与 0-15 库切换 - 前端恢复 Redis 集群场景下 db0-db15 的数据库选择与展示 - 后端新增集群逻辑库命名空间前缀映射,统一 key/pattern 读写隔离 - 覆盖扫描、读取、写入、删除、重命名等核心操作的键映射规则 - 集群命令通道支持 SELECT 逻辑切库与 FLUSHDB 逻辑库清空 - refs #145 * ✨ feat(DataGrid): 大数据表虚拟滚动性能优化及UI一致性修复 - 启用动态虚拟滚动(数据量≥500行自动切换),解决万行数据表卡顿问题 - 虚拟模式下EditableCell改用div渲染,CSS选择器从元素级改为类级适配虚拟DOM - 修复虚拟模式双水平滚动条:样式化rc-virtual-list内置滚动条为胶囊外观,禁用自定义外部滚动条 - 为rc-virtual-list水平滚动条添加鼠标滚轮支持(MutationObserver + marginLeft驱动) - 修复白色主题透明模式下列名悬浮Tooltip对比度不足的问题 - 新增白色主题全局滚动条样式适配透明模式(App.css) - App.tsx主题token与组件样式优化 - refs #147 * 🔧 chore(app): 清理 App.tsx 类型告警并收敛前端壳层实现 - 清除未使用代码和冗余状态 - 替换弃用 API 以消除 IDE 提示 - 显式处理浮动 Promise 避免告警 - 保持现有更新检查和代理设置行为不变 --------- Co-authored-by: Syngnat <yangguofeng919@gmail.com>
487 lines
13 KiB
Go
487 lines
13 KiB
Go
package db
|
||
|
||
import (
|
||
"bufio"
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"io"
|
||
"os"
|
||
"os/exec"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
"GoNavi-Wails/internal/connection"
|
||
"GoNavi-Wails/internal/logger"
|
||
)
|
||
|
||
const (
|
||
optionalAgentMethodConnect = "connect"
|
||
optionalAgentMethodClose = "close"
|
||
optionalAgentMethodPing = "ping"
|
||
optionalAgentMethodQuery = "query"
|
||
optionalAgentMethodExec = "exec"
|
||
optionalAgentMethodGetDatabases = "getDatabases"
|
||
optionalAgentMethodGetTables = "getTables"
|
||
optionalAgentMethodGetCreateStmt = "getCreateStatement"
|
||
optionalAgentMethodGetColumns = "getColumns"
|
||
optionalAgentMethodGetAllColumns = "getAllColumns"
|
||
optionalAgentMethodGetIndexes = "getIndexes"
|
||
optionalAgentMethodGetForeignKeys = "getForeignKeys"
|
||
optionalAgentMethodGetTriggers = "getTriggers"
|
||
optionalAgentMethodApplyChanges = "applyChanges"
|
||
optionalAgentDefaultScannerMaxBytes = 8 << 20
|
||
)
|
||
|
||
type optionalAgentRequest struct {
|
||
ID int64 `json:"id"`
|
||
Method string `json:"method"`
|
||
Config *connection.ConnectionConfig `json:"config,omitempty"`
|
||
Query string `json:"query,omitempty"`
|
||
TimeoutMs int64 `json:"timeoutMs,omitempty"`
|
||
DBName string `json:"dbName,omitempty"`
|
||
TableName string `json:"tableName,omitempty"`
|
||
Changes *connection.ChangeSet `json:"changes,omitempty"`
|
||
}
|
||
|
||
type optionalAgentResponse struct {
|
||
ID int64 `json:"id"`
|
||
Success bool `json:"success"`
|
||
Error string `json:"error,omitempty"`
|
||
Data json.RawMessage `json:"data,omitempty"`
|
||
Fields []string `json:"fields,omitempty"`
|
||
RowsAffected int64 `json:"rowsAffected,omitempty"`
|
||
}
|
||
|
||
type optionalDriverAgentClient struct {
|
||
cmd *exec.Cmd
|
||
stdin io.WriteCloser
|
||
reader *bufio.Reader
|
||
nextID int64
|
||
mu sync.Mutex
|
||
stderrMu sync.Mutex
|
||
stderr strings.Builder
|
||
driver string
|
||
}
|
||
|
||
func newOptionalDriverAgentClient(driverType string, executablePath string) (*optionalDriverAgentClient, error) {
|
||
pathText := strings.TrimSpace(executablePath)
|
||
if pathText == "" {
|
||
return nil, fmt.Errorf("%s 驱动代理路径为空", driverDisplayName(driverType))
|
||
}
|
||
info, err := os.Stat(pathText)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("%s 驱动代理不存在:%s", driverDisplayName(driverType), pathText)
|
||
}
|
||
if info.IsDir() {
|
||
return nil, fmt.Errorf("%s 驱动代理路径是目录:%s", driverDisplayName(driverType), pathText)
|
||
}
|
||
|
||
cmd := exec.Command(pathText)
|
||
configureAgentProcess(cmd)
|
||
stdin, err := cmd.StdinPipe()
|
||
if err != nil {
|
||
return nil, fmt.Errorf("创建 %s 驱动代理 stdin 失败:%w", driverDisplayName(driverType), err)
|
||
}
|
||
stdout, err := cmd.StdoutPipe()
|
||
if err != nil {
|
||
return nil, fmt.Errorf("创建 %s 驱动代理 stdout 失败:%w", driverDisplayName(driverType), err)
|
||
}
|
||
stderr, err := cmd.StderrPipe()
|
||
if err != nil {
|
||
return nil, fmt.Errorf("创建 %s 驱动代理 stderr 失败:%w", driverDisplayName(driverType), err)
|
||
}
|
||
if err := cmd.Start(); err != nil {
|
||
return nil, fmt.Errorf("启动 %s 驱动代理失败:%w", driverDisplayName(driverType), err)
|
||
}
|
||
|
||
client := &optionalDriverAgentClient{
|
||
cmd: cmd,
|
||
stdin: stdin,
|
||
reader: bufio.NewReader(stdout),
|
||
driver: normalizeRuntimeDriverType(driverType),
|
||
}
|
||
go client.captureStderr(stderr)
|
||
return client, nil
|
||
}
|
||
|
||
func (c *optionalDriverAgentClient) captureStderr(stderr io.Reader) {
|
||
scanner := bufio.NewScanner(stderr)
|
||
buffer := make([]byte, 0, 8<<10)
|
||
scanner.Buffer(buffer, optionalAgentDefaultScannerMaxBytes)
|
||
for scanner.Scan() {
|
||
line := strings.TrimSpace(scanner.Text())
|
||
if line == "" {
|
||
continue
|
||
}
|
||
c.stderrMu.Lock()
|
||
if c.stderr.Len() > 0 {
|
||
c.stderr.WriteString(" | ")
|
||
}
|
||
c.stderr.WriteString(line)
|
||
c.stderrMu.Unlock()
|
||
}
|
||
}
|
||
|
||
func (c *optionalDriverAgentClient) stderrText() string {
|
||
c.stderrMu.Lock()
|
||
defer c.stderrMu.Unlock()
|
||
return strings.TrimSpace(c.stderr.String())
|
||
}
|
||
|
||
func (c *optionalDriverAgentClient) call(req optionalAgentRequest, out interface{}, fields *[]string, rowsAffected *int64) error {
|
||
c.mu.Lock()
|
||
defer c.mu.Unlock()
|
||
|
||
c.nextID++
|
||
req.ID = c.nextID
|
||
|
||
payload, err := json.Marshal(req)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
payload = append(payload, '\n')
|
||
if _, err := c.stdin.Write(payload); err != nil {
|
||
stderrText := c.stderrText()
|
||
if stderrText == "" {
|
||
return fmt.Errorf("调用 %s 驱动代理失败:%w", driverDisplayName(c.driver), err)
|
||
}
|
||
return fmt.Errorf("调用 %s 驱动代理失败:%w(stderr: %s)", driverDisplayName(c.driver), err, stderrText)
|
||
}
|
||
|
||
line, err := c.reader.ReadBytes('\n')
|
||
if err != nil {
|
||
stderrText := c.stderrText()
|
||
if stderrText == "" {
|
||
return fmt.Errorf("读取 %s 驱动代理响应失败:%w", driverDisplayName(c.driver), err)
|
||
}
|
||
return fmt.Errorf("读取 %s 驱动代理响应失败:%w(stderr: %s)", driverDisplayName(c.driver), err, stderrText)
|
||
}
|
||
|
||
var resp optionalAgentResponse
|
||
if err := json.Unmarshal(line, &resp); err != nil {
|
||
return fmt.Errorf("解析 %s 驱动代理响应失败:%w", driverDisplayName(c.driver), err)
|
||
}
|
||
if !resp.Success {
|
||
errText := strings.TrimSpace(resp.Error)
|
||
if errText == "" {
|
||
errText = fmt.Sprintf("%s 驱动代理返回失败", driverDisplayName(c.driver))
|
||
}
|
||
return errors.New(errText)
|
||
}
|
||
|
||
if fields != nil {
|
||
*fields = resp.Fields
|
||
}
|
||
if rowsAffected != nil {
|
||
*rowsAffected = resp.RowsAffected
|
||
}
|
||
if out != nil && len(resp.Data) > 0 {
|
||
if err := decodeJSONWithUseNumber(resp.Data, out); err != nil {
|
||
return fmt.Errorf("解析 %s 驱动代理数据失败:%w", driverDisplayName(c.driver), err)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (c *optionalDriverAgentClient) close() error {
|
||
c.mu.Lock()
|
||
defer c.mu.Unlock()
|
||
var closeErr error
|
||
if c.stdin != nil {
|
||
_ = c.stdin.Close()
|
||
}
|
||
if c.cmd != nil && c.cmd.Process != nil {
|
||
if err := c.cmd.Process.Kill(); err != nil {
|
||
closeErr = err
|
||
}
|
||
}
|
||
if c.cmd != nil {
|
||
_ = c.cmd.Wait()
|
||
}
|
||
return closeErr
|
||
}
|
||
|
||
type OptionalDriverAgentDB struct {
|
||
driverType string
|
||
client *optionalDriverAgentClient
|
||
}
|
||
|
||
func newOptionalDriverAgentDatabase(driverType string) databaseFactory {
|
||
normalized := normalizeRuntimeDriverType(driverType)
|
||
return func() Database {
|
||
return &OptionalDriverAgentDB{driverType: normalized}
|
||
}
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) Connect(config connection.ConnectionConfig) error {
|
||
if d.client != nil {
|
||
_ = d.client.close()
|
||
d.client = nil
|
||
}
|
||
|
||
executablePath, err := ResolveOptionalDriverAgentExecutablePath("", d.driverType)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
logger.Infof("%s 驱动代理路径:%s", driverDisplayName(d.driverType), executablePath)
|
||
client, err := newOptionalDriverAgentClient(d.driverType, executablePath)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodConnect,
|
||
Config: &config,
|
||
}, nil, nil, nil); err != nil {
|
||
_ = client.close()
|
||
return err
|
||
}
|
||
d.client = client
|
||
return nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) Close() error {
|
||
if d.client == nil {
|
||
return nil
|
||
}
|
||
_ = d.client.call(optionalAgentRequest{Method: optionalAgentMethodClose}, nil, nil, nil)
|
||
err := d.client.close()
|
||
d.client = nil
|
||
return err
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) Ping() error {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return client.call(optionalAgentRequest{Method: optionalAgentMethodPing}, nil, nil, nil)
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) QueryContext(ctx context.Context, query string) ([]map[string]interface{}, []string, error) {
|
||
if err := ctx.Err(); err != nil {
|
||
return nil, nil, err
|
||
}
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
var data []map[string]interface{}
|
||
var fields []string
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodQuery,
|
||
Query: query,
|
||
TimeoutMs: timeoutMsFromContext(ctx),
|
||
}, &data, &fields, nil); err != nil {
|
||
return nil, nil, err
|
||
}
|
||
return data, fields, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
var data []map[string]interface{}
|
||
var fields []string
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodQuery,
|
||
Query: query,
|
||
}, &data, &fields, nil); err != nil {
|
||
return nil, nil, err
|
||
}
|
||
return data, fields, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) ExecContext(ctx context.Context, query string) (int64, error) {
|
||
if err := ctx.Err(); err != nil {
|
||
return 0, err
|
||
}
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
var affected int64
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodExec,
|
||
Query: query,
|
||
TimeoutMs: timeoutMsFromContext(ctx),
|
||
}, nil, nil, &affected); err != nil {
|
||
return 0, err
|
||
}
|
||
return affected, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) Exec(query string) (int64, error) {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
var affected int64
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodExec,
|
||
Query: query,
|
||
}, nil, nil, &affected); err != nil {
|
||
return 0, err
|
||
}
|
||
return affected, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) GetDatabases() ([]string, error) {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var dbs []string
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodGetDatabases,
|
||
}, &dbs, nil, nil); err != nil {
|
||
return nil, err
|
||
}
|
||
return dbs, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) GetTables(dbName string) ([]string, error) {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var tables []string
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodGetTables,
|
||
DBName: dbName,
|
||
}, &tables, nil, nil); err != nil {
|
||
return nil, err
|
||
}
|
||
return tables, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) GetCreateStatement(dbName, tableName string) (string, error) {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
var sqlText string
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodGetCreateStmt,
|
||
DBName: dbName,
|
||
TableName: tableName,
|
||
}, &sqlText, nil, nil); err != nil {
|
||
return "", err
|
||
}
|
||
return sqlText, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) GetColumns(dbName, tableName string) ([]connection.ColumnDefinition, error) {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var columns []connection.ColumnDefinition
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodGetColumns,
|
||
DBName: dbName,
|
||
TableName: tableName,
|
||
}, &columns, nil, nil); err != nil {
|
||
return nil, err
|
||
}
|
||
return columns, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) GetAllColumns(dbName string) ([]connection.ColumnDefinitionWithTable, error) {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var columns []connection.ColumnDefinitionWithTable
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodGetAllColumns,
|
||
DBName: dbName,
|
||
}, &columns, nil, nil); err != nil {
|
||
return nil, err
|
||
}
|
||
return columns, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) GetIndexes(dbName, tableName string) ([]connection.IndexDefinition, error) {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var indexes []connection.IndexDefinition
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodGetIndexes,
|
||
DBName: dbName,
|
||
TableName: tableName,
|
||
}, &indexes, nil, nil); err != nil {
|
||
return nil, err
|
||
}
|
||
return indexes, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) GetForeignKeys(dbName, tableName string) ([]connection.ForeignKeyDefinition, error) {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var keys []connection.ForeignKeyDefinition
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodGetForeignKeys,
|
||
DBName: dbName,
|
||
TableName: tableName,
|
||
}, &keys, nil, nil); err != nil {
|
||
return nil, err
|
||
}
|
||
return keys, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) GetTriggers(dbName, tableName string) ([]connection.TriggerDefinition, error) {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var triggers []connection.TriggerDefinition
|
||
if err := client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodGetTriggers,
|
||
DBName: dbName,
|
||
TableName: tableName,
|
||
}, &triggers, nil, nil); err != nil {
|
||
return nil, err
|
||
}
|
||
return triggers, nil
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) ApplyChanges(tableName string, changes connection.ChangeSet) error {
|
||
client, err := d.requireClient()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return client.call(optionalAgentRequest{
|
||
Method: optionalAgentMethodApplyChanges,
|
||
TableName: tableName,
|
||
Changes: &changes,
|
||
}, nil, nil, nil)
|
||
}
|
||
|
||
func (d *OptionalDriverAgentDB) requireClient() (*optionalDriverAgentClient, error) {
|
||
if d.client == nil {
|
||
return nil, fmt.Errorf("connection not open")
|
||
}
|
||
return d.client, nil
|
||
}
|
||
|
||
func timeoutMsFromContext(ctx context.Context) int64 {
|
||
deadline, ok := ctx.Deadline()
|
||
if !ok {
|
||
return 0
|
||
}
|
||
remaining := time.Until(deadline).Milliseconds()
|
||
if remaining <= 0 {
|
||
return 1
|
||
}
|
||
return remaining
|
||
}
|