feat: 18 引擎驱动全家桶 + 驱动管理/离线包 + 设置中心 + 工作台交互打磨
驱动接入(全部本机真实例验收): - Elasticsearch 原生 SQL API、InfluxDB 原生 HTTP+Flux(官方 JDBC 付费许可,实测弃用) - ZooKeeper/MongoDB JVM agent(连接时自动下载官方驱动 jar,sha256 钉版本到 ~/.opsxdb/drivers/) - 达梦 JDBC agent 桥就绪(待厂商 jar+实例);TiDB 真实例验收 - 协议族代码审查放行 PolarDB/TDSQL/OceanBase/Kingbase/GaussDB/Oracle - 驱动能力模型:按驱动开关对象树分组/右键菜单/行编辑,NoSQL 树拍平(对象直挂库节点) 驱动管理: - 驱动管理弹框(状态/预装/离线包导入/JDK 检测)+ /api/drivers/* 三接口 - opsxdb-driver 驱动商店仓库(清单/fetch.sh/pack.sh 离线包/import.sh) - 连接时懒加载:DMG 零 Java 产物,首连自动下载(实测 ZK 九件套 1.35s),离线包自动回退 UI/交互: - 设置中心(⚙):常规/执行/结果/驱动/快捷键/关于,左导航+右面板(对标 opsxshell) - 实例级展开箭头(行首 SVG chevron,替代字体回退失真的 Unicode 三角) - 树内嵌套渲染修复:对象树挂在激活连接下;工具栏窄窗口不断行(按钮 nowrap+整颗换行) - 侧栏连接行精简(图标+连接名)、SQLite SSH 守卫、Oracle 死参数清理 质量: - Go 协议层/离线回退单测、E2E 门控测试(ES/ZK/Influx/Mongo/TiDB/JDBC 桥) - 驱动注册表单一事实来源 connection-types.json,README/BACKLOG/AGENTS 同步
This commit is contained in:
1 parent
26f459d1b8
commit
a9510235bb
156 files changed
+17200
-2520
No files matched your search
@@ -0,0 +1,89 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Config AI 配置;provider: stub|openai(OpenAI 兼容协议)。
|
||||
type Config struct {
|
||||
Provider string `json:"provider"`
|
||||
BaseURL string `json:"baseURL"`
|
||||
APIKey string `json:"apiKey,omitempty"`
|
||||
Model string `json:"model"`
|
||||
}
|
||||
|
||||
func configPath() (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(home, ".opsxdb", "ai.json"), nil
|
||||
}
|
||||
|
||||
// LoadConfig 读取配置:~/.opsxdb/ai.json 优先,env(OPSXDB_AI_*)兜底逐字段补充。
|
||||
func LoadConfig() Config {
|
||||
cfg := Config{
|
||||
Provider: strings.TrimSpace(os.Getenv("OPSXDB_AI_PROVIDER")),
|
||||
BaseURL: strings.TrimSpace(os.Getenv("OPSXDB_AI_BASE_URL")),
|
||||
APIKey: strings.TrimSpace(os.Getenv("OPSXDB_AI_API_KEY")),
|
||||
Model: strings.TrimSpace(os.Getenv("OPSXDB_AI_MODEL")),
|
||||
}
|
||||
if p, err := configPath(); err == nil {
|
||||
if b, err := os.ReadFile(p); err == nil {
|
||||
var f Config
|
||||
if json.Unmarshal(b, &f) == nil {
|
||||
if f.Provider != "" {
|
||||
cfg.Provider = f.Provider
|
||||
}
|
||||
if f.BaseURL != "" {
|
||||
cfg.BaseURL = f.BaseURL
|
||||
}
|
||||
if f.APIKey != "" {
|
||||
cfg.APIKey = f.APIKey
|
||||
}
|
||||
if f.Model != "" {
|
||||
cfg.Model = f.Model
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if cfg.Provider == "" {
|
||||
if cfg.APIKey != "" {
|
||||
cfg.Provider = "openai"
|
||||
} else {
|
||||
cfg.Provider = "stub"
|
||||
}
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// SaveConfig 写 ~/.opsxdb/ai.json(0600,与本机 token 同威胁模型)。
|
||||
func SaveConfig(c Config) error {
|
||||
p, err := configPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
b, err := json.MarshalIndent(c, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(p, b, 0600)
|
||||
}
|
||||
|
||||
// MaskedKey 返回脱敏后的 key 展示形式(sk-***)。
|
||||
func (c Config) MaskedKey() string {
|
||||
k := c.APIKey
|
||||
if k == "" {
|
||||
return ""
|
||||
}
|
||||
if len(k) <= 6 {
|
||||
return "***"
|
||||
}
|
||||
return k[:3] + "***"
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultOpenAIBaseURL = "https://api.openai.com/v1"
|
||||
defaultOpenAIModel = "gpt-4o-mini"
|
||||
)
|
||||
|
||||
// OpenAIProvider OpenAI 兼容协议实现(DeepSeek/通义/本地 vLLM 同协议可用)。
|
||||
type OpenAIProvider struct {
|
||||
baseURL string
|
||||
apiKey string
|
||||
model string
|
||||
hc *http.Client
|
||||
}
|
||||
|
||||
func NewOpenAIProvider(cfg Config) *OpenAIProvider {
|
||||
base := strings.TrimRight(strings.TrimSpace(cfg.BaseURL), "/")
|
||||
if base == "" {
|
||||
base = defaultOpenAIBaseURL
|
||||
}
|
||||
model := strings.TrimSpace(cfg.Model)
|
||||
if model == "" {
|
||||
model = defaultOpenAIModel
|
||||
}
|
||||
return &OpenAIProvider{
|
||||
baseURL: base,
|
||||
apiKey: cfg.APIKey,
|
||||
model: model,
|
||||
hc: &http.Client{Timeout: 60 * time.Second},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *OpenAIProvider) Name() string { return "openai" }
|
||||
|
||||
type chatCompletionReq struct {
|
||||
Model string `json:"model"`
|
||||
Messages []Message `json:"messages"`
|
||||
}
|
||||
|
||||
type chatCompletionRes struct {
|
||||
Choices []struct {
|
||||
Message Message `json:"message"`
|
||||
} `json:"choices"`
|
||||
Error *struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
// Complete 单次非流式调用;ctx 全透传,HTTP 层 60s 超时兜底。
|
||||
func (p *OpenAIProvider) Complete(ctx context.Context, req ChatReq) (ChatRes, error) {
|
||||
model := p.model
|
||||
if strings.TrimSpace(req.Model) != "" {
|
||||
model = strings.TrimSpace(req.Model)
|
||||
}
|
||||
payload := chatCompletionReq{
|
||||
Model: model,
|
||||
Messages: BuildMessages(req),
|
||||
}
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return ChatRes{}, err
|
||||
}
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, p.baseURL+"/chat/completions", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return ChatRes{}, err
|
||||
}
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+p.apiKey)
|
||||
|
||||
resp, err := p.hc.Do(httpReq)
|
||||
if err != nil {
|
||||
return ChatRes{}, fmt.Errorf("AI 请求失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
||||
if err != nil {
|
||||
return ChatRes{}, fmt.Errorf("读取 AI 响应失败: %w", err)
|
||||
}
|
||||
var out chatCompletionRes
|
||||
if err := json.Unmarshal(raw, &out); err != nil {
|
||||
return ChatRes{}, fmt.Errorf("解析 AI 响应失败(HTTP %d)", resp.StatusCode)
|
||||
}
|
||||
if out.Error != nil {
|
||||
return ChatRes{}, fmt.Errorf("AI 服务返回错误: %s", out.Error.Message)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return ChatRes{}, fmt.Errorf("AI 服务 HTTP %d", resp.StatusCode)
|
||||
}
|
||||
if len(out.Choices) == 0 {
|
||||
return ChatRes{}, fmt.Errorf("AI 服务未返回候选结果")
|
||||
}
|
||||
content := strings.TrimSpace(out.Choices[0].Message.Content)
|
||||
return ChatRes{Content: content, SQL: extractSQL(content)}, nil
|
||||
}
|
||||
|
||||
var sqlFence = regexp.MustCompile("(?s)```sql\\s*\n(.+?)```")
|
||||
|
||||
// extractSQL 从模型回复中提取 ```sql 代码块内容。
|
||||
func extractSQL(content string) string {
|
||||
m := sqlFence.FindStringSubmatch(content)
|
||||
if len(m) < 2 {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(m[1])
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// schemaContextCap 注入给模型的库表结构上下文长度上限。
|
||||
const schemaContextCap = 4000
|
||||
|
||||
// BuildMessages 按 action 选 system 模板装配对话;req.Schema 非空时注入库表列上下文。
|
||||
func BuildMessages(req ChatReq) []Message {
|
||||
driver := req.Driver
|
||||
if driver == "" {
|
||||
driver = "mysql"
|
||||
}
|
||||
sys := "你是资深数据库工程师助手,服务于数据库客户端 OpsxDB。回答使用中文,SQL 代码放在 ```sql 代码块中。"
|
||||
var task string
|
||||
switch strings.ToLower(req.Action) {
|
||||
case "generate", "ask":
|
||||
task = fmt.Sprintf("根据用户描述生成一条 %s 方言的 SQL。只输出可执行 SQL 与极简说明。", driver)
|
||||
case "explain":
|
||||
task = "逐部分解释用户给出的 SQL:访问路径、索引使用、潜在代价。"
|
||||
case "optimize":
|
||||
task = "审查用户给出的 SQL 并给出优化版本:索引建议、重写建议、说明理由。"
|
||||
case "fix":
|
||||
task = "根据报错信息修复用户给出的 SQL,指出错误原因并给出修正后的完整 SQL。"
|
||||
case "dialect":
|
||||
task = fmt.Sprintf("把用户给出的 SQL 转换为 %s 方言,保持语义一致。", driver)
|
||||
case "sample":
|
||||
task = "为当前库生成一条有代表性的示例查询 SQL。"
|
||||
default:
|
||||
task = "回答用户关于数据库 / SQL 的问题。"
|
||||
}
|
||||
var sb strings.Builder
|
||||
sb.WriteString(sys)
|
||||
sb.WriteString("\n")
|
||||
sb.WriteString(task)
|
||||
if req.Database != "" {
|
||||
sb.WriteString(fmt.Sprintf("\n当前数据库: %s(%s)", req.Database, driver))
|
||||
}
|
||||
if s := strings.TrimSpace(req.Schema); s != "" {
|
||||
if len(s) > schemaContextCap {
|
||||
s = s[:schemaContextCap]
|
||||
}
|
||||
sb.WriteString("\n当前数据库结构:\n")
|
||||
sb.WriteString(s)
|
||||
}
|
||||
|
||||
var user strings.Builder
|
||||
if p := strings.TrimSpace(req.Prompt); p != "" {
|
||||
user.WriteString(p)
|
||||
}
|
||||
if s := strings.TrimSpace(req.SQL); s != "" {
|
||||
if user.Len() > 0 {
|
||||
user.WriteString("\n\n")
|
||||
}
|
||||
user.WriteString("SQL:\n")
|
||||
user.WriteString(s)
|
||||
}
|
||||
if e := strings.TrimSpace(req.Error); e != "" {
|
||||
user.WriteString("\n\n报错:\n")
|
||||
user.WriteString(e)
|
||||
}
|
||||
if user.Len() == 0 {
|
||||
user.WriteString("你好")
|
||||
}
|
||||
return []Message{
|
||||
{Role: "system", Content: sb.String()},
|
||||
{Role: "user", Content: user.String()},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package ai
|
||||
|
||||
import "context"
|
||||
|
||||
// Message 对话消息(OpenAI 兼容协议)。
|
||||
type Message struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// ChatReq 在 stub 时代字段上演进,前端契约不变;新增 schema/model 可选字段。
|
||||
type ChatReq struct {
|
||||
Action string `json:"action"`
|
||||
Prompt string `json:"prompt"`
|
||||
SQL string `json:"sql"`
|
||||
Driver string `json:"driver"`
|
||||
Database string `json:"database"`
|
||||
Error string `json:"error"`
|
||||
Schema string `json:"schema,omitempty"` // 前端注入的库表列上下文
|
||||
Model string `json:"model,omitempty"`
|
||||
}
|
||||
|
||||
type ChatRes struct {
|
||||
Content string `json:"content"`
|
||||
SQL string `json:"sql,omitempty"`
|
||||
}
|
||||
|
||||
// Provider AI 提供方抽象。本轮只做非流式 Complete;
|
||||
// Stream(ctx, req) (<-chan string, error) 预留,SSE 流式留待后续版本。
|
||||
type Provider interface {
|
||||
Name() string
|
||||
Complete(ctx context.Context, req ChatReq) (ChatRes, error)
|
||||
}
|
||||
|
||||
// Default 按当前配置选择 Provider;未配置 API Key 时回落 stub(离线占位)。
|
||||
func Default() Provider {
|
||||
cfg := LoadConfig()
|
||||
switch cfg.Provider {
|
||||
case "openai":
|
||||
if cfg.APIKey != "" {
|
||||
return NewOpenAIProvider(cfg)
|
||||
}
|
||||
}
|
||||
return stubProvider{}
|
||||
}
|
||||
@@ -1,25 +1,21 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ChatReq struct {
|
||||
Action string `json:"action"`
|
||||
Prompt string `json:"prompt"`
|
||||
SQL string `json:"sql"`
|
||||
Driver string `json:"driver"`
|
||||
Database string `json:"database"`
|
||||
Error string `json:"error"`
|
||||
// stubProvider 离线占位实现:未配置真实模型时的兜底,内容为字符串模板。
|
||||
type stubProvider struct{}
|
||||
|
||||
func (stubProvider) Name() string { return "stub" }
|
||||
|
||||
func (stubProvider) Complete(_ context.Context, req ChatReq) (ChatRes, error) {
|
||||
return chatStub(req), nil
|
||||
}
|
||||
|
||||
type ChatRes struct {
|
||||
Content string `json:"content"`
|
||||
SQL string `json:"sql,omitempty"`
|
||||
}
|
||||
|
||||
func Chat(req ChatReq) ChatRes {
|
||||
func chatStub(req ChatReq) ChatRes {
|
||||
driver := req.Driver
|
||||
if driver == "" {
|
||||
driver = "mysql"
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,379 @@
|
||||
package db
|
||||
|
||||
// agent_e2e_test.go —— JDBC agent 桥端到端验证(真实 JVM + 真实 JDBC 驱动)。
|
||||
// 前置:本机 JDK、agent jar(OPSXDB_AGENT_JAR 或默认 ~/.opsxdb/agents/jdbc/)。
|
||||
// 触发:OPSXDB_E2E_JDBC=1 才运行(CI 无 JVM 时自动跳过)。
|
||||
// 覆盖:StartAgent 全链路(gson 校验/JVM 启动/握手)→ open → DDL/DML → query → 元数据。
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestE2EAgentSQLiteBridge(t *testing.T) {
|
||||
if os.Getenv("OPSXDB_E2E_JDBC") != "1" {
|
||||
t.Skip("置 OPSXDB_E2E_JDBC=1 并备好 JDK 与 agent jar 后运行")
|
||||
}
|
||||
home, _ := os.UserHomeDir()
|
||||
jars := filepath.Join(home, ".opsxdb", "agents", "jdbc", "drivers", "sqlite-e2e")
|
||||
if _, err := os.Stat(filepath.Join(jars, "sqlite-jdbc-3.46.1.3.jar")); err != nil {
|
||||
t.Skipf("缺 sqlite-jdbc jar: %s", jars)
|
||||
}
|
||||
t.Setenv("OPSXDB_E2E_JARS", jars)
|
||||
|
||||
spec := AgentSpec{
|
||||
DriverKey: "e2e-sqlite",
|
||||
DriverClass: "org.sqlite.JDBC",
|
||||
URLTemplate: "jdbc:sqlite:{{.Database}}",
|
||||
JarsDirEnv: "OPSXDB_E2E_JARS",
|
||||
}
|
||||
agentDriverSpecs[spec.DriverKey] = spec // 注册进表,AgentConfigured 才能按 JarsDirEnv 解析
|
||||
t.Cleanup(func() { delete(agentDriverSpecs, spec.DriverKey) })
|
||||
cli, err := StartAgent(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("StartAgent: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { cli.Shutdown(context.Background()) })
|
||||
|
||||
dbPath := filepath.Join(t.TempDir(), "e2e.db")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
handle := ""
|
||||
{
|
||||
var res struct {
|
||||
Handle string `json:"handle"`
|
||||
}
|
||||
if err := cli.call(ctx, "open", map[string]any{"url": "jdbc:sqlite:" + dbPath, "user": "", "password": ""}, &res); err != nil {
|
||||
t.Fatalf("open: %v (stderr=%s)", err, cli.stderrTail.String())
|
||||
}
|
||||
handle = res.Handle
|
||||
}
|
||||
mustExec := func(sql string) {
|
||||
t.Helper()
|
||||
var r struct {
|
||||
AffectedRows int64 `json:"affectedRows"`
|
||||
}
|
||||
if err := cli.call(ctx, "exec", map[string]any{"handle": handle, "sql": sql, "timeoutSec": 30}, &r); err != nil {
|
||||
t.Fatalf("exec %q: %v (stderr=%s)", sql, err, cli.stderrTail.String())
|
||||
}
|
||||
}
|
||||
mustExec("CREATE TABLE demo(id INTEGER PRIMARY KEY, name TEXT)")
|
||||
mustExec("INSERT INTO demo(name) VALUES ('opsx-e2e')")
|
||||
|
||||
{
|
||||
var res struct {
|
||||
Columns []string `json:"columns"`
|
||||
Rows []map[string]interface{} `json:"rows"`
|
||||
}
|
||||
if err := cli.call(ctx, "query", map[string]any{"handle": handle, "sql": "SELECT id, name FROM demo", "maxRows": 100, "timeoutSec": 30}, &res); err != nil {
|
||||
t.Fatalf("query: %v", err)
|
||||
}
|
||||
if len(res.Rows) != 1 || res.Rows[0]["name"] != "opsx-e2e" {
|
||||
t.Fatalf("rows = %+v", res.Rows)
|
||||
}
|
||||
}
|
||||
{
|
||||
var res struct {
|
||||
Names []string `json:"names"`
|
||||
}
|
||||
if err := cli.call(ctx, "metaTables", map[string]any{"handle": handle, "schema": ""}, &res); err != nil {
|
||||
t.Fatalf("metaTables: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, n := range res.Names {
|
||||
if n == "demo" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("metaTables 未包含 demo: %v", res.Names)
|
||||
}
|
||||
}
|
||||
{
|
||||
var res struct {
|
||||
Columns []ColumnInfo `json:"columns"`
|
||||
}
|
||||
if err := cli.call(ctx, "metaColumns", map[string]any{"handle": handle, "schema": "", "table": "demo"}, &res); err != nil {
|
||||
t.Fatalf("metaColumns: %v", err)
|
||||
}
|
||||
if len(res.Columns) != 2 {
|
||||
t.Fatalf("columns = %+v", res.Columns)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestE2EElasticsearch —— 真实 ES 8(docker,安全关闭)+ 官方 x-pack-sql-jdbc 走 agent 桥全链路。
|
||||
// 触发:OPSXDB_E2E_ES=1;前置 ES :9200 可连、~/.opsxdb/agents/jdbc/drivers/elasticsearch/ 有官方 JDBC jar。
|
||||
func TestE2EElasticsearch(t *testing.T) {
|
||||
if os.Getenv("OPSXDB_E2E_ES") != "1" {
|
||||
t.Skip("置 OPSXDB_E2E_ES=1 并启动 ES :9200 后运行")
|
||||
}
|
||||
// 种子文档(让 SHOW TABLES / SELECT 有真实对象)
|
||||
req, _ := http.NewRequest(http.MethodPut, "http://127.0.0.1:9200/e2e_smoke/_doc/1?refresh=true", strings.NewReader(`{"name":"opsx","n":42}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Skipf("ES :9200 不可达: %v", err)
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode >= 400 {
|
||||
t.Fatalf("种子文档写入失败: HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
c := ConnReq{Driver: "elasticsearch", Host: "127.0.0.1", Port: 9200}
|
||||
if err := TestConnection(c); err != nil {
|
||||
t.Fatalf("TestConnection: %v", err)
|
||||
}
|
||||
dbs, err := ListDatabases(context.Background(), c)
|
||||
if err != nil || len(dbs) != 1 || dbs[0] != "default" {
|
||||
t.Fatalf("ListDatabases = %v, %v", dbs, err)
|
||||
}
|
||||
cc := c
|
||||
cc.Database = "default"
|
||||
tables, err := ListTables(context.Background(), cc)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTables: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, n := range tables {
|
||||
if n == "e2e_smoke" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("tables 未含 e2e_smoke: %v", tables)
|
||||
}
|
||||
r := ExecSQL(context.Background(), ConnReq{Driver: "elasticsearch", Host: "127.0.0.1", Port: 9200, Database: "default", SQL: "SELECT name, n FROM e2e_smoke", Limit: 100})
|
||||
if r.Error != "" {
|
||||
t.Fatalf("ExecSQL: %s (stderr 见测试日志)", r.Error)
|
||||
}
|
||||
if r.RowCount < 1 {
|
||||
t.Fatalf("rows = %+v", r.Rows)
|
||||
}
|
||||
}
|
||||
|
||||
// TestE2EZooKeeper —— 真实 ZK(docker)+ OpsxZkAgent 全链路:连接/浏览/读写/删除。
|
||||
// 触发:OPSXDB_E2E_ZK=1;前置 ZK :2181、~/.opsxdb/agents/jdbc/drivers/zookeeper/ 有官方 jar。
|
||||
func TestE2EZooKeeper(t *testing.T) {
|
||||
if os.Getenv("OPSXDB_E2E_ZK") != "1" {
|
||||
t.Skip("置 OPSXDB_E2E_ZK=1 并启动 ZK :2181 后运行")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
|
||||
c := ConnReq{Driver: "zookeeper", Host: "127.0.0.1", Port: 2181}
|
||||
if err := TestConnection(c); err != nil {
|
||||
t.Skipf("ZK :2181 不可达: %v", err)
|
||||
}
|
||||
dbs, err := ListDatabases(ctx, c)
|
||||
if err != nil || len(dbs) == 0 {
|
||||
t.Fatalf("ListDatabases = %v, %v", dbs, err)
|
||||
}
|
||||
hasZk := false
|
||||
for _, n := range dbs {
|
||||
if n == "zookeeper" {
|
||||
hasZk = true
|
||||
}
|
||||
}
|
||||
if !hasZk {
|
||||
t.Fatalf("根下应含 zookeeper: %v", dbs)
|
||||
}
|
||||
cc := c
|
||||
cc.Database = "zookeeper"
|
||||
tables, err := ListTables(ctx, cc)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTables: %v", err)
|
||||
}
|
||||
hasConfig := false
|
||||
for _, n := range tables {
|
||||
if n == "config" {
|
||||
hasConfig = true
|
||||
}
|
||||
}
|
||||
if !hasConfig {
|
||||
t.Fatalf("/zookeeper 下应含 config: %v", tables)
|
||||
}
|
||||
// 读:GET
|
||||
r := ExecSQL(ctx, ConnReq{Driver: "zookeeper", Host: "127.0.0.1", Port: 2181, Database: "zookeeper", SQL: "GET /zookeeper/config", Limit: 100})
|
||||
if r.Error != "" {
|
||||
t.Fatalf("GET: %s", r.Error)
|
||||
}
|
||||
if r.RowCount != 1 || r.Rows[0]["path"] != "/zookeeper/config" {
|
||||
t.Fatalf("GET rows = %+v", r.Rows)
|
||||
}
|
||||
// 写:CREATE → GET → SET → DELETE(DELETE 无 WHERE 属危险语句,走确认通道)
|
||||
w := func(sql string) {
|
||||
t.Helper()
|
||||
res := ExecSQL(ctx, ConnReq{Driver: "zookeeper", Host: "127.0.0.1", Port: 2181, SQL: sql, ConfirmDanger: true})
|
||||
if res.Error != "" {
|
||||
t.Fatalf("%s → %s", sql, res.Error)
|
||||
}
|
||||
}
|
||||
w("CREATE /opsxe2e hello-opsx")
|
||||
r = ExecSQL(ctx, ConnReq{Driver: "zookeeper", Host: "127.0.0.1", Port: 2181, SQL: "GET /opsxe2e", Limit: 100})
|
||||
if r.Rows[0]["data"] != "hello-opsx" {
|
||||
t.Fatalf("CREATE 后 data = %v", r.Rows[0]["data"])
|
||||
}
|
||||
w("SET /opsxe2e updated")
|
||||
r = ExecSQL(ctx, ConnReq{Driver: "zookeeper", Host: "127.0.0.1", Port: 2181, SQL: "GET /opsxe2e", Limit: 100})
|
||||
if r.Rows[0]["data"] != "updated" {
|
||||
t.Fatalf("SET 后 data = %v", r.Rows[0]["data"])
|
||||
}
|
||||
// 危险语句未确认时必须先返回 NeedConfirm
|
||||
guard := ExecSQL(ctx, ConnReq{Driver: "zookeeper", Host: "127.0.0.1", Port: 2181, SQL: "DELETE /opsxe2e"})
|
||||
if !guard.NeedConfirm {
|
||||
t.Fatalf("DELETE 未确认应返回 NeedConfirm,got %+v", guard)
|
||||
}
|
||||
w("DELETE /opsxe2e")
|
||||
}
|
||||
|
||||
// TestE2EInfluxDB —— 真实 InfluxDB 2.7(docker)原生 HTTP 全链路:bucket/measurement/Flux 查询。
|
||||
// 触发:OPSXDB_E2E_INFLUX=1;前置 InfluxDB :8086(org opsx / token opsxdb-demo-token / bucket demo)。
|
||||
func TestE2EInfluxDB(t *testing.T) {
|
||||
if os.Getenv("OPSXDB_E2E_INFLUX") != "1" {
|
||||
t.Skip("置 OPSXDB_E2E_INFLUX=1 并启动 InfluxDB :8086 后运行")
|
||||
}
|
||||
// 种子点(行协议写入)
|
||||
wreq, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1:8086/api/v2/write?org=opsx&bucket=demo&precision=s",
|
||||
strings.NewReader("e2e_m,site=local v=42"))
|
||||
wreq.Header.Set("Authorization", "Token opsxdb-demo-token")
|
||||
wreq.Header.Set("Content-Type", "text/plain; charset=utf-8")
|
||||
wresp, err := http.DefaultClient.Do(wreq)
|
||||
if err != nil {
|
||||
t.Skipf("InfluxDB :8086 不可达: %v", err)
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, wresp.Body)
|
||||
wresp.Body.Close()
|
||||
if wresp.StatusCode >= 400 {
|
||||
t.Fatalf("种子写入失败: HTTP %d", wresp.StatusCode)
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond) // 存储引擎落盘
|
||||
|
||||
c := ConnReq{Driver: "influxdb", Host: "127.0.0.1", Port: 8086, User: "opsx", Password: "opsxdb-demo-token"}
|
||||
if err := TestConnection(c); err != nil {
|
||||
t.Fatalf("TestConnection: %v", err)
|
||||
}
|
||||
dbs, err := ListDatabases(context.Background(), c)
|
||||
if err != nil {
|
||||
t.Fatalf("ListDatabases: %v", err)
|
||||
}
|
||||
hasDemo := false
|
||||
for _, n := range dbs {
|
||||
if n == "demo" {
|
||||
hasDemo = true
|
||||
}
|
||||
}
|
||||
if !hasDemo {
|
||||
t.Fatalf("buckets 未含 demo: %v", dbs)
|
||||
}
|
||||
cc := c
|
||||
cc.Database = "demo"
|
||||
tables, err := ListTables(context.Background(), cc)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTables: %v", err)
|
||||
}
|
||||
hasM := false
|
||||
for _, n := range tables {
|
||||
if n == "e2e_m" {
|
||||
hasM = true
|
||||
}
|
||||
}
|
||||
if !hasM {
|
||||
t.Fatalf("measurements 未含 e2e_m: %v", tables)
|
||||
}
|
||||
r := ExecSQL(context.Background(), ConnReq{
|
||||
Driver: "influxdb", Host: "127.0.0.1", Port: 8086, User: "opsx", Password: "opsxdb-demo-token",
|
||||
SQL: `from(bucket: "demo") |> range(start: -1h) |> filter(fn: (r) => r._measurement == "e2e_m")`,
|
||||
Limit: 100,
|
||||
})
|
||||
if r.Error != "" {
|
||||
t.Fatalf("Flux 查询: %s", r.Error)
|
||||
}
|
||||
if r.RowCount < 1 {
|
||||
t.Fatalf("rows = %+v", r.Rows)
|
||||
}
|
||||
}
|
||||
|
||||
// TestE2EMongoDB —— 真实 MongoDB 7 + OpsxMongoAgent 全链路:库/集合浏览、DSL 查改增删、危险确认。
|
||||
// 触发:OPSXDB_E2E_MONGO=1;前置 MongoDB :27017、~/.opsxdb/agents/jdbc/drivers/mongodb/ 有官方 jar。
|
||||
func TestE2EMongoDB(t *testing.T) {
|
||||
if os.Getenv("OPSXDB_E2E_MONGO") != "1" {
|
||||
t.Skip("置 OPSXDB_E2E_MONGO=1 并启动 MongoDB :27017 后运行")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
|
||||
c := ConnReq{Driver: "mongodb", Host: "127.0.0.1", Port: 27017}
|
||||
if err := TestConnection(c); err != nil {
|
||||
t.Skipf("MongoDB :27017 不可达: %v", err)
|
||||
}
|
||||
// 种子:插入文档(确认通道)
|
||||
seed := ExecSQL(ctx, ConnReq{Driver: "mongodb", Host: "127.0.0.1", Port: 27017, Database: "opsxe2e", SQL: `db.c1.insertOne({"name":"opsx","n":7})`, ConfirmDanger: false})
|
||||
if seed.Error != "" {
|
||||
t.Fatalf("insertOne: %s", seed.Error)
|
||||
}
|
||||
dbs, err := ListDatabases(ctx, c)
|
||||
if err != nil || len(dbs) == 0 {
|
||||
t.Fatalf("ListDatabases = %v, %v", dbs, err)
|
||||
}
|
||||
hasDb := false
|
||||
for _, n := range dbs {
|
||||
if n == "opsxe2e" {
|
||||
hasDb = true
|
||||
}
|
||||
}
|
||||
if !hasDb {
|
||||
t.Fatalf("库列表未含 opsxe2e: %v", dbs)
|
||||
}
|
||||
cc := c
|
||||
cc.Database = "opsxe2e"
|
||||
tables, err := ListTables(ctx, cc)
|
||||
if err != nil || len(tables) == 0 {
|
||||
t.Fatalf("ListTables = %v, %v", tables, err)
|
||||
}
|
||||
found := false
|
||||
for _, n := range tables {
|
||||
if n == "c1" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("集合列表未含 c1: %v", tables)
|
||||
}
|
||||
cols, err := ListColumns(ctx, ConnReq{Driver: "mongodb", Host: "127.0.0.1", Port: 27017, Database: "opsxe2e", Table: "c1"})
|
||||
if err != nil || len(cols) == 0 {
|
||||
t.Fatalf("ListColumns = %v, %v", cols, err)
|
||||
}
|
||||
// 查询
|
||||
q := ExecSQL(ctx, ConnReq{Driver: "mongodb", Host: "127.0.0.1", Port: 27017, Database: "opsxe2e", SQL: `db.c1.find({"name":"opsx"})`, Limit: 100})
|
||||
if q.Error != "" || q.RowCount < 1 {
|
||||
t.Fatalf("find: err=%s rows=%+v", q.Error, q.Rows)
|
||||
}
|
||||
// 危险确认:deleteMany 未确认必须先返回 NeedConfirm
|
||||
guard := ExecSQL(ctx, ConnReq{Driver: "mongodb", Host: "127.0.0.1", Port: 27017, Database: "opsxe2e", SQL: `db.c1.deleteMany({"name":"opsx"})`})
|
||||
if !guard.NeedConfirm {
|
||||
t.Fatalf("deleteMany 未确认应 NeedConfirm,got %+v", guard)
|
||||
}
|
||||
// 更新 + 删除(确认通道)
|
||||
u := ExecSQL(ctx, ConnReq{Driver: "mongodb", Host: "127.0.0.1", Port: 27017, Database: "opsxe2e", SQL: `db.c1.updateOne({"name":"opsx"}, {"$set":{"n":8}})`, ConfirmDanger: true})
|
||||
if u.Error != "" {
|
||||
t.Fatalf("updateOne: %s", u.Error)
|
||||
}
|
||||
d := ExecSQL(ctx, ConnReq{Driver: "mongodb", Host: "127.0.0.1", Port: 27017, Database: "opsxe2e", SQL: `db.c1.deleteMany({"name":"opsx"})`, ConfirmDanger: true})
|
||||
if d.Error != "" {
|
||||
t.Fatalf("deleteMany: %s", d.Error)
|
||||
}
|
||||
cnt := ExecSQL(ctx, ConnReq{Driver: "mongodb", Host: "127.0.0.1", Port: 27017, Database: "opsxe2e", SQL: `db.c1.count({"name":"opsx"})`})
|
||||
if fmt.Sprintf("%v", cnt.Rows[0]["n"]) != "0" {
|
||||
t.Fatalf("count after delete = %v", cnt.Rows[0]["n"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,394 @@
|
||||
package db
|
||||
|
||||
// agent_test.go —— JDBC agent 桥的协议层测试。
|
||||
// 用 helper process 扮演 JVM agent(TestMain 拦截 -agent-stub),
|
||||
// 无需本机安装 Java 即可验证:握手、请求/响应路由、超时、进程退出、并发串行写。
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestMain 拦截 stub 模式:测试二进制被当成 agent 子进程启动时直接进入协议循环。
|
||||
func TestMain(m *testing.M) {
|
||||
for _, a := range os.Args {
|
||||
if a == "-agent-stub" {
|
||||
runAgentStub()
|
||||
return
|
||||
}
|
||||
}
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
// runAgentStub 扮演 OpsxJdbcAgent 的最小协议面(与 agents/jdbc/OpsxJdbcAgent.java 对应)。
|
||||
func runAgentStub() {
|
||||
out := bufio.NewWriter(os.Stdout)
|
||||
announce := agentResponse{ID: 0, OK: true}
|
||||
announce.Result = json.RawMessage(`{"agent":"opsx-agent-jdbc","version":"0.1.0","protocol":1}`)
|
||||
write := func(r agentResponse) {
|
||||
b, _ := json.Marshal(r)
|
||||
_, _ = out.WriteString(string(b) + "\n")
|
||||
out.Flush()
|
||||
}
|
||||
write(announce)
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
in.Buffer(make([]byte, 1024*1024), 1024*1024)
|
||||
for in.Scan() {
|
||||
var req agentRequest
|
||||
if json.Unmarshal(in.Bytes(), &req) != nil {
|
||||
continue
|
||||
}
|
||||
args := req.Args
|
||||
fail := func(msg string) agentResponse { return agentResponse{ID: req.ID, OK: false, Error: msg} }
|
||||
switch req.Op {
|
||||
case "hello":
|
||||
r := agentResponse{ID: req.ID, OK: true}
|
||||
r.Result = json.RawMessage(`{"agent":"opsx-agent-jdbc","version":"0.1.0","protocol":1}`)
|
||||
write(r)
|
||||
case "open":
|
||||
if args["url"] == "" || args["url"] == nil {
|
||||
write(fail("missing url"))
|
||||
break
|
||||
}
|
||||
r := agentResponse{ID: req.ID, OK: true}
|
||||
r.Result = json.RawMessage(`{"handle":"h1"}`)
|
||||
write(r)
|
||||
case "ping":
|
||||
write(agentResponse{ID: req.ID, OK: true})
|
||||
case "query":
|
||||
if strings.Contains(fmt.Sprintf("%v", args["sql"]), "SLOW") {
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
r := agentResponse{ID: req.ID, OK: true}
|
||||
r.Result = json.RawMessage(`{"columns":["id","name"],"rows":[{"id":"1","name":"demo"}],"truncated":false}`)
|
||||
write(r)
|
||||
case "exec":
|
||||
r := agentResponse{ID: req.ID, OK: true}
|
||||
r.Result = json.RawMessage(`{"affectedRows":3}`)
|
||||
write(r)
|
||||
case "metaDatabases":
|
||||
r := agentResponse{ID: req.ID, OK: true}
|
||||
r.Result = json.RawMessage(`{"names":["MAIN","SYS"]}`)
|
||||
write(r)
|
||||
case "close":
|
||||
write(agentResponse{ID: req.ID, OK: true})
|
||||
case "shutdown":
|
||||
write(agentResponse{ID: req.ID, OK: true})
|
||||
os.Exit(0)
|
||||
default:
|
||||
write(fail("unknown op: " + req.Op))
|
||||
}
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// TestAgentStubHelper 占位:stub 子进程以 -test.run=TestAgentStubHelper 启动时
|
||||
// 不会走到这里(TestMain 已拦截),但名字必须存在以匹配 -test.run。
|
||||
func TestAgentStubHelper(t *testing.T) {}
|
||||
|
||||
func startStubAgent(t *testing.T) *AgentClient {
|
||||
t.Helper()
|
||||
cmd := exec.Command(os.Args[0], "-test.run=TestAgentStubHelper", "--", "-agent-stub")
|
||||
cli, err := StartAgentFromCmd(cmd, agentDriverSpecs["dameng"])
|
||||
if err != nil {
|
||||
t.Fatalf("StartAgentFromCmd: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { cli.Shutdown(context.Background()) })
|
||||
return cli
|
||||
}
|
||||
|
||||
func TestAgentClientRoundTrip(t *testing.T) {
|
||||
cli := startStubAgent(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// open → query → exec → metaDatabases → ping
|
||||
handle := ""
|
||||
{
|
||||
var res struct {
|
||||
Handle string `json:"handle"`
|
||||
}
|
||||
if err := cli.call(ctx, "open", map[string]any{"url": "jdbc:dm://127.0.0.1:5236", "user": "SYSDBA"}, &res); err != nil {
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
if res.Handle != "h1" {
|
||||
t.Fatalf("handle = %q, want h1", res.Handle)
|
||||
}
|
||||
handle = res.Handle
|
||||
}
|
||||
{
|
||||
var res struct {
|
||||
Columns []string `json:"columns"`
|
||||
Rows []map[string]interface{} `json:"rows"`
|
||||
}
|
||||
if err := cli.call(ctx, "query", map[string]any{"handle": handle, "sql": "SELECT 1", "maxRows": 100}, &res); err != nil {
|
||||
t.Fatalf("query: %v", err)
|
||||
}
|
||||
if len(res.Columns) != 2 || res.Rows[0]["name"] != "demo" {
|
||||
t.Fatalf("query result mismatch: %+v", res)
|
||||
}
|
||||
}
|
||||
{
|
||||
var res struct {
|
||||
AffectedRows int64 `json:"affectedRows"`
|
||||
}
|
||||
if err := cli.call(ctx, "exec", map[string]any{"handle": handle, "sql": "UPDATE t SET a=1"}, &res); err != nil {
|
||||
t.Fatalf("exec: %v", err)
|
||||
}
|
||||
if res.AffectedRows != 3 {
|
||||
t.Fatalf("affectedRows = %d, want 3", res.AffectedRows)
|
||||
}
|
||||
}
|
||||
{
|
||||
var res struct {
|
||||
Names []string `json:"names"`
|
||||
}
|
||||
if err := cli.call(ctx, "metaDatabases", map[string]any{"handle": handle}, &res); err != nil {
|
||||
t.Fatalf("metaDatabases: %v", err)
|
||||
}
|
||||
if len(res.Names) != 2 || res.Names[0] != "MAIN" {
|
||||
t.Fatalf("names = %v", res.Names)
|
||||
}
|
||||
}
|
||||
if err := cli.call(ctx, "ping", map[string]any{"handle": handle}, nil); err != nil {
|
||||
t.Fatalf("ping: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentCallTimeout(t *testing.T) {
|
||||
cli := startStubAgent(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
defer cancel()
|
||||
var res map[string]any
|
||||
err := cli.call(ctx, "query", map[string]any{"handle": "h1", "sql": "SELECT SLOW", "maxRows": 10}, &res)
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("err = %v, want DeadlineExceeded", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentProcessExit(t *testing.T) {
|
||||
cli := startStubAgent(t)
|
||||
cli.Kill() // 杀进程后再调用,必须报进程退出而非挂死
|
||||
var res map[string]any
|
||||
err := cli.call(context.Background(), "ping", map[string]any{"handle": "h1"}, &res)
|
||||
if err == nil {
|
||||
t.Fatal("期望进程退出错误,got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentConcurrentCalls(t *testing.T) {
|
||||
cli := startStubAgent(t)
|
||||
ctx := context.Background()
|
||||
if err := cli.call(ctx, "open", map[string]any{"url": "jdbc:dm://h:5236"}, nil); err != nil {
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
const n = 24
|
||||
var wg sync.WaitGroup
|
||||
errs := make([]error, n)
|
||||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
var res struct {
|
||||
Columns []string `json:"columns"`
|
||||
}
|
||||
errs[i] = cli.call(ctx, "query", map[string]any{"handle": "h1", "sql": "SELECT 1", "maxRows": 1}, &res)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
for i, err := range errs {
|
||||
if err != nil {
|
||||
t.Fatalf("call %d: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderAgentURL(t *testing.T) {
|
||||
spec := agentDriverSpecs["dameng"]
|
||||
url, err := renderAgentURL(spec, ConnReq{Host: "127.0.0.1", Port: 5236})
|
||||
if err != nil {
|
||||
t.Fatalf("render: %v", err)
|
||||
}
|
||||
if url != "jdbc:dm://127.0.0.1:5236" {
|
||||
t.Fatalf("url = %q", url)
|
||||
}
|
||||
if _, err := renderAgentURL(spec, ConnReq{Host: "", Port: 5236}); err == nil {
|
||||
t.Fatal("空主机必须报错")
|
||||
}
|
||||
if _, err := renderAgentURL(spec, ConnReq{Host: "127.0.0.1", Port: 0}); err == nil {
|
||||
t.Fatal("零端口必须报错")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloadPinnedChecksum(t *testing.T) {
|
||||
payload := []byte("fake-gson-bytes")
|
||||
good := sha1.Sum(payload)
|
||||
cases := []struct {
|
||||
name string
|
||||
body []byte
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{"校验通过", payload, hex.EncodeToString(good[:]), false},
|
||||
{"校验失败拒绝落盘", []byte("tampered"), hex.EncodeToString(good[:]), true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write(tc.body)
|
||||
}))
|
||||
defer srv.Close()
|
||||
dest := t.TempDir() + "/gson.jar"
|
||||
err := downloadPinned(srv.URL, dest, tc.want)
|
||||
if tc.wantErr && err == nil {
|
||||
t.Fatal("期望校验失败")
|
||||
}
|
||||
if !tc.wantErr {
|
||||
if err != nil {
|
||||
t.Fatalf("download: %v", err)
|
||||
}
|
||||
if _, statErr := os.Stat(dest); statErr != nil {
|
||||
t.Fatalf("文件未落盘: %v", statErr)
|
||||
}
|
||||
} else {
|
||||
if _, statErr := os.Stat(dest); statErr == nil {
|
||||
t.Fatal("校验失败时文件不应存在")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentConfigured(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("OPSXDB_DAMENG_JARS", dir)
|
||||
if ok, _ := AgentConfigured("dameng"); ok {
|
||||
t.Fatal("空目录不应判为已配置")
|
||||
}
|
||||
if err := os.WriteFile(dir+"/DmJdbcDriver-8.1.3.140.jar", []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, _ := AgentConfigured("dameng"); !ok {
|
||||
t.Fatal("放置 jar 后应判为已配置")
|
||||
}
|
||||
if IsAgentDriver("mysql") {
|
||||
t.Fatal("mysql 不应是 agent 驱动")
|
||||
}
|
||||
if !IsAgentDriver("dameng") {
|
||||
t.Fatal("dameng 应是 agent 驱动")
|
||||
}
|
||||
}
|
||||
|
||||
// TestEnsureAgentRuntimeOfflinePack —— 网络不可用(镜像指向无效 + sha256 不匹配)时,
|
||||
// ensureAgentRuntime 必须从离线驱动包导入 jar(pack.sh 产物)。
|
||||
func TestEnsureAgentRuntimeOfflinePack(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("OPSXDB_HOME", home)
|
||||
t.Setenv("OPSXDB_AGENT_JAR", filepath.Join(home, "agent", "opsx-agent-jdbc.jar"))
|
||||
// 网络必失败:镜像指向本地空端口 + 错误的 sha256
|
||||
t.Setenv("MAVEN_MIRROR", "http://127.0.0.1:1/none")
|
||||
badSHA := sha1.Sum([]byte("bad"))
|
||||
spec := AgentSpec{
|
||||
DriverKey: "e2e-offline",
|
||||
DriverClass: "x",
|
||||
URLTemplate: "jdbc:x://{{.Host}}:{{.Port}}",
|
||||
JarsDirEnv: "OPSXDB_OFFLINE_JARS",
|
||||
}
|
||||
agentDriverSpecs[spec.DriverKey] = spec
|
||||
t.Cleanup(func() { delete(agentDriverSpecs, spec.DriverKey) })
|
||||
agentDriverArtifacts[spec.DriverKey] = []agentArtifact{
|
||||
{"demo.jar", "com.nope:demo:1.0", hex.EncodeToString(badSHA[:])},
|
||||
}
|
||||
|
||||
// 构造离线包:manifest + 正确的 jar 内容(sha256 以包内 manifest 为准)
|
||||
jarBytes := []byte("fake-jar-content")
|
||||
files := map[string]string{"drivers/e2e-offline/demo.jar": string(jarBytes)}
|
||||
manifestFiles := []map[string]string{{"path": "drivers/e2e-offline/demo.jar", "sha256": fmt.Sprintf("%x", sha256.Sum256(jarBytes))}}
|
||||
zipPath := filepath.Join(home, "offline.zip")
|
||||
if err := writeTestZip(zipPath, manifestFiles, files); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("OPSXDB_OFFLINE_ZIP", zipPath)
|
||||
|
||||
// agent jar 也在包里(确保 ensureAgentJar 的离线路径不拦路)
|
||||
if err := importFromOfflinePack(map[string]string{"pack/agent/opsx-agent-jdbc.jar": filepath.Join(home, "agent", "opsx-agent-jdbc.jar")}); err != nil {
|
||||
// agent jar 不在此包属预期——单独构造一份最小 agent 条目
|
||||
agentBytes := []byte("fake-agent")
|
||||
mf2 := []map[string]string{{"path": "agent/opsx-agent-jdbc.jar", "sha256": fmt.Sprintf("%x", sha256.Sum256(agentBytes))}}
|
||||
f2 := map[string]string{"agent/opsx-agent-jdbc.jar": string(agentBytes)}
|
||||
if werr := writeTestZip(zipPath, append(manifestFiles, mf2...), mergeMaps(files, f2)); werr != nil {
|
||||
t.Fatal(werr)
|
||||
}
|
||||
}
|
||||
|
||||
if err := ensureAgentRuntime(spec); err != nil {
|
||||
t.Fatalf("ensureAgentRuntime 应走离线包成功: %v", err)
|
||||
}
|
||||
got, err := os.ReadFile(filepath.Join(home, "drivers", "e2e-offline", "demo.jar"))
|
||||
if err != nil || string(got) != string(jarBytes) {
|
||||
t.Fatalf("离线导入内容不符: %v", err)
|
||||
}
|
||||
// 负例:manifest hash 不符必须拒
|
||||
jarBytes2 := []byte("tampered")
|
||||
mf3 := []map[string]string{{"path": "drivers/e2e-offline/demo.jar", "sha256": fmt.Sprintf("%x", sha256.Sum256(jarBytes2))}}
|
||||
if err := writeTestZip(zipPath, mf3, map[string]string{"drivers/e2e-offline/demo.jar": "not-the-same-content"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
os.Remove(filepath.Join(home, "drivers", "e2e-offline", "demo.jar"))
|
||||
if err := ensureAgentRuntime(spec); err == nil {
|
||||
t.Fatal("manifest 与实际内容不符时必须失败")
|
||||
}
|
||||
}
|
||||
|
||||
func writeTestZip(path string, manifestFiles []map[string]string, files map[string]string) error {
|
||||
zf, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer zf.Close()
|
||||
zw := zip.NewWriter(zf)
|
||||
mf, err := zw.Create("pack/manifest.json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b, _ := json.Marshal(map[string]any{"bundleVersion": 1, "files": manifestFiles})
|
||||
if _, err := mf.Write(b); err != nil {
|
||||
return err
|
||||
}
|
||||
for rel, content := range files {
|
||||
w, err := zw.Create("pack/" + rel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := w.Write([]byte(content)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return zw.Close()
|
||||
}
|
||||
|
||||
func mergeMaps(a, b map[string]string) map[string]string {
|
||||
out := map[string]string{}
|
||||
for k, v := range a {
|
||||
out[k] = v
|
||||
}
|
||||
for k, v := range b {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -1,30 +1,87 @@
|
||||
package db
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IsDangerousSQL 识别可能破坏数据的语句,供前端二次确认。
|
||||
func IsDangerousSQL(sql string) bool {
|
||||
s := stripSQLLead(sql)
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
up := strings.ToUpper(s)
|
||||
for _, p := range []string{"DROP ", "DROP\t", "TRUNCATE ", "ALTER ", "GRANT ", "REVOKE "} {
|
||||
if strings.HasPrefix(up, strings.TrimSpace(p)) || strings.HasPrefix(up, p) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(up, "DELETE") {
|
||||
return !containsWhere(up)
|
||||
}
|
||||
if strings.HasPrefix(up, "UPDATE") {
|
||||
return !containsWhere(up)
|
||||
}
|
||||
return false
|
||||
// 危险语句规则:词边界匹配,作用于 StripComments 之后的纯代码文本。
|
||||
// 词表:DROP/TRUNCATE/ALTER/GRANT/REVOKE/CREATE USER|ROLE/RENAME TABLE/
|
||||
// REPLACE INTO/LOAD DATA/INTO OUTFILE|DUMPFILE/xp_cmdshell/COPY..PROGRAM/EXEC/CALL。
|
||||
var dangerPatterns = []struct {
|
||||
re *regexp.Regexp
|
||||
desc string
|
||||
}{
|
||||
{regexp.MustCompile(`\bDROP\b`), "DROP"},
|
||||
{regexp.MustCompile(`\bTRUNCATE\b`), "TRUNCATE"},
|
||||
{regexp.MustCompile(`\bALTER\b`), "ALTER"},
|
||||
{regexp.MustCompile(`\bGRANT\b`), "GRANT"},
|
||||
{regexp.MustCompile(`\bREVOKE\b`), "REVOKE"},
|
||||
{regexp.MustCompile(`\bCREATE\s+(USER|ROLE)\b`), "CREATE USER/ROLE"},
|
||||
{regexp.MustCompile(`\bRENAME\s+TABLE\b`), "RENAME TABLE"},
|
||||
{regexp.MustCompile(`\bREPLACE\s+INTO\b`), "REPLACE INTO"},
|
||||
{regexp.MustCompile(`\bLOAD\s+DATA\b`), "LOAD DATA"},
|
||||
{regexp.MustCompile(`\bINTO\s+(OUTFILE|DUMPFILE)\b`), "INTO OUTFILE/DUMPFILE"},
|
||||
{regexp.MustCompile(`(?i)\bxp_cmdshell\b`), "xp_cmdshell"},
|
||||
{regexp.MustCompile(`\bCOPY\b[^;]*\b(FROM|TO)\s+PROGRAM\b`), "COPY FROM/TO PROGRAM"},
|
||||
}
|
||||
|
||||
func containsWhere(up string) bool {
|
||||
return strings.Contains(up, " WHERE ") || strings.Contains(up, "\nWHERE ") || strings.Contains(up, "\tWHERE ")
|
||||
var (
|
||||
reWriteNoWhere = regexp.MustCompile(`\b(DELETE|UPDATE)\b`)
|
||||
reWhere = regexp.MustCompile(`\bWHERE\b`)
|
||||
reExecCallHead = regexp.MustCompile(`^(EXEC|EXECUTE|CALL)\b`)
|
||||
)
|
||||
|
||||
// CheckDangerousSQL 服务端权威的危险语句判定(前端预检仅为 UX)。
|
||||
// 逐语句检查;注释/字符串内的关键字不计;MySQL /*! */ 可执行注释内容参与检查;
|
||||
// WITH 包裹的写操作因全文扫描 CTE 体同样命中。
|
||||
// 返回命中的危险原因清单(去重)。
|
||||
func CheckDangerousSQL(driver, sqlText string) []string {
|
||||
_ = driver // 词表当前各驱动通用,保留参数便于后续按方言细化
|
||||
seen := map[string]struct{}{}
|
||||
var hits []string
|
||||
add := func(desc, stmt string) {
|
||||
if _, ok := seen[desc]; ok {
|
||||
return
|
||||
}
|
||||
seen[desc] = struct{}{}
|
||||
hits = append(hits, desc+": "+truncateSQL(stmt, 80))
|
||||
}
|
||||
for _, stmt := range SplitStatements(sqlText) {
|
||||
code := strings.ToUpper(StripComments(stmt))
|
||||
code = strings.TrimSpace(code)
|
||||
if code == "" {
|
||||
continue
|
||||
}
|
||||
for _, p := range dangerPatterns {
|
||||
if p.re.MatchString(code) {
|
||||
add(p.desc, stmt)
|
||||
}
|
||||
}
|
||||
// DELETE/UPDATE 无 WHERE(含 WITH CTE 体内的写操作)
|
||||
if reWriteNoWhere.MatchString(code) && !reWhere.MatchString(code) {
|
||||
kw := reWriteNoWhere.FindString(code)
|
||||
add(kw+" 无 WHERE", stmt)
|
||||
}
|
||||
// 存储过程/动态执行仅匹配语句开头,降低误报
|
||||
if m := reExecCallHead.FindString(code); m != "" {
|
||||
add(m, stmt)
|
||||
}
|
||||
}
|
||||
return hits
|
||||
}
|
||||
|
||||
// IsDangerousSQL 兼容旧调用:命中任意危险规则即 true。
|
||||
func IsDangerousSQL(sql string) bool {
|
||||
return len(CheckDangerousSQL("", sql)) > 0
|
||||
}
|
||||
|
||||
func truncateSQL(s string, n int) string {
|
||||
s = strings.Join(strings.Fields(s), " ")
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
return s[:n] + "…"
|
||||
}
|
||||
|
||||
func stripSQLLead(sql string) string {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package db
|
||||
|
||||
import "testing"
|
||||
|
||||
// 危险语句命中/放行矩阵,含四类历史绕过:多语句、/*! */ 可执行注释、
|
||||
// 注释吞掉 WHERE、WITH CTE 包裹写操作。
|
||||
func TestCheckDangerousSQLMatrix(t *testing.T) {
|
||||
dangerous := []string{
|
||||
"DROP TABLE users",
|
||||
"drop table users", // 大小写
|
||||
"TRUNCATE TABLE t", // TRUNCATE
|
||||
"ALTER TABLE t ADD COLUMN c int", // ALTER(误报方向维持现状)
|
||||
"GRANT ALL ON *.* TO 'u'", // GRANT
|
||||
"REVOKE ALL ON *.* FROM 'u'", // REVOKE
|
||||
"CREATE USER 'u'@'%' IDENTIFIED BY 'p'", // CREATE USER
|
||||
"CREATE ROLE app", // CREATE ROLE
|
||||
"RENAME TABLE a TO b", // RENAME TABLE
|
||||
"REPLACE INTO t VALUES (1)", // REPLACE INTO
|
||||
"LOAD DATA INFILE '/tmp/x' INTO TABLE t", // LOAD DATA
|
||||
"SELECT * INTO OUTFILE '/tmp/x' FROM t", // INTO OUTFILE
|
||||
"SELECT * INTO DUMPFILE '/tmp/x' FROM t", // INTO DUMPFILE
|
||||
"EXEC xp_cmdshell 'dir'", // xp_cmdshell
|
||||
"EXEC sp_configure 'x'", // EXEC 开头
|
||||
"CALL do_thing()", // CALL 开头
|
||||
"COPY t FROM PROGRAM 'cmd'", // COPY FROM PROGRAM
|
||||
"DELETE FROM users", // DELETE 无 WHERE
|
||||
"UPDATE users SET x=1", // UPDATE 无 WHERE
|
||||
"SELECT 1; DROP TABLE t", // 多语句绕过
|
||||
"/*!50000 DROP TABLE t*/", // MySQL 可执行注释
|
||||
"SELECT 1; /*!40101 SET @x = 1*/; DROP TABLE t", // 可执行注释 + 多语句
|
||||
"UPDATE t SET x=1 -- WHERE id=2", // 注释吞掉 WHERE
|
||||
"UPDATE t SET x=1 # WHERE id=2", // # 注释吞掉 WHERE
|
||||
"UPDATE t SET x=1 /* WHERE id=2 */", // 块注释吞掉 WHERE
|
||||
"WITH x AS (SELECT 1) DELETE FROM t", // CTE 包裹 DELETE 无 WHERE
|
||||
"WITH x AS (SELECT 1) UPDATE t SET a=1", // CTE 包裹 UPDATE 无 WHERE
|
||||
}
|
||||
for _, sql := range dangerous {
|
||||
if hits := CheckDangerousSQL("mysql", sql); len(hits) == 0 {
|
||||
t.Errorf("应命中但未命中: %q", sql)
|
||||
}
|
||||
}
|
||||
|
||||
safe := []string{
|
||||
"SELECT * FROM users",
|
||||
"SELECT * FROM users WHERE id = 1",
|
||||
"DELETE FROM users WHERE id = 1",
|
||||
"UPDATE users SET x = 1 WHERE id = 2",
|
||||
"UPDATE users SET x = 1\nWHERE id = 2",
|
||||
"INSERT INTO t VALUES (1)",
|
||||
"CREATE TABLE t (id int)",
|
||||
"SELECT 'DROP TABLE x'", // 字符串内关键字
|
||||
"SELECT * FROM t WHERE note = 'truncate it'", // 字符串内关键字
|
||||
"-- DROP TABLE t\nSELECT 1", // 注释内关键字
|
||||
"SELECT * FROM t /* DROP TABLE x */ WHERE 1=1", // 块注释内关键字
|
||||
"SELECT * FROM updated WHERE id = 1", // 词边界:updated 不算 UPDATE
|
||||
"SELECT 1; SELECT 2",
|
||||
"WITH x AS (SELECT 1) SELECT * FROM x",
|
||||
"SHOW TABLES",
|
||||
"EXPLAIN SELECT 1",
|
||||
}
|
||||
for _, sql := range safe {
|
||||
if hits := CheckDangerousSQL("mysql", sql); len(hits) > 0 {
|
||||
t.Errorf("误报: %q → %v", sql, hits)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 旧接口兼容:IsDangerousSQL 委托 CheckDangerousSQL。
|
||||
func TestIsDangerousSQLCompat(t *testing.T) {
|
||||
if !IsDangerousSQL("DROP TABLE users") {
|
||||
t.Fatal("drop")
|
||||
}
|
||||
if IsDangerousSQL("SELECT * FROM users") {
|
||||
t.Fatal("select")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetDDL 返回表/视图的建表语句。mysql 族走 SHOW CREATE TABLE,sqlite 读 sqlite_master,
|
||||
// pg/mssql/oracle 用 ListColumns 元数据在 Go 侧拼装 CREATE TABLE。
|
||||
func GetDDL(ctx context.Context, c ConnReq) (string, error) {
|
||||
ctx = reqCtx(ctx)
|
||||
if c.Table == "" {
|
||||
return "", fmt.Errorf("缺少表名")
|
||||
}
|
||||
if isRedis(c.Driver) {
|
||||
return "", fmt.Errorf("该驱动不支持 DDL 查看")
|
||||
}
|
||||
// pg/mssql/oracle:复用列元数据拼装,避免再查一次库
|
||||
switch {
|
||||
case isPostgresFamily(c.Driver), isMSSQL(c.Driver), driverKey(c.Driver) == "oracle":
|
||||
cols, err := ListColumns(ctx, c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buildDDLFromColumns(c.Driver, c.Table, cols)
|
||||
}
|
||||
var out string
|
||||
err := withDB(c, func(db *sql.DB) error {
|
||||
var err error
|
||||
switch {
|
||||
case isMySQLFamily(c.Driver):
|
||||
out, err = getDDLMySQL(ctx, db, c)
|
||||
case driverKey(c.Driver) == "sqlite":
|
||||
out, err = getDDLSQLite(ctx, db, c.Table)
|
||||
default:
|
||||
err = unsupportedDriverErr(c)
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// getDDLMySQL SHOW CREATE TABLE 第二列即 DDL(列名随对象类型变化:Create Table/Create View)。
|
||||
func getDDLMySQL(ctx context.Context, dbh *sql.DB, c ConnReq) (string, error) {
|
||||
ident := quoteMySQLIdent(c.Table)
|
||||
if c.Database != "" {
|
||||
ident = quoteMySQLIdent(c.Database) + "." + ident
|
||||
}
|
||||
rows, err := dbh.QueryContext(ctx, "SHOW CREATE TABLE "+ident)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer rows.Close()
|
||||
if rows.Next() {
|
||||
var name string
|
||||
var ddl sql.NullString
|
||||
if err := rows.Scan(&name, &ddl); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !ddl.Valid {
|
||||
return "", fmt.Errorf("无法读取 %s 的 DDL", c.Table)
|
||||
}
|
||||
return ddl.String, nil
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", fmt.Errorf("表 %s 不存在", c.Table)
|
||||
}
|
||||
|
||||
// getDDLSQLite sqlite_master 的 sql 列即建表语句。
|
||||
func getDDLSQLite(ctx context.Context, dbh *sql.DB, table string) (string, error) {
|
||||
var ddl sql.NullString
|
||||
err := dbh.QueryRowContext(ctx, `SELECT sql FROM sqlite_master WHERE name = ?`, table).Scan(&ddl)
|
||||
if err == sql.ErrNoRows {
|
||||
return "", fmt.Errorf("表 %s 不存在", table)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !ddl.Valid {
|
||||
return "", fmt.Errorf("表 %s 无 DDL 记录", table)
|
||||
}
|
||||
return ddl.String, nil
|
||||
}
|
||||
|
||||
// buildDDLFromColumns 用列元数据拼装 CREATE TABLE(类型直接用 ColumnInfo.Type)。
|
||||
func buildDDLFromColumns(driver, table string, cols []ColumnInfo) (string, error) {
|
||||
if len(cols) == 0 {
|
||||
return "", fmt.Errorf("表 %s 不存在或无列", table)
|
||||
}
|
||||
var pks []string
|
||||
for _, col := range cols {
|
||||
if col.Key == "PRI" {
|
||||
pks = append(pks, quoteIdentFor(driver, col.Name))
|
||||
}
|
||||
}
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "CREATE TABLE %s (\n", quoteTableNameFor(driver, table))
|
||||
for i, col := range cols {
|
||||
line := " " + quoteIdentFor(driver, col.Name) + " " + col.Type
|
||||
if !col.Nullable {
|
||||
line += " NOT NULL"
|
||||
}
|
||||
// 非最后一列、或后面还有 PRIMARY KEY 子句时需要逗号
|
||||
if i < len(cols)-1 || len(pks) > 0 {
|
||||
line += ","
|
||||
}
|
||||
b.WriteString(line + "\n")
|
||||
}
|
||||
if len(pks) > 0 {
|
||||
b.WriteString(" PRIMARY KEY (" + strings.Join(pks, ", ") + ")\n")
|
||||
}
|
||||
b.WriteString(");\n")
|
||||
return b.String(), nil
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// newSQLiteFile 建一个 sqlite 测试库文件并执行建表/插数语句,返回 ConnReq。
|
||||
func newSQLiteFile(t *testing.T, stmts ...string) ConnReq {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "test.db")
|
||||
if err := os.WriteFile(path, []byte{}, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, s := range stmts {
|
||||
res := ExecSQL(context.Background(), ConnReq{Driver: "sqlite", Database: path, SQL: s})
|
||||
if res.Error != "" {
|
||||
t.Fatalf("初始化语句失败 %q: %s", s, res.Error)
|
||||
}
|
||||
}
|
||||
return ConnReq{Driver: "sqlite", Database: path}
|
||||
}
|
||||
|
||||
// R2-B5:sqlite 返回 sqlite_master 中的原始建表语句。
|
||||
func TestGetDDLSQLite(t *testing.T) {
|
||||
c := newSQLiteFile(t, `CREATE TABLE t_person (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER)`)
|
||||
c.Table = "t_person"
|
||||
ddl, err := GetDDL(context.Background(), c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(ddl, "CREATE TABLE t_person") || !strings.Contains(ddl, "name TEXT") {
|
||||
t.Fatalf("DDL 内容不符: %s", ddl)
|
||||
}
|
||||
|
||||
c.Table = "no_such_table"
|
||||
if _, err := GetDDL(context.Background(), c); err == nil {
|
||||
t.Fatal("不存在的表应报错")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDDLRedis(t *testing.T) {
|
||||
_, err := GetDDL(context.Background(), ConnReq{Driver: "redis", Table: "k"})
|
||||
if err == nil || !strings.Contains(err.Error(), "不支持") {
|
||||
t.Fatalf("redis 应报不支持: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDDLMissingTable(t *testing.T) {
|
||||
_, err := GetDDL(context.Background(), ConnReq{Driver: "sqlite", Table: ""})
|
||||
if err == nil {
|
||||
t.Fatal("空表名应报错")
|
||||
}
|
||||
}
|
||||
|
||||
// R2-B5:pg/mssql/oracle 族用列元数据拼装,各 family 引号不同。
|
||||
func TestBuildDDLFromColumns(t *testing.T) {
|
||||
cols := []ColumnInfo{
|
||||
{Name: "id", Type: "integer", Nullable: false, Key: "PRI"},
|
||||
{Name: "na\"me", Type: "text", Nullable: true},
|
||||
}
|
||||
// pg:双引号加倍
|
||||
ddl, err := buildDDLFromColumns("postgres", "public.t", cols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(ddl, `"public"."t"`) || !strings.Contains(ddl, `"na""me" text`) ||
|
||||
!strings.Contains(ddl, `"id" integer NOT NULL,`) || !strings.Contains(ddl, `PRIMARY KEY ("id")`) {
|
||||
t.Fatalf("pg DDL 不符:\n%s", ddl)
|
||||
}
|
||||
// mssql:中括号
|
||||
ddl, err = buildDDLFromColumns("mssql", "dbo.t", cols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(ddl, `[id] integer NOT NULL`) || !strings.Contains(ddl, `PRIMARY KEY ([id])`) {
|
||||
t.Fatalf("mssql DDL 不符:\n%s", ddl)
|
||||
}
|
||||
// oracle:原名
|
||||
ddl, err = buildDDLFromColumns("oracle", "T", cols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(ddl, `CREATE TABLE T (`) || !strings.Contains(ddl, `PRIMARY KEY (id)`) {
|
||||
t.Fatalf("oracle DDL 不符:\n%s", ddl)
|
||||
}
|
||||
// 无列报错
|
||||
if _, err := buildDDLFromColumns("postgres", "t", nil); err == nil {
|
||||
t.Fatal("无列应报错")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package db
|
||||
|
||||
// es.go —— Elasticsearch 原生接入(HTTP + X-Pack SQL REST API)。
|
||||
//
|
||||
// 为什么不用官方 JDBC:x-pack-sql-jdbc 在 basic 许可下直接拒绝工作
|
||||
// ("current license is non-compliant for [jdbc]",实测 8.15.3),而 SQL REST API
|
||||
// 在 basic 许可可用 —— 走 net/http 零依赖接入,也免除 JVM 负担。
|
||||
//
|
||||
// 能力边界(v1,对标 SQL 工作台):
|
||||
// - 对象树:单库节点 "default" → SHOW TABLES 列出索引
|
||||
// - 查询:SQL 编辑器里的 ES SQL(SELECT/SHOW/DESC…)直发 /_sql
|
||||
// - ES SQL 只读:DDL/DML 由 ES 自身拒绝;危险语句拦截仍在本层先过一遍
|
||||
// - 分页:/_sql 为游标式,前端 offset 翻页暂不支持(首屏 fetch_size 截断)
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func isES(d string) bool { return driverKey(d) == "elasticsearch" }
|
||||
|
||||
func esBase(c ConnReq) string {
|
||||
host := strings.TrimSpace(c.Host)
|
||||
if host == "" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
port := c.Port
|
||||
if port == 0 {
|
||||
port = 9200
|
||||
}
|
||||
return fmt.Sprintf("http://%s:%d", host, port)
|
||||
}
|
||||
|
||||
// esDo 发请求并在设置了用户时启用 Basic Auth(ES 开启 security 时用)。
|
||||
func esDo(ctx context.Context, c ConnReq, method, path string, body, out any) error {
|
||||
var rd io.Reader
|
||||
if body != nil {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rd = bytes.NewReader(b)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, method, esBase(c)+path, rd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if c.User != "" {
|
||||
req.SetBasicAuth(c.User, c.Password)
|
||||
}
|
||||
resp, err := (&http.Client{Timeout: 30 * time.Second}).Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 400 {
|
||||
msg, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
return fmt.Errorf("ES HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(msg)))
|
||||
}
|
||||
if out != nil {
|
||||
return json.NewDecoder(resp.Body).Decode(out)
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
return nil
|
||||
}
|
||||
|
||||
func esPing(c ConnReq) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
|
||||
defer cancel()
|
||||
var info struct {
|
||||
Tagline string `json:"tagline"`
|
||||
}
|
||||
if err := esDo(ctx, c, http.MethodGet, "/", nil, &info); err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Tagline == "" {
|
||||
return fmt.Errorf("ES 响应异常:缺少 tagline")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// esSQLResult /_sql 的 JSON 响应(format=json)。
|
||||
type esSQLResult struct {
|
||||
Columns []struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
} `json:"columns"`
|
||||
Rows [][]interface{} `json:"rows"`
|
||||
}
|
||||
|
||||
func esRunSQL(ctx context.Context, c ConnReq, sqlText string, fetchSize int) (ExecResult, error) {
|
||||
var r esSQLResult
|
||||
body := map[string]any{"query": sqlText}
|
||||
if fetchSize > 0 {
|
||||
body["fetch_size"] = fetchSize
|
||||
}
|
||||
if err := esDo(ctx, c, http.MethodPost, "/_sql", body, &r); err != nil {
|
||||
return ExecResult{}, err
|
||||
}
|
||||
cols := make([]string, 0, len(r.Columns))
|
||||
for _, col := range r.Columns {
|
||||
cols = append(cols, col.Name)
|
||||
}
|
||||
rows := make([]map[string]interface{}, 0, len(r.Rows))
|
||||
for _, row := range r.Rows {
|
||||
m := make(map[string]interface{}, len(cols))
|
||||
for i, v := range row {
|
||||
if i < len(cols) {
|
||||
m[cols[i]] = v
|
||||
}
|
||||
}
|
||||
rows = append(rows, m)
|
||||
}
|
||||
return ExecResult{
|
||||
Columns: cols,
|
||||
Rows: rows,
|
||||
RowCount: len(rows),
|
||||
Truncated: fetchSize > 0 && len(rows) >= fetchSize,
|
||||
Limit: fetchSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func esListDatabases() []string { return []string{"default"} }
|
||||
|
||||
func esListTables(ctx context.Context, c ConnReq) ([]string, error) {
|
||||
res, err := esRunSQL(ctx, c, "SHOW TABLES", 10000)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []string{}
|
||||
if len(res.Columns) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
// SHOW TABLES 列为 [catalog, name, type, kind]:取 "name" 列(ES 8 实测),退化为首列
|
||||
idx := 0
|
||||
for i, col := range res.Columns {
|
||||
if col == "name" {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, row := range res.Rows {
|
||||
if v, ok := row[res.Columns[idx]].(string); ok && v != "" {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func esListColumns(ctx context.Context, c ConnReq) ([]ColumnInfo, error) {
|
||||
table := strings.TrimSpace(c.Table)
|
||||
if table == "" {
|
||||
return nil, fmt.Errorf("缺少表名")
|
||||
}
|
||||
res, err := esRunSQL(ctx, c, "DESCRIBE \""+strings.ReplaceAll(table, "\"", "")+"\"", 1000)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cols := []ColumnInfo{}
|
||||
for _, row := range res.Rows {
|
||||
name, _ := row["column"].(string)
|
||||
typ, _ := row["type"].(string)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
cols = append(cols, ColumnInfo{Name: name, Type: typ, Nullable: true})
|
||||
}
|
||||
return cols, nil
|
||||
}
|
||||
|
||||
// esExecSQL 全部按查询处理:ES SQL 只读,写语句由 ES 拒绝;危险语句在 ops.go 入口已拦。
|
||||
func esExecSQL(ctx context.Context, c ConnReq, start time.Time) ExecResult {
|
||||
if c.Offset > 0 {
|
||||
return ExecResult{Columns: []string{}, Rows: []map[string]interface{}{}, Error: "ES 游标分页暂不支持翻页(仅首屏,可缩小范围重查)", TimeMs: time.Since(start).Milliseconds()}
|
||||
}
|
||||
limit := rowLimit(c)
|
||||
if limit > 10000 {
|
||||
limit = 10000
|
||||
}
|
||||
res, err := esRunSQL(ctx, c, strings.TrimSpace(c.SQL), limit)
|
||||
if err != nil {
|
||||
return execFail(start, err)
|
||||
}
|
||||
res.TimeMs = time.Since(start).Milliseconds()
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultExportLimit = 100000
|
||||
MaxExportLimit = 500000
|
||||
exportFlushRows = 512
|
||||
)
|
||||
|
||||
// ExportAllowedQuery 导出专用的只读校验:首个关键词须为 SELECT/WITH/SHOW/DESCRIBE/EXPLAIN。
|
||||
func ExportAllowedQuery(sqlText string) bool {
|
||||
s := stripSQLLead(strings.TrimSpace(sqlText))
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
up := strings.ToUpper(s)
|
||||
for _, p := range []string{"SELECT", "WITH", "SHOW", "DESCRIBE", "EXPLAIN"} {
|
||||
if strings.HasPrefix(up, p+" ") || up == p || strings.HasPrefix(up, p+"\t") || strings.HasPrefix(up, p+"\n") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// SelectAllSQL 表模式导出的取数 SQL:按 family 引用(支持 schema.table 多段)。
|
||||
func SelectAllSQL(driver, table string) string {
|
||||
return "SELECT * FROM " + quoteTableNameFor(driver, table)
|
||||
}
|
||||
|
||||
// ExportQuery 把查询结果按 c.Format(csv/json/insert)流式编码写入 w,避免全量载入内存。
|
||||
// limit <= 0 用默认上限 100000,硬顶 MaxExportLimit。行值经 normalize 保真(同 ExecSQL)。
|
||||
func ExportQuery(ctx context.Context, c ConnReq, w io.Writer) error {
|
||||
ctx = reqCtx(ctx)
|
||||
format := strings.ToLower(strings.TrimSpace(c.Format))
|
||||
sqlText := strings.TrimSpace(c.SQL)
|
||||
if sqlText == "" {
|
||||
return fmt.Errorf("缺少导出 SQL")
|
||||
}
|
||||
limit := c.Limit
|
||||
if limit <= 0 {
|
||||
limit = DefaultExportLimit
|
||||
}
|
||||
if limit > MaxExportLimit {
|
||||
limit = MaxExportLimit
|
||||
}
|
||||
return withDB(c, func(db *sql.DB) error {
|
||||
rows, err := db.QueryContext(ctx, sqlText)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
cols, err := rows.Columns()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bw := bufio.NewWriterSize(w, 64*1024)
|
||||
enc := newExportEncoder(bw, format, c.Driver, exportTargetTable(c), cols)
|
||||
if err := enc.begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
n := 0
|
||||
for rows.Next() {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
raw := make([]interface{}, len(cols))
|
||||
ptrs := make([]interface{}, len(cols))
|
||||
for i := range raw {
|
||||
ptrs[i] = &raw[i]
|
||||
}
|
||||
if err := rows.Scan(ptrs...); err != nil {
|
||||
return err
|
||||
}
|
||||
vals := make([]interface{}, len(cols))
|
||||
for i := range raw {
|
||||
vals[i] = normalize(raw[i])
|
||||
}
|
||||
if err := enc.row(vals); err != nil {
|
||||
return err
|
||||
}
|
||||
n++
|
||||
if n%exportFlushRows == 0 {
|
||||
if err := bw.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if n >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := enc.end(); err != nil {
|
||||
return err
|
||||
}
|
||||
return bw.Flush()
|
||||
})
|
||||
}
|
||||
|
||||
// exportTargetTable insert 格式的目标表名:显式 table 优先,否则用通用名。
|
||||
func exportTargetTable(c ConnReq) string {
|
||||
if strings.TrimSpace(c.Table) != "" {
|
||||
return c.Table
|
||||
}
|
||||
return "result"
|
||||
}
|
||||
|
||||
// exportEncoder 按格式把行流式编码进 buffered writer。
|
||||
type exportEncoder struct {
|
||||
bw *bufio.Writer
|
||||
format string
|
||||
driver string
|
||||
table string
|
||||
cols []string
|
||||
csvW *csv.Writer
|
||||
// jsonPending 上一行 JSON,延迟一行写出,使行间逗号紧跟行尾而非独占一行
|
||||
jsonPending string
|
||||
n int
|
||||
}
|
||||
|
||||
func newExportEncoder(bw *bufio.Writer, format, driver, table string, cols []string) *exportEncoder {
|
||||
return &exportEncoder{bw: bw, format: format, driver: driver, table: table, cols: cols}
|
||||
}
|
||||
|
||||
func (e *exportEncoder) begin() error {
|
||||
switch e.format {
|
||||
case "csv":
|
||||
e.csvW = csv.NewWriter(e.bw)
|
||||
return e.csvW.Write(e.cols)
|
||||
case "json":
|
||||
_, err := e.bw.WriteString("[\n")
|
||||
return err
|
||||
case "insert":
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("不支持的导出格式: %s", e.format)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *exportEncoder) row(vals []interface{}) error {
|
||||
e.n++
|
||||
switch e.format {
|
||||
case "csv":
|
||||
rec := make([]string, len(vals))
|
||||
for i, v := range vals {
|
||||
rec[i] = csvCell(v)
|
||||
}
|
||||
return e.csvW.Write(rec)
|
||||
case "json":
|
||||
m := make(map[string]interface{}, len(e.cols))
|
||||
for i, col := range e.cols {
|
||||
m[col] = vals[i]
|
||||
}
|
||||
var rb bytes.Buffer
|
||||
if err := json.NewEncoder(&rb).Encode(m); err != nil {
|
||||
return err
|
||||
}
|
||||
line := strings.TrimSuffix(rb.String(), "\n")
|
||||
if e.jsonPending != "" {
|
||||
if _, err := e.bw.WriteString(e.jsonPending + ",\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
e.jsonPending = line
|
||||
return nil
|
||||
case "insert":
|
||||
var b strings.Builder
|
||||
b.WriteString("INSERT INTO ")
|
||||
b.WriteString(quoteTableNameFor(e.driver, e.table))
|
||||
b.WriteString(" (")
|
||||
for i, col := range e.cols {
|
||||
if i > 0 {
|
||||
b.WriteString(", ")
|
||||
}
|
||||
b.WriteString(quoteIdentFor(e.driver, col))
|
||||
}
|
||||
b.WriteString(") VALUES (")
|
||||
for i, v := range vals {
|
||||
if i > 0 {
|
||||
b.WriteString(", ")
|
||||
}
|
||||
b.WriteString(sqlLiteral(v))
|
||||
}
|
||||
b.WriteString(");\n")
|
||||
_, err := e.bw.WriteString(b.String())
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *exportEncoder) end() error {
|
||||
switch e.format {
|
||||
case "csv":
|
||||
e.csvW.Flush()
|
||||
return e.csvW.Error()
|
||||
case "json":
|
||||
if e.jsonPending != "" {
|
||||
if _, err := e.bw.WriteString(e.jsonPending + "\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
e.jsonPending = ""
|
||||
}
|
||||
_, err := e.bw.WriteString("]\n")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func csvCell(v interface{}) string {
|
||||
switch t := v.(type) {
|
||||
case nil:
|
||||
return ""
|
||||
case string:
|
||||
return t
|
||||
default:
|
||||
return fmt.Sprintf("%v", t)
|
||||
}
|
||||
}
|
||||
|
||||
// sqlLiteral insert 字面量:NULL/数字原样,字符串单引号 doubling。
|
||||
func sqlLiteral(v interface{}) string {
|
||||
switch t := v.(type) {
|
||||
case nil:
|
||||
return "NULL"
|
||||
case string:
|
||||
return "'" + strings.ReplaceAll(t, "'", "''") + "'"
|
||||
case bool:
|
||||
if t {
|
||||
return "1"
|
||||
}
|
||||
return "0"
|
||||
case time.Time:
|
||||
return "'" + t.Format(time.RFC3339Nano) + "'"
|
||||
case []byte:
|
||||
return "'" + strings.ReplaceAll(string(t), "'", "''") + "'"
|
||||
default:
|
||||
return fmt.Sprintf("%v", t)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newExportConn(t *testing.T) ConnReq {
|
||||
return newSQLiteFile(t,
|
||||
`CREATE TABLE t_export (id INTEGER PRIMARY KEY, name TEXT, note TEXT)`,
|
||||
`INSERT INTO t_export (id, name, note) VALUES (1, 'alice', 'hi, "q"')`,
|
||||
`INSERT INTO t_export (id, name, note) VALUES (2, 'bo''b', NULL)`,
|
||||
`INSERT INTO t_export (id, name, note) VALUES (3, 'carol', 'line1`+"\n"+`line2')`,
|
||||
)
|
||||
}
|
||||
|
||||
// R2-B7:csv 输出形状(表头 + csv 引号转义由 encoding/csv 负责)。
|
||||
func TestExportCSV(t *testing.T) {
|
||||
c := newExportConn(t)
|
||||
c.SQL = `SELECT id, name, note FROM t_export ORDER BY id`
|
||||
c.Format = "csv"
|
||||
var buf bytes.Buffer
|
||||
if err := ExportQuery(context.Background(), c, &buf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := buf.String()
|
||||
if !strings.HasPrefix(out, "id,name,note\n") {
|
||||
t.Fatalf("csv 表头错误: %q", out)
|
||||
}
|
||||
// 含逗号/引号的字段整体加引号、内部引号 doubling
|
||||
if !strings.Contains(out, `"hi, ""q"""`) {
|
||||
t.Fatalf("csv 转义错误: %q", out)
|
||||
}
|
||||
// 单引号 doubling 是 SQL 字面量层;csv 层原样输出 'bo''b' 存好的 bo'b
|
||||
if !strings.Contains(out, "2,bo'b,") {
|
||||
t.Fatalf("csv 内容错误: %q", out)
|
||||
}
|
||||
// 多行字面量会使单条记录跨物理行,用 csv.Reader 按记录解析
|
||||
recs, err := csv.NewReader(strings.NewReader(out)).ReadAll()
|
||||
if err != nil {
|
||||
t.Fatalf("csv 输出应可解析: %v", err)
|
||||
}
|
||||
if len(recs) != 4 { // 表头 + 3 行
|
||||
t.Fatalf("应为表头+3 行: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportJSON(t *testing.T) {
|
||||
c := newExportConn(t)
|
||||
c.SQL = `SELECT id, name, note FROM t_export ORDER BY id`
|
||||
c.Format = "json"
|
||||
var buf bytes.Buffer
|
||||
if err := ExportQuery(context.Background(), c, &buf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var rows []map[string]interface{}
|
||||
if err := json.Unmarshal(buf.Bytes(), &rows); err != nil {
|
||||
t.Fatalf("json 输出应可解析: %v\n%s", err, buf.String())
|
||||
}
|
||||
if len(rows) != 3 {
|
||||
t.Fatalf("应为 3 行: %v", rows)
|
||||
}
|
||||
if rows[0]["name"] != "alice" || rows[1]["note"] != nil {
|
||||
t.Fatalf("行值错误: %v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportInsert(t *testing.T) {
|
||||
c := newExportConn(t)
|
||||
c.SQL = `SELECT id, name, note FROM t_export ORDER BY id`
|
||||
c.Format = "insert"
|
||||
c.Table = "t_export"
|
||||
var buf bytes.Buffer
|
||||
if err := ExportQuery(context.Background(), c, &buf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := buf.String()
|
||||
if !strings.Contains(out, `INSERT INTO "t_export" ("id", "name", "note") VALUES (1, 'alice', 'hi, "q"');`) {
|
||||
t.Fatalf("insert 首行错误: %q", out)
|
||||
}
|
||||
// 字符串单引号 doubling;NULL 原样
|
||||
if !strings.Contains(out, `VALUES (2, 'bo''b', NULL);`) {
|
||||
t.Fatalf("insert 字面量错误: %q", out)
|
||||
}
|
||||
// 多行字面量内换行原样保留
|
||||
if !strings.Contains(out, "line1\nline2") {
|
||||
t.Fatalf("insert 换行字面量错误: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
// limit 生效(含默认上限收敛)。
|
||||
func TestExportLimit(t *testing.T) {
|
||||
c := newExportConn(t)
|
||||
c.SQL = `SELECT id FROM t_export ORDER BY id`
|
||||
c.Format = "csv"
|
||||
c.Limit = 2
|
||||
var buf bytes.Buffer
|
||||
if err := ExportQuery(context.Background(), c, &buf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows := strings.Split(strings.TrimSpace(buf.String()), "\n")
|
||||
if len(rows) != 3 { // 表头 + 2 行
|
||||
t.Fatalf("limit=2 应只导 2 行: %q", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportAllowedQuery(t *testing.T) {
|
||||
for _, ok := range []string{
|
||||
"SELECT * FROM t", "WITH x AS (SELECT 1) SELECT * FROM x",
|
||||
"SHOW TABLES", "DESCRIBE t", "EXPLAIN SELECT 1",
|
||||
"-- c\nSELECT 1",
|
||||
} {
|
||||
if !ExportAllowedQuery(ok) {
|
||||
t.Fatalf("应允许只读: %q", ok)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{
|
||||
"DELETE FROM t", "INSERT INTO t VALUES (1)", "DROP TABLE t",
|
||||
"UPDATE t SET a=1", "", " ",
|
||||
} {
|
||||
if ExportAllowedQuery(bad) {
|
||||
t.Fatalf("不应允许: %q", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportBadFormat(t *testing.T) {
|
||||
c := newExportConn(t)
|
||||
c.SQL = "SELECT 1"
|
||||
c.Format = "xml"
|
||||
var buf bytes.Buffer
|
||||
if err := ExportQuery(context.Background(), c, &buf); err == nil {
|
||||
t.Fatal("未知格式应报错")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package db
|
||||
|
||||
// family_e2e_test.go —— 协议兼容族真实例验收(MySQL 族:TiDB;PG 族:openGauss)。
|
||||
// 触发:OPSXDB_E2E_TIDB=1 / OPSXDB_E2E_GAUSS=1;验证族内真实例后注册表才可翻 verified。
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestE2ETiDB —— 真实 TiDB(pingcap/tidb 单容器,MySQL 协议族)。
|
||||
func TestE2ETiDB(t *testing.T) {
|
||||
if os.Getenv("OPSXDB_E2E_TIDB") != "1" {
|
||||
t.Skip("置 OPSXDB_E2E_TIDB=1 并启动 TiDB :4000 后运行(docker run -p 4000:4000 pingcap/tidb:v7.5.0)")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
c := ConnReq{Driver: "tidb", Host: "127.0.0.1", Port: 4000, User: "root"}
|
||||
if err := TestConnection(c); err != nil {
|
||||
t.Skipf("TiDB :4000 不可达: %v", err)
|
||||
}
|
||||
dbs, err := ListDatabases(ctx, c)
|
||||
if err != nil || len(dbs) == 0 {
|
||||
t.Fatalf("ListDatabases = %v, %v", dbs, err)
|
||||
}
|
||||
// 建库建表写读
|
||||
must := func(sql string, db string) ExecResult {
|
||||
t.Helper()
|
||||
return ExecSQL(ctx, ConnReq{Driver: "tidb", Host: "127.0.0.1", Port: 4000, User: "root", Database: db, SQL: sql, ConfirmDanger: true})
|
||||
}
|
||||
if r := must("CREATE DATABASE IF NOT EXISTS opsxe2e", ""); r.Error != "" {
|
||||
t.Fatalf("create db: %s", r.Error)
|
||||
}
|
||||
if r := must("CREATE TABLE IF NOT EXISTS fam_t(id INT PRIMARY KEY, name VARCHAR(32))", "opsxe2e"); r.Error != "" {
|
||||
t.Fatalf("create table: %s", r.Error)
|
||||
}
|
||||
if r := must(`INSERT INTO fam_t VALUES (1, 'tidb-ok')`, "opsxe2e"); r.Error != "" {
|
||||
t.Fatalf("insert: %s", r.Error)
|
||||
}
|
||||
r := must("SELECT * FROM fam_t", "opsxe2e")
|
||||
if r.Error != "" || r.RowCount != 1 || r.Rows[0]["name"] != "tidb-ok" {
|
||||
t.Fatalf("select: err=%s rows=%+v", r.Error, r.Rows)
|
||||
}
|
||||
}
|
||||
|
||||
// TestE2EOpenGauss —— 真实 openGauss(PG 协议族;Apple Silicon 需 --platform linux/amd64)。
|
||||
func TestE2EOpenGauss(t *testing.T) {
|
||||
if os.Getenv("OPSXDB_E2E_GAUSS") != "1" {
|
||||
t.Skip("置 OPSXDB_E2E_GAUSS=1 并启动 openGauss :5432 后运行")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
c := ConnReq{Driver: "gaussdb", Host: "127.0.0.1", Port: 5432, User: "omm", Password: "Demo@123", Database: "postgres"}
|
||||
if err := TestConnection(c); err != nil {
|
||||
t.Skipf("openGauss :5432 不可达: %v", err)
|
||||
}
|
||||
dbs, err := ListDatabases(ctx, c)
|
||||
if err != nil || len(dbs) == 0 {
|
||||
t.Fatalf("ListDatabases = %v, %v", dbs, err)
|
||||
}
|
||||
q := ExecSQL(ctx, ConnReq{Driver: "gaussdb", Host: "127.0.0.1", Port: 5432, User: "omm", Password: "Demo@123", Database: "postgres", SQL: "SELECT 1 AS one"})
|
||||
if q.Error != "" {
|
||||
t.Fatalf("select: %s", q.Error)
|
||||
}
|
||||
if q.RowCount != 1 {
|
||||
t.Fatalf("rows = %+v", q.Rows)
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,44 @@ package db
|
||||
|
||||
import "strings"
|
||||
|
||||
// quoteMySQLIdent 反引号标识符转义:内部反引号加倍(“),与前端 sql.ts quoteIdent 对齐。
|
||||
func quoteMySQLIdent(s string) string {
|
||||
return "`" + strings.ReplaceAll(s, "`", "") + "`"
|
||||
return "`" + strings.ReplaceAll(s, "`", "``") + "`"
|
||||
}
|
||||
|
||||
func quotePGIdent(s string) string {
|
||||
return `"` + strings.ReplaceAll(s, `"`, `""`) + `"`
|
||||
}
|
||||
|
||||
// quoteIdentFor 按驱动族引用标识符:mysql 反引号加倍、pg/sqlite 双引号加倍、
|
||||
// mssql 中括号加倍、oracle 沿用原名(大写约定)。
|
||||
func quoteIdentFor(driver, name string) string {
|
||||
switch {
|
||||
case isMySQLFamily(driver):
|
||||
return quoteMySQLIdent(name)
|
||||
case isMSSQL(driver):
|
||||
return "[" + strings.ReplaceAll(name, "]", "]]") + "]"
|
||||
case driverKey(driver) == "oracle":
|
||||
return name
|
||||
case driverKey(driver) == "sqlite":
|
||||
return quoteSQLiteIdent(name)
|
||||
default:
|
||||
return quotePGIdent(name)
|
||||
}
|
||||
}
|
||||
|
||||
// quoteTableNameFor 引用表名(支持 schema.table 多段,oracle 除外沿用原名)。
|
||||
func quoteTableNameFor(driver, table string) string {
|
||||
if driverKey(driver) == "oracle" {
|
||||
return table
|
||||
}
|
||||
parts := strings.Split(table, ".")
|
||||
for i := range parts {
|
||||
parts[i] = quoteIdentFor(driver, parts[i])
|
||||
}
|
||||
return strings.Join(parts, ".")
|
||||
}
|
||||
|
||||
func splitSchemaTable(name string) (schema, table string) {
|
||||
name = strings.TrimSpace(name)
|
||||
if i := strings.LastIndex(name, "."); i > 0 {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// inflightCancels 在执行的请求注册表:requestId → cancel,供 /api/db/cancel 远程取消。
|
||||
// 请求完成(正常/异常)必须 UnregisterInflight,避免 map 泄漏。
|
||||
var inflightCancels = struct {
|
||||
sync.Mutex
|
||||
m map[string]context.CancelFunc
|
||||
}{m: make(map[string]context.CancelFunc)}
|
||||
|
||||
// RegisterInflight 注册一个可按 requestId 取消的在执行请求。
|
||||
// requestID 为空或 cancel 为 nil 时忽略。
|
||||
func RegisterInflight(requestID string, cancel context.CancelFunc) {
|
||||
if requestID == "" || cancel == nil {
|
||||
return
|
||||
}
|
||||
inflightCancels.Lock()
|
||||
defer inflightCancels.Unlock()
|
||||
inflightCancels.m[requestID] = cancel
|
||||
}
|
||||
|
||||
// UnregisterInflight 请求结束时注销(幂等)。
|
||||
func UnregisterInflight(requestID string) {
|
||||
inflightCancels.Lock()
|
||||
defer inflightCancels.Unlock()
|
||||
delete(inflightCancels.m, requestID)
|
||||
}
|
||||
|
||||
// CancelInflight 按 requestId 取消在执行的请求;不存在则返回 false。
|
||||
// 取消后即注销,重复取消返回 false。
|
||||
func CancelInflight(requestID string) bool {
|
||||
inflightCancels.Lock()
|
||||
cancel, ok := inflightCancels.m[requestID]
|
||||
if ok {
|
||||
delete(inflightCancels.m, requestID)
|
||||
}
|
||||
inflightCancels.Unlock()
|
||||
if ok {
|
||||
cancel()
|
||||
}
|
||||
return ok
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// R2-B4:注册→取消→CancelFunc 被调用;注销后 CancelInflight 返回 false。
|
||||
func TestInflightCancel(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
const id = "req-cancel-1"
|
||||
RegisterInflight(id, cancel)
|
||||
if !CancelInflight(id) {
|
||||
t.Fatal("已注册应能取消")
|
||||
}
|
||||
if ctx.Err() != context.Canceled {
|
||||
t.Fatal("CancelFunc 应已被调用")
|
||||
}
|
||||
// 取消后即注销,重复取消返回 false
|
||||
if CancelInflight(id) {
|
||||
t.Fatal("注销后 CancelInflight 应返回 false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInflightUnregister(t *testing.T) {
|
||||
_, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
const id = "req-unregister-1"
|
||||
RegisterInflight(id, cancel)
|
||||
UnregisterInflight(id)
|
||||
if CancelInflight(id) {
|
||||
t.Fatal("注销后不应再取消到")
|
||||
}
|
||||
// 幂等:重复注销不 panic
|
||||
UnregisterInflight(id)
|
||||
}
|
||||
|
||||
// 空 id / nil cancel 注册应被忽略。
|
||||
func TestInflightRegisterGuards(t *testing.T) {
|
||||
RegisterInflight("", func() {})
|
||||
if CancelInflight("") {
|
||||
t.Fatal("空 requestId 不应注册成功")
|
||||
}
|
||||
RegisterInflight("req-nil-cancel", nil)
|
||||
if CancelInflight("req-nil-cancel") {
|
||||
t.Fatal("nil cancel 不应注册成功")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
package db
|
||||
|
||||
// influx.go —— InfluxDB 2.x 原生接入(HTTP API,零第三方依赖)。
|
||||
//
|
||||
// 字段映射(连接表单):
|
||||
//
|
||||
// 主机/端口 → 服务地址(默认 :8086)
|
||||
// 用户 → org(如 opsx)
|
||||
// 密码 → admin token(如 opsxdb-demo-token)
|
||||
// 库名 → bucket(如 demo)
|
||||
//
|
||||
// 能力:对象树 bucket → measurement(Flux schema.measurements);
|
||||
// SQL 编辑器直写 Flux(from(bucket: ...) |> range(...) ...),结果按注解 CSV 解析为表格。
|
||||
// Flux 只读(写走 /write 专用通道),危险语句拦截在 ops.go 入口统一过一遍。
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func isInflux(d string) bool { return driverKey(d) == "influxdb" }
|
||||
|
||||
func ifxBase(c ConnReq) string {
|
||||
host := strings.TrimSpace(c.Host)
|
||||
if host == "" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
port := c.Port
|
||||
if port == 0 {
|
||||
port = 8086
|
||||
}
|
||||
return fmt.Sprintf("http://%s:%d", host, port)
|
||||
}
|
||||
|
||||
func ifxToken(c ConnReq) string { return c.Password }
|
||||
|
||||
// ifxDo 统一请求:Token 鉴权 + org 查询参数。
|
||||
func ifxDo(ctx context.Context, c ConnReq, method, path string, body io.Reader, out any) error {
|
||||
req, err := http.NewRequestWithContext(ctx, method, ifxBase(c)+path, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ifxToken(c) != "" {
|
||||
req.Header.Set("Authorization", "Token "+ifxToken(c))
|
||||
}
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
resp, err := (&http.Client{Timeout: 30 * time.Second}).Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 400 {
|
||||
msg, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
return fmt.Errorf("InfluxDB HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(msg)))
|
||||
}
|
||||
if out != nil {
|
||||
return json.NewDecoder(resp.Body).Decode(out)
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
return nil
|
||||
}
|
||||
|
||||
func ifxOrg(c ConnReq) (string, error) {
|
||||
org := strings.TrimSpace(c.User)
|
||||
if org == "" {
|
||||
return "", fmt.Errorf("InfluxDB 2.x 需要填「用户」= org 名(如 opsx)")
|
||||
}
|
||||
return org, nil
|
||||
}
|
||||
|
||||
func ifxPing(c ConnReq) error {
|
||||
org, err := ifxOrg(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
|
||||
defer cancel()
|
||||
var out struct {
|
||||
Buckets []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"buckets"`
|
||||
}
|
||||
return ifxDo(ctx, c, http.MethodGet, "/api/v2/buckets?org="+org, nil, &out)
|
||||
}
|
||||
|
||||
func ifxListDatabases(ctx context.Context, c ConnReq) ([]string, error) {
|
||||
org, err := ifxOrg(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out struct {
|
||||
Buckets []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"buckets"`
|
||||
}
|
||||
if err := ifxDo(ctx, c, http.MethodGet, "/api/v2/buckets?org="+org, nil, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := []string{}
|
||||
for _, b := range out.Buckets {
|
||||
names = append(names, b.Name)
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// ifxQueryFlux 跑 Flux 并把注解 CSV 解析为表格。
|
||||
func ifxQueryFlux(ctx context.Context, c ConnReq, flux string, maxRows int) (ExecResult, error) {
|
||||
org, err := ifxOrg(c)
|
||||
if err != nil {
|
||||
return ExecResult{}, err
|
||||
}
|
||||
body := map[string]string{"query": flux, "type": "flux"}
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return ExecResult{}, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ifxBase(c)+"/api/v2/query?org="+org, strings.NewReader(string(b)))
|
||||
if err != nil {
|
||||
return ExecResult{}, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/csv")
|
||||
if ifxToken(c) != "" {
|
||||
req.Header.Set("Authorization", "Token "+ifxToken(c))
|
||||
}
|
||||
resp, err := (&http.Client{Timeout: 60 * time.Second}).Do(req)
|
||||
if err != nil {
|
||||
return ExecResult{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return ExecResult{}, err
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
return ExecResult{}, fmt.Errorf("InfluxDB HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(raw)))
|
||||
}
|
||||
return parseAnnotatedCSV(string(raw), maxRows)
|
||||
}
|
||||
|
||||
// parseAnnotatedCSV 解析 InfluxDB 注解 CSV:
|
||||
// #group/#datatype/#default 注释行 + 空行 + header 行 + 数据行。
|
||||
func parseAnnotatedCSV(text string, maxRows int) (ExecResult, error) {
|
||||
lines := strings.Split(strings.ReplaceAll(text, "\r\n", "\n"), "\n")
|
||||
headerIdx := -1
|
||||
var header []string
|
||||
for i, ln := range lines {
|
||||
s := strings.TrimSpace(ln)
|
||||
if s == "" || strings.HasPrefix(s, "#") {
|
||||
continue
|
||||
}
|
||||
headerIdx = i
|
||||
header = strings.Split(ln, ",")
|
||||
break
|
||||
}
|
||||
if headerIdx < 0 {
|
||||
return ExecResult{Columns: []string{}, Rows: []map[string]interface{}{}}, nil
|
||||
}
|
||||
res := ExecResult{Columns: header, Rows: []map[string]interface{}{}}
|
||||
for _, ln := range lines[headerIdx+1:] {
|
||||
if strings.TrimSpace(ln) == "" {
|
||||
continue
|
||||
}
|
||||
cells := strings.Split(ln, ",")
|
||||
row := make(map[string]interface{}, len(header))
|
||||
for i, col := range header {
|
||||
v := ""
|
||||
if i < len(cells) {
|
||||
v = cells[i]
|
||||
}
|
||||
row[col] = v
|
||||
}
|
||||
res.Rows = append(res.Rows, row)
|
||||
if len(res.Rows) >= maxRows {
|
||||
res.Truncated = true
|
||||
break
|
||||
}
|
||||
}
|
||||
res.RowCount = len(res.Rows)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func ifxListTables(ctx context.Context, c ConnReq) ([]string, error) {
|
||||
bucket := strings.TrimSpace(c.Database)
|
||||
if bucket == "" {
|
||||
return nil, fmt.Errorf("InfluxDB 列表需要库名(bucket)")
|
||||
}
|
||||
flux := fmt.Sprintf("import \"influxdata/influxdb/schema\"\nschema.measurements(bucket: \"%s\")", strings.ReplaceAll(bucket, "\"", ""))
|
||||
res, err := ifxQueryFlux(ctx, c, flux, 10000)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []string{}
|
||||
if len(res.Columns) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
// schema.measurements 输出列为 [,result,table,_value]:优先 _value 列,退化为最后一列
|
||||
idx := len(res.Columns) - 1
|
||||
for i, col := range res.Columns {
|
||||
if col == "_value" {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
col := res.Columns[idx]
|
||||
for _, row := range res.Rows {
|
||||
if v, ok := row[col].(string); ok && v != "" {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ifxExecSQL 编辑器内容按 Flux 处理(只读)。
|
||||
func ifxExecSQL(ctx context.Context, c ConnReq, start time.Time) ExecResult {
|
||||
if c.Offset > 0 {
|
||||
return ExecResult{Columns: []string{}, Rows: []map[string]interface{}{}, Error: "InfluxDB Flux 结果暂不支持翻页(可缩小 range 重查)", TimeMs: time.Since(start).Milliseconds()}
|
||||
}
|
||||
limit := rowLimit(c)
|
||||
if limit > MaxRowLimit {
|
||||
limit = MaxRowLimit
|
||||
}
|
||||
res, err := ifxQueryFlux(ctx, c, strings.TrimSpace(c.SQL), limit)
|
||||
if err != nil {
|
||||
return execFail(start, err)
|
||||
}
|
||||
res.TimeMs = time.Since(start).Milliseconds()
|
||||
return res
|
||||
}
|
||||
|
||||
// ifxListColumns measurement 的字段探查:Flux schema.fieldKeys + tagKeys 合并。
|
||||
func ifxListColumns(ctx context.Context, c ConnReq) ([]ColumnInfo, error) {
|
||||
bucket := strings.TrimSpace(c.Database)
|
||||
table := strings.TrimSpace(c.Table)
|
||||
if bucket == "" || table == "" {
|
||||
return nil, fmt.Errorf("InfluxDB 列探查需要库名与 measurement")
|
||||
}
|
||||
safeB := strings.ReplaceAll(bucket, "\"", "")
|
||||
safeM := strings.ReplaceAll(table, "\"", "")
|
||||
flux := fmt.Sprintf(`import "influxdata/influxdb/schema"
|
||||
union(tables: [
|
||||
schema.fieldKeys(bucket: "%s", predicate: (r) => r._measurement == "%s"),
|
||||
schema.tagKeys(bucket: "%s", predicate: (r) => r._measurement == "%s")
|
||||
])`, safeB, safeM, safeB, safeM)
|
||||
res, err := ifxQueryFlux(ctx, c, flux, 1000)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cols := []ColumnInfo{}
|
||||
seen := map[string]bool{}
|
||||
if len(res.Columns) == 0 {
|
||||
return cols, nil
|
||||
}
|
||||
last := res.Columns[len(res.Columns)-1]
|
||||
for _, row := range res.Rows {
|
||||
name, _ := row[last].(string)
|
||||
if name == "" || seen[name] {
|
||||
continue
|
||||
}
|
||||
seen[name] = true
|
||||
cols = append(cols, ColumnInfo{Name: name, Type: "tag/field", Nullable: true})
|
||||
}
|
||||
return cols, nil
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -10,6 +11,16 @@ import (
|
||||
)
|
||||
|
||||
func openMSSQL(c ConnReq) (*sql.DB, error) {
|
||||
dsn, err := mssqlDSN(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sql.Open("sqlserver", dsn)
|
||||
}
|
||||
|
||||
// mssqlDSN 拼 sqlserver:// DSN;账号密码走 url.UserPassword 编码,
|
||||
// 修复含空格 / '+' 等特殊字符密码连接失败的问题(url.QueryEscape 不适用 userinfo)。
|
||||
func mssqlDSN(c ConnReq) (string, error) {
|
||||
host := c.Host
|
||||
if host == "" {
|
||||
host = "127.0.0.1"
|
||||
@@ -44,7 +55,7 @@ func openMSSQL(c ConnReq) (*sql.DB, error) {
|
||||
q.Set("TrustServerCertificate", "false")
|
||||
ca := strings.TrimSpace(c.SSLCA)
|
||||
if mode == "verify-ca" && ca == "" {
|
||||
return nil, fmt.Errorf("verify-ca 模式需要填写 CA 证书路径")
|
||||
return "", fmt.Errorf("verify-ca 模式需要填写 CA 证书路径")
|
||||
}
|
||||
if ca != "" {
|
||||
// go-mssqldb msdsn certificate= builds RootCAs from PEM/DER
|
||||
@@ -54,15 +65,19 @@ func openMSSQL(c ConnReq) (*sql.DB, error) {
|
||||
q.Set("hostnameincertificate", host)
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("未知 SSL 模式: %s", mode)
|
||||
return "", fmt.Errorf("未知 SSL 模式: %s", mode)
|
||||
}
|
||||
dsn := fmt.Sprintf("sqlserver://%s:%s@%s:%d?%s",
|
||||
url.QueryEscape(user), url.QueryEscape(c.Password), host, port, q.Encode())
|
||||
return sql.Open("sqlserver", dsn)
|
||||
u := &url.URL{
|
||||
Scheme: "sqlserver",
|
||||
User: url.UserPassword(user, c.Password),
|
||||
Host: fmt.Sprintf("%s:%d", host, port),
|
||||
RawQuery: q.Encode(),
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func listDatabasesMSSQL(db *sql.DB) ([]string, error) {
|
||||
rows, err := db.Query(`SELECT name FROM sys.databases WHERE name NOT IN ('master','tempdb','model','msdb') ORDER BY name`)
|
||||
func listDatabasesMSSQL(ctx context.Context, db *sql.DB) ([]string, error) {
|
||||
rows, err := db.QueryContext(ctx, `SELECT name FROM sys.databases WHERE name NOT IN ('master','tempdb','model','msdb') ORDER BY name`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -78,9 +93,9 @@ func listDatabasesMSSQL(db *sql.DB) ([]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func listTablesMSSQL(db *sql.DB, database string) ([]string, error) {
|
||||
func listTablesMSSQL(ctx context.Context, db *sql.DB, database string) ([]string, error) {
|
||||
_ = database
|
||||
rows, err := db.Query(`
|
||||
rows, err := db.QueryContext(ctx, `
|
||||
SELECT TABLE_SCHEMA + '.' + TABLE_NAME
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE TABLE_TYPE = 'BASE TABLE'
|
||||
@@ -100,33 +115,39 @@ func listTablesMSSQL(db *sql.DB, database string) ([]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func listColumnsMSSQL(dbh *sql.DB, _, table string) ([]ColumnInfo, error) {
|
||||
func listColumnsMSSQL(ctx context.Context, dbh *sql.DB, _, table string) ([]ColumnInfo, error) {
|
||||
schema, t := splitSchemaTable(table)
|
||||
if schema == "" {
|
||||
schema = "dbo"
|
||||
}
|
||||
rows, err := dbh.Query(`
|
||||
SELECT COLUMN_NAME, DATA_TYPE,
|
||||
ISNULL(CAST(CHARACTER_MAXIMUM_LENGTH AS varchar), ''),
|
||||
IS_NULLABLE, COLUMN_DEFAULT
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_SCHEMA = @p1 AND TABLE_NAME = @p2
|
||||
ORDER BY ORDINAL_POSITION`, schema, t)
|
||||
// 关联 sys.indexes / sys.index_columns 补主键标记(命中则 Key='PRI')
|
||||
rows, err := dbh.QueryContext(ctx, `
|
||||
SELECT c.COLUMN_NAME, c.DATA_TYPE,
|
||||
ISNULL(CAST(c.CHARACTER_MAXIMUM_LENGTH AS varchar), ''),
|
||||
c.IS_NULLABLE, c.COLUMN_DEFAULT,
|
||||
CASE WHEN pk.name IS NOT NULL THEN 'PRI' ELSE '' END
|
||||
FROM INFORMATION_SCHEMA.COLUMNS c
|
||||
LEFT JOIN sys.tables t ON t.name = c.TABLE_NAME AND SCHEMA_NAME(t.schema_id) = c.TABLE_SCHEMA
|
||||
LEFT JOIN sys.indexes i ON i.object_id = t.object_id AND i.is_primary_key = 1
|
||||
LEFT JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
|
||||
LEFT JOIN sys.columns pk ON pk.object_id = ic.object_id AND pk.column_id = ic.column_id AND pk.name = c.COLUMN_NAME
|
||||
WHERE c.TABLE_SCHEMA = @p1 AND c.TABLE_NAME = @p2
|
||||
ORDER BY c.ORDINAL_POSITION`, schema, t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []ColumnInfo
|
||||
for rows.Next() {
|
||||
var name, typ, extra, nullable string
|
||||
var name, typ, extra, nullable, key string
|
||||
var def sql.NullString
|
||||
if err := rows.Scan(&name, &typ, &extra, &nullable, &def); err != nil {
|
||||
if err := rows.Scan(&name, &typ, &extra, &nullable, &def, &key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if extra != "" && extra != "-1" {
|
||||
typ = typ + "(" + extra + ")"
|
||||
}
|
||||
col := ColumnInfo{Name: name, Type: typ, Nullable: nullable == "YES"}
|
||||
col := ColumnInfo{Name: name, Type: typ, Nullable: nullable == "YES", Key: key}
|
||||
if def.Valid {
|
||||
col.Default = def.String
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mysqldriver "github.com/go-sql-driver/mysql"
|
||||
@@ -41,7 +43,7 @@ func openMySQL(c ConnReq) (*sql.DB, error) {
|
||||
case "disable":
|
||||
// no TLS
|
||||
case "skip-verify":
|
||||
if c.SSLCA == "" && c.SSLCert == "" && c.SSLKey == "" {
|
||||
if !hasTLSMaterial(c) {
|
||||
cfg.TLSConfig = "skip-verify"
|
||||
} else {
|
||||
name, err := registerMySQLTLS(c)
|
||||
@@ -51,7 +53,7 @@ func openMySQL(c ConnReq) (*sql.DB, error) {
|
||||
cfg.TLSConfig = name
|
||||
}
|
||||
case "require", "verify-ca":
|
||||
if c.SSLCA == "" && c.SSLCert == "" && c.SSLKey == "" && mode == "require" {
|
||||
if !hasTLSMaterial(c) && mode == "require" {
|
||||
cfg.TLSConfig = "true"
|
||||
} else {
|
||||
name, err := registerMySQLTLS(c)
|
||||
@@ -66,6 +68,16 @@ func openMySQL(c ConnReq) (*sql.DB, error) {
|
||||
return sql.Open("mysql", cfg.FormatDSN())
|
||||
}
|
||||
|
||||
// hasTLSMaterial 是否提供了任何 CA/客户端证书材料(路径或 PEM 内容)。
|
||||
func hasTLSMaterial(c ConnReq) bool {
|
||||
return strings.TrimSpace(c.SSLCA) != "" ||
|
||||
strings.TrimSpace(c.SSLCert) != "" ||
|
||||
strings.TrimSpace(c.SSLKey) != "" ||
|
||||
strings.TrimSpace(c.SSLCAContent) != "" ||
|
||||
strings.TrimSpace(c.SSLCertContent) != "" ||
|
||||
strings.TrimSpace(c.SSLKeyContent) != ""
|
||||
}
|
||||
|
||||
func registerMySQLTLS(c ConnReq) (string, error) {
|
||||
tlsCfg, err := tlsConfigFrom(c)
|
||||
if err != nil {
|
||||
@@ -74,7 +86,8 @@ func registerMySQLTLS(c ConnReq) (string, error) {
|
||||
if tlsCfg == nil {
|
||||
return "", fmt.Errorf("SSL 已启用但未能构建 TLS 配置")
|
||||
}
|
||||
sum := sha256.Sum256([]byte(EffectiveSSLMode(c) + "|" + c.SSLCA + "|" + c.SSLCert + "|" + c.SSLKey + "|" + c.Host))
|
||||
sum := sha256.Sum256([]byte(EffectiveSSLMode(c) + "|" + c.SSLCA + "|" + c.SSLCert + "|" + c.SSLKey +
|
||||
"|" + c.SSLCAContent + "|" + c.SSLCertContent + "|" + c.SSLKeyContent + "|" + c.TLSServerName + "|" + c.Host))
|
||||
name := "opsxdb-" + hex.EncodeToString(sum[:8])
|
||||
mysqldriver.DeregisterTLSConfig(name)
|
||||
if err := mysqldriver.RegisterTLSConfig(name, tlsCfg); err != nil {
|
||||
@@ -83,8 +96,8 @@ func registerMySQLTLS(c ConnReq) (string, error) {
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func listDatabasesMySQL(db *sql.DB) ([]string, error) {
|
||||
rows, err := db.Query("SHOW DATABASES")
|
||||
func listDatabasesMySQL(ctx context.Context, db *sql.DB) ([]string, error) {
|
||||
rows, err := db.QueryContext(ctx, "SHOW DATABASES")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -100,12 +113,12 @@ func listDatabasesMySQL(db *sql.DB) ([]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func listTablesMySQL(db *sql.DB, database string) ([]string, error) {
|
||||
func listTablesMySQL(ctx context.Context, db *sql.DB, database string) ([]string, error) {
|
||||
q := "SHOW TABLES"
|
||||
if database != "" {
|
||||
q = "SHOW TABLES FROM " + quoteMySQLIdent(database)
|
||||
}
|
||||
rows, err := db.Query(q)
|
||||
rows, err := db.QueryContext(ctx, q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -121,12 +134,12 @@ func listTablesMySQL(db *sql.DB, database string) ([]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func listColumnsMySQL(db *sql.DB, database, table string) ([]ColumnInfo, error) {
|
||||
func listColumnsMySQL(ctx context.Context, db *sql.DB, database, table string) ([]ColumnInfo, error) {
|
||||
ident := quoteMySQLIdent(table)
|
||||
if database != "" {
|
||||
ident = quoteMySQLIdent(database) + "." + ident
|
||||
}
|
||||
rows, err := db.Query("SHOW FULL COLUMNS FROM " + ident)
|
||||
rows, err := db.QueryContext(ctx, "SHOW FULL COLUMNS FROM "+ident)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -29,6 +29,15 @@ func isPostgresFamily(d string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func isMSSQL(d string) bool {
|
||||
switch driverKey(d) {
|
||||
case "sqlserver", "mssql":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Open 按驱动打开 *sql.DB
|
||||
func Open(c ConnReq) (*sql.DB, error) {
|
||||
var (
|
||||
@@ -53,12 +62,23 @@ func Open(c ConnReq) (*sql.DB, error) {
|
||||
return nil, err
|
||||
}
|
||||
db.SetConnMaxLifetime(2 * time.Minute)
|
||||
db.SetConnMaxIdleTime(5 * time.Minute) // 防空闲连接堆积
|
||||
db.SetMaxOpenConns(5)
|
||||
db.SetMaxIdleConns(2)
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func TestConnection(c ConnReq) error {
|
||||
if isAgentDriver(c.Driver) {
|
||||
// JDBC agent 桥的 TCP 由 JVM 侧发起,SSH 隧道(本地端口转发)对 agent 不生效,明确拒绝
|
||||
if c.SSHEnabled {
|
||||
return fmt.Errorf("%s 走 JDBC agent,暂不支持 SSH 隧道", c.Driver)
|
||||
}
|
||||
if err := agentPing(c); err != nil {
|
||||
return fmt.Errorf("数据库: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
dial, tun, err := ApplySSHTunnel(c)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -76,6 +96,15 @@ func testDial(c ConnReq) error {
|
||||
if isRedis(c.Driver) {
|
||||
return testRedis(c)
|
||||
}
|
||||
if isES(c.Driver) {
|
||||
return esPing(c)
|
||||
}
|
||||
if isInflux(c.Driver) {
|
||||
return ifxPing(c)
|
||||
}
|
||||
if isAgentDriver(c.Driver) {
|
||||
return agentPing(c)
|
||||
}
|
||||
db, err := Open(c)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
+329
-61
@@ -2,15 +2,67 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ListDatabases(c ConnReq) ([]string, error) {
|
||||
// withDB 统一获取/释放 *sql.DB:
|
||||
// - c.SessionID != "" → 会话内按 database 池化复用,不 Close;
|
||||
// - 否则 → ApplySSHTunnel(c) + Open + defer Close(短生命周期隧道随请求关闭)。
|
||||
func withDB(c ConnReq, fn func(*sql.DB) error) error {
|
||||
if c.SessionID != "" {
|
||||
s, err := GetSession(c.SessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := s.sqlDB(c.Database)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fn(db)
|
||||
}
|
||||
dial, tun, err := ApplySSHTunnel(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tun != nil {
|
||||
defer tun.Close()
|
||||
}
|
||||
db, err := Open(dial)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
return fn(db)
|
||||
}
|
||||
|
||||
func unsupportedDriverErr(c ConnReq) error {
|
||||
return fmt.Errorf("不支持的驱动「%s」(当前支持: mysql/mariadb/tidb、postgresql、sqlserver、oracle、sqlite、redis;agent 桥: dameng)", c.Driver)
|
||||
}
|
||||
|
||||
func reqCtx(ctx context.Context) context.Context {
|
||||
if ctx == nil {
|
||||
return context.Background()
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func ListDatabases(ctx context.Context, c ConnReq) ([]string, error) {
|
||||
ctx = reqCtx(ctx)
|
||||
if isRedis(c.Driver) {
|
||||
return listDatabasesRedis(c)
|
||||
return listDatabasesRedis(ctx, c)
|
||||
}
|
||||
if isES(c.Driver) {
|
||||
return esListDatabases(), nil
|
||||
}
|
||||
if isInflux(c.Driver) {
|
||||
return ifxListDatabases(ctx, c)
|
||||
}
|
||||
if isAgentDriver(c.Driver) {
|
||||
return agentListDatabases(ctx, c)
|
||||
}
|
||||
cc := c
|
||||
switch {
|
||||
@@ -25,78 +77,110 @@ func ListDatabases(c ConnReq) ([]string, error) {
|
||||
cc.Database = "master"
|
||||
}
|
||||
}
|
||||
db, err := Open(cc)
|
||||
var out []string
|
||||
err := withDB(cc, func(db *sql.DB) error {
|
||||
var err error
|
||||
switch {
|
||||
case isMySQLFamily(c.Driver):
|
||||
out, err = listDatabasesMySQL(ctx, db)
|
||||
case isPostgresFamily(c.Driver):
|
||||
out, err = listDatabasesPostgres(ctx, db)
|
||||
case driverKey(c.Driver) == "sqlserver" || driverKey(c.Driver) == "mssql":
|
||||
out, err = listDatabasesMSSQL(ctx, db)
|
||||
case driverKey(c.Driver) == "oracle":
|
||||
out, err = listDatabasesOracle(ctx, db)
|
||||
case driverKey(c.Driver) == "sqlite":
|
||||
out, err = listDatabasesSQLite(db)
|
||||
default:
|
||||
err = unsupportedDriverErr(c)
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer db.Close()
|
||||
switch {
|
||||
case isMySQLFamily(c.Driver):
|
||||
return listDatabasesMySQL(db)
|
||||
case isPostgresFamily(c.Driver):
|
||||
return listDatabasesPostgres(db)
|
||||
case driverKey(c.Driver) == "sqlserver" || driverKey(c.Driver) == "mssql":
|
||||
return listDatabasesMSSQL(db)
|
||||
case driverKey(c.Driver) == "oracle":
|
||||
return listDatabasesOracle(db)
|
||||
case driverKey(c.Driver) == "sqlite":
|
||||
return listDatabasesSQLite(db)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported driver")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func ListTables(c ConnReq) ([]string, error) {
|
||||
func ListTables(ctx context.Context, c ConnReq) ([]string, error) {
|
||||
ctx = reqCtx(ctx)
|
||||
if isRedis(c.Driver) {
|
||||
return listTablesRedis(c)
|
||||
return listTablesRedis(ctx, c)
|
||||
}
|
||||
db, err := Open(c)
|
||||
if isES(c.Driver) {
|
||||
return esListTables(ctx, c)
|
||||
}
|
||||
if isInflux(c.Driver) {
|
||||
return ifxListTables(ctx, c)
|
||||
}
|
||||
if isAgentDriver(c.Driver) {
|
||||
return agentListTables(ctx, c)
|
||||
}
|
||||
var out []string
|
||||
err := withDB(c, func(db *sql.DB) error {
|
||||
var err error
|
||||
switch {
|
||||
case isMySQLFamily(c.Driver):
|
||||
out, err = listTablesMySQL(ctx, db, c.Database)
|
||||
case isPostgresFamily(c.Driver):
|
||||
out, err = listTablesPostgres(ctx, db, c.Database)
|
||||
case driverKey(c.Driver) == "sqlserver" || driverKey(c.Driver) == "mssql":
|
||||
out, err = listTablesMSSQL(ctx, db, c.Database)
|
||||
case driverKey(c.Driver) == "oracle":
|
||||
out, err = listTablesOracle(ctx, db, c.Database)
|
||||
case driverKey(c.Driver) == "sqlite":
|
||||
out, err = listTablesSQLite(ctx, db, c.Database)
|
||||
default:
|
||||
err = unsupportedDriverErr(c)
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer db.Close()
|
||||
switch {
|
||||
case isMySQLFamily(c.Driver):
|
||||
return listTablesMySQL(db, c.Database)
|
||||
case isPostgresFamily(c.Driver):
|
||||
return listTablesPostgres(db, c.Database)
|
||||
case driverKey(c.Driver) == "sqlserver" || driverKey(c.Driver) == "mssql":
|
||||
return listTablesMSSQL(db, c.Database)
|
||||
case driverKey(c.Driver) == "oracle":
|
||||
return listTablesOracle(db, c.Database)
|
||||
case driverKey(c.Driver) == "sqlite":
|
||||
return listTablesSQLite(db, c.Database)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported driver")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func ListColumns(c ConnReq) ([]ColumnInfo, error) {
|
||||
func ListColumns(ctx context.Context, c ConnReq) ([]ColumnInfo, error) {
|
||||
ctx = reqCtx(ctx)
|
||||
if c.Table == "" {
|
||||
return nil, fmt.Errorf("缺少表名")
|
||||
}
|
||||
if isRedis(c.Driver) {
|
||||
return listColumnsRedis(c, c.Table)
|
||||
return listColumnsRedis(ctx, c, c.Table)
|
||||
}
|
||||
db, err := Open(c)
|
||||
if isES(c.Driver) {
|
||||
return esListColumns(ctx, c)
|
||||
}
|
||||
if isInflux(c.Driver) {
|
||||
return ifxListColumns(ctx, c)
|
||||
}
|
||||
if isAgentDriver(c.Driver) {
|
||||
return agentListColumns(ctx, c)
|
||||
}
|
||||
var out []ColumnInfo
|
||||
err := withDB(c, func(db *sql.DB) error {
|
||||
var err error
|
||||
switch {
|
||||
case isMySQLFamily(c.Driver):
|
||||
out, err = listColumnsMySQL(ctx, db, c.Database, c.Table)
|
||||
case isPostgresFamily(c.Driver):
|
||||
out, err = listColumnsPostgres(ctx, db, c.Database, c.Table)
|
||||
case driverKey(c.Driver) == "sqlserver" || driverKey(c.Driver) == "mssql":
|
||||
out, err = listColumnsMSSQL(ctx, db, c.Database, c.Table)
|
||||
case driverKey(c.Driver) == "oracle":
|
||||
out, err = listColumnsOracle(ctx, db, c.Database, c.Table)
|
||||
case driverKey(c.Driver) == "sqlite":
|
||||
out, err = listColumnsSQLite(ctx, db, c.Database, c.Table)
|
||||
default:
|
||||
err = unsupportedDriverErr(c)
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer db.Close()
|
||||
switch {
|
||||
case isMySQLFamily(c.Driver):
|
||||
return listColumnsMySQL(db, c.Database, c.Table)
|
||||
case isPostgresFamily(c.Driver):
|
||||
return listColumnsPostgres(db, c.Database, c.Table)
|
||||
case driverKey(c.Driver) == "sqlserver" || driverKey(c.Driver) == "mssql":
|
||||
return listColumnsMSSQL(db, c.Database, c.Table)
|
||||
case driverKey(c.Driver) == "oracle":
|
||||
return listColumnsOracle(db, c.Database, c.Table)
|
||||
case driverKey(c.Driver) == "sqlite":
|
||||
return listColumnsSQLite(db, c.Database, c.Table)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported driver")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func rowLimit(c ConnReq) int {
|
||||
@@ -137,12 +221,21 @@ func execFail(start time.Time, err error) ExecResult {
|
||||
}
|
||||
}
|
||||
|
||||
// needConfirm 构造危险语句确认响应(不执行)。
|
||||
func needConfirm(start time.Time, hits []string) ExecResult {
|
||||
return ExecResult{
|
||||
Columns: []string{},
|
||||
Rows: []map[string]interface{}{},
|
||||
TimeMs: time.Since(start).Milliseconds(),
|
||||
Error: "检测到危险语句: " + strings.Join(hits, "; "),
|
||||
NeedConfirm: true,
|
||||
}
|
||||
}
|
||||
|
||||
// ExecSQL 执行查询 / 非查询语句。ctx 来自 HTTP 请求,客户端断开/Abort 时可取消 DB 调用。
|
||||
func ExecSQL(ctx context.Context, c ConnReq) ExecResult {
|
||||
start := time.Now()
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
ctx = reqCtx(ctx)
|
||||
if err := ctx.Err(); err != nil {
|
||||
return execFail(start, err)
|
||||
}
|
||||
@@ -152,18 +245,44 @@ func ExecSQL(ctx context.Context, c ConnReq) ExecResult {
|
||||
if isRedis(c.Driver) {
|
||||
return execRedis(ctx, c)
|
||||
}
|
||||
db, err := Open(c)
|
||||
// 服务端权威危险语句拦截:未确认时返回 needConfirm,不执行
|
||||
if !c.ConfirmDanger {
|
||||
dangerSQL := c.SQL
|
||||
if driverKey(c.Driver) == "mongodb" {
|
||||
// Mongo DSL 动词映射进词表:deleteMany 等无 WHERE 概念,按 DELETE/UPDATE 同等管控
|
||||
dangerSQL = mongoVerbAsSQL(dangerSQL)
|
||||
}
|
||||
if hits := CheckDangerousSQL(c.Driver, dangerSQL); len(hits) > 0 {
|
||||
return needConfirm(start, hits)
|
||||
}
|
||||
}
|
||||
if isAgentDriver(c.Driver) {
|
||||
return agentExecSQL(ctx, c, start)
|
||||
}
|
||||
if isES(c.Driver) {
|
||||
return esExecSQL(ctx, c, start)
|
||||
}
|
||||
if isInflux(c.Driver) {
|
||||
return ifxExecSQL(ctx, c, start)
|
||||
}
|
||||
var res ExecResult
|
||||
err := withDB(c, func(db *sql.DB) error {
|
||||
res = execOnDB(ctx, db, start, c)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return execFail(start, err)
|
||||
}
|
||||
defer db.Close()
|
||||
return res
|
||||
}
|
||||
|
||||
func execOnDB(ctx context.Context, db *sql.DB, start time.Time, c ConnReq) ExecResult {
|
||||
if !isQuerySQL(c.SQL) {
|
||||
res, err := db.ExecContext(ctx, c.SQL)
|
||||
r, err := db.ExecContext(ctx, c.SQL)
|
||||
if err != nil {
|
||||
return execFail(start, err)
|
||||
}
|
||||
affected, _ := res.RowsAffected()
|
||||
affected, _ := r.RowsAffected()
|
||||
return ExecResult{
|
||||
Columns: []string{"affected_rows"},
|
||||
Rows: []map[string]interface{}{{"affected_rows": affected}},
|
||||
@@ -183,12 +302,22 @@ func ExecSQL(ctx context.Context, c ConnReq) ExecResult {
|
||||
return execFail(start, err)
|
||||
}
|
||||
limit := rowLimit(c)
|
||||
offset := c.Offset
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
var resultRows []map[string]interface{}
|
||||
truncated := false
|
||||
skipped := 0
|
||||
for rows.Next() {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return execFail(start, err)
|
||||
}
|
||||
// offset 分页:先丢弃前 offset 行
|
||||
if skipped < offset {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
raw := make([]interface{}, len(cols))
|
||||
ptrs := make([]interface{}, len(cols))
|
||||
for i := range raw {
|
||||
@@ -220,9 +349,148 @@ func ExecSQL(ctx context.Context, c ConnReq) ExecResult {
|
||||
TimeMs: time.Since(start).Milliseconds(),
|
||||
Truncated: truncated,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}
|
||||
}
|
||||
|
||||
// ExplainSQL 服务端统一的执行计划入口(替代前端方言包装)。
|
||||
// mysql/pg/sqlite 走行列计划;mssql 用 SHOWPLAN_XML(钉住单连接);oracle 走 DBMS_XPLAN。
|
||||
func ExplainSQL(ctx context.Context, c ConnReq) ExplainResult {
|
||||
start := time.Now()
|
||||
ctx = reqCtx(ctx)
|
||||
sqlText := strings.TrimSpace(c.SQL)
|
||||
if sqlText == "" {
|
||||
return ExplainResult{Error: "empty sql", TimeMs: time.Since(start).Milliseconds()}
|
||||
}
|
||||
sqlText = strings.TrimSuffix(sqlText, ";")
|
||||
if isRedis(c.Driver) {
|
||||
return ExplainResult{Error: "Redis 不支持执行计划", TimeMs: time.Since(start).Milliseconds()}
|
||||
}
|
||||
if isES(c.Driver) {
|
||||
return ExplainResult{Error: "ES SQL 不支持执行计划", TimeMs: time.Since(start).Milliseconds()}
|
||||
}
|
||||
if isInflux(c.Driver) {
|
||||
return ExplainResult{Error: "InfluxDB 不支持执行计划", TimeMs: time.Since(start).Milliseconds()}
|
||||
}
|
||||
if isAgentDriver(c.Driver) {
|
||||
return agentExplain(ctx, c, sqlText)
|
||||
}
|
||||
// 危险语句不做计划
|
||||
if hits := CheckDangerousSQL(c.Driver, sqlText); len(hits) > 0 {
|
||||
return ExplainResult{Error: "检测到危险语句: " + strings.Join(hits, "; "), TimeMs: time.Since(start).Milliseconds()}
|
||||
}
|
||||
|
||||
var res ExplainResult
|
||||
err := withDB(c, func(db *sql.DB) error {
|
||||
res = explainOnDB(ctx, db, c, sqlText)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return ExplainResult{Error: errMsg(err), TimeMs: time.Since(start).Milliseconds()}
|
||||
}
|
||||
res.TimeMs = time.Since(start).Milliseconds()
|
||||
return res
|
||||
}
|
||||
|
||||
func explainOnDB(ctx context.Context, db *sql.DB, c ConnReq, sqlText string) ExplainResult {
|
||||
fail := func(err error) ExplainResult {
|
||||
return ExplainResult{Error: errMsg(err)}
|
||||
}
|
||||
switch {
|
||||
case isMySQLFamily(c.Driver):
|
||||
return explainRows(ctx, db, "EXPLAIN "+sqlText)
|
||||
case isPostgresFamily(c.Driver):
|
||||
return explainRows(ctx, db, "EXPLAIN "+sqlText)
|
||||
case driverKey(c.Driver) == "sqlite":
|
||||
return explainRows(ctx, db, "EXPLAIN QUERY PLAN "+sqlText)
|
||||
case driverKey(c.Driver) == "sqlserver" || driverKey(c.Driver) == "mssql":
|
||||
// SET SHOWPLAN_XML 必须与查询同连接,钉住单连接
|
||||
conn, err := db.Conn(ctx)
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
if _, err := conn.ExecContext(ctx, "SET SHOWPLAN_XML ON"); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
defer func() { _, _ = conn.ExecContext(context.Background(), "SET SHOWPLAN_XML OFF") }()
|
||||
rows, err := conn.QueryContext(ctx, sqlText)
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var sb strings.Builder
|
||||
for rows.Next() {
|
||||
var s sql.NullString
|
||||
if err := rows.Scan(&s); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
sb.WriteString(s.String)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
return ExplainResult{Text: sb.String()}
|
||||
case driverKey(c.Driver) == "oracle":
|
||||
if _, err := db.ExecContext(ctx, "EXPLAIN PLAN FOR "+sqlText); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
rows, err := db.QueryContext(ctx, "SELECT plan_table_output FROM TABLE(DBMS_XPLAN.DISPLAY())")
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var sb strings.Builder
|
||||
for rows.Next() {
|
||||
var s sql.NullString
|
||||
if err := rows.Scan(&s); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
sb.WriteString(s.String)
|
||||
sb.WriteByte('\n')
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
return ExplainResult{Text: sb.String()}
|
||||
default:
|
||||
return ExplainResult{Error: errMsg(unsupportedDriverErr(c))}
|
||||
}
|
||||
}
|
||||
|
||||
// explainRows 以行列形式读取执行计划(mysql/pg/sqlite)。
|
||||
func explainRows(ctx context.Context, db *sql.DB, q string) ExplainResult {
|
||||
rows, err := db.QueryContext(ctx, q)
|
||||
if err != nil {
|
||||
return ExplainResult{Error: errMsg(err)}
|
||||
}
|
||||
defer rows.Close()
|
||||
cols, err := rows.Columns()
|
||||
if err != nil {
|
||||
return ExplainResult{Error: errMsg(err)}
|
||||
}
|
||||
out := []map[string]interface{}{}
|
||||
for rows.Next() {
|
||||
raw := make([]interface{}, len(cols))
|
||||
ptrs := make([]interface{}, len(cols))
|
||||
for i := range raw {
|
||||
ptrs[i] = &raw[i]
|
||||
}
|
||||
if err := rows.Scan(ptrs...); err != nil {
|
||||
return ExplainResult{Error: errMsg(err)}
|
||||
}
|
||||
m := make(map[string]interface{}, len(cols))
|
||||
for i, col := range cols {
|
||||
m[col] = normalize(raw[i])
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return ExplainResult{Error: errMsg(err)}
|
||||
}
|
||||
return ExplainResult{Columns: cols, Rows: out}
|
||||
}
|
||||
|
||||
func isQuerySQL(sql string) bool {
|
||||
s := stripSQLLead(sql)
|
||||
if s == "" {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -58,9 +59,9 @@ func openOracle(c ConnReq) (*sql.DB, error) {
|
||||
return sql.Open("oracle", dsn)
|
||||
}
|
||||
|
||||
func listDatabasesOracle(db *sql.DB) ([]string, error) {
|
||||
func listDatabasesOracle(ctx context.Context, db *sql.DB) ([]string, error) {
|
||||
// 返回当前用户可见 schema
|
||||
rows, err := db.Query(`SELECT username FROM all_users ORDER BY username`)
|
||||
rows, err := db.QueryContext(ctx, `SELECT username FROM all_users ORDER BY username`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -76,15 +77,15 @@ func listDatabasesOracle(db *sql.DB) ([]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func listTablesOracle(db *sql.DB, schema string) ([]string, error) {
|
||||
func listTablesOracle(ctx context.Context, db *sql.DB, schema string) ([]string, error) {
|
||||
var (
|
||||
rows *sql.Rows
|
||||
err error
|
||||
)
|
||||
if schema == "" {
|
||||
rows, err = db.Query(`SELECT table_name FROM user_tables ORDER BY table_name`)
|
||||
rows, err = db.QueryContext(ctx, `SELECT table_name FROM user_tables ORDER BY table_name`)
|
||||
} else {
|
||||
rows, err = db.Query(`SELECT table_name FROM all_tables WHERE owner = UPPER(:1) ORDER BY table_name`, schema)
|
||||
rows, err = db.QueryContext(ctx, `SELECT table_name FROM all_tables WHERE owner = UPPER(:1) ORDER BY table_name`, schema)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -101,7 +102,7 @@ func listTablesOracle(db *sql.DB, schema string) ([]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func listColumnsOracle(dbh *sql.DB, schema, table string) ([]ColumnInfo, error) {
|
||||
func listColumnsOracle(ctx context.Context, dbh *sql.DB, schema, table string) ([]ColumnInfo, error) {
|
||||
owner, t := splitSchemaTable(table)
|
||||
if owner == "" {
|
||||
owner = schema
|
||||
@@ -110,16 +111,27 @@ func listColumnsOracle(dbh *sql.DB, schema, table string) ([]ColumnInfo, error)
|
||||
rows *sql.Rows
|
||||
err error
|
||||
)
|
||||
// 关联约束元数据补主键标记(命中则 Key='PRI');沿用既有 UPPER() 绑定约定
|
||||
if owner == "" {
|
||||
rows, err = dbh.Query(`
|
||||
SELECT column_name, data_type, nullable, data_default
|
||||
FROM user_tab_columns WHERE table_name = UPPER(:1)
|
||||
ORDER BY column_id`, t)
|
||||
rows, err = dbh.QueryContext(ctx, `
|
||||
SELECT c.column_name, c.data_type, c.nullable, c.data_default,
|
||||
CASE WHEN acc.column_name IS NOT NULL THEN 'PRI' ELSE '' END
|
||||
FROM user_tab_columns c
|
||||
LEFT JOIN user_constraints ac ON ac.table_name = UPPER(:1) AND ac.constraint_type = 'P'
|
||||
LEFT JOIN user_cons_columns acc ON acc.constraint_name = ac.constraint_name
|
||||
AND acc.table_name = ac.table_name AND acc.column_name = c.column_name
|
||||
WHERE c.table_name = UPPER(:2)
|
||||
ORDER BY c.column_id`, t, t)
|
||||
} else {
|
||||
rows, err = dbh.Query(`
|
||||
SELECT column_name, data_type, nullable, data_default
|
||||
FROM all_tab_columns WHERE owner = UPPER(:1) AND table_name = UPPER(:2)
|
||||
ORDER BY column_id`, owner, t)
|
||||
rows, err = dbh.QueryContext(ctx, `
|
||||
SELECT c.column_name, c.data_type, c.nullable, c.data_default,
|
||||
CASE WHEN acc.column_name IS NOT NULL THEN 'PRI' ELSE '' END
|
||||
FROM all_tab_columns c
|
||||
LEFT JOIN all_constraints ac ON ac.owner = UPPER(:1) AND ac.table_name = UPPER(:2) AND ac.constraint_type = 'P'
|
||||
LEFT JOIN all_cons_columns acc ON acc.owner = ac.owner AND acc.constraint_name = ac.constraint_name
|
||||
AND acc.column_name = c.column_name
|
||||
WHERE c.owner = UPPER(:3) AND c.table_name = UPPER(:4)
|
||||
ORDER BY c.column_id`, owner, t, owner, t)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -127,12 +139,12 @@ func listColumnsOracle(dbh *sql.DB, schema, table string) ([]ColumnInfo, error)
|
||||
defer rows.Close()
|
||||
var out []ColumnInfo
|
||||
for rows.Next() {
|
||||
var name, typ, nullable string
|
||||
var name, typ, nullable, key string
|
||||
var def sql.NullString
|
||||
if err := rows.Scan(&name, &typ, &nullable, &def); err != nil {
|
||||
if err := rows.Scan(&name, &typ, &nullable, &def, &key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
col := ColumnInfo{Name: name, Type: typ, Nullable: nullable == "Y"}
|
||||
col := ColumnInfo{Name: name, Type: typ, Nullable: nullable == "Y", Key: key}
|
||||
if def.Valid {
|
||||
col.Default = def.String
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -36,6 +37,9 @@ func openPostgres(c ConnReq) (*sql.DB, error) {
|
||||
case "verify-ca":
|
||||
sslmode = "verify-ca"
|
||||
if strings.TrimSpace(c.SSLCA) == "" {
|
||||
if strings.TrimSpace(c.SSLCAContent) != "" {
|
||||
return nil, fmt.Errorf("verify-ca 模式需要 CA 证书路径(pg 驱动暂不支持证书内容,请改用 sslCA)")
|
||||
}
|
||||
return nil, fmt.Errorf("verify-ca 模式需要填写 CA 证书路径")
|
||||
}
|
||||
default:
|
||||
@@ -68,8 +72,8 @@ func pqEscape(v string) string {
|
||||
return "'" + strings.ReplaceAll(strings.ReplaceAll(v, `\`, `\\`), `'`, `\'`) + "'"
|
||||
}
|
||||
|
||||
func listDatabasesPostgres(db *sql.DB) ([]string, error) {
|
||||
rows, err := db.Query(`SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY 1`)
|
||||
func listDatabasesPostgres(ctx context.Context, db *sql.DB) ([]string, error) {
|
||||
rows, err := db.QueryContext(ctx, `SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY 1`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -85,10 +89,10 @@ func listDatabasesPostgres(db *sql.DB) ([]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func listTablesPostgres(db *sql.DB, database string) ([]string, error) {
|
||||
func listTablesPostgres(ctx context.Context, db *sql.DB, database string) ([]string, error) {
|
||||
// 当前连接库下的 public 表;切库需重新 Open
|
||||
_ = database
|
||||
rows, err := db.Query(`
|
||||
rows, err := db.QueryContext(ctx, `
|
||||
SELECT CASE WHEN schemaname = 'public' THEN tablename ELSE schemaname || '.' || tablename END
|
||||
FROM pg_catalog.pg_tables
|
||||
WHERE schemaname NOT IN ('pg_catalog','information_schema')
|
||||
@@ -108,33 +112,37 @@ func listTablesPostgres(db *sql.DB, database string) ([]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func listColumnsPostgres(dbh *sql.DB, _, table string) ([]ColumnInfo, error) {
|
||||
func listColumnsPostgres(ctx context.Context, dbh *sql.DB, _, table string) ([]ColumnInfo, error) {
|
||||
schema, t := splitSchemaTable(table)
|
||||
if schema == "" {
|
||||
schema = "public"
|
||||
}
|
||||
rows, err := dbh.Query(`
|
||||
SELECT column_name, data_type,
|
||||
COALESCE(character_maximum_length::text, numeric_precision::text, ''),
|
||||
is_nullable, column_default
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = $1 AND table_name = $2
|
||||
ORDER BY ordinal_position`, schema, t)
|
||||
// to_regclass 解析失败(表不存在/跨 schema)返回 NULL 而非报错,主键标记自然落空
|
||||
rows, err := dbh.QueryContext(ctx, `
|
||||
SELECT c.column_name, c.data_type,
|
||||
COALESCE(c.character_maximum_length::text, c.numeric_precision::text, ''),
|
||||
c.is_nullable, c.column_default,
|
||||
CASE WHEN a.attname IS NOT NULL THEN 'PRI' ELSE '' END
|
||||
FROM information_schema.columns c
|
||||
LEFT JOIN pg_index i ON i.indrelid = to_regclass(quote_ident($1)||'.'||quote_ident($2)) AND i.indisprimary
|
||||
LEFT JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attname = c.column_name AND a.attnum = ANY(i.indkey)
|
||||
WHERE c.table_schema = $1 AND c.table_name = $2
|
||||
ORDER BY c.ordinal_position`, schema, t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []ColumnInfo
|
||||
for rows.Next() {
|
||||
var name, typ, extra, nullable string
|
||||
var name, typ, extra, nullable, key string
|
||||
var def sql.NullString
|
||||
if err := rows.Scan(&name, &typ, &extra, &nullable, &def); err != nil {
|
||||
if err := rows.Scan(&name, &typ, &extra, &nullable, &def, &key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if extra != "" {
|
||||
typ = typ + "(" + extra + ")"
|
||||
}
|
||||
col := ColumnInfo{Name: name, Type: typ, Nullable: nullable == "YES"}
|
||||
col := ColumnInfo{Name: name, Type: typ, Nullable: nullable == "YES", Key: key}
|
||||
if def.Valid {
|
||||
col.Default = def.String
|
||||
}
|
||||
|
||||
+736
-158
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,164 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
)
|
||||
|
||||
func redisTestReq(t *testing.T, mr *miniredis.Miniredis) ConnReq {
|
||||
t.Helper()
|
||||
host, portStr, err := net.SplitHostPort(mr.Addr())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
port, _ := strconv.Atoi(portStr)
|
||||
return ConnReq{Driver: "redis", Host: host, Port: port, Database: "0"}
|
||||
}
|
||||
|
||||
// 写命令白名单:SET EX / HSET / EXPIRE / MSET / INCR 等应直接可用。
|
||||
func TestExecRedisWriteWhitelist(t *testing.T) {
|
||||
mr, err := miniredis.Run()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer mr.Close()
|
||||
base := redisTestReq(t, mr)
|
||||
ctx := context.Background()
|
||||
|
||||
run := func(sql string) ExecResult {
|
||||
c := base
|
||||
c.SQL = sql
|
||||
return execRedis(ctx, c)
|
||||
}
|
||||
|
||||
if r := run(`SET k1 "v with space" EX 100`); r.Error != "" {
|
||||
t.Fatalf("SET EX: %s", r.Error)
|
||||
}
|
||||
if v, _ := mr.Get("k1"); v != "v with space" {
|
||||
t.Fatalf("SET 引号分组值被破坏: %q", v)
|
||||
}
|
||||
if ttl := mr.TTL("k1"); ttl != 100*time.Second {
|
||||
t.Fatalf("SET EX TTL: %v", ttl)
|
||||
}
|
||||
|
||||
if r := run("HSET h1 f1 v1 f2 v2"); r.Error != "" {
|
||||
t.Fatalf("HSET: %s", r.Error)
|
||||
}
|
||||
if mr.HGet("h1", "f2") != "v2" {
|
||||
t.Fatal("HSET 未生效")
|
||||
}
|
||||
|
||||
if r := run("EXPIRE h1 60"); r.Error != "" {
|
||||
t.Fatalf("EXPIRE: %s", r.Error)
|
||||
}
|
||||
if ttl := mr.TTL("h1"); ttl != 60*time.Second {
|
||||
t.Fatalf("EXPIRE 未生效: %v", ttl)
|
||||
}
|
||||
if r := run("PERSIST h1"); r.Error != "" {
|
||||
t.Fatalf("PERSIST: %s", r.Error)
|
||||
}
|
||||
if ttl := mr.TTL("h1"); ttl != 0 {
|
||||
t.Fatalf("PERSIST 未生效: %v", ttl)
|
||||
}
|
||||
|
||||
if r := run("MSET a 1 b 2"); r.Error != "" {
|
||||
t.Fatalf("MSET: %s", r.Error)
|
||||
}
|
||||
if r := run("MGET a b"); r.Error != "" || r.RowCount != 2 {
|
||||
t.Fatalf("MGET: %#v", r)
|
||||
}
|
||||
if r := run("INCR n1"); r.Error != "" {
|
||||
t.Fatalf("INCR: %s", r.Error)
|
||||
}
|
||||
if r := run("SADD s1 m1 m2"); r.Error != "" {
|
||||
t.Fatalf("SADD: %s", r.Error)
|
||||
}
|
||||
if r := run("ZADD z1 1.5 zm"); r.Error != "" {
|
||||
t.Fatalf("ZADD: %s", r.Error)
|
||||
}
|
||||
if r := run("LPUSH l1 x y"); r.Error != "" {
|
||||
t.Fatalf("LPUSH: %s", r.Error)
|
||||
}
|
||||
if r := run("RENAME a a2"); r.Error != "" {
|
||||
t.Fatalf("RENAME: %s", r.Error)
|
||||
}
|
||||
if !mr.Exists("a2") {
|
||||
t.Fatal("RENAME 未生效")
|
||||
}
|
||||
}
|
||||
|
||||
// 危险命令二段确认:FLUSHALL/KEYS 未确认不执行,确认后放行。
|
||||
func TestExecRedisDangerConfirm(t *testing.T) {
|
||||
mr, err := miniredis.Run()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer mr.Close()
|
||||
base := redisTestReq(t, mr)
|
||||
ctx := context.Background()
|
||||
_ = mr.Set("k", "v")
|
||||
|
||||
run := func(sql string, confirm bool) ExecResult {
|
||||
c := base
|
||||
c.SQL = sql
|
||||
c.ConfirmDanger = confirm
|
||||
return execRedis(ctx, c)
|
||||
}
|
||||
|
||||
if r := run("FLUSHALL", false); !r.NeedConfirm {
|
||||
t.Fatalf("FLUSHALL 未确认应 needConfirm: %#v", r)
|
||||
}
|
||||
if !mr.Exists("k") {
|
||||
t.Fatal("未确认的 FLUSHALL 不应执行")
|
||||
}
|
||||
if r := run("KEYS *", false); !r.NeedConfirm {
|
||||
t.Fatalf("KEYS 未确认应 needConfirm: %#v", r)
|
||||
}
|
||||
if r := run("KEYS *", true); r.Error != "" || r.RowCount != 1 {
|
||||
t.Fatalf("KEYS 确认后应放行: %#v", r)
|
||||
}
|
||||
if r := run("FLUSHDB", true); r.Error != "" {
|
||||
t.Fatalf("FLUSHDB 确认后应放行: %#v", r)
|
||||
}
|
||||
if mr.Exists("k") {
|
||||
t.Fatal("确认后的 FLUSHDB 应已执行")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecRedisGenericDoAndScan(t *testing.T) {
|
||||
mr, err := miniredis.Run()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer mr.Close()
|
||||
base := redisTestReq(t, mr)
|
||||
ctx := context.Background()
|
||||
_ = mr.Set("user:1", "a")
|
||||
_ = mr.Set("user:2", "b")
|
||||
_ = mr.Set("other", "c")
|
||||
|
||||
run := func(sql string) ExecResult {
|
||||
c := base
|
||||
c.SQL = sql
|
||||
return execRedis(ctx, c)
|
||||
}
|
||||
|
||||
if r := run("EXISTS user:1"); r.Error != "" || r.RowCount != 1 {
|
||||
t.Fatalf("EXISTS via Do: %#v", r)
|
||||
}
|
||||
if r := run("SCAN 0 MATCH user:* COUNT 50"); r.Error != "" {
|
||||
t.Fatalf("SCAN MATCH: %#v", r)
|
||||
} else {
|
||||
if r.RowCount != 2 {
|
||||
t.Fatalf("SCAN MATCH want 2 got %d %#v", r.RowCount, r.Rows)
|
||||
}
|
||||
}
|
||||
if r := run("ECHO hello"); r.Error != "" {
|
||||
t.Fatalf("ECHO via Do: %#v", r)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"testing"
|
||||
@@ -28,7 +29,7 @@ func TestListGetDeleteRedisKeys(t *testing.T) {
|
||||
port, _ := strconv.Atoi(portStr)
|
||||
base := ConnReq{Driver: "redis", Host: host, Port: port, Database: "0"}
|
||||
|
||||
list := ListRedisKeys(base)
|
||||
list := ListRedisKeys(context.Background(), base)
|
||||
if list.Error != "" {
|
||||
t.Fatal(list.Error)
|
||||
}
|
||||
@@ -50,33 +51,36 @@ func TestListGetDeleteRedisKeys(t *testing.T) {
|
||||
t.Fatalf("unexpected ttl %d", byName["str:a"].TTL)
|
||||
}
|
||||
|
||||
gv := GetRedisKey(ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "str:a"})
|
||||
gv := GetRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "str:a"})
|
||||
if gv.Error != "" || gv.RowCount != 1 || gv.Rows[0]["value"] != "hello" {
|
||||
t.Fatalf("get string %#v", gv)
|
||||
}
|
||||
gh := GetRedisKey(ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "hash:b"})
|
||||
gh := GetRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "hash:b"})
|
||||
if gh.Error != "" || gh.RowCount < 1 || gh.Columns[0] != "field" {
|
||||
t.Fatalf("get hash %#v", gh)
|
||||
}
|
||||
gl := GetRedisKey(ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "list:c"})
|
||||
if gh.Rows[0]["field"] != "f1" {
|
||||
t.Fatalf("hash fields should be sorted, got %#v", gh.Rows)
|
||||
}
|
||||
gl := GetRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "list:c"})
|
||||
if gl.Error != "" || gl.RowCount < 2 {
|
||||
t.Fatalf("get list %#v", gl)
|
||||
}
|
||||
gs := GetRedisKey(ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "set:d"})
|
||||
gs := GetRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "set:d"})
|
||||
if gs.Error != "" || gs.RowCount < 2 {
|
||||
t.Fatalf("get set %#v", gs)
|
||||
}
|
||||
gz := GetRedisKey(ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "zset:e"})
|
||||
gz := GetRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "zset:e"})
|
||||
if gz.Error != "" || gz.RowCount != 1 {
|
||||
t.Fatalf("get zset %#v", gz)
|
||||
}
|
||||
|
||||
pat := ListRedisKeys(ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Pattern: "str:*"})
|
||||
pat := ListRedisKeys(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Pattern: "str:*"})
|
||||
if pat.Error != "" || len(pat.Keys) != 1 || pat.Keys[0].Name != "str:a" {
|
||||
t.Fatalf("pattern %#v", pat)
|
||||
}
|
||||
|
||||
del := DeleteRedisKey(ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "str:a"})
|
||||
del := DeleteRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "str:a"})
|
||||
if del.Error != "" {
|
||||
t.Fatal(del.Error)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseRedisAddrs(t *testing.T) {
|
||||
got := parseRedisAddrs("127.0.0.1:26379\n127.0.0.1:26380, 10.0.0.1:26379")
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("want 3 addrs, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenRedisModeValidation(t *testing.T) {
|
||||
_, err := openRedis(ConnReq{Driver: "redis", RedisMode: "sentinel"})
|
||||
if err == nil || !strings.Contains(err.Error(), "Sentinel") {
|
||||
t.Fatalf("sentinel without nodes: %v", err)
|
||||
}
|
||||
_, err = openRedis(ConnReq{Driver: "redis", RedisMode: "sentinel", RedisNodes: "127.0.0.1:26379"})
|
||||
if err == nil || !strings.Contains(err.Error(), "masterName") {
|
||||
t.Fatalf("sentinel without master: %v", err)
|
||||
}
|
||||
_, err = openRedis(ConnReq{Driver: "redis", RedisMode: "cluster"})
|
||||
if err == nil || !strings.Contains(err.Error(), "Cluster") {
|
||||
t.Fatalf("cluster without nodes: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ObjectHit 对象搜索结果条目。
|
||||
type ObjectHit struct {
|
||||
Schema string `json:"schema"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"` // table | view
|
||||
}
|
||||
|
||||
// SearchObjects 按关键词模糊搜索表/视图,最多返回 50 条。redis 返回空。
|
||||
func SearchObjects(ctx context.Context, c ConnReq) ([]ObjectHit, error) {
|
||||
ctx = reqCtx(ctx)
|
||||
if isRedis(c.Driver) {
|
||||
return []ObjectHit{}, nil
|
||||
}
|
||||
var out []ObjectHit
|
||||
err := withDB(c, func(db *sql.DB) error {
|
||||
var err error
|
||||
switch {
|
||||
case isMySQLFamily(c.Driver):
|
||||
out, err = searchObjectsMySQL(ctx, db, c.Database, c.Keyword)
|
||||
case isPostgresFamily(c.Driver):
|
||||
out, err = searchObjectsPostgres(ctx, db, c.Keyword)
|
||||
case isMSSQL(c.Driver):
|
||||
out, err = searchObjectsMSSQL(ctx, db, c.Keyword)
|
||||
case driverKey(c.Driver) == "oracle":
|
||||
out, err = searchObjectsOracle(ctx, db, c.Database, c.User, c.Keyword)
|
||||
case driverKey(c.Driver) == "sqlite":
|
||||
out, err = searchObjectsSQLite(ctx, db, c.Keyword)
|
||||
default:
|
||||
err = unsupportedDriverErr(c)
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if out == nil {
|
||||
out = []ObjectHit{}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func likePattern(keyword string) string {
|
||||
return "%" + keyword + "%"
|
||||
}
|
||||
|
||||
// hitType information_schema 的 table_type 含 VIEW 即为视图。
|
||||
func hitType(tableType string) string {
|
||||
if strings.Contains(strings.ToUpper(tableType), "VIEW") {
|
||||
return "view"
|
||||
}
|
||||
return "table"
|
||||
}
|
||||
|
||||
func searchObjectsMySQL(ctx context.Context, dbh *sql.DB, database, keyword string) ([]ObjectHit, error) {
|
||||
// database 为空时用当前库 DATABASE()
|
||||
q := `SELECT table_schema, table_name, table_type FROM information_schema.TABLES
|
||||
WHERE table_schema = DATABASE() AND table_name LIKE ? LIMIT 50`
|
||||
args := []interface{}{likePattern(keyword)}
|
||||
if strings.TrimSpace(database) != "" {
|
||||
q = `SELECT table_schema, table_name, table_type FROM information_schema.TABLES
|
||||
WHERE table_schema = ? AND table_name LIKE ? LIMIT 50`
|
||||
args = []interface{}{database, likePattern(keyword)}
|
||||
}
|
||||
return scanObjectHits(ctx, dbh, q, args, "mysql")
|
||||
}
|
||||
|
||||
func searchObjectsPostgres(ctx context.Context, dbh *sql.DB, keyword string) ([]ObjectHit, error) {
|
||||
return scanObjectHits(ctx, dbh,
|
||||
`SELECT table_schema, table_name, table_type FROM information_schema.tables
|
||||
WHERE table_schema NOT IN ('pg_catalog','information_schema') AND table_name LIKE $1 LIMIT 50`,
|
||||
[]interface{}{likePattern(keyword)}, "pg")
|
||||
}
|
||||
|
||||
func searchObjectsMSSQL(ctx context.Context, dbh *sql.DB, keyword string) ([]ObjectHit, error) {
|
||||
// MSSQL 无 LIMIT,用 TOP 50
|
||||
return scanObjectHits(ctx, dbh,
|
||||
`SELECT TOP 50 TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM information_schema.TABLES
|
||||
WHERE TABLE_NAME LIKE @p1`,
|
||||
[]interface{}{likePattern(keyword)}, "mssql")
|
||||
}
|
||||
|
||||
func searchObjectsOracle(ctx context.Context, dbh *sql.DB, database, user, keyword string) ([]ObjectHit, error) {
|
||||
owner := strings.ToUpper(strings.TrimSpace(database))
|
||||
if owner == "" {
|
||||
owner = strings.ToUpper(strings.TrimSpace(user))
|
||||
}
|
||||
return scanObjectHits(ctx, dbh,
|
||||
`SELECT owner, table_name, 'table' FROM all_tables WHERE owner = :1 AND table_name LIKE :2
|
||||
UNION ALL
|
||||
SELECT owner, view_name, 'view' FROM all_views WHERE owner = :3 AND view_name LIKE :4
|
||||
FETCH FIRST 50 ROWS ONLY`,
|
||||
[]interface{}{owner, likePattern(keyword), owner, likePattern(keyword)}, "oracle")
|
||||
}
|
||||
|
||||
func searchObjectsSQLite(ctx context.Context, dbh *sql.DB, keyword string) ([]ObjectHit, error) {
|
||||
return scanObjectHits(ctx, dbh,
|
||||
`SELECT '', name, type FROM sqlite_master WHERE name LIKE ? AND type IN ('table','view') LIMIT 50`,
|
||||
[]interface{}{likePattern(keyword)}, "sqlite")
|
||||
}
|
||||
|
||||
func scanObjectHits(ctx context.Context, dbh *sql.DB, q string, args []interface{}, dialect string) ([]ObjectHit, error) {
|
||||
rows, err := dbh.QueryContext(ctx, q, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []ObjectHit{}
|
||||
for rows.Next() {
|
||||
var schema, name, typ string
|
||||
if err := rows.Scan(&schema, &name, &typ); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t := "table"
|
||||
switch dialect {
|
||||
case "sqlite":
|
||||
t = typ // sqlite_master.type 直接是 table/view
|
||||
default:
|
||||
t = hitType(typ)
|
||||
}
|
||||
out = append(out, ObjectHit{Schema: schema, Name: name, Type: t})
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// R2-B6:sqlite 搜索命中表与视图,类型区分正确。
|
||||
func TestSearchObjectsSQLite(t *testing.T) {
|
||||
c := newSQLiteFile(t,
|
||||
`CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)`,
|
||||
`CREATE TABLE orders (id INTEGER PRIMARY KEY)`,
|
||||
`CREATE VIEW v_users AS SELECT id, name FROM users`,
|
||||
)
|
||||
c.Keyword = "user"
|
||||
hits, err := SearchObjects(context.Background(), c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(hits) != 2 {
|
||||
t.Fatalf("应命中 2 个(user 前缀的表和视图): %+v", hits)
|
||||
}
|
||||
byName := map[string]ObjectHit{}
|
||||
for _, h := range hits {
|
||||
byName[h.Name] = h
|
||||
}
|
||||
if byName["users"].Type != "table" {
|
||||
t.Fatalf("users 应为 table: %+v", byName["users"])
|
||||
}
|
||||
if byName["v_users"].Type != "view" {
|
||||
t.Fatalf("v_users 应为 view: %+v", byName["v_users"])
|
||||
}
|
||||
|
||||
// 不命中
|
||||
c.Keyword = "nothing_match"
|
||||
hits, err = SearchObjects(context.Background(), c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(hits) != 0 {
|
||||
t.Fatalf("不应命中: %+v", hits)
|
||||
}
|
||||
}
|
||||
|
||||
// redis 返回空结果而非报错。
|
||||
func TestSearchObjectsRedis(t *testing.T) {
|
||||
c := ConnReq{Driver: "redis", Keyword: "k"}
|
||||
hits, err := SearchObjects(context.Background(), c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(hits) != 0 {
|
||||
t.Fatalf("redis 应返回空: %+v", hits)
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -14,14 +16,21 @@ import (
|
||||
const sessionTTL = 45 * time.Minute
|
||||
const maxSessions = 64
|
||||
|
||||
// maxPoolHandles 每会话缓存的 *sql.DB 句柄上限(按 database 分键),超出时关闭最久未用。
|
||||
const maxPoolHandles = 8
|
||||
|
||||
type Session struct {
|
||||
ID string
|
||||
Conn ConnReq
|
||||
mu sync.Mutex
|
||||
sqlPool map[string]*sql.DB
|
||||
redisCli *redis.Client
|
||||
tunnel *SSHTunnel
|
||||
LastUsed time.Time
|
||||
ID string
|
||||
Conn ConnReq
|
||||
mu sync.Mutex
|
||||
sqlPool map[string]*sql.DB
|
||||
poolUsed map[string]time.Time // sqlPool 各键最近使用时间,供 LRU 轮换
|
||||
redisClis map[int]redis.UniversalClient // 按 DB 序号缓存的 Redis 客户端(DB 是连接级参数)
|
||||
// agentHandles: agent 驱动(如达梦)按 database 缓存的 JDBC 句柄;
|
||||
// JVM 进程走全局池(agentPool),会话关闭只关句柄、不杀进程。
|
||||
agentHandles map[string]string
|
||||
tunnel *SSHTunnel
|
||||
LastUsed time.Time
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -39,12 +48,14 @@ func init() {
|
||||
}()
|
||||
}
|
||||
|
||||
func newSessionID() string {
|
||||
// newSessionID 生成 128bit 随机会话 ID;crypto/rand 失败时返回错误,
|
||||
// 不再静默降级为低熵时间戳。
|
||||
func newSessionID() (string, error) {
|
||||
var b [16]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return fmt.Sprintf("%d", time.Now().UnixNano())
|
||||
return "", fmt.Errorf("生成会话 ID 失败: %w", err)
|
||||
}
|
||||
return hex.EncodeToString(b[:])
|
||||
return hex.EncodeToString(b[:]), nil
|
||||
}
|
||||
|
||||
func OpenSession(c ConnReq) (string, error) {
|
||||
@@ -54,6 +65,10 @@ func OpenSession(c ConnReq) (string, error) {
|
||||
c.Pattern = ""
|
||||
c.SessionID = ""
|
||||
|
||||
// agent 驱动(JDBC 桥)不走本地 SSH 端口转发(转发对 JVM 侧 TCP 无效)
|
||||
if isAgentDriver(c.Driver) && c.SSHEnabled {
|
||||
return "", fmt.Errorf("%s 走 JDBC agent,暂不支持 SSH 隧道", c.Driver)
|
||||
}
|
||||
dial, tun, err := ApplySSHTunnel(c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -73,16 +88,25 @@ func OpenSession(c ConnReq) (string, error) {
|
||||
}
|
||||
return "", fmt.Errorf("连接会话已满,请关闭不用的连接后重试")
|
||||
}
|
||||
id := newSessionID()
|
||||
id, err := newSessionID()
|
||||
if err != nil {
|
||||
sessionsMu.Unlock()
|
||||
if tun != nil {
|
||||
tun.Close()
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
// Persist dialed endpoints (127.0.0.1:local) so later ops reuse the tunnel.
|
||||
stored := dial
|
||||
stored.SSHEnabled = false
|
||||
s := &Session{
|
||||
ID: id,
|
||||
Conn: stored,
|
||||
sqlPool: map[string]*sql.DB{},
|
||||
tunnel: tun,
|
||||
LastUsed: time.Now(),
|
||||
ID: id,
|
||||
Conn: stored,
|
||||
sqlPool: map[string]*sql.DB{},
|
||||
poolUsed: map[string]time.Time{},
|
||||
agentHandles: map[string]string{},
|
||||
tunnel: tun,
|
||||
LastUsed: time.Now(),
|
||||
}
|
||||
sessions[id] = s
|
||||
sessionsMu.Unlock()
|
||||
@@ -143,9 +167,21 @@ func (s *Session) closeAll() {
|
||||
_ = dbh.Close()
|
||||
delete(s.sqlPool, k)
|
||||
}
|
||||
if s.redisCli != nil {
|
||||
_ = s.redisCli.Close()
|
||||
s.redisCli = nil
|
||||
s.poolUsed = map[string]time.Time{}
|
||||
for k, cli := range s.redisClis {
|
||||
_ = cli.Close()
|
||||
delete(s.redisClis, k)
|
||||
}
|
||||
// agent 句柄随会话释放;JVM 进程归全局池,进程生命周期不挂钩会话
|
||||
if len(s.agentHandles) > 0 {
|
||||
if cli, err := getAgentClient(s.Conn.Driver); err == nil {
|
||||
for _, h := range s.agentHandles {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
_ = cli.call(ctx, "close", map[string]any{"handle": h}, nil)
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
s.agentHandles = map[string]string{}
|
||||
}
|
||||
if s.tunnel != nil {
|
||||
s.tunnel.Close()
|
||||
@@ -153,6 +189,151 @@ func (s *Session) closeAll() {
|
||||
}
|
||||
}
|
||||
|
||||
// agentConn 按 database 懒建/复用 agent JDBC 句柄(模式同 sqlDB)。
|
||||
// 调用方不得持有 s.mu(本函数内部自行加锁;getAgentClient 用独立的全局池锁)。
|
||||
func (s *Session) agentConn(c ConnReq) (*AgentClient, string, error) {
|
||||
cli, err := getAgentClient(c.Driver)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
key := c.Database
|
||||
s.mu.Lock()
|
||||
if h, ok := s.agentHandles[key]; ok {
|
||||
s.mu.Unlock()
|
||||
return cli, h, nil
|
||||
}
|
||||
s.mu.Unlock()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
h, err := agentOpen(ctx, cli, c)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if old, ok := s.agentHandles[key]; ok {
|
||||
// 双检:并发下已有别的 goroutine 建好同键句柄,回收多余句柄
|
||||
go func() {
|
||||
ctx2, cancel2 := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel2()
|
||||
_ = cli.call(ctx2, "close", map[string]any{"handle": h}, nil)
|
||||
}()
|
||||
return cli, old, nil
|
||||
}
|
||||
s.agentHandles[key] = h
|
||||
return cli, h, nil
|
||||
}
|
||||
|
||||
// touch 滚动续期(GetSession 已调用,池化路径同样经 GetSession 进入)。
|
||||
func (s *Session) touch() {
|
||||
s.mu.Lock()
|
||||
s.LastUsed = time.Now()
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
// sqlDB 按 database 懒加载缓存 *sql.DB,会话内复用连接池;
|
||||
// 超过 maxPoolHandles 个句柄时关闭最久未用的池。
|
||||
func (s *Session) sqlDB(database string) (*sql.DB, error) {
|
||||
// SQLite 的 database 是文件路径,对象树的 "main" 不能覆盖它
|
||||
if driverKey(s.Conn.Driver) == "sqlite" {
|
||||
database = ""
|
||||
}
|
||||
s.mu.Lock()
|
||||
if dbh, ok := s.sqlPool[database]; ok {
|
||||
s.poolUsed[database] = time.Now()
|
||||
s.mu.Unlock()
|
||||
return dbh, nil
|
||||
}
|
||||
s.mu.Unlock()
|
||||
|
||||
conn := s.Conn
|
||||
if database != "" {
|
||||
conn.Database = database
|
||||
}
|
||||
dbh, err := Open(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
// 双检:并发下可能已有别的 goroutine 建好同键池
|
||||
if old, ok := s.sqlPool[database]; ok {
|
||||
s.poolUsed[database] = time.Now()
|
||||
_ = dbh.Close()
|
||||
return old, nil
|
||||
}
|
||||
// 超上限先淘汰最久未用
|
||||
for len(s.sqlPool) >= maxPoolHandles {
|
||||
var lruKey string
|
||||
var lruTime time.Time
|
||||
for k := range s.sqlPool {
|
||||
t := s.poolUsed[k]
|
||||
if lruKey == "" || t.Before(lruTime) {
|
||||
lruKey, lruTime = k, t
|
||||
}
|
||||
}
|
||||
_ = s.sqlPool[lruKey].Close()
|
||||
delete(s.sqlPool, lruKey)
|
||||
delete(s.poolUsed, lruKey)
|
||||
}
|
||||
s.sqlPool[database] = dbh
|
||||
s.poolUsed[database] = time.Now()
|
||||
return dbh, nil
|
||||
}
|
||||
|
||||
// redisClient 懒建会话级 Redis 单例,避免每次命令新建连接。
|
||||
func (s *Session) redisClient() (redis.UniversalClient, error) {
|
||||
// 按连接配置缓存:DB 序号是 go-redis 的连接级参数(建连时钉死),
|
||||
// 对象树里切换 db0/db1 必须各建一个客户端,否则全部打到首个 db。
|
||||
return s.redisClientFor(s.Conn)
|
||||
}
|
||||
|
||||
func (s *Session) redisClientFor(c ConnReq) (redis.UniversalClient, error) {
|
||||
db := redisDBIndex(c)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.redisClis == nil {
|
||||
s.redisClis = map[int]redis.UniversalClient{}
|
||||
}
|
||||
if cli, ok := s.redisClis[db]; ok {
|
||||
return cli, nil
|
||||
}
|
||||
// 以会话凭据 + 目标 DB 建连(Sentinel/Cluster 模式 DB 恒为 0,天然只有一份)
|
||||
cli, err := openRedis(ConnReq{
|
||||
Driver: s.Conn.Driver,
|
||||
Host: s.Conn.Host,
|
||||
Port: s.Conn.Port,
|
||||
User: s.Conn.User,
|
||||
Password: s.Conn.Password,
|
||||
Database: strconv.Itoa(db),
|
||||
RedisMode: s.Conn.RedisMode,
|
||||
RedisNodes: s.Conn.RedisNodes,
|
||||
MasterName: s.Conn.MasterName,
|
||||
SentinelPassword: s.Conn.SentinelPassword,
|
||||
SSL: s.Conn.SSL,
|
||||
SSLMode: s.Conn.SSLMode,
|
||||
SSLCA: s.Conn.SSLCA,
|
||||
SSLCert: s.Conn.SSLCert,
|
||||
SSLKey: s.Conn.SSLKey,
|
||||
SSHEnabled: s.Conn.SSHEnabled,
|
||||
SSHHost: s.Conn.SSHHost,
|
||||
SSHPort: s.Conn.SSHPort,
|
||||
SSHUser: s.Conn.SSHUser,
|
||||
SSHAuth: s.Conn.SSHAuth,
|
||||
SSHPassword: s.Conn.SSHPassword,
|
||||
SSHPrivateKey: s.Conn.SSHPrivateKey,
|
||||
SSHPassphrase: s.Conn.SSHPassphrase,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.redisClis[db] = cli
|
||||
return cli, nil
|
||||
}
|
||||
|
||||
func (s *Session) ConnFor(req ConnReq) ConnReq {
|
||||
c := s.Conn
|
||||
// SQLite 的 database 是文件路径,对象树里的 "main" 不能覆盖它
|
||||
@@ -163,7 +344,16 @@ func (s *Session) ConnFor(req ConnReq) ConnReq {
|
||||
c.Table = req.Table
|
||||
c.Key = req.Key
|
||||
c.Pattern = req.Pattern
|
||||
c.Keyword = req.Keyword
|
||||
c.Format = req.Format
|
||||
c.Limit = req.Limit
|
||||
c.Offset = req.Offset
|
||||
c.ConfirmDanger = req.ConfirmDanger
|
||||
// 逐请求字段:新增 ConnReq 字段时若属于"每次请求携带"语义,必须在此补拷贝,
|
||||
// 否则会话路径会静默丢弃(前端全部走会话,等于功能失效)
|
||||
c.TimeoutMs = req.TimeoutMs
|
||||
c.Cursor = req.Cursor
|
||||
c.RequestID = req.RequestID
|
||||
c.SessionID = s.ID
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// touchFile 先建空文件(零字节即合法 SQLite 库),绕开存在性检查。
|
||||
func touchFile(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, []byte{}, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func newTestSession(driver, database string) *Session {
|
||||
return &Session{
|
||||
ID: "test-pool",
|
||||
Conn: ConnReq{Driver: driver, Database: database},
|
||||
sqlPool: map[string]*sql.DB{},
|
||||
poolUsed: map[string]time.Time{},
|
||||
LastUsed: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// 池化:同 database 复用同一 *sql.DB 句柄。
|
||||
func TestSessionSqlDBReuse(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "pool.db")
|
||||
touchFile(t, dbFile)
|
||||
s := newTestSession("sqlite", dbFile)
|
||||
defer s.closeAll()
|
||||
|
||||
a, err := s.sqlDB("")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := s.sqlDB("")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if a != b {
|
||||
t.Fatal("同 database 应复用同一 *sql.DB 句柄")
|
||||
}
|
||||
// sqlite 下 database 参数不覆盖文件路径
|
||||
c, err := s.sqlDB("main")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c != a {
|
||||
t.Fatal("sqlite 会话应忽略 database 复用同一句柄")
|
||||
}
|
||||
}
|
||||
|
||||
// 池化上限:超过 8 个句柄时关闭最久未用(LRU 轮换)。
|
||||
func TestSessionSqlDBLRU(t *testing.T) {
|
||||
// mysql 驱动 sql.Open 不立即建连,可离线验证池管理逻辑
|
||||
s := newTestSession("mysql", "")
|
||||
defer s.closeAll()
|
||||
|
||||
first, err := s.sqlDB("db0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 1; i <= maxPoolHandles; i++ {
|
||||
time.Sleep(2 * time.Millisecond) // 保证 LRU 时间序确定
|
||||
if _, err := s.sqlDB(string(rune('a' + i))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
s.mu.Lock()
|
||||
n := len(s.sqlPool)
|
||||
_, firstAlive := s.sqlPool["db0"]
|
||||
s.mu.Unlock()
|
||||
if n > maxPoolHandles {
|
||||
t.Fatalf("池句柄数 %d 超上限 %d", n, maxPoolHandles)
|
||||
}
|
||||
if firstAlive {
|
||||
t.Fatal("最久未用句柄应被淘汰")
|
||||
}
|
||||
_ = first
|
||||
}
|
||||
|
||||
// 会话路径端到端:ExecSQL 走池化,两次执行共享句柄且结果正确。
|
||||
func TestExecSQLSessionPooling(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "exec.db")
|
||||
touchFile(t, dbFile)
|
||||
id, err := OpenSession(ConnReq{Driver: "sqlite", Database: dbFile})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer CloseSession(id)
|
||||
|
||||
mk := func(sqlText string) ConnReq {
|
||||
c, err := ResolveConn(ConnReq{SessionID: id, SQL: sqlText})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
if r := ExecSQL(context.Background(), mk("CREATE TABLE t (id INTEGER PRIMARY KEY, v TEXT)")); r.Error != "" {
|
||||
t.Fatal(r.Error)
|
||||
}
|
||||
if r := ExecSQL(context.Background(), mk("INSERT INTO t (v) VALUES ('a'),('b'),('c')")); r.Error != "" {
|
||||
t.Fatal(r.Error)
|
||||
}
|
||||
r := ExecSQL(context.Background(), mk("SELECT * FROM t ORDER BY id"))
|
||||
if r.Error != "" || r.RowCount != 3 {
|
||||
t.Fatalf("查询结果错误: %#v", r)
|
||||
}
|
||||
|
||||
s, err := GetSession(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.mu.Lock()
|
||||
poolSize := len(s.sqlPool)
|
||||
s.mu.Unlock()
|
||||
if poolSize != 1 {
|
||||
t.Fatalf("会话池应仅 1 个句柄,实际 %d", poolSize)
|
||||
}
|
||||
|
||||
// offset 分页:跳过前 1 行取 1 行
|
||||
p := mk("SELECT * FROM t ORDER BY id")
|
||||
p.Offset = 1
|
||||
p.Limit = 1
|
||||
r2 := ExecSQL(context.Background(), p)
|
||||
if r2.Error != "" || r2.RowCount != 1 {
|
||||
t.Fatalf("offset 分页错误: %#v", r2)
|
||||
}
|
||||
if r2.Rows[0]["v"] != "b" {
|
||||
t.Fatalf("offset=1 应返回第 2 行,实际 %#v", r2.Rows[0])
|
||||
}
|
||||
if r2.Offset != 1 || !r2.Truncated {
|
||||
t.Fatalf("分页元数据错误: offset=%d truncated=%v", r2.Offset, r2.Truncated)
|
||||
}
|
||||
|
||||
// 会话路径危险确认:ConfirmDanger 必须经 ConnFor 透传
|
||||
d := mk("DROP TABLE t")
|
||||
if r := ExecSQL(context.Background(), d); !r.NeedConfirm {
|
||||
t.Fatalf("会话路径未确认应拦截: %#v", r)
|
||||
}
|
||||
d.ConfirmDanger = true
|
||||
if r := ExecSQL(context.Background(), d); r.Error != "" || r.NeedConfirm {
|
||||
t.Fatalf("会话路径确认后应放行: %#v", r)
|
||||
}
|
||||
|
||||
// 逐请求字段透传:RequestID/TimeoutMs 丢失会让取消/超时在前端(全走会话路径)静默失效
|
||||
s2, err := GetSession(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c2 := s2.ConnFor(ConnReq{Driver: "sqlite", SessionID: id, SQL: "SELECT 1", RequestID: "req-1", TimeoutMs: 5000})
|
||||
if c2.RequestID != "req-1" || c2.TimeoutMs != 5000 {
|
||||
t.Fatalf("ConnFor 应透传 RequestID/TimeoutMs: %#v", c2)
|
||||
}
|
||||
}
|
||||
|
||||
// 危险语句二段流:未确认拦截,确认后放行。
|
||||
func TestExecSQLDangerConfirm(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "danger.db")
|
||||
touchFile(t, dbFile)
|
||||
c := ConnReq{Driver: "sqlite", Database: dbFile}
|
||||
|
||||
r := ExecSQL(context.Background(), ConnReq{Driver: "sqlite", Database: dbFile, SQL: "CREATE TABLE t (id int)"})
|
||||
if r.Error != "" {
|
||||
t.Fatal(r.Error)
|
||||
}
|
||||
// 未确认:DROP 不执行
|
||||
r = ExecSQL(context.Background(), ConnReq{Driver: "sqlite", Database: dbFile, SQL: "DROP TABLE t"})
|
||||
if !r.NeedConfirm {
|
||||
t.Fatalf("应 needConfirm: %#v", r)
|
||||
}
|
||||
if r.Error == "" {
|
||||
t.Fatal("应返回命中原因")
|
||||
}
|
||||
// 表仍在
|
||||
r = ExecSQL(context.Background(), ConnReq{Driver: "sqlite", Database: dbFile, SQL: "SELECT count(*) AS n FROM t"})
|
||||
if r.Error != "" {
|
||||
t.Fatalf("DROP 不应已执行: %s", r.Error)
|
||||
}
|
||||
// 确认后放行
|
||||
c.SQL = "DROP TABLE t"
|
||||
c.ConfirmDanger = true
|
||||
r = ExecSQL(context.Background(), c)
|
||||
if r.Error != "" || r.NeedConfirm {
|
||||
t.Fatalf("确认后应放行: %#v", r)
|
||||
}
|
||||
// 表已删
|
||||
r = ExecSQL(context.Background(), ConnReq{Driver: "sqlite", Database: dbFile, SQL: "SELECT count(*) FROM t"})
|
||||
if r.Error == "" {
|
||||
t.Fatal("确认后 DROP 应已生效")
|
||||
}
|
||||
}
|
||||
|
||||
// bigint 精度:超 2^53 的 int64 以字符串无损返回。
|
||||
func TestExecSQLBigint(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "bigint.db")
|
||||
touchFile(t, dbFile)
|
||||
mk := func(sqlText string) ConnReq {
|
||||
return ConnReq{Driver: "sqlite", Database: dbFile, SQL: sqlText}
|
||||
}
|
||||
if r := ExecSQL(context.Background(), mk("CREATE TABLE b (id INTEGER PRIMARY KEY)")); r.Error != "" {
|
||||
t.Fatal(r.Error)
|
||||
}
|
||||
if r := ExecSQL(context.Background(), mk("INSERT INTO b (id) VALUES (9223372036854775807)")); r.Error != "" {
|
||||
t.Fatal(r.Error)
|
||||
}
|
||||
r := ExecSQL(context.Background(), mk("SELECT id FROM b"))
|
||||
if r.Error != "" {
|
||||
t.Fatal(r.Error)
|
||||
}
|
||||
if r.Rows[0]["id"] != "9223372036854775807" {
|
||||
t.Fatalf("bigint 应字符串保真: %#v", r.Rows[0]["id"])
|
||||
}
|
||||
}
|
||||
|
||||
// EXPLAIN:sqlite 行列计划非空。
|
||||
func TestExplainSQLite(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "explain.db")
|
||||
touchFile(t, dbFile)
|
||||
c := ConnReq{Driver: "sqlite", Database: dbFile, SQL: "CREATE TABLE e (id int)"}
|
||||
if r := ExecSQL(context.Background(), c); r.Error != "" {
|
||||
t.Fatal(r.Error)
|
||||
}
|
||||
r := ExplainSQL(context.Background(), ConnReq{Driver: "sqlite", Database: dbFile, SQL: "SELECT * FROM e"})
|
||||
if r.Error != "" {
|
||||
t.Fatal(r.Error)
|
||||
}
|
||||
if len(r.Rows) == 0 {
|
||||
t.Fatal("sqlite EXPLAIN QUERY PLAN 应返回行列计划")
|
||||
}
|
||||
}
|
||||
|
||||
// SQLite 打开不存在文件应报错而非静默建空库。
|
||||
func TestSQLiteMissingFile(t *testing.T) {
|
||||
_, err := Open(ConnReq{Driver: "sqlite", Database: filepath.Join(t.TempDir(), "not-exist", "x.db")})
|
||||
if err == nil {
|
||||
t.Fatal("不存在的 sqlite 路径应报错")
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,17 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
// openSQLite 打开 SQLite 库文件;文件不存在时报错而非静默建空库。
|
||||
// DSN 追加 busy_timeout / foreign_keys pragma。
|
||||
func openSQLite(c ConnReq) (*sql.DB, error) {
|
||||
path := c.Database
|
||||
if path == "" {
|
||||
@@ -15,15 +20,37 @@ func openSQLite(c ConnReq) (*sql.DB, error) {
|
||||
if path == "" {
|
||||
return nil, fmt.Errorf("SQLite 需要 database 填文件路径")
|
||||
}
|
||||
return sql.Open("sqlite", path)
|
||||
dsn := path
|
||||
switch {
|
||||
case path == ":memory:":
|
||||
// 统一走 file: URI 以便携带 _pragma 参数
|
||||
dsn = "file::memory:"
|
||||
case strings.HasPrefix(path, "file:"):
|
||||
// 调用方自带 URI 参数,不再做存在性检查
|
||||
default:
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return nil, fmt.Errorf("SQLite 文件不存在: %s", path)
|
||||
}
|
||||
dsn = "file:" + path
|
||||
}
|
||||
sep := "?"
|
||||
if strings.Contains(dsn, "?") {
|
||||
sep = "&"
|
||||
}
|
||||
dsn += sep + "_pragma=busy_timeout(5000)&_pragma=foreign_keys(1)"
|
||||
return sql.Open("sqlite", dsn)
|
||||
}
|
||||
|
||||
func quoteSQLiteIdent(s string) string {
|
||||
return `"` + strings.ReplaceAll(s, `"`, `""`) + `"`
|
||||
}
|
||||
|
||||
func listDatabasesSQLite(_ *sql.DB) ([]string, error) {
|
||||
return []string{"main"}, nil
|
||||
}
|
||||
|
||||
func listTablesSQLite(db *sql.DB, _ string) ([]string, error) {
|
||||
rows, err := db.Query(`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY 1`)
|
||||
func listTablesSQLite(ctx context.Context, db *sql.DB, _ string) ([]string, error) {
|
||||
rows, err := db.QueryContext(ctx, `SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY 1`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -39,8 +66,8 @@ func listTablesSQLite(db *sql.DB, _ string) ([]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func listColumnsSQLite(dbh *sql.DB, _, table string) ([]ColumnInfo, error) {
|
||||
rows, err := dbh.Query(`PRAGMA table_info(` + quotePGIdent(table) + `)`)
|
||||
func listColumnsSQLite(ctx context.Context, dbh *sql.DB, _, table string) ([]ColumnInfo, error) {
|
||||
rows, err := dbh.QueryContext(ctx, `PRAGMA table_info(`+quoteSQLiteIdent(table)+`)`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
package db
|
||||
|
||||
import "strings"
|
||||
|
||||
// SplitStatements 按分号切分 SQL 为多条语句,忽略单/双引号字符串与
|
||||
// 行注释(--、#)、块注释(/* */)内的分号。语义对齐前端 sql.ts splitStatements。
|
||||
// 返回的语句已去除首尾空白,空语句被丢弃。
|
||||
func SplitStatements(sql string) []string {
|
||||
var out []string
|
||||
var cur strings.Builder
|
||||
i := 0
|
||||
n := len(sql)
|
||||
inS, inD, inLine, inBlock := false, false, false, false
|
||||
flush := func() {
|
||||
if s := strings.TrimSpace(cur.String()); s != "" {
|
||||
out = append(out, s)
|
||||
}
|
||||
cur.Reset()
|
||||
}
|
||||
for i < n {
|
||||
ch := sql[i]
|
||||
var nxt byte
|
||||
if i+1 < n {
|
||||
nxt = sql[i+1]
|
||||
}
|
||||
switch {
|
||||
case inLine:
|
||||
cur.WriteByte(ch)
|
||||
if ch == '\n' {
|
||||
inLine = false
|
||||
}
|
||||
i++
|
||||
case inBlock:
|
||||
cur.WriteByte(ch)
|
||||
if ch == '*' && nxt == '/' {
|
||||
cur.WriteByte(nxt)
|
||||
inBlock = false
|
||||
i += 2
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
case !inS && !inD && ch == '-' && nxt == '-':
|
||||
inLine = true
|
||||
cur.WriteString("--")
|
||||
i += 2
|
||||
case !inS && !inD && ch == '#':
|
||||
inLine = true
|
||||
cur.WriteByte(ch)
|
||||
i++
|
||||
case !inS && !inD && ch == '/' && nxt == '*':
|
||||
inBlock = true
|
||||
cur.WriteString("/*")
|
||||
i += 2
|
||||
case ch == '\'' && !inD:
|
||||
inS = !inS
|
||||
cur.WriteByte(ch)
|
||||
i++
|
||||
case ch == '"' && !inS:
|
||||
inD = !inD
|
||||
cur.WriteByte(ch)
|
||||
i++
|
||||
case ch == ';' && !inS && !inD:
|
||||
flush()
|
||||
i++
|
||||
default:
|
||||
cur.WriteByte(ch)
|
||||
i++
|
||||
}
|
||||
}
|
||||
flush()
|
||||
return out
|
||||
}
|
||||
|
||||
// StripComments 剥除 --、#、/* */ 注释,并把字符串字面量替换为 ” 占位,
|
||||
// 便于在纯代码文本上做危险关键字判定。
|
||||
// MySQL 可执行注释 /*! ... */ 的内容会被保留参与检查(去掉包裹的 /*! 与 */)。
|
||||
func StripComments(sql string) string {
|
||||
var b strings.Builder
|
||||
b.Grow(len(sql))
|
||||
i := 0
|
||||
n := len(sql)
|
||||
for i < n {
|
||||
ch := sql[i]
|
||||
var nxt byte
|
||||
if i+1 < n {
|
||||
nxt = sql[i+1]
|
||||
}
|
||||
switch {
|
||||
// 行注释 -- 与 #
|
||||
case ch == '-' && nxt == '-':
|
||||
for i < n && sql[i] != '\n' {
|
||||
i++
|
||||
}
|
||||
b.WriteByte(' ')
|
||||
case ch == '#':
|
||||
for i < n && sql[i] != '\n' {
|
||||
i++
|
||||
}
|
||||
b.WriteByte(' ')
|
||||
// 块注释;/*! ... */ 为 MySQL 可执行注释,保留内容
|
||||
case ch == '/' && nxt == '*':
|
||||
exec := i+2 < n && sql[i+2] == '!'
|
||||
start := i + 2
|
||||
if exec {
|
||||
start = i + 3
|
||||
}
|
||||
j := strings.Index(sql[start:], "*/")
|
||||
if j < 0 {
|
||||
if exec {
|
||||
b.WriteString(sql[start:])
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
if exec {
|
||||
b.WriteByte(' ')
|
||||
b.WriteString(sql[start : start+j])
|
||||
b.WriteByte(' ')
|
||||
} else {
|
||||
b.WriteByte(' ')
|
||||
}
|
||||
i = start + j + 2
|
||||
// 单引号字面量(处理 '' 与 \' 转义)
|
||||
case ch == '\'':
|
||||
b.WriteString("''")
|
||||
i++
|
||||
for i < n {
|
||||
if sql[i] == '\'' {
|
||||
if i+1 < n && sql[i+1] == '\'' {
|
||||
i += 2
|
||||
continue
|
||||
}
|
||||
i++
|
||||
break
|
||||
}
|
||||
if sql[i] == '\\' && i+1 < n {
|
||||
i += 2
|
||||
continue
|
||||
}
|
||||
i++
|
||||
}
|
||||
// 双引号标识符/字面量同样置空内容(防 "DROP" 作为标识符误报)
|
||||
case ch == '"':
|
||||
b.WriteString(`""`)
|
||||
i++
|
||||
for i < n {
|
||||
if sql[i] == '"' {
|
||||
if i+1 < n && sql[i+1] == '"' {
|
||||
i += 2
|
||||
continue
|
||||
}
|
||||
i++
|
||||
break
|
||||
}
|
||||
i++
|
||||
}
|
||||
// 反引号标识符内容置空
|
||||
case ch == '`':
|
||||
b.WriteString("``")
|
||||
i++
|
||||
for i < n && sql[i] != '`' {
|
||||
i++
|
||||
}
|
||||
if i < n {
|
||||
i++
|
||||
}
|
||||
default:
|
||||
b.WriteByte(ch)
|
||||
i++
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSplitStatements(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
want []string
|
||||
}{
|
||||
{"SELECT 1", []string{"SELECT 1"}},
|
||||
{"SELECT 1; SELECT 2;", []string{"SELECT 1", "SELECT 2"}},
|
||||
{"SELECT ';'; SELECT 2", []string{"SELECT ';'", "SELECT 2"}},
|
||||
{"SELECT 1; -- c;mt\nSELECT 2", []string{"SELECT 1", "-- c;mt\nSELECT 2"}},
|
||||
{"/* a;b */ SELECT 1; SELECT 2", []string{"/* a;b */ SELECT 1", "SELECT 2"}},
|
||||
{`SELECT "a;b"; SELECT 2`, []string{`SELECT "a;b"`, "SELECT 2"}},
|
||||
{";;", nil},
|
||||
{"", nil},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := SplitStatements(tc.in)
|
||||
if len(got) != len(tc.want) {
|
||||
t.Fatalf("SplitStatements(%q) = %#v, want %#v", tc.in, got, tc.want)
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != tc.want[i] {
|
||||
t.Fatalf("SplitStatements(%q)[%d] = %q, want %q", tc.in, i, got[i], tc.want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripComments(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
has string // 结果应包含(大小写敏感原文)
|
||||
missing string // 结果不应包含
|
||||
}{
|
||||
{"SELECT 1 -- WHERE x", "SELECT 1", "WHERE x"},
|
||||
{"UPDATE t SET x=1 # note\nWHERE id=1", "WHERE id=1", "note"},
|
||||
{"SELECT /* hint */ 1", "SELECT", "hint"},
|
||||
{"SELECT /*!50000 DROP TABLE t*/", "DROP TABLE t", "/*!"},
|
||||
{"SELECT 'DROP TABLE x'", "SELECT", "DROP TABLE x"},
|
||||
{`SELECT "col;drop" FROM t`, "FROM t", "col;drop"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := StripComments(tc.in)
|
||||
if tc.has != "" && !strings.Contains(got, tc.has) {
|
||||
t.Fatalf("StripComments(%q) = %q, 应包含 %q", tc.in, got, tc.has)
|
||||
}
|
||||
if tc.missing != "" && strings.Contains(got, tc.missing) {
|
||||
t.Fatalf("StripComments(%q) = %q, 不应包含 %q", tc.in, got, tc.missing)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,10 +207,20 @@ func ApplySSHTunnel(c ConnReq) (ConnReq, *SSHTunnel, error) {
|
||||
if err != nil {
|
||||
return c, nil, fmt.Errorf("SSH: %w", err)
|
||||
}
|
||||
return rewriteHostForTunnel(c, tun.LocalPort), tun, nil
|
||||
}
|
||||
|
||||
// rewriteHostForTunnel 把连接改写到隧道本地端口(127.0.0.1:<localPort>)。
|
||||
// 改写会丢失真实 Host,导致隧道内 TLS ServerName 校验失败(127.0.0.1 不在证书 SAN 里),
|
||||
// 故改写前把原 Host 记入 TLSServerName(用户显式指定时保留其值)。
|
||||
func rewriteHostForTunnel(c ConnReq, localPort int) ConnReq {
|
||||
out := c
|
||||
if strings.TrimSpace(out.TLSServerName) == "" {
|
||||
out.TLSServerName = out.Host
|
||||
}
|
||||
out.Host = "127.0.0.1"
|
||||
out.Port = tun.LocalPort
|
||||
out.Port = localPort
|
||||
// Avoid nested tunnel if helpers re-enter
|
||||
out.SSHEnabled = false
|
||||
return out, tun, nil
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package db
|
||||
|
||||
import "testing"
|
||||
|
||||
// R2-B1:SSH 隧道改写 Host 后,TLS ServerName 必须保留原 Host(隧道内 TLS 校验依赖)。
|
||||
func TestRewriteHostForTunnelPreservesServerName(t *testing.T) {
|
||||
c := ConnReq{Host: "db.internal", Port: 3306, SSHEnabled: true}
|
||||
out := rewriteHostForTunnel(c, 45678)
|
||||
if out.Host != "127.0.0.1" || out.Port != 45678 {
|
||||
t.Fatalf("隧道地址改写错误: %s:%d", out.Host, out.Port)
|
||||
}
|
||||
if out.SSHEnabled {
|
||||
t.Fatal("SSHEnabled 应置 false,防嵌套隧道")
|
||||
}
|
||||
if out.TLSServerName != "db.internal" {
|
||||
t.Fatalf("TLSServerName 应保留原 Host, got %q", out.TLSServerName)
|
||||
}
|
||||
}
|
||||
|
||||
// 用户显式指定 tlsServerName 时,隧道改写不得覆盖。
|
||||
func TestRewriteHostForTunnelKeepsExplicitServerName(t *testing.T) {
|
||||
c := ConnReq{Host: "db.internal", TLSServerName: "db.example.com", SSHEnabled: true}
|
||||
out := rewriteHostForTunnel(c, 40001)
|
||||
if out.TLSServerName != "db.example.com" {
|
||||
t.Fatalf("显式 TLSServerName 应保留, got %q", out.TLSServerName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTLSServerNameFor(t *testing.T) {
|
||||
if got := tlsServerNameFor(ConnReq{Host: "db.example"}); got != "db.example" {
|
||||
t.Fatalf("空 tlsserverName 应用 Host, got %q", got)
|
||||
}
|
||||
if got := tlsServerNameFor(ConnReq{Host: "127.0.0.1", TLSServerName: "db.example"}); got != "db.example" {
|
||||
t.Fatalf("显式 tlsserverName 优先, got %q", got)
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,15 @@ func SSLEnabled(c ConnReq) bool {
|
||||
return EffectiveSSLMode(c) != "disable"
|
||||
}
|
||||
|
||||
// tlsServerNameFor 返回 TLS 校验用的 ServerName:显式 tlsServerName 优先,否则用 Host。
|
||||
// SSH 隧道把 Host 改写成 127.0.0.1 之前,ApplySSHTunnel 会把原 Host 存入 TLSServerName。
|
||||
func tlsServerNameFor(c ConnReq) string {
|
||||
if v := strings.TrimSpace(c.TLSServerName); v != "" {
|
||||
return v
|
||||
}
|
||||
return strings.TrimSpace(c.Host)
|
||||
}
|
||||
|
||||
// tlsConfigFrom builds a *tls.Config from ConnReq paths/mode.
|
||||
// Returns (nil, nil) when SSL is disabled.
|
||||
func tlsConfigFrom(c ConnReq) (*tls.Config, error) {
|
||||
@@ -41,7 +50,7 @@ func tlsConfigFrom(c ConnReq) (*tls.Config, error) {
|
||||
cfg := &tls.Config{
|
||||
MinVersion: tls.VersionTLS12,
|
||||
}
|
||||
if host := strings.TrimSpace(c.Host); host != "" {
|
||||
if host := tlsServerNameFor(c); host != "" {
|
||||
cfg.ServerName = host
|
||||
}
|
||||
|
||||
@@ -56,14 +65,26 @@ func tlsConfigFrom(c ConnReq) (*tls.Config, error) {
|
||||
return nil, fmt.Errorf("未知 SSL 模式: %s", mode)
|
||||
}
|
||||
|
||||
if path := strings.TrimSpace(c.SSLCA); path != "" {
|
||||
pem, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取 CA 证书失败 (%s): %w", path, err)
|
||||
caContent := strings.TrimSpace(c.SSLCAContent)
|
||||
caPath := strings.TrimSpace(c.SSLCA)
|
||||
if caContent != "" || caPath != "" {
|
||||
// 内容优先于路径(免在服务端落盘/共享文件)
|
||||
var pem []byte
|
||||
if caContent != "" {
|
||||
pem = []byte(caContent)
|
||||
} else {
|
||||
var err error
|
||||
pem, err = os.ReadFile(caPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取 CA 证书失败 (%s): %w", caPath, err)
|
||||
}
|
||||
}
|
||||
pool := x509.NewCertPool()
|
||||
if !pool.AppendCertsFromPEM(pem) {
|
||||
return nil, fmt.Errorf("CA 证书不是有效 PEM: %s", path)
|
||||
if caContent != "" {
|
||||
return nil, fmt.Errorf("CA 证书内容不是有效 PEM")
|
||||
}
|
||||
return nil, fmt.Errorf("CA 证书不是有效 PEM: %s", caPath)
|
||||
}
|
||||
cfg.RootCAs = pool
|
||||
if mode == "verify-ca" {
|
||||
@@ -98,9 +119,21 @@ func tlsConfigFrom(c ConnReq) (*tls.Config, error) {
|
||||
return nil, fmt.Errorf("verify-ca 模式需要填写 CA 证书路径")
|
||||
}
|
||||
|
||||
certContent := strings.TrimSpace(c.SSLCertContent)
|
||||
keyContent := strings.TrimSpace(c.SSLKeyContent)
|
||||
certPath := strings.TrimSpace(c.SSLCert)
|
||||
keyPath := strings.TrimSpace(c.SSLKey)
|
||||
if certPath != "" || keyPath != "" {
|
||||
if certContent != "" || keyContent != "" {
|
||||
// 内容与路径二选一,各自成对:证书与密钥必须同时给
|
||||
if certContent == "" || keyContent == "" {
|
||||
return nil, fmt.Errorf("客户端证书与密钥需同时填写")
|
||||
}
|
||||
cert, err := tls.X509KeyPair([]byte(certContent), []byte(keyContent))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("加载客户端证书内容失败: %w", err)
|
||||
}
|
||||
cfg.Certificates = []tls.Certificate{cert}
|
||||
} else if certPath != "" || keyPath != "" {
|
||||
if certPath == "" || keyPath == "" {
|
||||
return nil, fmt.Errorf("客户端证书与密钥需同时填写")
|
||||
}
|
||||
|
||||
@@ -84,6 +84,78 @@ func TestTLSConfigFromBadCAPath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// R2-B1:显式 tlsServerName 优先于 Host(tlsConfigFrom 层)。
|
||||
func TestTLSConfigFromTLSServerName(t *testing.T) {
|
||||
cfg, err := tlsConfigFrom(ConnReq{SSLMode: "require", Host: "127.0.0.1", TLSServerName: "db.example.com"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg == nil || cfg.ServerName != "db.example.com" {
|
||||
t.Fatalf("ServerName 应取 tlsServerName, got %#v", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
// R2-B2:CA/客户端证书内容优先于路径;成对校验仍然生效。
|
||||
func TestTLSConfigFromContentCerts(t *testing.T) {
|
||||
caPEM, certPEM, keyPEM := genTestCertMaterials(t)
|
||||
|
||||
cfg, err := tlsConfigFrom(ConnReq{
|
||||
SSLMode: "require", Host: "db.example.com",
|
||||
SSLCAContent: caPEM, SSLCertContent: certPEM, SSLKeyContent: keyPEM,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg == nil || cfg.RootCAs == nil {
|
||||
t.Fatal("内容 CA 应构建 RootCAs")
|
||||
}
|
||||
if len(cfg.Certificates) != 1 {
|
||||
t.Fatal("内容客户端证书应加载")
|
||||
}
|
||||
if cfg.ServerName != "db.example.com" {
|
||||
t.Fatalf("ServerName 错误: %q", cfg.ServerName)
|
||||
}
|
||||
|
||||
// 内容 CA 非法 PEM 应报错(不回退路径)
|
||||
if _, err := tlsConfigFrom(ConnReq{SSLMode: "require", SSLCAContent: "not-a-pem"}); err == nil {
|
||||
t.Fatal("非法 CA 内容应报错")
|
||||
}
|
||||
// 证书与密钥仍需成对:只给内容证书不给内容密钥
|
||||
if _, err := tlsConfigFrom(ConnReq{SSLMode: "require", SSLCertContent: certPEM}); err == nil {
|
||||
t.Fatal("半对内容证书应报错")
|
||||
}
|
||||
}
|
||||
|
||||
// genTestCertMaterials 生成自签名 CA 与客户端证书 PEM 串。
|
||||
func genTestCertMaterials(t *testing.T) (caPEM, certPEM, keyPEM string) {
|
||||
t.Helper()
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
keyDER, err := x509.MarshalECPrivateKey(key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
keyPEM = string(pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}))
|
||||
|
||||
tmpl := &x509.Certificate{
|
||||
SerialNumber: big.NewInt(2),
|
||||
Subject: pkix.Name{CommonName: "opsxdb-test-cert"},
|
||||
NotBefore: time.Now().Add(-time.Hour),
|
||||
NotAfter: time.Now().Add(24 * time.Hour),
|
||||
KeyUsage: x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
||||
BasicConstraintsValid: true,
|
||||
}
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
certPEM = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}))
|
||||
return certPEM, certPEM, keyPEM // 自签名,CA 与客户端证书同一张
|
||||
}
|
||||
|
||||
func writeTestCA(path string) error {
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package db
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"encoding/hex"
|
||||
"strconv"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const DefaultRowLimit = 1000
|
||||
const MaxRowLimit = 10000
|
||||
@@ -20,11 +25,31 @@ type ConnReq struct {
|
||||
SSLCA string `json:"sslCA"`
|
||||
SSLCert string `json:"sslCert"`
|
||||
SSLKey string `json:"sslKey"`
|
||||
// 证书 PEM 内容,优先于上面的文件路径(客户端与服务端分离部署时免共享文件)
|
||||
SSLCAContent string `json:"sslCAContent"`
|
||||
SSLCertContent string `json:"sslCertContent"`
|
||||
SSLKeyContent string `json:"sslKeyContent"`
|
||||
// TLSServerName 显式指定 TLS 校验用的 ServerName;SSH 隧道改写 Host 前
|
||||
// 会把原 Host 自动填入,避免隧道内 TLS 校验失败。
|
||||
TLSServerName string `json:"tlsServerName"`
|
||||
|
||||
SessionID string `json:"sessionId"`
|
||||
Table string `json:"table"`
|
||||
Limit int `json:"limit"`
|
||||
Key string `json:"key"` // Redis key (get/delete)
|
||||
Offset int `json:"offset"` // 结果分页:跳过前 offset 行再按 limit 收集
|
||||
Key string `json:"key"` // Redis key (get/delete)
|
||||
Pattern string `json:"pattern"` // Redis SCAN match
|
||||
Cursor int64 `json:"cursor"` // Redis 值分页游标(hash/set=SCAN 游标;list/zset=偏移)
|
||||
Keyword string `json:"keyword"` // 对象搜索关键词
|
||||
Format string `json:"format"` // 导出格式: csv | json | insert
|
||||
|
||||
// TimeoutMs > 0 时 /db/exec、/db/explain 以此为语句超时
|
||||
TimeoutMs int `json:"timeoutMs"`
|
||||
// RequestID 非空时注册 in-flight 注册表,可通过 /api/db/cancel 取消
|
||||
RequestID string `json:"requestId"`
|
||||
|
||||
// ConfirmDanger 为 true 表示前端已完成危险语句确认,服务端放行本次执行。
|
||||
ConfirmDanger bool `json:"confirmDanger"`
|
||||
|
||||
SSHEnabled bool `json:"sshEnabled"`
|
||||
SSHHost string `json:"sshHost"`
|
||||
@@ -34,6 +59,15 @@ type ConnReq struct {
|
||||
SSHPassword string `json:"sshPassword"`
|
||||
SSHPrivateKey string `json:"sshPrivateKey"`
|
||||
SSHPassphrase string `json:"sshPassphrase"`
|
||||
|
||||
// Redis 拓扑:standalone(默认)| sentinel | cluster
|
||||
RedisMode string `json:"redisMode"`
|
||||
// Sentinel/Cluster 节点列表,host:port 多行或逗号分隔
|
||||
RedisNodes string `json:"redisNodes"`
|
||||
// Sentinel 主节点名(必填)
|
||||
MasterName string `json:"masterName"`
|
||||
// Sentinel 自身密码(可空);Redis 密码仍用 Password
|
||||
SentinelPassword string `json:"sentinelPassword"`
|
||||
}
|
||||
|
||||
type ColumnInfo struct {
|
||||
@@ -54,14 +88,38 @@ type ExecResult struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
Truncated bool `json:"truncated,omitempty"`
|
||||
Limit int `json:"limit,omitempty"`
|
||||
Offset int `json:"offset,omitempty"`
|
||||
// NeedConfirm 为 true 表示命中危险语句,需前端确认后带 confirmDanger 重发。
|
||||
NeedConfirm bool `json:"needConfirm,omitempty"`
|
||||
// Redis 值游标分页:>0 还有更多(HGETALL/SMEMBERS/ZRANGE/LRANGE 单键读透传)
|
||||
NextCursor int64 `json:"nextCursor,omitempty"`
|
||||
}
|
||||
|
||||
// ExplainResult 执行计划结果:行列(计划表格)与 Text(XML/文本计划)二选一或兼有。
|
||||
type ExplainResult struct {
|
||||
Columns []string `json:"columns,omitempty"`
|
||||
Rows []map[string]interface{} `json:"rows,omitempty"`
|
||||
Text string `json:"text,omitempty"` // MSSQL SHOWPLAN_XML / Oracle 文本计划
|
||||
TimeMs int64 `json:"timeMs"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func normalize(v interface{}) interface{} {
|
||||
switch t := v.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case int64:
|
||||
// bigint 超 JS 安全整数时转字符串保真,避免前端精度丢失错行
|
||||
if t > 9007199254740991 || t < -9007199254740991 {
|
||||
return strconv.FormatInt(t, 10)
|
||||
}
|
||||
return t
|
||||
case []byte:
|
||||
return string(t)
|
||||
if utf8.Valid(t) {
|
||||
return string(t)
|
||||
}
|
||||
// BLOB 等非 UTF-8 数据以十六进制标注返回,替代乱码
|
||||
return `\x` + hex.EncodeToString(t)
|
||||
case time.Time:
|
||||
return t.Format(time.RFC3339Nano)
|
||||
default:
|
||||
@@ -94,5 +152,6 @@ type RedisKeyValue struct {
|
||||
TimeMs int64 `json:"timeMs"`
|
||||
Truncated bool `json:"truncated,omitempty"`
|
||||
Limit int `json:"limit,omitempty"`
|
||||
NextCursor int64 `json:"nextCursor"` // >0 表示还有更多(续载时回传 cursor)
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeBigint(t *testing.T) {
|
||||
// 超 JS 安全整数 → 字符串保真
|
||||
if got := normalize(int64(9223372036854775807)); got != "9223372036854775807" {
|
||||
t.Fatalf("bigint max: %#v", got)
|
||||
}
|
||||
if got := normalize(int64(-9223372036854775807)); got != "-9223372036854775807" {
|
||||
t.Fatalf("bigint min: %#v", got)
|
||||
}
|
||||
// 安全范围内保持 number
|
||||
if got := normalize(int64(9007199254740991)); got != int64(9007199254740991) {
|
||||
t.Fatalf("safe int: %#v", got)
|
||||
}
|
||||
if got := normalize(int64(9007199254740992)); got != "9007199254740992" {
|
||||
t.Fatalf("边界+1 应转字符串: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeBytes(t *testing.T) {
|
||||
if got := normalize([]byte("hello")); got != "hello" {
|
||||
t.Fatalf("utf8 bytes: %#v", got)
|
||||
}
|
||||
if got := normalize([]byte("中文")); got != "中文" {
|
||||
t.Fatalf("utf8 中文: %#v", got)
|
||||
}
|
||||
// 非 UTF-8 → \x 十六进制
|
||||
if got := normalize([]byte{0xff, 0x00, 0x01}); got != `\xff0001` {
|
||||
t.Fatalf("blob hex: %#v", got)
|
||||
}
|
||||
if got := normalize(nil); got != nil {
|
||||
t.Fatalf("nil: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMssqlDSNEncoding(t *testing.T) {
|
||||
// 含空格 / '+' 的密码应经 userinfo 编码,不被 QueryEscape 破坏
|
||||
dsn, err := mssqlDSN(ConnReq{Driver: "sqlserver", Host: "h1", Port: 1433, User: "sa", Password: "p@ ss+word", Database: "db1"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "p%40%20ss+word"
|
||||
if !strings.Contains(dsn, want) {
|
||||
t.Fatalf("DSN 密码编码不符合预期: %s(应含 %s)", dsn, want)
|
||||
}
|
||||
if !strings.Contains(dsn, "sqlserver://sa:") || !strings.Contains(dsn, "@h1:1433") {
|
||||
t.Fatalf("DSN 结构错误: %s", dsn)
|
||||
}
|
||||
if !strings.Contains(dsn, "database=db1") {
|
||||
t.Fatalf("DSN 缺 database: %s", dsn)
|
||||
}
|
||||
// verify-ca 无 CA 应报错
|
||||
if _, err := mssqlDSN(ConnReq{Driver: "sqlserver", Host: "h1", SSLMode: "verify-ca"}); err == nil {
|
||||
t.Fatal("verify-ca 缺 CA 应报错")
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ OpsxDB 是一款**本地优先**的桌面数据库工作台,面向运维与开
|
||||
| 安全通道 | 每连接独立配置 SSL/TLS 与 SSH 隧道;密码本机混淆存储 |
|
||||
| AI 面板 | 生成 / 解释 / 优化 / 修复 / 方言转换 / 样例 SQL |
|
||||
| 危险操作防护 | DROP / TRUNCATE / 无 WHERE 的 UPDATE·DELETE 执行前确认 |
|
||||
| 双形态 | 桌面 GUI(Tauri 2)与 Web(单二进制 + 静态页)共用一套界面 |
|
||||
| 桌面唯一形态 | 桌面 GUI(Tauri 2),本地后端随应用自动拉起 |
|
||||
|
||||
## 支持的数据库
|
||||
|
||||
@@ -33,7 +33,7 @@ OpsxDB 是一款**本地优先**的桌面数据库工作台,面向运维与开
|
||||
## 为什么选 OpsxDB
|
||||
|
||||
- **数据主权**:连接配置与查询历史只存在本机;后端默认仅监听 127.0.0.1。
|
||||
- **开箱即用**:`./run.sh web` 一条命令起 Web 形态;桌面版双击即用,后端随应用拉起。
|
||||
- **开箱即用**:桌面版双击即用,本地后端随应用自动拉起;官网与文档也由它内置提供。
|
||||
- **为快设计**:点表即看数据、⌘Enter 即执行、结果三键取走(复制 / CSV / INSERT)。
|
||||
- **轻量克制**:一个 Go 二进制 + 一份前端,内网可部署,无遥测无账号。
|
||||
|
||||
|
||||
@@ -4,17 +4,18 @@
|
||||
|
||||
桌面版(主交付形态)双击 OpsxDB 应用即可,本地后端随应用自动拉起。
|
||||
|
||||
从源码跑 Web / 桌面形态:
|
||||
从源码运行(开发):
|
||||
|
||||
```bash
|
||||
git clone <repo> opsxdb && cd opsxdb
|
||||
npm install
|
||||
|
||||
./run.sh web # Web 形态:Gin :8788 + Vite 开发壳 :5174
|
||||
./run.sh gui # 桌面形态:Tauri 2 窗口(复用同一套界面)
|
||||
./run.sh gui # 桌面形态(开发):Tauri 窗口 + 本地后端
|
||||
./run.sh web # 仅启动官网与文档 → http://127.0.0.1:8788/
|
||||
./run.sh package # 打桌面安装包(DMG,内嵌后端)
|
||||
```
|
||||
|
||||
> **注意**:浏览器里的 `:5174` 是 Vite 热更新开发壳,仅调试用;产品页面(官网 `/`、文档 `/docs`)由后端 `:8788` 直接提供。
|
||||
> **注意**:客户端只在桌面 App 里运行。`./run.sh gui` 起的 `:5174` 是桌面 devUrl 的资产服务,不是浏览器客户端;浏览器只用于官网 `/` 与文档 `/docs`(后端 `:8788` 直接提供)。
|
||||
|
||||
## 2. 新建连接
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ OpsxDB 的界面为「高频操作」设计:从打开应用到看到数据,
|
||||
|
||||
- 点**表名**:直接打开前 100 行数据;
|
||||
- 展开**表**:查看列结构;
|
||||
- Redis:按键分隔符编文件夹,点键打开类型化详情(TTL / JSON / 增删字段);
|
||||
- 库列表懒加载,大实例也不卡。
|
||||
|
||||
## 编辑器与结果
|
||||
|
||||
@@ -20,7 +20,7 @@ postgres://opsx:pass@127.0.0.1:5432/app
|
||||
| MySQL / PostgreSQL / SQL Server | standard | host · port · user · password · database · ssl · ssh |
|
||||
| Oracle | oracle | host · port · user · password · **service**(如 ORCL) |
|
||||
| SQLite | file | database 填本地文件路径(如 `~/data/demo.sqlite`) |
|
||||
| Redis | redis | host · port · password · database 序号 |
|
||||
| Redis | redis | host · port · password · database 序号 · **模式**(单机 / 哨兵 / 集群)· 键分隔符 |
|
||||
|
||||
表单只收集服务端实际消费的字段;历史遗留的死参数不会出现在表单里。
|
||||
|
||||
@@ -39,4 +39,12 @@ postgres://opsx:pass@127.0.0.1:5432/app
|
||||
|
||||
数据库在跳板机后面时,开启 **ssh** 字段组,填写跳板机地址与凭据:OpsxDB 会先建 SSH 隧道,再把数据库连接走隧道转发,无需把库端口暴露到公网。
|
||||
|
||||
## Redis 哨兵与集群
|
||||
|
||||
- **哨兵**:模式选 Sentinel,填写 Sentinel 节点列表(`host:port`,多行)和主节点名 `masterName`;
|
||||
- **集群**:模式选 Cluster,填写至少一个种子节点。对象树只显示 DB 0(Cluster 无 `SELECT`);
|
||||
- 单机模式只填 host/port,与原先一致。
|
||||
|
||||
字段细节与键浏览见 [Redis 工作台](redis)。
|
||||
|
||||
> **提示**:连接保存后,后续查询仅携带 `sessionId`,账密不会随每次请求上送;密码在本机混淆存储。
|
||||
@@ -4,6 +4,7 @@ SQL 工作台是日常使用率最高的界面,所有交互围绕「快」设
|
||||
|
||||
- **⌘/Ctrl + Enter**:执行光标所在的**当前语句**;有选区时执行**选区**;
|
||||
- 不是「整页脚本」模式 —— 多条语句互不干扰,想跑哪句把光标放哪;
|
||||
- Redis 连接共用同一编辑器,按**行**执行命令(见 [Redis 工作台](redis)),不另开 CLI;
|
||||
- 执行在会话上运行,连接断开后自动提示。
|
||||
|
||||
## 多 Tab 与分栏
|
||||
|
||||
@@ -1,34 +1,93 @@
|
||||
Redis 连接使用专用表单(redis),连上后既是命令行又是键空间浏览器。
|
||||
Redis 连接使用专用表单(redis)。连上后,对象树是键空间浏览器,编辑器是 Redis 命令行 —— **和 SQL 共用同一套输入框**,不另开 CLI 页。
|
||||
|
||||
## 为什么命令行不单独做一套
|
||||
|
||||
SQL 工作台已经有:多 Tab、⌘/Ctrl+Enter、查询历史、书签、危险确认、取消。Redis 命令同样是「输入 → 执行 → 结果表」。
|
||||
|
||||
拆成独立 CLI(终端 REPL)会让快捷键、历史、危险确认分叉,也和 Navicat 式多引擎工作台不一致。
|
||||
|
||||
因此:
|
||||
|
||||
| | SQL | Redis |
|
||||
| --- | --- | --- |
|
||||
| 输入框 | 同一 CodeMirror | 同一 CodeMirror(占位/补全换成命令) |
|
||||
| 切分 | 分号语句 | **按行**(`SET k "a;b"` 不会被分号拆开) |
|
||||
| 执行 | ⌘/Ctrl+Enter 当前语句或选区 | ⌘/Ctrl+Enter 当前行或选区 |
|
||||
| 结果 | 结果表 | 命令结果表;从对象树打开键则进入键详情 |
|
||||
|
||||
Another Redis Desktop Manager 的独立 CLI Tab、opsxsql 的单行命令 + 输出日志,列为后续 P1,不是现在的主路径。
|
||||
|
||||
## 键空间浏览
|
||||
|
||||
对象树对 Redis 显示 DB0–15(Cluster 仅 0)。展开库后:
|
||||
|
||||
- **SCAN 过滤**:输入 `user:*` 点「应用」;生产大库请带前缀,避免扫全库。最多展示 500 个键,超出提示截断。
|
||||
- **按分隔符编文件夹**:连接表单的「键分隔符」默认 `:`,`CEX:FOLLOW:FOLLOWERS:1` 显示为 `CEX / FOLLOW / FOLLOWERS / 1`。文件夹显示数量;右键可「只看此前缀」「在此新建键」。
|
||||
- **类型色点**:string 绿 / hash 蓝 / list 橙 / set 紫 / zset 青。
|
||||
- **点键**:打开键详情(不是假 SELECT)。
|
||||
|
||||
## 键详情
|
||||
|
||||
对标 Another Redis Desktop Manager 的 Hash/List/Set 视图:
|
||||
|
||||
- 类型徽章、键名(回车重命名)、TTL(回车或「保存 TTL」,`-1` 永久)、刷新、导出还原命令、删除;
|
||||
- **结构**表:Hash 为 ID / Field / Value,JSON 值自动美化;可新增字段、编辑、删除;
|
||||
- **JSON** 视图:整体格式化;
|
||||
- 表内关键字过滤。
|
||||
|
||||
String 可直接改值保存。
|
||||
|
||||
## 命令执行
|
||||
|
||||
SQL 编辑器同样适用于 Redis —— 直接输入命令,⌘/Ctrl + Enter 执行:
|
||||
编辑器直接写 Redis 命令:
|
||||
|
||||
```bash
|
||||
PING
|
||||
SET demo:key "hello" EX 60
|
||||
GET demo:key
|
||||
KEYS demo:*
|
||||
DEL demo:key
|
||||
HGETALL demo:hash
|
||||
SCAN 0 MATCH demo:* COUNT 100
|
||||
INFO memory
|
||||
```
|
||||
|
||||
- 返回值直接显示在结果面板;
|
||||
- 集群 / 哨兵模式按各自连接方式配置(哨兵填写主节点名)。
|
||||
- 未点名的命令走通用 `Do`(`EXISTS` / `ECHO` / `INFO` / `CLIENT LIST` 等不必再加白名单);
|
||||
- `FLUSHALL` / `FLUSHDB` / `KEYS` / `CONFIG` 仍要二段确认;
|
||||
- `KEYS *` 在大库上会阻塞 —— 请用 `SCAN MATCH`。
|
||||
|
||||
## 键空间浏览
|
||||
### 切换逻辑库(SELECT)
|
||||
|
||||
- **KEYS 扫描**:按模式浏览键(生产大库建议用前缀收窄范围);
|
||||
- 键类型视图:string / hash / list / set / zset 分类型展示;
|
||||
- **TTL 管理**:查看与设置过期时间,常用于缓存排障;
|
||||
- 批量操作:选中多个键批量删除。
|
||||
`SELECT 1` 是 Redis 官方的切库命令,编辑器直接支持 —— 语义与 redis-cli 一致:**切换后本 Tab 的后续命令都在目标库执行**:
|
||||
|
||||
> **注意**:`KEYS *` 在大库上会阻塞 Redis,OpsxDB 不做强制拦截 —— 生产环境请自觉使用带前缀的模式。
|
||||
```bash
|
||||
SELECT 1
|
||||
GET db1:only:key # 在 db1 执行
|
||||
```
|
||||
|
||||
- 没有也没有 `USE`(那是 SQL 语法)—— 输入 `USE db1` 会得到 Redis 原生报错
|
||||
`ERR unknown command 'USE'`,与 redis-cli 行为一致;
|
||||
- 切库只影响当前查询 Tab(多 Tab 可各连各的库),左侧对象树点开对应库可浏览键;
|
||||
- Cluster 模式只有 DB 0,`SELECT` 无效。
|
||||
|
||||
## 连接参数
|
||||
|
||||
| 字段 | 说明 |
|
||||
| --- | --- |
|
||||
| host / port | 默认 127.0.0.1:6379 |
|
||||
| password | ACL / requirepass |
|
||||
| database | 逻辑库序号(0-15 常见) |
|
||||
| ssl | 红色连接(TLS)开关 |
|
||||
| host / port | 单机默认 127.0.0.1:6379 |
|
||||
| password / user | ACL / requirepass |
|
||||
| database | 逻辑库序号(0–15 常见;Cluster 忽略) |
|
||||
| 键分隔符 | 对象树文件夹分隔,默认 `:` |
|
||||
| ssl | TLS |
|
||||
| redisMode | `standalone`(默认)/ `sentinel` / `cluster` |
|
||||
| redisNodes | Sentinel / Cluster 节点列表,`host:port` 多行或逗号分隔 |
|
||||
| masterName | Sentinel 主节点名(必填) |
|
||||
| sentinelPassword | Sentinel 密码(可空) |
|
||||
|
||||
### Sentinel
|
||||
|
||||
模式选「哨兵」,填写至少一个 Sentinel 地址和 `masterName`(如 `mymaster`)。逻辑库序号仍可用。
|
||||
|
||||
### Cluster
|
||||
|
||||
模式选「集群」,填写至少一个种子节点。Cluster 不支持 `SELECT` 多库,对象树只暴露 DB 0。槽位路由由客户端处理,键浏览走 `SCAN`。
|
||||
|
||||
> **环境**:Sentinel / Cluster 表单与后端已接好;真实拓扑联调见 `docs/BACKLOG.md` 环境缺口。
|
||||
@@ -1,10 +1,10 @@
|
||||
## 产品形态
|
||||
|
||||
**Q:浏览器里的 5174 端口是什么?**
|
||||
Vite 热更新开发壳,仅开发调试用。产品形态是桌面应用(Tauri 2)或自托管 Web 服务,页面与 API 由后端 `:8788` 提供。
|
||||
**Q:能在浏览器里操作数据库吗?**
|
||||
不能。OpsxDB 客户端唯一形态是桌面 App(Tauri 2);浏览器只承载官网与文档(由本地后端 `:8788` 内置提供)。开发时的 `:5174` 只是桌面 devUrl 的资产服务,不是网页客户端。
|
||||
|
||||
**Q:桌面版和 Web 版功能有差别吗?**
|
||||
没有。同一套 UI、同一份后端,桌面版只是多了应用壳(自动拉起本地后端、原生窗口、安装包形态)。
|
||||
**Q:官网和文档部署在哪?**
|
||||
就内置在你运行的那个二进制里:`http://127.0.0.1:8788/` 是官网,`/docs` 是文档;无需额外部署。
|
||||
|
||||
**Q:怎么打安装包?**
|
||||
`./run.sh package` 产出 Tauri 安装包(DMG)并内嵌 sidecar 后端二进制。
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var blockedHosts = map[string]struct{}{
|
||||
"169.254.169.254": {},
|
||||
"metadata.google.internal": {},
|
||||
}
|
||||
|
||||
// ValidateURL applies MVP SSRF basics. Week 2 should add configurable CIDR
|
||||
// deny-lists and DNS-rebinding protection before exposing this beyond localhost.
|
||||
func ValidateURL(raw string) (*url.URL, error) {
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid url: %w", err)
|
||||
}
|
||||
if u.Scheme != "http" && u.Scheme != "https" {
|
||||
return nil, fmt.Errorf("only http/https schemes are allowed")
|
||||
}
|
||||
if u.Hostname() == "" {
|
||||
return nil, fmt.Errorf("url host is required")
|
||||
}
|
||||
host := strings.ToLower(strings.TrimSuffix(u.Hostname(), "."))
|
||||
if _, blocked := blockedHosts[host]; blocked {
|
||||
return nil, fmt.Errorf("blocked metadata host")
|
||||
}
|
||||
if ip := net.ParseIP(host); ip != nil && ip.Equal(net.ParseIP("169.254.169.254")) {
|
||||
return nil, fmt.Errorf("blocked metadata address")
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package proxy
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidateURL(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
raw string
|
||||
ok bool
|
||||
}{
|
||||
{"https://example.com/a", true},
|
||||
{"file:///etc/passwd", false},
|
||||
{"http://169.254.169.254/latest/meta-data", false},
|
||||
{"http://metadata.google.internal/computeMetadata/v1", false},
|
||||
} {
|
||||
_, err := ValidateURL(tc.raw)
|
||||
if tc.ok && err != nil {
|
||||
t.Fatalf("%s: %v", tc.raw, err)
|
||||
}
|
||||
if !tc.ok && err == nil {
|
||||
t.Fatalf("expected rejection: %s", tc.raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const maxResponseBytes = 10 << 20 // 10 MiB MVP safety cap
|
||||
|
||||
type Handler struct {
|
||||
BaseClient *http.Client
|
||||
}
|
||||
|
||||
func NewHandler() *Handler {
|
||||
return &Handler{BaseClient: &http.Client{Timeout: 30 * time.Second}}
|
||||
}
|
||||
|
||||
func (h *Handler) ProxyHTTP(c *gin.Context) {
|
||||
var spec HttpSpec
|
||||
if err := c.ShouldBindJSON(&spec); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid HttpSpec", "detail": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
method := strings.ToUpper(spec.Method)
|
||||
switch method {
|
||||
case http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete:
|
||||
default:
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported method"})
|
||||
return
|
||||
}
|
||||
|
||||
u, err := ValidateURL(spec.URL)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
query := u.Query()
|
||||
for key, value := range spec.Query {
|
||||
query.Set(key, value)
|
||||
}
|
||||
u.RawQuery = query.Encode()
|
||||
|
||||
var body io.Reader
|
||||
if spec.Body != nil && spec.Body.Mode != "none" && method != http.MethodGet {
|
||||
body = bytes.NewBufferString(spec.Body.Raw)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(c.Request.Context(), method, u.String(), body)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "cannot create request", "detail": err.Error()})
|
||||
return
|
||||
}
|
||||
for key, value := range spec.Headers {
|
||||
// Hop-by-hop headers are managed by net/http.
|
||||
if !isHopByHop(key) {
|
||||
req.Header.Set(key, value)
|
||||
}
|
||||
}
|
||||
if spec.Body != nil && spec.Body.Mode == "json" && req.Header.Get("Content-Type") == "" {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
client := h.BaseClient
|
||||
if spec.TimeoutMS > 0 {
|
||||
client = &http.Client{Timeout: time.Duration(spec.TimeoutMS) * time.Millisecond}
|
||||
}
|
||||
started := time.Now()
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "upstream request failed", "detail": err.Error()})
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
payload, err := io.ReadAll(io.LimitReader(res.Body, maxResponseBytes+1))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "cannot read upstream response", "detail": err.Error()})
|
||||
return
|
||||
}
|
||||
if len(payload) > maxResponseBytes {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "upstream response exceeds 10 MiB limit"})
|
||||
return
|
||||
}
|
||||
|
||||
headers := make(map[string]string, len(res.Header))
|
||||
for key, values := range res.Header {
|
||||
headers[strings.ToLower(key)] = strings.Join(values, ", ")
|
||||
}
|
||||
c.JSON(http.StatusOK, HttpResult{
|
||||
Status: res.StatusCode,
|
||||
Headers: headers,
|
||||
Body: string(payload),
|
||||
TimeMS: time.Since(started).Milliseconds(),
|
||||
Size: len(payload),
|
||||
})
|
||||
}
|
||||
|
||||
func isHopByHop(name string) bool {
|
||||
switch strings.ToLower(name) {
|
||||
case "connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailer", "transfer-encoding", "upgrade", "host", "content-length":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package proxy
|
||||
|
||||
type BodySpec struct {
|
||||
Mode string `json:"mode"`
|
||||
Raw string `json:"raw,omitempty"`
|
||||
}
|
||||
|
||||
type HttpSpec struct {
|
||||
Method string `json:"method" binding:"required"`
|
||||
URL string `json:"url" binding:"required"`
|
||||
Headers map[string]string `json:"headers"`
|
||||
Query map[string]string `json:"query,omitempty"`
|
||||
Body *BodySpec `json:"body,omitempty"`
|
||||
TimeoutMS int `json:"timeoutMs,omitempty"`
|
||||
}
|
||||
|
||||
type HttpResult struct {
|
||||
Status int `json:"status"`
|
||||
Headers map[string]string `json:"headers"`
|
||||
Body string `json:"body"`
|
||||
TimeMS int64 `json:"timeMs"`
|
||||
Size int `json:"size"`
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AuthMiddleware 本地鉴权:
|
||||
// - token == "" → 直通(本机 dev 无感);
|
||||
// - /health、/api/health 豁免;
|
||||
// - 其余 /api/** 要求 Header "Authorization: Bearer <token>",失败 401。
|
||||
func AuthMiddleware(token string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if token == "" {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
p := c.Request.URL.Path
|
||||
if p == "/health" || p == "/api/health" {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(p, "/api/") {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if c.GetHeader("Authorization") == "Bearer "+token {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "未授权"})
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,13 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"opsxdb/server/internal/ai"
|
||||
@@ -11,10 +16,13 @@ import (
|
||||
"opsxdb/server/internal/site"
|
||||
)
|
||||
|
||||
func NewRouter() *gin.Engine {
|
||||
// Version 由 cmd/opsxdb 注入,/health 响应携带供桌面 sidecar 版本握手。
|
||||
var Version = "dev"
|
||||
|
||||
func NewRouter(token string) *gin.Engine {
|
||||
r := gin.New()
|
||||
_ = r.SetTrustedProxies(nil)
|
||||
r.Use(gin.Logger(), gin.Recovery(), localhostCORS())
|
||||
r.Use(gin.Logger(), gin.Recovery(), localhostCORS(), AuthMiddleware(token))
|
||||
|
||||
r.GET("/health", health)
|
||||
r.GET("/api/health", health)
|
||||
@@ -53,7 +61,10 @@ func NewRouter() *gin.Engine {
|
||||
|
||||
api.POST("/db/close", func(c *gin.Context) {
|
||||
var req db.ConnReq
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if req.SessionID != "" {
|
||||
db.CloseSession(req.SessionID)
|
||||
}
|
||||
@@ -66,7 +77,7 @@ func NewRouter() *gin.Engine {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
list, err := db.ListDatabases(req)
|
||||
list, err := db.ListDatabases(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -80,7 +91,7 @@ func NewRouter() *gin.Engine {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
list, err := db.ListTables(req)
|
||||
list, err := db.ListTables(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -94,7 +105,7 @@ func NewRouter() *gin.Engine {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
list, err := db.ListColumns(req)
|
||||
list, err := db.ListColumns(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -108,10 +119,135 @@ func NewRouter() *gin.Engine {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
res := db.ExecSQL(c.Request.Context(), req)
|
||||
ctx, done := withExecControl(c, req)
|
||||
defer done()
|
||||
res := db.ExecSQL(ctx, req)
|
||||
c.JSON(http.StatusOK, res)
|
||||
})
|
||||
|
||||
api.POST("/db/explain", func(c *gin.Context) {
|
||||
req, err := bindConn(c, true)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
ctx, done := withExecControl(c, req)
|
||||
defer done()
|
||||
res := db.ExplainSQL(ctx, req)
|
||||
c.JSON(http.StatusOK, res)
|
||||
})
|
||||
|
||||
// /db/cancel 取消在执行的请求(按 requestId),body: {"requestId":"..."}
|
||||
api.POST("/db/cancel", func(c *gin.Context) {
|
||||
var req struct {
|
||||
RequestID string `json:"requestId"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(req.RequestID) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "requestId 不能为空"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "cancelled": db.CancelInflight(req.RequestID)})
|
||||
})
|
||||
|
||||
// /db/ddl 查看建表语句,body=ConnBody + table → {"ddl":"...","error":""}
|
||||
api.POST("/db/ddl", func(c *gin.Context) {
|
||||
req, err := bindConn(c, true)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"ddl": "", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(req.Table) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"ddl": "", "error": "缺少表名"})
|
||||
return
|
||||
}
|
||||
ddl, err := db.GetDDL(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"ddl": "", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ddl": ddl, "error": ""})
|
||||
})
|
||||
|
||||
// /db/search 对象搜索,body=ConnBody + keyword → {"results":[...],"error":""}
|
||||
api.POST("/db/search", func(c *gin.Context) {
|
||||
req, err := bindConn(c, true)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"results": []db.ObjectHit{}, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(req.Keyword) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"results": []db.ObjectHit{}, "error": "缺少关键词"})
|
||||
return
|
||||
}
|
||||
results, err := db.SearchObjects(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"results": []db.ObjectHit{}, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"results": results, "error": ""})
|
||||
})
|
||||
|
||||
// /db/export 导出中心,body=ConnBody + format(csv|json|insert) + sql?/table? + limit?
|
||||
api.POST("/db/export", func(c *gin.Context) {
|
||||
req, err := bindConn(c, true)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
format := strings.ToLower(strings.TrimSpace(req.Format))
|
||||
contentType, ext := exportContentType(format)
|
||||
if ext == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "format 仅支持 csv/json/insert"})
|
||||
return
|
||||
}
|
||||
sqlText := strings.TrimSpace(req.SQL)
|
||||
if sqlText == "" && strings.TrimSpace(req.Table) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请提供 sql 或 table"})
|
||||
return
|
||||
}
|
||||
if sqlText != "" {
|
||||
// sql 模式:多条时只取第一条
|
||||
stmts := db.SplitStatements(sqlText)
|
||||
if len(stmts) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "sql 为空"})
|
||||
return
|
||||
}
|
||||
sqlText = stmts[0]
|
||||
// 导出仅允许只读查询,已确认也不放行写操作
|
||||
if !db.ExportAllowedQuery(sqlText) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "导出仅允许只读查询(SELECT/WITH/SHOW/DESCRIBE/EXPLAIN)"})
|
||||
return
|
||||
}
|
||||
if hits := db.CheckDangerousSQL(req.Driver, sqlText); len(hits) > 0 {
|
||||
if !req.ConfirmDanger {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "检测到危险语句: " + strings.Join(hits, "; "),
|
||||
"needConfirm": true,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
req.SQL = sqlText
|
||||
} else {
|
||||
// 表模式:SELECT * FROM <按 family 引号>
|
||||
req.SQL = db.SelectAllSQL(req.Driver, req.Table)
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="export-%s-%d.%s"`, format, time.Now().Unix(), ext))
|
||||
c.Header("Content-Type", contentType)
|
||||
if err := db.ExportQuery(ctx, req, c.Writer); err != nil {
|
||||
// 尚未流出数据时可回 JSON 错误;已流式写出则只能截断
|
||||
if !c.Writer.Written() {
|
||||
c.Writer.Header().Del("Content-Disposition")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
api.POST("/db/redis/keys", func(c *gin.Context) {
|
||||
req, err := bindConn(c, true)
|
||||
if err != nil {
|
||||
@@ -122,7 +258,7 @@ func NewRouter() *gin.Engine {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "仅 Redis 可用"})
|
||||
return
|
||||
}
|
||||
res := db.ListRedisKeys(req)
|
||||
res := db.ListRedisKeys(c.Request.Context(), req)
|
||||
if res.Error != "" {
|
||||
c.JSON(http.StatusBadRequest, res)
|
||||
return
|
||||
@@ -140,7 +276,7 @@ func NewRouter() *gin.Engine {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "仅 Redis 可用"})
|
||||
return
|
||||
}
|
||||
res := db.GetRedisKey(req)
|
||||
res := db.GetRedisKey(c.Request.Context(), req)
|
||||
if res.Error != "" && res.Type == "" {
|
||||
c.JSON(http.StatusBadRequest, res)
|
||||
return
|
||||
@@ -158,7 +294,7 @@ func NewRouter() *gin.Engine {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "仅 Redis 可用"})
|
||||
return
|
||||
}
|
||||
res := db.DeleteRedisKey(req)
|
||||
res := db.DeleteRedisKey(c.Request.Context(), req)
|
||||
if res.Error != "" {
|
||||
c.JSON(http.StatusBadRequest, res)
|
||||
return
|
||||
@@ -166,20 +302,127 @@ func NewRouter() *gin.Engine {
|
||||
c.JSON(http.StatusOK, res)
|
||||
})
|
||||
|
||||
// /db/redis/del-pattern 目录(前缀)批量删除:body=ConnBody + pattern(如 "agent:*")
|
||||
api.POST("/db/redis/del-pattern", func(c *gin.Context) {
|
||||
req, err := bindConn(c, true)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "deleted": 0})
|
||||
return
|
||||
}
|
||||
if !db.IsRedisDriver(req.Driver) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "仅 Redis 可用", "deleted": 0})
|
||||
return
|
||||
}
|
||||
res := db.DeleteRedisPattern(c.Request.Context(), req)
|
||||
if res.Error != "" {
|
||||
c.JSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, res)
|
||||
})
|
||||
|
||||
// 驱动管理:状态 / 预装 / 离线包导入(agent 驱动)
|
||||
api.GET("/drivers/status", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"drivers": db.DriversStatus()})
|
||||
})
|
||||
api.POST("/drivers/install", func(c *gin.Context) {
|
||||
var req struct {
|
||||
Driver string `json:"driver"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil || req.Driver == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "缺少 driver"})
|
||||
return
|
||||
}
|
||||
if err := db.InstallDriver(req.Driver); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "drivers": db.DriversStatus()})
|
||||
})
|
||||
api.POST("/drivers/import", func(c *gin.Context) {
|
||||
fh, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "缺少 file 字段(zip)"})
|
||||
return
|
||||
}
|
||||
if !strings.HasSuffix(strings.ToLower(fh.Filename), ".zip") {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "只接受 .zip 离线包"})
|
||||
return
|
||||
}
|
||||
tmp := filepath.Join(os.TempDir(), "opsxdb-driver-import-*.zip")
|
||||
f, err := os.CreateTemp(os.TempDir(), "opsxdb-driver-import-*.zip")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
tmp = f.Name()
|
||||
defer os.Remove(tmp)
|
||||
if err := c.SaveUploadedFile(fh, tmp); err != nil {
|
||||
f.Close()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
f.Close()
|
||||
n, err := db.ImportOfflinePack(tmp)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "imported": n, "drivers": db.DriversStatus()})
|
||||
})
|
||||
|
||||
api.POST("/ai/chat", func(c *gin.Context) {
|
||||
var req ai.ChatReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, ai.Chat(req))
|
||||
res, err := ai.Default().Complete(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, res)
|
||||
})
|
||||
|
||||
// GET /api/ai/config 返回脱敏配置:apiKey 只回掩码与是否已配置
|
||||
api.GET("/ai/config", func(c *gin.Context) {
|
||||
cfg := ai.LoadConfig()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"provider": cfg.Provider,
|
||||
"baseURL": cfg.BaseURL,
|
||||
"model": cfg.Model,
|
||||
"hasKey": cfg.APIKey != "",
|
||||
"apiKey": cfg.MaskedKey(),
|
||||
})
|
||||
})
|
||||
|
||||
api.POST("/ai/config", func(c *gin.Context) {
|
||||
var req ai.Config
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := ai.SaveConfig(req); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
})
|
||||
|
||||
api.POST("/ai/generate", func(c *gin.Context) {
|
||||
var req ai.ChatReq
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
req.Action = "generate"
|
||||
c.JSON(http.StatusOK, ai.Chat(req))
|
||||
res, err := ai.Default().Complete(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, res)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -197,18 +440,54 @@ func bindConn(c *gin.Context, allowSession bool) (db.ConnReq, error) {
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// withExecControl 按 timeoutMs/requestId 包装请求 ctx,返回 cleanup(处理完调用:注销 in-flight + 释放计时器)。
|
||||
// timeoutMs > 0 时以此作为语句超时;requestId 非空时注册进 in-flight 表,/api/db/cancel 可取消。
|
||||
func withExecControl(c *gin.Context, req db.ConnReq) (context.Context, func()) {
|
||||
ctx := c.Request.Context()
|
||||
var cancel context.CancelFunc
|
||||
if req.TimeoutMs > 0 {
|
||||
ctx, cancel = context.WithTimeout(ctx, time.Duration(req.TimeoutMs)*time.Millisecond)
|
||||
}
|
||||
if req.RequestID != "" {
|
||||
if cancel == nil {
|
||||
ctx, cancel = context.WithCancel(ctx)
|
||||
}
|
||||
db.RegisterInflight(req.RequestID, cancel)
|
||||
}
|
||||
cleanup := func() {
|
||||
if req.RequestID != "" {
|
||||
db.UnregisterInflight(req.RequestID)
|
||||
}
|
||||
if cancel != nil {
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
return ctx, cleanup
|
||||
}
|
||||
|
||||
// exportContentType 返回导出格式对应的 Content-Type 与扩展名;不支持的格式 ext 为空。
|
||||
func exportContentType(format string) (contentType, ext string) {
|
||||
switch format {
|
||||
case "csv":
|
||||
return "text/csv", "csv"
|
||||
case "json":
|
||||
return "application/json", "json"
|
||||
case "insert":
|
||||
return "text/plain", "sql"
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
|
||||
func health(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "service": "opsxdb"})
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "service": "opsxdb", "version": Version})
|
||||
}
|
||||
|
||||
func localhostCORS() gin.HandlerFunc {
|
||||
// 5174 = 桌面 devUrl(Tauri dev 的 Vite 资产服务);tauri:// = 打包桌面壳。
|
||||
// 浏览器不作为客户端,不再放行其它来源。
|
||||
allowed := map[string]struct{}{
|
||||
"http://localhost:5174": {},
|
||||
"http://127.0.0.1:5174": {},
|
||||
"http://localhost:5173": {},
|
||||
"http://127.0.0.1:5173": {},
|
||||
"http://localhost:1420": {},
|
||||
"http://127.0.0.1:1420": {},
|
||||
"tauri://localhost": {},
|
||||
"http://tauri.localhost": {},
|
||||
}
|
||||
|
||||
@@ -0,0 +1,374 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func doPost(t *testing.T, r http.Handler, path, body, token string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
|
||||
// 鉴权矩阵:token 为空直通;token 非空时无头 401、正确头 200;health 豁免。
|
||||
func TestAuthMatrix(t *testing.T) {
|
||||
open := NewRouter("")
|
||||
if w := doPost(t, open, "/api/db/exec", `{"driver":"sqlite","sql":"SELECT 1"}`, ""); w.Code == http.StatusUnauthorized {
|
||||
t.Fatal("token 为空应直通")
|
||||
}
|
||||
|
||||
authed := NewRouter("tok123")
|
||||
if w := doPost(t, authed, "/api/db/exec", `{"driver":"sqlite","sql":"SELECT 1"}`, ""); w.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("无 token 应 401,实际 %d", w.Code)
|
||||
}
|
||||
if w := doPost(t, authed, "/api/db/exec", `{"driver":"sqlite","sql":"SELECT 1"}`, "wrong"); w.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("错误 token 应 401,实际 %d", w.Code)
|
||||
}
|
||||
// health 豁免
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
w := httptest.NewRecorder()
|
||||
authed.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("health 应豁免,实际 %d", w.Code)
|
||||
}
|
||||
var healthBody map[string]interface{}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &healthBody); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if healthBody["version"] == nil {
|
||||
t.Fatal("health 应携带 version")
|
||||
}
|
||||
}
|
||||
|
||||
// 危险 SQL 二段流 + 确认放行(HTTP 层)。
|
||||
func TestExecDangerHTTP(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "http.db")
|
||||
if err := os.WriteFile(dbFile, []byte{}, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := NewRouter("")
|
||||
|
||||
mk := func(sql string, confirm bool) string {
|
||||
b, _ := json.Marshal(map[string]interface{}{
|
||||
"driver": "sqlite", "database": dbFile, "sql": sql, "confirmDanger": confirm,
|
||||
})
|
||||
return string(b)
|
||||
}
|
||||
|
||||
w := doPost(t, r, "/api/db/exec", mk("CREATE TABLE t (id int)", false), "")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("建表失败: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// 未确认:needConfirm 且不执行
|
||||
w = doPost(t, r, "/api/db/exec", mk("DROP TABLE t", false), "")
|
||||
var res struct {
|
||||
Error string `json:"error"`
|
||||
NeedConfirm bool `json:"needConfirm"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !res.NeedConfirm || res.Error == "" {
|
||||
t.Fatalf("应 needConfirm: %s", w.Body.String())
|
||||
}
|
||||
// 表仍在
|
||||
w = doPost(t, r, "/api/db/exec", mk("SELECT count(*) FROM t", false), "")
|
||||
if strings.Contains(w.Body.String(), `"error"`) {
|
||||
t.Fatalf("DROP 不应已执行: %s", w.Body.String())
|
||||
}
|
||||
// 确认后放行
|
||||
w = doPost(t, r, "/api/db/exec", mk("DROP TABLE t", true), "")
|
||||
if strings.Contains(w.Body.String(), `"error"`) {
|
||||
t.Fatalf("确认后应放行: %s", w.Body.String())
|
||||
}
|
||||
// 多语句绕过同样被拦
|
||||
w = doPost(t, r, "/api/db/exec", mk("SELECT 1; DROP TABLE t", false), "")
|
||||
if !strings.Contains(w.Body.String(), `"needConfirm":true`) {
|
||||
t.Fatalf("多语句绕过应被拦截: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// EXPLAIN 端点:sqlite 行列计划。
|
||||
func TestExplainHTTP(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "ex.db")
|
||||
if err := os.WriteFile(dbFile, []byte{}, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := NewRouter("")
|
||||
w := doPost(t, r, "/api/db/explain", `{"driver":"sqlite","database":"`+dbFile+`","sql":"SELECT 1"}`, "")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
Rows []map[string]interface{} `json:"rows"`
|
||||
Error string `json:"error"`
|
||||
TimeMs int64 `json:"timeMs"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Error != "" || len(res.Rows) == 0 {
|
||||
t.Fatalf("sqlite explain 应有行列结果: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// AI 配置保存/读取(脱敏),chat 未配置回落 stub。
|
||||
func TestAIConfigHTTP(t *testing.T) {
|
||||
// 隔离 HOME,避免读写真实 ~/.opsxdb
|
||||
tmp := t.TempDir()
|
||||
t.Setenv("HOME", tmp)
|
||||
|
||||
r := NewRouter("")
|
||||
w := doPost(t, r, "/api/ai/config", `{"provider":"openai","baseURL":"http://x.local/v1","apiKey":"sk-secret-123","model":"m1"}`, "")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("保存配置: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
// 文件权限 0600
|
||||
fi, err := os.Stat(filepath.Join(tmp, ".opsxdb", "ai.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fi.Mode().Perm() != 0600 {
|
||||
t.Fatalf("ai.json 权限应 0600,实际 %v", fi.Mode().Perm())
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/ai/config", nil)
|
||||
w = httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
var cfg map[string]interface{}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg["hasKey"] != true || cfg["model"] != "m1" || cfg["provider"] != "openai" {
|
||||
t.Fatalf("配置回读错误: %v", cfg)
|
||||
}
|
||||
if key, _ := cfg["apiKey"].(string); strings.Contains(key, "secret") {
|
||||
t.Fatalf("apiKey 应脱敏: %v", key)
|
||||
}
|
||||
|
||||
// 未配置 API Key 的 stub 回落(全新 HOME)
|
||||
t.Setenv("HOME", t.TempDir())
|
||||
w = doPost(t, r, "/api/ai/chat", `{"action":"generate","prompt":"查用户"}`, "")
|
||||
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "content") {
|
||||
t.Fatalf("stub 回落: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// R2-B4:/api/db/cancel 空 requestId 400;未知 requestId cancelled=false。
|
||||
func TestCancelEndpoint(t *testing.T) {
|
||||
r := NewRouter("")
|
||||
w := doPost(t, r, "/api/db/cancel", `{"requestId":""}`, "")
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("空 requestId 应 400,实际 %d", w.Code)
|
||||
}
|
||||
w = doPost(t, r, "/api/db/cancel", `{"requestId":"never-existed"}`, "")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
OK bool `json:"ok"`
|
||||
Cancelled bool `json:"cancelled"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !res.OK || res.Cancelled {
|
||||
t.Fatalf("未知 requestId 应 cancelled=false: %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
// R2-B7:/api/db/export 三种格式的响应头与内容形状。
|
||||
func TestExportHTTPEndpoint(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "exp.db")
|
||||
if err := os.WriteFile(dbFile, []byte{}, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := NewRouter("")
|
||||
mk := func(fields map[string]interface{}) string {
|
||||
fields["driver"] = "sqlite"
|
||||
fields["database"] = dbFile
|
||||
b, _ := json.Marshal(fields)
|
||||
return string(b)
|
||||
}
|
||||
if w := doPost(t, r, "/api/db/exec", mk(map[string]interface{}{
|
||||
"sql": `CREATE TABLE t_exp (id INTEGER PRIMARY KEY, name TEXT)`,
|
||||
}), ""); w.Code != http.StatusOK {
|
||||
t.Fatalf("建表失败: %s", w.Body.String())
|
||||
}
|
||||
if w := doPost(t, r, "/api/db/exec", mk(map[string]interface{}{
|
||||
"sql": `INSERT INTO t_exp (id, name) VALUES (1, 'alice'), (2, 'bob')`,
|
||||
}), ""); strings.Contains(w.Body.String(), `"error"`) {
|
||||
t.Fatalf("插数失败: %s", w.Body.String())
|
||||
}
|
||||
|
||||
// csv:Content-Disposition + 内容
|
||||
w := doPost(t, r, "/api/db/export", mk(map[string]interface{}{
|
||||
"format": "csv", "table": "t_exp",
|
||||
}), "")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("csv 导出: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
cd := w.Header().Get("Content-Disposition")
|
||||
if !strings.Contains(cd, `attachment; filename="export-csv-`) || !strings.HasSuffix(cd, `.csv"`) {
|
||||
t.Fatalf("Content-Disposition 错误: %q", cd)
|
||||
}
|
||||
if ct := w.Header().Get("Content-Type"); ct != "text/csv" {
|
||||
t.Fatalf("Content-Type 错误: %q", ct)
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "id,name\n1,alice\n2,bob") {
|
||||
t.Fatalf("csv 内容错误: %q", w.Body.String())
|
||||
}
|
||||
|
||||
// json
|
||||
w = doPost(t, r, "/api/db/export", mk(map[string]interface{}{
|
||||
"format": "json", "sql": "SELECT id, name FROM t_exp ORDER BY id",
|
||||
}), "")
|
||||
if w.Code != http.StatusOK || w.Header().Get("Content-Type") != "application/json" {
|
||||
t.Fatalf("json 导出: %d %q", w.Code, w.Header().Get("Content-Type"))
|
||||
}
|
||||
var rows []map[string]interface{}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &rows); err != nil || len(rows) != 2 {
|
||||
t.Fatalf("json 内容应可解析且 2 行: %v %s", err, w.Body.String())
|
||||
}
|
||||
|
||||
// insert
|
||||
w = doPost(t, r, "/api/db/export", mk(map[string]interface{}{
|
||||
"format": "insert", "table": "t_exp", "sql": "SELECT id, name FROM t_exp ORDER BY id",
|
||||
}), "")
|
||||
if w.Code != http.StatusOK || w.Header().Get("Content-Type") != "text/plain" {
|
||||
t.Fatalf("insert 导出: %d %q", w.Code, w.Header().Get("Content-Type"))
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), `INSERT INTO "t_exp" ("id", "name") VALUES (1, 'alice');`) {
|
||||
t.Fatalf("insert 内容错误: %q", w.Body.String())
|
||||
}
|
||||
|
||||
// limit 生效
|
||||
w = doPost(t, r, "/api/db/export", mk(map[string]interface{}{
|
||||
"format": "csv", "table": "t_exp", "limit": 1,
|
||||
}), "")
|
||||
lines := strings.Split(strings.TrimSpace(w.Body.String()), "\n")
|
||||
if len(lines) != 2 {
|
||||
t.Fatalf("limit=1 应表头+1 行: %q", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// R2-B7:导出安全闸门——非只读拒绝(即使已确认),危险只读语句走二段流。
|
||||
func TestExportHTTPRejections(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "expr.db")
|
||||
if err := os.WriteFile(dbFile, []byte{}, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := NewRouter("")
|
||||
mk := func(fields map[string]interface{}) string {
|
||||
fields["driver"] = "sqlite"
|
||||
fields["database"] = dbFile
|
||||
b, _ := json.Marshal(fields)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// 写操作开头:未确认也拒绝,已确认也拒绝
|
||||
w := doPost(t, r, "/api/db/export", mk(map[string]interface{}{
|
||||
"format": "csv", "sql": "DROP TABLE t",
|
||||
}), "")
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("写操作应 400: %d", w.Code)
|
||||
}
|
||||
w = doPost(t, r, "/api/db/export", mk(map[string]interface{}{
|
||||
"format": "csv", "sql": "DELETE FROM t", "confirmDanger": true,
|
||||
}), "")
|
||||
if w.Code != http.StatusBadRequest || strings.Contains(w.Body.String(), "needConfirm") {
|
||||
t.Fatalf("已确认写操作仍应拒绝且不带 needConfirm: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// 只读但命中危险(INTO OUTFILE):未确认 → needConfirm 二段流
|
||||
w = doPost(t, r, "/api/db/export", mk(map[string]interface{}{
|
||||
"format": "csv", "sql": "SELECT * INTO OUTFILE '/tmp/x' FROM t",
|
||||
}), "")
|
||||
if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), `"needConfirm":true`) {
|
||||
t.Fatalf("危险只读应 needConfirm: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
// 已确认:放行到执行层(sqlite 语法错误,但不再 needConfirm)
|
||||
w = doPost(t, r, "/api/db/export", mk(map[string]interface{}{
|
||||
"format": "csv", "sql": "SELECT * INTO OUTFILE '/tmp/x' FROM t", "confirmDanger": true,
|
||||
}), "")
|
||||
if strings.Contains(w.Body.String(), "needConfirm") {
|
||||
t.Fatalf("确认后不应再 needConfirm: %s", w.Body.String())
|
||||
}
|
||||
|
||||
// 缺 sql 与 table / 非法 format
|
||||
w = doPost(t, r, "/api/db/export", mk(map[string]interface{}{"format": "csv"}), "")
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("缺 sql/table 应 400: %d", w.Code)
|
||||
}
|
||||
w = doPost(t, r, "/api/db/export", mk(map[string]interface{}{
|
||||
"format": "xml", "sql": "SELECT 1",
|
||||
}), "")
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("非法 format 应 400: %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// R2-B5/R2-B6:/api/db/ddl 与 /api/db/search 的 HTTP 层冒烟。
|
||||
func TestDDLAndSearchHTTP(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "meta.db")
|
||||
if err := os.WriteFile(dbFile, []byte{}, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := NewRouter("")
|
||||
mk := func(fields map[string]interface{}) string {
|
||||
fields["driver"] = "sqlite"
|
||||
fields["database"] = dbFile
|
||||
b, _ := json.Marshal(fields)
|
||||
return string(b)
|
||||
}
|
||||
doPost(t, r, "/api/db/exec", mk(map[string]interface{}{
|
||||
"sql": `CREATE TABLE t_ddl (id INTEGER PRIMARY KEY, name TEXT)`,
|
||||
}), "")
|
||||
|
||||
w := doPost(t, r, "/api/db/ddl", mk(map[string]interface{}{"table": "t_ddl"}), "")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("ddl: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
var ddlRes struct {
|
||||
DDL string `json:"ddl"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &ddlRes); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ddlRes.Error != "" || !strings.Contains(ddlRes.DDL, "CREATE TABLE t_ddl") {
|
||||
t.Fatalf("ddl 响应错误: %+v", ddlRes)
|
||||
}
|
||||
w = doPost(t, r, "/api/db/ddl", mk(map[string]interface{}{}), "")
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("缺 table 应 400: %d", w.Code)
|
||||
}
|
||||
|
||||
w = doPost(t, r, "/api/db/search", mk(map[string]interface{}{"keyword": "ddl"}), "")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("search: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
var searchRes struct {
|
||||
Results []map[string]interface{} `json:"results"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &searchRes); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if searchRes.Error != "" || len(searchRes.Results) != 1 || searchRes.Results[0]["name"] != "t_ddl" {
|
||||
t.Fatalf("search 响应错误: %+v", searchRes)
|
||||
}
|
||||
}
|
||||
@@ -105,7 +105,7 @@
|
||||
<div class="stat"><b>7</b><span>内置驱动</span></div>
|
||||
<div class="stat"><b>18</b><span>可连数据库</span></div>
|
||||
<div class="stat"><b>0</b><span>数据外发</span></div>
|
||||
<div class="stat"><b>1</b><span>个二进制即可自托管</span></div>
|
||||
<div class="stat"><b>1</b><span>个二进制内置官网与文档</span></div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
@@ -196,7 +196,7 @@
|
||||
<section class="section" id="download">
|
||||
<div class="wrap">
|
||||
<h2 class="sec-title">三种形态,同一套体验</h2>
|
||||
<p class="sec-sub">主交付是桌面 GUI;Web 与内网部署复用同一个 Go 二进制。</p>
|
||||
<p class="sec-sub">客户端唯一形态是桌面 GUI;官网与文档内置在同一个 Go 二进制里。</p>
|
||||
<div class="pricing three">
|
||||
<div class="price-card featured">
|
||||
<div class="plan-tag">推荐 · 主交付</div>
|
||||
@@ -212,16 +212,16 @@
|
||||
<a class="btn btn-primary" href="/docs/quickstart" target="_blank" rel="noopener">从源码构建</a>
|
||||
</div>
|
||||
<div class="price-card">
|
||||
<div class="plan">Web 自托管</div>
|
||||
<div class="price"><b>1 个二进制</b></div>
|
||||
<div class="plan-sub">Gin + 内置静态页</div>
|
||||
<div class="plan">官网 + 文档</div>
|
||||
<div class="price"><b>内置</b></div>
|
||||
<div class="plan-sub">随二进制提供,无需部署</div>
|
||||
<ul>
|
||||
<li>✓ <code>./run.sh web</code> 一条命令启动</li>
|
||||
<li>✓ 官网与文档随二进制内置</li>
|
||||
<li>✓ 浏览器直接用,无需安装</li>
|
||||
<li>✓ 适合快速体验与调试</li>
|
||||
<li>✓ 官网 <code>/</code> 与文档 <code>/docs</code> 内置于后端</li>
|
||||
<li>✓ 文档支持全文搜索 / 打印 / 导出 PDF</li>
|
||||
<li>✓ 浏览器只看站点与文档,不承载客户端</li>
|
||||
<li>✓ 随版本发布自动更新</li>
|
||||
</ul>
|
||||
<a class="btn btn-primary" href="/docs/quickstart" target="_blank" rel="noopener">查看快速开始</a>
|
||||
<a class="btn btn-primary" href="/docs" target="_blank" rel="noopener">浏览文档</a>
|
||||
</div>
|
||||
<div class="price-card enterprise">
|
||||
<div class="plan-tag dark">内网部署</div>
|
||||
@@ -246,8 +246,8 @@
|
||||
<h2 class="sec-title">常见问题</h2>
|
||||
<div class="faq">
|
||||
<details open><summary>我的连接密码保存在哪?安全吗?</summary><p>全部保存在本机(混淆存储),查询时只携带会话 ID,密码不会随每次请求上送,也不会同步到任何云端。</p></details>
|
||||
<details><summary>浏览器里打开的 5174 端口是什么?</summary><p>那是开发调试用的 Vite 热更新壳,不是产品形态。正式形态是桌面应用(或自托管的 Web 服务),由后端 :8788 提供页面与 API。</p></details>
|
||||
<details><summary>和 DBeaver / Navicat 有什么差别?</summary><p>OpsxDB 面向运维与开发的日常高频操作(连库、点表、写 SQL、取结果),界面密度和快捷键为「快」设计;本地优先、单二进制自托管、内置中文文档。重度管理功能(ER 图、数据比对等)在路线图中。</p></details>
|
||||
<details><summary>能在浏览器里操作数据库吗?</summary><p>不能。客户端唯一形态是桌面应用(Tauri 2);浏览器只承载官网与文档,由后端 :8788 内置提供。开发时的 :5174 只是桌面 devUrl 的资产服务。</p></details>
|
||||
<details><summary>和 DBeaver / Navicat 有什么差别?</summary><p>OpsxDB 面向运维与开发的日常高频操作(连库、点表、写 SQL、取结果),界面密度和快捷键为「快」设计;本地优先、后端随桌面应用拉起、内置中文文档。重度管理功能(ER 图、数据比对等)在路线图中。</p></details>
|
||||
<details><summary>Oracle 支持好了吗?</summary><p>驱动代码已就绪(纯 Go 的 go-ora,无需 JVM),但本机 docker 验收在 Apple Silicon 上受镜像限制未通过,需 x86 环境或真库完成验收,详见文档《驱动支持》。</p></details>
|
||||
<details><summary>AI 功能需要联网吗?</summary><p>当前版本 AI 面板为本地 stub(生成 / 解释 / 优化等动作的框架已就位),接入模型服务的版本会明确标注数据流向。</p></details>
|
||||
<details><summary>支持达梦 / MongoDB 吗?</summary><p>已列入路线:常用库 native 内置,长尾库(达梦、MongoDB、ZooKeeper、ES、InfluxDB)规划 agent 按需接入,驱动分发走独立仓库并校验 sha256。</p></details>
|
||||
|
||||
Reference in new issue
Block a user