Procházet zdrojové kódy

Release v0.1.121123

qdy před 2 měsíci
revize
3589e67f06

+ 3
- 3
factory/database/db_factory.go Zobrazit soubor

@@ -121,19 +121,19 @@ func (f *DBFactory) GetDBType() string {
121 121
 }
122 122
 
123 123
 // QueryToJSON 快捷查询,直接返回 JSON 字节流
124
-func (f *DBFactory) QueryToJSON(sql string, reqCtx *ctx.RequestContext) *types.QueryResult {
124
+func (f *DBFactory) QueryToJSON(sql string, reqCtx *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}] {
125 125
 	return functions.QueryToJSON(f.db, sql, reqCtx)
126 126
 }
127 127
 
128 128
 // QueryParamsToJSON 位置参数查询并返回 JSON 字节数据
129
-func (f *DBFactory) QueryPositionalToJSON(sql string, params []interface{}, reqCtx *ctx.RequestContext) *types.QueryResult {
129
+func (f *DBFactory) QueryPositionalToJSON(sql string, params []interface{}, reqCtx *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}] {
130 130
 
131 131
 	return functions.QueryPositionalToJSON(f.db, sql, params, reqCtx)
132 132
 }
133 133
 
134 134
 // QueryParamsNameToJSON 命名参数查询并返回 JSON 字节数据
135 135
 // params 可以是 map[string]interface{} 或结构体
136
-func (f *DBFactory) QueryParamsNameToJSON(sql string, params map[string]interface{}, reqCtx *ctx.RequestContext) *types.QueryResult {
136
+func (f *DBFactory) QueryParamsNameToJSON(sql string, params map[string]interface{}, reqCtx *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}] {
137 137
 
138 138
 	return functions.QueryParamsNameToJSON(f.db, sql, params, reqCtx)
139 139
 }

+ 1
- 1
factory/rabbitmq/consumer_factory.go Zobrazit soubor

