Quellcode durchsuchen

添加注册中心代码,注册到consul

qdy vor 2 Monaten
Ursprung
Commit
e829169ce9

+ 1
- 1
factory/doris/doris_factory.go Datei anzeigen

@@ -45,7 +45,7 @@ func GetDorisFactory(httpFactory *http.HTTPFactory) (*DorisFactory, error) {
45 45
 			return
46 46
 		}
47 47
 
48
-		dorisConfig := cfg.GetDoris()
48
+		dorisConfig := cfg.GetDorisConfig()
49 49
 		instance = &DorisFactory{
50 50
 			client:   httpFactory.GetUnderlyingClient(),
51 51
 			FEHost:   dorisConfig.FEHost,

+ 1
- 1
factory/rabbitmq/rabbitmq_factory.go Datei anzeigen

@@ -26,7 +26,7 @@ func NewRabbitMQFactory() (*RabbitMQFactory, error) {
26 26
 		return nil, fmt.Errorf("failed to load config: %v", err)
27 27
 	}
28 28
 
29
-	rabbitConfig := cfg.GetRabbitMQ()
29
+	rabbitConfig := cfg.GetRabbitMQConfig()
30 30
 	if rabbitConfig.Host == "" || rabbitConfig.Port == 0 {
31 31
 		return nil, fmt.Errorf("rabbitmq configuration is incomplete")
32 32
 	}

+ 7
- 8
factory/redis/redis_factory.go Datei anzeigen

@@ -6,6 +6,7 @@ import (
6 6
 	"time"
7 7
 
8 8
 	"git.x2erp.com/qdy/go-base/config"
9
+	"git.x2erp.com/qdy/go-base/config/subconfigs"
9 10
 	"github.com/go-redis/redis/v8"
10 11
 )
11 12
 
@@ -13,27 +14,25 @@ var ctx = context.Background()
13 14
 
14 15
 // RedisFactory Redis客户端工厂
15 16
 type RedisFactory struct {
16
-	config config.IConfig
17
+	redisConfig *subconfigs.RedisConfig
17 18
 }
18 19
 
19 20
 // NewRedisFactory 创建Redis客户端工厂
20 21
 func NewRedisFactory() (*RedisFactory, error) {
21
-	cfg, err := config.GetConfig()
22 22
 
23
-	if err != nil {
24
-		return nil, fmt.Errorf("failed to load config: %v", err)
25
-	}
23
+	cfg := config.GetRedis()
26 24
 
27
-	if !cfg.IsRedisConfigured() {
25
+	// 可以添加配置验证
26
+	if cfg == nil || cfg.Host == "" {
28 27
 		return nil, fmt.Errorf("redis configuration is incomplete")
29 28
 	}
30 29
 
31
-	return &RedisFactory{config: cfg}, nil
30
+	return &RedisFactory{redisConfig: cfg}, nil
32 31
 }
33 32
 
34 33
 // CreateRedisClient 创建Redis客户端
35 34
 func (f *RedisFactory) CreateRedisClient() *RedisClient {
36
-	redisConfig := f.config.GetRedis()
35
+	redisConfig := f.redisConfig
37 36
 
38 37
 	client := redis.NewClient(&redis.Options{
39 38
 		Addr:     fmt.Sprintf("%s:%d", redisConfig.Host, redisConfig.Port),

+ 213
- 0
sqldef/dict_sql_generator.go Datei anzeigen

@@ -0,0 +1,213 @@
1
+package sqldef
2
+
3
+import (
4
+	"fmt"
5
+	"strings"
6
+	"time"
7
+	"unicode/utf8"
8
+
9
+	"git.x2erp.com/qdy/go-db/sqldef/generators"
10
+)
11
+
12
+// DictSQLGenerator 字典表SQL生成器
13
+type DictSQLGenerator struct {
14
+	TableIDPrefix  string // 表ID前缀,如 "TBL"
15
+	ColumnIDPrefix string // 字段ID前缀,如 "COL"
16
+}
17
+
18
+// NewDictSQLGenerator 创建生成器
19
+func NewDictSQLGenerator() *DictSQLGenerator {
20
+	return &DictSQLGenerator{
21
+		TableIDPrefix:  "TBL",
22
+		ColumnIDPrefix: "COL",
23
+	}
24
+}
25
+
26
+// DictSQLs 字典表SQL集合
27
+type DictSQLs struct {
28
+	TableSQL   string            // 主表SQL
29
+	ColumnSQLs map[string]string // 字段名 -> SQL映射
30
+}
31
+
32
+// GenerateDictSQLsFromTable 从TableDDL生成字典表SQL
33
+func (gen *DictSQLGenerator) GenerateDictSQLsFromTable(table generators.TableDDL) (*DictSQLs, error) {
34
+	if table.Schema == nil {
35
+		return nil, fmt.Errorf("表 %s 没有Schema信息", table.Name)
36
+	}
37
+
38
+	// 生成表ID
39
+	tableID := gen.generateTableID(table.Name)
40
+
41
+	// 1. 生成主表SQL
42
+	tableSQL := gen.generateMainTableSQL(tableID, table)
43
+
44
+	// 2. 生成子表SQL映射
45
+	columnSQLs := gen.generateColumnSQLs(tableID, table.Schema.Columns)
46
+
47
+	return &DictSQLs{
48
+		TableSQL:   tableSQL,
49
+		ColumnSQLs: columnSQLs,
50
+	}, nil
51
+}
52
+
53
+// GenerateDictSQLsFromRegistry 从注册表中获取所有表的字典SQL
54
+func (gen *DictSQLGenerator) GenerateDictSQLsFromRegistry() (map[string]*DictSQLs, error) {
55
+	globalRegistry.ensureInit()
56
+
57
+	globalRegistry.mu.RLock()
58
+	defer globalRegistry.mu.RUnlock()
59
+
60
+	result := make(map[string]*DictSQLs)
61
+
62
+	for tableName, tableDDL := range globalRegistry.tables {
63
+		sqls, err := gen.GenerateDictSQLsFromTable(tableDDL)
64
+		if err != nil {
65
+			return nil, fmt.Errorf("生成表 %s 的字典SQL失败: %w", tableName, err)
66
+		}
67
+		result[tableName] = sqls
68
+	}
69
+
70
+	return result, nil
71
+}
72
+
73
+// generateTableID 生成表ID
74
+func (gen *DictSQLGenerator) generateTableID(tableName string) string {
75
+	// 使用表名首字母大写 + 时间戳
76
+	timestamp := time.Now().UnixNano() / 1000000 // 毫秒
77
+	firstChar, _ := utf8.DecodeRuneInString(tableName)
78
+	if firstChar == utf8.RuneError {
79
+		firstChar = 'T'
80
+	}
81
+
82
+	return fmt.Sprintf("%s%c%06d", gen.TableIDPrefix, firstChar, timestamp%1000000)
83
+}
84
+
85
+// generateMainTableSQL 生成主表SQL
86
+func (gen *DictSQLGenerator) generateMainTableSQL(tableID string, table generators.TableDDL) string {
87
+	// 转义表注释中的单引号
88
+	comment := strings.ReplaceAll(table.Comment, "'", "''")
89
+
90
+	sql := fmt.Sprintf("INSERT INTO dict_table (table_id, table_name, table_comment, created_at) VALUES ('%s', '%s', '%s', NOW());",
91
+		tableID, table.Name, comment)
92
+
93
+	return sql
94
+}
95
+
96
+// generateColumnSQLs 生成子表SQL映射
97
+func (gen *DictSQLGenerator) generateColumnSQLs(tableID string, columns []generators.ColumnSchema) map[string]string {
98
+	sqlMap := make(map[string]string)
99
+
100
+	for i, column := range columns {
101
+		// 生成字段ID
102
+		columnID := gen.generateColumnID(tableID, column.Name, i)
103
+
104
+		// 转义字段注释中的单引号
105
+		comment := strings.ReplaceAll(column.Comment, "'", "''")
106
+
107
+		// 生成字段类型字符串
108
+		columnType := gen.generateColumnTypeString(column)
109
+
110
+		// 判断是否可为空
111
+		isNullable := "1" // true
112
+		if gen.isNotNullColumn(column.Options) {
113
+			isNullable = "0" // false
114
+		}
115
+
116
+		// 处理默认值
117
+		defaultValue := gen.formatDefaultValue(column.Default)
118
+
119
+		// 判断是否为主键
120
+		isPrimaryKey := "0" // false
121
+		if gen.isPrimaryKeyColumn(column.Options) {
122
+			isPrimaryKey = "1" // true
123
+		}
124
+
125
+		sql := fmt.Sprintf("INSERT INTO dict_column (column_id, table_id, column_name, column_type, column_comment, is_nullable, column_default, sort_order, created_at, is_primary_key) VALUES ('%s', '%s', '%s', '%s', '%s', %s, %s, %d, NOW(), %s);",
126
+			columnID, tableID, column.Name, columnType, comment, isNullable, defaultValue, i, isPrimaryKey)
127
+
128
+		sqlMap[column.Name] = sql
129
+	}
130
+
131
+	return sqlMap
132
+}
133
+
134
+// generateColumnID 生成字段ID
135
+func (gen *DictSQLGenerator) generateColumnID(tableID string, columnName string, index int) string {
136
+	// 使用表ID + 字段名首字母 + 序号
137
+	firstChar, _ := utf8.DecodeRuneInString(columnName)
138
+	if firstChar == utf8.RuneError {
139
+		firstChar = 'C'
140
+	}
141
+
142
+	return fmt.Sprintf("%s%c%02d", gen.ColumnIDPrefix, firstChar, index)
143
+}
144
+
145
+// generateColumnTypeString 生成字段类型字符串
146
+func (gen *DictSQLGenerator) generateColumnTypeString(column generators.ColumnSchema) string {
147
+	switch column.Type {
148
+	case TypeVarchar:
149
+		return fmt.Sprintf("VARCHAR(%d)", column.Length)
150
+	case TypeChar:
151
+		return fmt.Sprintf("CHAR(%d)", column.Length)
152
+	case TypeDecimal:
153
+		return fmt.Sprintf("DECIMAL(%d,%d)", column.Precision, column.Scale)
154
+	case TypeInt, TypeBigInt, TypeTinyInt:
155
+		return column.Type
156
+	case TypeDateTime, TypeTimestamp, TypeDate, TypeTime:
157
+		return column.Type
158
+	case TypeText:
159
+		return "TEXT"
160
+	case TypeBool:
161
+		return "BOOL"
162
+	case TypeJson:
163
+		return "JSON"
164
+	case TypeBlob:
165
+		return "BLOB"
166
+	case TypeFloat:
167
+		return "FLOAT"
168
+	case TypeDouble:
169
+		return "DOUBLE"
170
+	default:
171
+		return column.Type
172
+	}
173
+}
174
+
175
+// isNotNullColumn 判断是否为NOT NULL列
176
+func (gen *DictSQLGenerator) isNotNullColumn(options []string) bool {
177
+	for _, opt := range options {
178
+		if strings.EqualFold(opt, "NOT NULL") {
179
+			return true
180
+		}
181
+	}
182
+	return false
183
+}
184
+
185
+// isPrimaryKeyColumn 判断是否为主键列
186
+func (gen *DictSQLGenerator) isPrimaryKeyColumn(options []string) bool {
187
+	for _, opt := range options {
188
+		if strings.EqualFold(opt, "PRIMARY KEY") {
189
+			return true
190
+		}
191
+	}
192
+	return false
193
+}
194
+
195
+// formatDefaultValue 格式化默认值
196
+func (gen *DictSQLGenerator) formatDefaultValue(defaultValue string) string {
197
+	if defaultValue == "" {
198
+		return "NULL"
199
+	}
200
+
201
+	// 如果是 CURRENT_TIMESTAMP 等函数,直接使用
202
+	upperValue := strings.ToUpper(defaultValue)
203
+	if strings.Contains(upperValue, "CURRENT_TIMESTAMP") ||
204
+		strings.Contains(upperValue, "NULL") ||
205
+		strings.Contains(upperValue, "TRUE") ||
206
+		strings.Contains(upperValue, "FALSE") {
207
+		return fmt.Sprintf("'%s'", defaultValue)
208
+	}
209
+
210
+	// 其他情况需要转义单引号
211
+	escapedValue := strings.ReplaceAll(defaultValue, "'", "''")
212
+	return fmt.Sprintf("'%s'", escapedValue)
213
+}

+ 65
- 0
sqldef/table_manager.go Datei anzeigen

@@ -219,3 +219,68 @@ func DeleteAanCreateAllTables(executor DDLExecutor) error {
219 219
 	tm := GetTableManagerWithExecutor(executor)
220 220
 	return tm.createTables(true)
221 221
 }
222
+
223
+// GenerateDictSQLs 生成表的字典表SQL
224
+func (tm *TableManager) GenerateDictSQLs(tableName string) (string, map[string]string, error) {
225
+	// 获取表的DDL定义
226
+	tableDDL, exists := Get(tableName)
227
+	if !exists {
228
+		return "", nil, fmt.Errorf("表 %s 不存在或未注册", tableName)
229
+	}
230
+
231
+	// 生成字典SQL
232
+	generator := NewDictSQLGenerator()
233
+	sqls, err := generator.GenerateDictSQLsFromTable(tableDDL)
234
+	if err != nil {
235
+		return "", nil, fmt.Errorf("生成字典SQL失败: %w", err)
236
+	}
237
+
238
+	return sqls.TableSQL, sqls.ColumnSQLs, nil
239
+}
240
+
241
+// GenerateAllDictSQLs 生成所有注册表的字典表SQL
242
+func (tm *TableManager) GenerateAllDictSQLs() (map[string]struct {
243
+	TableSQL   string
244
+	ColumnSQLs map[string]string
245
+}, error) {
246
+	// 生成所有字典SQL
247
+	generator := NewDictSQLGenerator()
248
+	allSQLs, err := generator.GenerateDictSQLsFromRegistry()
249
+	if err != nil {
250
+		return nil, fmt.Errorf("生成所有字典SQL失败: %w", err)
251
+	}
252
+
253
+	// 转换为返回格式
254
+	result := make(map[string]struct {
255
+		TableSQL   string
256
+		ColumnSQLs map[string]string
257
+	})
258
+
259
+	for tableName, sqls := range allSQLs {
260
+		result[tableName] = struct {
261
+			TableSQL   string
262
+			ColumnSQLs map[string]string
263
+		}{
264
+			TableSQL:   sqls.TableSQL,
265
+			ColumnSQLs: sqls.ColumnSQLs,
266
+		}
267
+	}
268
+
269
+	return result, nil
270
+}
271
+
272
+// 包级便捷函数
273
+// GenerateDictSQLs 生成表的字典表SQL(便捷函数)
274
+func GenerateDictSQLs(tableName string) (string, map[string]string, error) {
275
+	tm := GetTableManager()
276
+	return tm.GenerateDictSQLs(tableName)
277
+}
278
+
279
+// GenerateAllDictSQLs 生成所有表的字典表SQL(便捷函数)
280
+func GenerateAllDictSQLs() (map[string]struct {
281
+	TableSQL   string
282
+	ColumnSQLs map[string]string
283
+}, error) {
284
+	tm := GetTableManager()
285
+	return tm.GenerateAllDictSQLs()
286
+}

Laden…
Abbrechen
Speichern