mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-22 08:50:17 +08:00
🐛 fix(ddl): 修复金仓建表语句缺少字段备注
- 在 fallback DDL 中追加字段备注语句 - 生成 COMMENT ON COLUMN 并处理单引号转义 - 补充金仓字段备注回归测试 Refs #459
This commit is contained in:
@@ -1105,6 +1105,7 @@ func buildFallbackCreateStatement(dbType string, schemaName string, tableName st
|
||||
|
||||
qualifiedTable := quoteTableIdentByType(dbType, schemaName, table)
|
||||
columnLines := make([]string, 0, len(columns)+1)
|
||||
columnCommentLines := make([]string, 0, len(columns))
|
||||
primaryKeys := make([]string, 0, 2)
|
||||
|
||||
for _, col := range columns {
|
||||
@@ -1131,6 +1132,9 @@ func buildFallbackCreateStatement(dbType string, schemaName string, tableName st
|
||||
}
|
||||
|
||||
columnLines = append(columnLines, " "+strings.Join(defParts, " "))
|
||||
if commentSQL := buildFallbackColumnCommentStatement(dbType, qualifiedTable, colNameRaw, col.Comment); commentSQL != "" {
|
||||
columnCommentLines = append(columnCommentLines, commentSQL)
|
||||
}
|
||||
if strings.EqualFold(strings.TrimSpace(col.Key), "PRI") {
|
||||
primaryKeys = append(primaryKeys, colName)
|
||||
}
|
||||
@@ -1149,9 +1153,23 @@ func buildFallbackCreateStatement(dbType string, schemaName string, tableName st
|
||||
ddl.WriteString(" (\n")
|
||||
ddl.WriteString(strings.Join(columnLines, ",\n"))
|
||||
ddl.WriteString("\n);")
|
||||
if len(columnCommentLines) > 0 {
|
||||
ddl.WriteString("\n")
|
||||
ddl.WriteString(strings.Join(columnCommentLines, "\n"))
|
||||
}
|
||||
return ddl.String(), nil
|
||||
}
|
||||
|
||||
func buildFallbackColumnCommentStatement(dbType string, qualifiedTable string, columnName string, comment string) string {
|
||||
colName := strings.TrimSpace(columnName)
|
||||
commentText := strings.TrimSpace(comment)
|
||||
if colName == "" || commentText == "" {
|
||||
return ""
|
||||
}
|
||||
columnRef := fmt.Sprintf("%s.%s", qualifiedTable, quoteIdentByType(dbType, colName))
|
||||
return fmt.Sprintf("COMMENT ON COLUMN %s IS '%s';", columnRef, strings.ReplaceAll(commentText, "'", "''"))
|
||||
}
|
||||
|
||||
func (a *App) DBGetColumns(config connection.ConnectionConfig, dbName string, tableName string) connection.QueryResult {
|
||||
runConfig := normalizeRunConfig(config, dbName)
|
||||
|
||||
|
||||
@@ -172,6 +172,35 @@ func TestResolveCreateStatementWithFallback_CustomKingbaseUsesPublicSchema(t *te
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveCreateStatementWithFallback_KingbaseIncludesColumnComments(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dbInst := &fakeCreateStatementDB{
|
||||
createSQL: "SHOW CREATE TABLE not directly supported in Kingbase/Postgres via SQL",
|
||||
columns: []connection.ColumnDefinition{
|
||||
{Name: "mes_third_sys_log_id", Type: "bigint", Nullable: "NO", Key: "PRI", Comment: "主键"},
|
||||
{Name: "api_name", Type: "character varying(100 char)", Nullable: "YES", Comment: "接口名称"},
|
||||
{Name: "request_param", Type: "longtext", Nullable: "YES", Comment: "请求参数's"},
|
||||
},
|
||||
}
|
||||
|
||||
ddl, err := resolveCreateStatementWithFallback(dbInst, connection.ConnectionConfig{
|
||||
Type: "kingbase",
|
||||
}, "demo_db", "ldf_server.mes_third_sys_log")
|
||||
if err != nil {
|
||||
t.Fatalf("resolveCreateStatementWithFallback() unexpected error: %v", err)
|
||||
}
|
||||
for _, want := range []string{
|
||||
`COMMENT ON COLUMN ldf_server.mes_third_sys_log.mes_third_sys_log_id IS '主键';`,
|
||||
`COMMENT ON COLUMN ldf_server.mes_third_sys_log.api_name IS '接口名称';`,
|
||||
`COMMENT ON COLUMN ldf_server.mes_third_sys_log.request_param IS '请求参数''s';`,
|
||||
} {
|
||||
if !strings.Contains(ddl, want) {
|
||||
t.Fatalf("expected fallback DDL to contain %q, got: %s", want, ddl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveCreateStatementWithFallback_KeepQualifiedSchema(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user