@@ -9,7 +9,7 @@ import (
9 9
 
10 10
 // MessageHandler 消息处理器接口
11 11
 type MessageHandler interface {
12
-	Process(ueueRequest *types.QueueRequest, body []byte) types.QueryResult
12
+	Process(ueueRequest *types.QueueRequest, body []byte) types.QueryResult[interface{}]
13 13
 }
14 14
 
15 15
 // Consumer 消费者结构体

+ 8
- 22
functions/query_json.go Zobrazit soubor

@@ -2,7 +2,6 @@ package functions
2 2
 
3 3
 import (
4 4
 	"database/sql"
5
-	"encoding/json"
6 5
 	"fmt"
7 6
 	"time"
8 7
 
@@ -12,7 +11,7 @@ import (
12 11
 )
13 12
 
14 13
 // QueryParamsNameToJSON 执行带命名参数的查询
15
-func QueryParamsNameToJSON(db *sqlx.DB, sql string, params map[string]interface{}, reqCtx *ctx.RequestContext) *types.QueryResult {
14
+func QueryParamsNameToJSON(db *sqlx.DB, sql string, params map[string]interface{}, reqCtx *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}] {
16 15
 	startTime := time.Now()
17 16
 
18 17
 	if sql == "" {
@@ -38,7 +37,7 @@ func QueryParamsNameToJSON(db *sqlx.DB, sql string, params map[string]interface{
38 37
 }
39 38
 
40 39
 // QueryPositionalToJSON 执行带位置参数的查询
41
-func QueryPositionalToJSON(db *sqlx.DB, sql string, params []interface{}, reqCtx *ctx.RequestContext) *types.QueryResult {
40
+func QueryPositionalToJSON(db *sqlx.DB, sql string, params []interface{}, reqCtx *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}] {
42 41
 	startTime := time.Now()
43 42
 
44 43
 	//fmt.Printf("positionalParams: %s", params)
@@ -59,7 +58,7 @@ func QueryPositionalToJSON(db *sqlx.DB, sql string, params []interface{}, reqCtx
59 58
 }
60 59
 
61 60
 // QueryToJSON 执行无参数的查询
62
-func QueryToJSON(db *sqlx.DB, sql string, reqCtx *ctx.RequestContext) *types.QueryResult {
61
+func QueryToJSON(db *sqlx.DB, sql string, reqCtx *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}] {
63 62
 	startTime := time.Now()
64 63
 
65 64
 	if sql == "" {
@@ -78,9 +77,9 @@ func QueryToJSON(db *sqlx.DB, sql string, reqCtx *ctx.RequestContext) *types.Que
78 77
 }
79 78
 
80 79
 // processQueryResult 处理查询结果(公共部分)
81
-func processQueryResult(rows *sql.Rows, startTime time.Time) *types.QueryResult {
82
-	result := &types.QueryResult{}
80
+func processQueryResult(rows *sql.Rows, startTime time.Time) *types.QueryResult[[]map[string]interface{}] {
83 81
 
82
+	result := &types.QueryResult[[]map[string]interface{}]{}
84 83
 	// 获取列信息
85 84
 	columns, err := rows.Columns()
86 85
 	if err != nil {
@@ -125,30 +124,17 @@ func processQueryResult(rows *sql.Rows, startTime time.Time) *types.QueryResult
125 124
 		return result
126 125
 	}
127 126
 
128
-	// 转换为JSON
129
-	jsonData, err := json.Marshal(results)
130
-	if err != nil {
131
-		result.Success = false
132
-		result.Error = fmt.Sprintf("JSON marshal failed: %v", err)
133
-		result.Time = time.Since(startTime).String()
134
-		return result
135
-	}
136
-
137 127
 	// 构建成功结果
138 128
 	result.Success = true
139
-	result.Data = map[string]interface{}{
140
-		"json":  string(jsonData),
141
-		"rows":  results,
142
-		"count": count,
143
-	}
129
+	result.Data = results
144 130
 	result.Count = count
145 131
 	result.Time = time.Since(startTime).String()
146 132
 	return result
147 133
 }
148 134
 
149 135
 // createErrorResult 创建错误结果的辅助函数
150
-func createErrorResult(errorMsg string, startTime time.Time, reqCtx *ctx.RequestContext) *types.QueryResult {
151
-	return &types.QueryResult{
136
+func createErrorResult(errorMsg string, startTime time.Time, reqCtx *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}] {
137
+	return &types.QueryResult[[]map[string]interface{}]{
152 138
 		Success:  false,
153 139
 		Error:    errorMsg,
154 140
 		Time:     time.Since(startTime).String(),

+ 2
- 2
myhandle/query_handler_json.go Zobrazit soubor

@@ -14,14 +14,14 @@ func QueryHandlerJson[T any, F any](
14 14
 	w http.ResponseWriter,
15 15
 	r *http.Request,
16 16
 	factory F,
17
-	handlerFunc func(F, T, *ctx.RequestContext) *types.QueryResult,
17
+	handlerFunc func(F, T, *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}],
18 18
 ) {
19 19
 	// 解析请求参数
20 20
 	var req T
21 21
 	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
22 22
 		// 如果解析失败,也返回 QueryResult 格式
23 23
 		w.Header().Set("Content-Type", "application/json")
24
-		json.NewEncoder(w).Encode(&types.QueryResult{
24
+		json.NewEncoder(w).Encode(&types.QueryResult[[]map[string]interface{}]{
25 25
 			Success: false,
26 26
 			Error:   "Invalid request body: " + err.Error(),
27 27
 			Time:    time.Now().Format(time.RFC3339),

+ 38
- 0
myhandle/query_handler_map.go Zobrazit soubor

@@ -0,0 +1,38 @@
1
+package myhandle
2
+
3
+import (
4
+	"encoding/json"
5
+	"net/http"
6
+	"time"
7
+
8
+	"git.x2erp.com/qdy/go-base/ctx"
9
+	"git.x2erp.com/qdy/go-base/types"
10
+)
11
+
12
+// queryHandler - 最简单的版本
13
+func QueryHandlerMap[T any, F any](
14
+	w http.ResponseWriter,
15
+	r *http.Request,
16
+	factory F,
17
+	handlerFunc func(F, T, *ctx.RequestContext) *types.QueryResult[map[string]interface{}],
18
+) {
19
+	// 解析请求参数
20
+	var req T
21
+	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
22
+		// 如果解析失败,也返回 QueryResult 格式
23
+		w.Header().Set("Content-Type", "application/json")
24
+		json.NewEncoder(w).Encode(&types.QueryResult[[]map[string]interface{}]{
25
+			Success: false,
26
+			Error:   "Invalid request body: " + err.Error(),
27
+			Time:    time.Now().Format(time.RFC3339),
28
+		})
29
+		return
30
+	}
31
+
32
+	// 执行业务逻辑
33
+	result := handlerFunc(factory, req, ctx.GetContext(r))
34
+
35
+	// 设置头,写入结果
36
+	w.Header().Set("Content-Type", "application/json")
37
+	json.NewEncoder(w).Encode(result)
38
+}

Loading…
Zrušit
Uložit