mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-27 03:10:11 +08:00
- 金仓外置驱动链路增加表名与变更字段归一化,修复 ApplyChanges 场景下双引号转义异常导致的 SQL 语法错误 - 新增金仓公共标识符工具并复用到 kingbase_impl 与 optional_driver_agent_impl,统一处理多重转义、schema.table 拆分与引用规范 - 金仓代理连接后自动探测并设置 search_path,降低查询时必须手写 schema 前缀的概率 - MongoDB 连接参数改为显式 host/hosts 优先,避免被 URI 中 localhost 覆盖;代理链路保留目标地址不再改写为本地地址 - 连接测试增加前后端超时收敛与日志增强,避免长时间转圈;连接错误文案在未启用 TLS 时移除误导性的“SSL”前缀 - 统一日志级别为 INFO/WARN/ERROR,默认日志目录收敛到 ~/.GoNavi/Logs,并补充驱动构建脚本 build-driver-agents.sh
165 lines
3.4 KiB
Go
165 lines
3.4 KiB
Go
package db
|
|
|
|
import "strings"
|
|
|
|
func normalizeKingbaseIdentCommon(raw string) string {
|
|
value := strings.TrimSpace(raw)
|
|
if value == "" {
|
|
return ""
|
|
}
|
|
|
|
// 兼容被多次 JSON 序列化后的转义引号:
|
|
// \\\"schema\\\" -> \"schema\" -> "schema"
|
|
for i := 0; i < 8; i++ {
|
|
next := strings.TrimSpace(value)
|
|
next = strings.ReplaceAll(next, `\\\"`, `\"`)
|
|
next = strings.ReplaceAll(next, `\"`, `"`)
|
|
if next == value {
|
|
break
|
|
}
|
|
value = next
|
|
}
|
|
value = strings.TrimSpace(value)
|
|
|
|
stripWrapperOnce := func(text string) string {
|
|
t := strings.TrimSpace(text)
|
|
if strings.HasPrefix(t, `\`) && len(t) > 1 {
|
|
t = strings.TrimSpace(strings.TrimPrefix(t, `\`))
|
|
}
|
|
if strings.HasSuffix(t, `\`) && len(t) > 1 {
|
|
t = strings.TrimSpace(strings.TrimSuffix(t, `\`))
|
|
}
|
|
if len(t) >= 4 && strings.HasPrefix(t, `\"`) && strings.HasSuffix(t, `\"`) {
|
|
return strings.TrimSpace(t[2 : len(t)-2])
|
|
}
|
|
if len(t) >= 2 && strings.HasPrefix(t, `"`) && strings.HasSuffix(t, `"`) {
|
|
return strings.TrimSpace(t[1 : len(t)-1])
|
|
}
|
|
if len(t) >= 2 && strings.HasPrefix(t, "`") && strings.HasSuffix(t, "`") {
|
|
return strings.TrimSpace(t[1 : len(t)-1])
|
|
}
|
|
if len(t) >= 2 && strings.HasPrefix(t, "[") && strings.HasSuffix(t, "]") {
|
|
return strings.TrimSpace(t[1 : len(t)-1])
|
|
}
|
|
return t
|
|
}
|
|
|
|
for i := 0; i < 8; i++ {
|
|
next := stripWrapperOnce(value)
|
|
if next == value {
|
|
break
|
|
}
|
|
value = next
|
|
}
|
|
value = strings.TrimSpace(value)
|
|
|
|
// 兼容错误的二次引用与残留反斜杠。
|
|
value = strings.ReplaceAll(value, `\"`, `"`)
|
|
value = strings.ReplaceAll(value, `""`, "")
|
|
value = strings.TrimSpace(value)
|
|
|
|
for i := 0; i < 8; i++ {
|
|
next := strings.TrimSpace(value)
|
|
changed := false
|
|
if strings.HasPrefix(next, `\`) && len(next) > 1 {
|
|
next = strings.TrimSpace(strings.TrimPrefix(next, `\`))
|
|
changed = true
|
|
}
|
|
if strings.HasSuffix(next, `\`) && len(next) > 1 {
|
|
next = strings.TrimSpace(strings.TrimSuffix(next, `\`))
|
|
changed = true
|
|
}
|
|
if !changed || next == value {
|
|
break
|
|
}
|
|
value = next
|
|
}
|
|
|
|
return strings.TrimSpace(value)
|
|
}
|
|
|
|
func splitKingbaseQualifiedNameCommon(raw string) (schema string, table string) {
|
|
text := strings.TrimSpace(raw)
|
|
if text == "" {
|
|
return "", ""
|
|
}
|
|
|
|
sep := findKingbaseQualifiedSeparator(text)
|
|
if sep < 0 {
|
|
return "", normalizeKingbaseIdentCommon(text)
|
|
}
|
|
|
|
schemaPart := normalizeKingbaseIdentCommon(text[:sep])
|
|
tablePart := normalizeKingbaseIdentCommon(text[sep+1:])
|
|
|
|
if tablePart == "" {
|
|
if schemaPart == "" {
|
|
return "", normalizeKingbaseIdentCommon(text)
|
|
}
|
|
return "", schemaPart
|
|
}
|
|
if schemaPart == "" {
|
|
return "", tablePart
|
|
}
|
|
return schemaPart, tablePart
|
|
}
|
|
|
|
func findKingbaseQualifiedSeparator(raw string) int {
|
|
inDouble := false
|
|
inBacktick := false
|
|
inBracket := false
|
|
escaped := false
|
|
|
|
for i := 0; i < len(raw); i++ {
|
|
ch := raw[i]
|
|
if escaped {
|
|
escaped = false
|
|
continue
|
|
}
|
|
|
|
if ch == '\\' {
|
|
escaped = true
|
|
continue
|
|
}
|
|
|
|
if inDouble {
|
|
if ch == '"' {
|
|
// SQL 双引号转义:"" 代表字面量 "
|
|
if i+1 < len(raw) && raw[i+1] == '"' {
|
|
i++
|
|
continue
|
|
}
|
|
inDouble = false
|
|
}
|
|
continue
|
|
}
|
|
|
|
if inBacktick {
|
|
if ch == '`' {
|
|
inBacktick = false
|
|
}
|
|
continue
|
|
}
|
|
|
|
if inBracket {
|
|
if ch == ']' {
|
|
inBracket = false
|
|
}
|
|
continue
|
|
}
|
|
|
|
switch ch {
|
|
case '"':
|
|
inDouble = true
|
|
case '`':
|
|
inBacktick = true
|
|
case '[':
|
|
inBracket = true
|
|
case '.':
|
|
return i
|
|
}
|
|
}
|
|
|
|
return -1
|
|
}
|