mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-06 20:03:05 +08:00
- 统一同库同步与跨库迁移入口,补充模式区分与风险提示 - 扩展 ClickHouse 与 PG-like 双向迁移,并新增 PG-like、ClickHouse、TDengine 到 MongoDB 的迁移路由 - 完善 TDengine 目标端建表规划、回归测试与需求追踪文档 - refs #51
124 lines
2.7 KiB
Go
124 lines
2.7 KiB
Go
package sync
|
|
|
|
import "strings"
|
|
|
|
func normalizeSyncMode(mode string) string {
|
|
m := strings.ToLower(strings.TrimSpace(mode))
|
|
switch m {
|
|
case "", "insert_update":
|
|
return "insert_update"
|
|
case "insert_only":
|
|
return "insert_only"
|
|
case "full_overwrite":
|
|
return "full_overwrite"
|
|
default:
|
|
return "insert_update"
|
|
}
|
|
}
|
|
|
|
func quoteIdentByType(dbType string, ident string) string {
|
|
if ident == "" {
|
|
return ident
|
|
}
|
|
|
|
switch dbType {
|
|
case "mysql", "mariadb", "diros", "sphinx", "clickhouse", "tdengine":
|
|
return "`" + strings.ReplaceAll(ident, "`", "``") + "`"
|
|
case "sqlserver":
|
|
escaped := strings.ReplaceAll(ident, "]", "]]")
|
|
return "[" + escaped + "]"
|
|
default:
|
|
return `"` + strings.ReplaceAll(ident, `"`, `""`) + `"`
|
|
}
|
|
}
|
|
|
|
func quoteQualifiedIdentByType(dbType string, ident string) string {
|
|
raw := strings.TrimSpace(ident)
|
|
if raw == "" {
|
|
return raw
|
|
}
|
|
|
|
parts := strings.Split(raw, ".")
|
|
if len(parts) <= 1 {
|
|
return quoteIdentByType(dbType, raw)
|
|
}
|
|
|
|
quotedParts := make([]string, 0, len(parts))
|
|
for _, part := range parts {
|
|
part = strings.TrimSpace(part)
|
|
if part == "" {
|
|
continue
|
|
}
|
|
quotedParts = append(quotedParts, quoteIdentByType(dbType, part))
|
|
}
|
|
|
|
if len(quotedParts) == 0 {
|
|
return quoteIdentByType(dbType, raw)
|
|
}
|
|
return strings.Join(quotedParts, ".")
|
|
}
|
|
|
|
func normalizeSchemaAndTable(dbType string, dbName string, tableName string) (string, string) {
|
|
rawTable := strings.TrimSpace(tableName)
|
|
rawDB := strings.TrimSpace(dbName)
|
|
if rawTable == "" {
|
|
return rawDB, rawTable
|
|
}
|
|
|
|
if parts := strings.SplitN(rawTable, ".", 2); len(parts) == 2 {
|
|
schema := strings.TrimSpace(parts[0])
|
|
table := strings.TrimSpace(parts[1])
|
|
if schema != "" && table != "" {
|
|
return schema, table
|
|
}
|
|
}
|
|
|
|
switch strings.ToLower(strings.TrimSpace(dbType)) {
|
|
case "postgres", "kingbase", "highgo", "vastbase":
|
|
return "public", rawTable
|
|
case "duckdb":
|
|
return "main", rawTable
|
|
default:
|
|
return rawDB, rawTable
|
|
}
|
|
}
|
|
|
|
func qualifiedNameForQuery(dbType string, schema string, table string, original string) string {
|
|
raw := strings.TrimSpace(original)
|
|
if raw == "" {
|
|
return raw
|
|
}
|
|
if strings.Contains(raw, ".") {
|
|
return raw
|
|
}
|
|
|
|
switch strings.ToLower(strings.TrimSpace(dbType)) {
|
|
case "postgres", "kingbase", "highgo", "vastbase":
|
|
s := strings.TrimSpace(schema)
|
|
if s == "" {
|
|
s = "public"
|
|
}
|
|
if table == "" {
|
|
return raw
|
|
}
|
|
return s + "." + table
|
|
case "duckdb":
|
|
s := strings.TrimSpace(schema)
|
|
if s == "" {
|
|
s = "main"
|
|
}
|
|
if table == "" {
|
|
return raw
|
|
}
|
|
return s + "." + table
|
|
case "mysql", "mariadb", "diros", "sphinx", "clickhouse", "tdengine":
|
|
s := strings.TrimSpace(schema)
|
|
if s == "" || table == "" {
|
|
return table
|
|
}
|
|
return s + "." + table
|
|
default:
|
|
return table
|
|
}
|
|
}
|