|
|
@@ -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(),
|