Selaa lähdekoodia

多数据库测试通过

qdy 2 kuukautta sitten
vanhempi
commit
e6df72bb2b

+ 23
- 19
functions/query_csv.go Näytä tiedosto

@@ -2,36 +2,40 @@ package functions
2 2
 
3 3
 import (
4 4
 	"git.x2erp.com/qdy/go-base/ctx"
5
-	"git.x2erp.com/qdy/go-base/types"
5
+	"git.x2erp.com/qdy/go-base/model/request"
6
+
6 7
 	"git.x2erp.com/qdy/go-db/factory/database"
7 8
 )
8 9
 
9 10
 // 执行查询,返回CSV数据格式。无参数查询
10
-func QueryToCSV(dbFactory *database.DBFactory, req types.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
11
-	csvData, err := dbFactory.QueryToCSV(req.SQL, req.WriterHeader, reqCtx)
12
-	//if err != nil {
13
-	//	logger.ErrorC(reqCtx, "QueryToCSV error: %v", err)
14
-	//}
15
-	return csvData, err
11
+func QueryToCSV(dbname string, dbsFactory *database.DBSFactory, req request.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
12
+
13
+	if dbFactory, err := dbsFactory.CreateDBFactory(dbname); err != nil {
14
+		return nil, err
15
+	} else {
16
+		return dbFactory.QueryToCSV(req.SQL, req.WriterHeader, reqCtx)
17
+	}
18
+
16 19
 }
17 20
 
18 21
 // 执行查询,返回CSV数据格式。带参数名称进行查询
19
-func QueryParamNameToCSV(dbFactory *database.DBFactory, req types.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
22
+func QueryParamNameToCSV(dbname string, dbsFactory *database.DBSFactory, req request.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
20 23
 
21
-	csvData, err := dbFactory.QueryParamsNameToCSV(req.SQL, req.WriterHeader, req.Params, reqCtx)
22
-	//if err != nil {
23
-	//	logger.ErrorC(reqCtx, "QueryParamsNameToCSV error: %v", err)
24
+	if dbFactory, err := dbsFactory.CreateDBFactory(dbname); err != nil {
25
+		return nil, err
26
+	} else {
27
+		return dbFactory.QueryParamsNameToCSV(req.SQL, req.WriterHeader, req.Params, reqCtx)
28
+	}
24 29
 
25
-	//}
26
-	return csvData, err
27 30
 }
28 31
 
29 32
 // 执行查询,返回CSV数据格式。带占位参数进行查询
30
-func QueryPositionalToCSV(dbFactory *database.DBFactory, req types.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
31
-	csvData, err := dbFactory.QueryPositionalToCSV(req.SQL, req.WriterHeader, req.PositionalParams, reqCtx)
32
-	//if err != nil {
33
-	//	logger.ErrorC(reqCtx, "QueryPositionalToCSV error: %v", err)
33
+func QueryPositionalToCSV(dbname string, dbsFactory *database.DBSFactory, req request.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
34
+
35
+	if dbFactory, err := dbsFactory.CreateDBFactory(dbname); err != nil {
36
+		return nil, err
37
+	} else {
38
+		return dbFactory.QueryPositionalToCSV(req.SQL, req.WriterHeader, req.PositionalParams, reqCtx)
39
+	}
34 40
 
35
-	//}
36
-	return csvData, err
37 41
 }

+ 25
- 7
functions/query_json.go Näytä tiedosto

@@ -2,23 +2,41 @@ package functions
2 2
 
3 3
 import (
4 4
 	"git.x2erp.com/qdy/go-base/ctx"
5
-	"git.x2erp.com/qdy/go-base/types"
5
+	"git.x2erp.com/qdy/go-base/model/request"
6
+	"git.x2erp.com/qdy/go-base/model/response"
7
+
6 8
 	"git.x2erp.com/qdy/go-db/factory/database"
7 9
 )
8 10
 
9 11
 // 执行查询,返回CSV数据格式。无参数查询
10
-func QueryToJSON(dbFactory *database.DBFactory, req types.QueryRequest, reqCtx *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}] {
11
-	return dbFactory.QueryToJSON(req.SQL, reqCtx)
12
+func QueryToJSON(dbname string, dbsFactory *database.DBSFactory, req request.QueryRequest, reqCtx *ctx.RequestContext) (*response.QueryResult[[]map[string]interface{}], error) {
13
+
14
+	if dbFactory, err := dbsFactory.CreateDBFactory(dbname); err != nil {
15
+		return nil, err
16
+	} else {
17
+		return dbFactory.QueryToJSON(req.SQL, reqCtx), nil
18
+	}
12 19
 
13 20
 }
14 21
 
15 22
 // 执行查询,返回CSV数据格式。带参数名称进行查询
16
-func QueryParamNameToJSON(dbFactory *database.DBFactory, req types.QueryRequest, reqCtx *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}] {
17
-	return dbFactory.QueryParamsNameToJSON(req.SQL, req.Params, reqCtx)
23
+func QueryParamNameToJSON(dbname string, dbsFactory *database.DBSFactory, req request.QueryRequest, reqCtx *ctx.RequestContext) (*response.QueryResult[[]map[string]interface{}], error) {
24
+
25
+	if dbFactory, err := dbsFactory.CreateDBFactory(dbname); err != nil {
26
+		return nil, err
27
+	} else {
28
+		return dbFactory.QueryParamsNameToJSON(req.SQL, req.Params, reqCtx), nil
29
+	}
30
+
18 31
 }
19 32
 
20 33
 // 执行查询,返回JSON数据格式。带占位参数进行查询
21
-func QueryPositionalToJSON(dbFactory *database.DBFactory, req types.QueryRequest, reqCtx *ctx.RequestContext) *types.QueryResult[[]map[string]interface{}] {
22
-	return dbFactory.QueryPositionalToJSON(req.SQL, req.PositionalParams, reqCtx)
34
+func QueryPositionalToJSON(dbname string, dbsFactory *database.DBSFactory, req request.QueryRequest, reqCtx *ctx.RequestContext) (*response.QueryResult[[]map[string]interface{}], error) {
35
+
36
+	if dbFactory, err := dbsFactory.CreateDBFactory(dbname); err != nil {
37
+		return nil, err
38
+	} else {
39
+		return dbFactory.QueryPositionalToJSON(req.SQL, req.PositionalParams, reqCtx), nil
40
+	}
23 41
 
24 42
 }

+ 81
- 31
main.go Näytä tiedosto

@@ -1,54 +1,104 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"net/http"
5
-
6
-	"git.x2erp.com/qdy/go-base/bootstraps"
7
-	"git.x2erp.com/qdy/go-base/middleware"
8 4
 	"git.x2erp.com/qdy/go-db/factory/database"
9
-	"git.x2erp.com/qdy/go-db/myhandle"
10 5
 	"git.x2erp.com/qdy/go-svc-agent/functions"
11
-	"go-micro.dev/v4/web"
6
+
7
+	"git.x2erp.com/qdy/go-base/config"
8
+	"git.x2erp.com/qdy/go-base/consul"
9
+	"git.x2erp.com/qdy/go-base/container"
10
+	"git.x2erp.com/qdy/go-base/ctx"
11
+	"git.x2erp.com/qdy/go-base/logger"
12
+	"git.x2erp.com/qdy/go-base/middleware"
13
+	"git.x2erp.com/qdy/go-base/model/request"
14
+	"git.x2erp.com/qdy/go-base/model/response"
15
+
16
+	"git.x2erp.com/qdy/go-base/webx"
17
+	"git.x2erp.com/qdy/go-base/webx/health"
18
+	"git.x2erp.com/qdy/go-base/webx/router"
12 19
 )
13 20
 
14 21
 var (
15
-	serviceName    = "svc-agent"
16
-	serviceVersion = "1.0.0"
22
+	appName    = "svc-core"
23
+	appVersion = "1"
17 24
 )
18 25
 
19 26
 func main() {
27
+	//0.日志
28
+	//logger.InitBootLog()
29
+	logBootFactory := logger.InitBootLog()
30
+
31
+	//1.获取配置文件
32
+	cfg := config.GetConfig()
33
+	cfg.SetAppName(appName)
34
+	cfg.SetAppVersion(appVersion)
35
+
36
+	//2.创建关闭容器
37
+	ctr := container.NewContainer(cfg)
38
+
39
+	//注册日志,实现自动关闭
40
+	container.Reg(ctr, logBootFactory)
41
+
42
+	//3.创建数据库工厂--如果需求
43
+	dbsFactory := container.Create(ctr, database.CreateDBSFactory)
44
+
45
+	// 赋值认证中间件参数
46
+	middleware.JWTAuthMiddlewareInit(cfg)
20 47
 
21
-	// 创建服务启动器
22
-	bootstrapper := bootstraps.NewServiceBootstrapper(serviceName, serviceVersion)
48
+	//得到webservice服务工厂
49
+	webxFactory := webx.GetWebServiceFactory()
23 50
 
24
-	//加载配置
25
-	bootstrapper.InitConfig()
51
+	//建立hhtpService服务
52
+	webServcie, _ := webxFactory.CreateService(cfg.GetServiceConfig())
26 53
 
27
-	//构建数据库工厂
28
-	bootstrapper.InitDatabase()
54
+	//建立路由-api
55
+	routerService := router.NewWebService(webServcie.GetRouter())
29 56
 
30
-	//dbFactory := bootstrapper.DbFactory
57
+	//注册路由--api
58
+	registerDefaultRouter(routerService, dbsFactory)
31 59
 
32
-	// 启动服务,传入路由注册函数
33
-	bootstrapper.Run(registerRoutes)
60
+	// 注册健康检查-api
61
+	health.RegisterConsulHealthCheck(routerService)
34 62
 
63
+	//启动服务
64
+	webServcie.Run()
65
+
66
+	//启用运行日志
67
+	container.Create(ctr, logger.InitRuntimeLogger)
68
+
69
+	//注册到注册中心
70
+	container.Create(ctr, consul.Register)
71
+	//等待关闭
72
+	webServcie.WaitForServiceShutdown(ctr)
35 73
 }
36 74
 
37
-// 注册所有路由
38
-func registerRoutes(webService web.Service, dbFactory *database.DBFactory) {
75
+func registerDefaultRouter(ws *router.RouterService, dbsFactory *database.DBSFactory) {
76
+
77
+	ws.POST("/api/query/json/{dbname}",
78
+		func(dbname string, req request.QueryRequest, reqCtx *ctx.RequestContext) (*response.QueryResult[[]map[string]interface{}], error) {
79
+
80
+			return functions.QueryToJSON(dbname, dbsFactory, req, reqCtx)
81
+		},
82
+	).Use(middleware.JWTAuthMiddleware).Register()
83
+
84
+	ws.POST("/api/query/csv/{dbname}",
85
+		func(dbname string, req request.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
39 86
 
40
-	// 查询接口 - JSON
41
-	webService.Handle("/api/query/json", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
42
-		myhandle.QueryHandlerJson(w, r, dbFactory, functions.QueryToJSON)
43
-	})))
87
+			return functions.QueryToCSV(dbname, dbsFactory, req, reqCtx)
88
+		},
89
+	).Use(middleware.JWTAuthMiddleware).Register()
90
+
91
+	ws.POST("/api/query/csv/param/{dbname}",
92
+		func(dbname string, req request.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
93
+
94
+			return functions.QueryPositionalToCSV(dbname, dbsFactory, req, reqCtx)
95
+
96
+		},
97
+	).Use(middleware.JWTAuthMiddleware).Register()
98
+
99
+}
44 100
 
45
-	// 查询接口 - CSV
46
-	webService.Handle("/api/query/csv", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
47
-		myhandle.QueryHandlerBytes(w, r, dbFactory, functions.QueryToCSV)
48
-	})))
101
+func getDBFactory(dbname string, dbsFactory *database.DBSFactory) (*database.DBFactory, error) {
49 102
 
50
-	// 查询接口 - CSV with positional params
51
-	webService.Handle("/api/query/csv/param", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52
-		myhandle.QueryHandlerBytes(w, r, dbFactory, functions.QueryPositionalToCSV)
53
-	})))
103
+	return dbsFactory.CreateDBFactory(dbname)
54 104
 }

+ 6
- 8
test/my0_test.go Näytä tiedosto

@@ -8,7 +8,8 @@ import (
8 8
 	"time"
9 9
 
10 10
 	"git.x2erp.com/qdy/go-base/config"
11
-	"git.x2erp.com/qdy/go-base/types"
11
+	"git.x2erp.com/qdy/go-base/model/request"
12
+
12 13
 	"git.x2erp.com/qdy/go-db/factory/doris"
13 14
 	"git.x2erp.com/qdy/go-db/factory/http"
14 15
 )
@@ -73,15 +74,12 @@ func TestQueryAndInsertToDoris1(t *testing.T) {
73 74
 	fmt.Println("Checking Doris table structure...")
74 75
 
75 76
 	// 获取Doris配置
76
-	cfg, err := config.GetConfig()
77
-	if err != nil {
78
-		t.Fatalf("failed to load config: %v", err)
79
-		return
80
-	}
77
+	cfg := config.GetConfig()
78
+
81 79
 	database := "X6_STOCK_DEV"
82 80
 	table := "A3_CLOTHING_LOG"
83 81
 	skipHeader := false // 改为true,跳过CSV头行
84
-	url := fmt.Sprintf("http://%s:%d/api/%s/%s/_stream_load", cfg.GetDoris().FEHost, cfg.GetDoris().FEPort, database, table)
82
+	url := fmt.Sprintf("http://%s:%d/api/%s/%s/_stream_load", cfg.GetDorisConfig().FEHost, cfg.GetDorisConfig().FEPort, database, table)
85 83
 	fmt.Printf("Doris stream load URL: %s\n", url)
86 84
 
87 85
 	// 计算需要的页数
@@ -119,7 +117,7 @@ func TestQueryAndInsertToDoris1(t *testing.T) {
119 117
 				queryStartTime := time.Now()
120 118
 
121 119
 				// 准备查询请求
122
-				queryRequest := types.QueryRequest{
120
+				queryRequest := request.QueryRequest{
123 121
 					SQL:              task.QuerySQL,
124 122
 					PositionalParams: task.QueryParams,
125 123
 					WriterHeader:     false,

+ 6
- 8
test/my_ora_clothingToDoris_test.go Näytä tiedosto

@@ -8,7 +8,8 @@ import (
8 8
 	"time"
9 9
 
10 10
 	"git.x2erp.com/qdy/go-base/config"
11
-	"git.x2erp.com/qdy/go-base/types"
11
+	"git.x2erp.com/qdy/go-base/model/request"
12
+
12 13
 	"git.x2erp.com/qdy/go-db/factory/doris"
13 14
 	"git.x2erp.com/qdy/go-db/factory/http"
14 15
 )
@@ -76,15 +77,12 @@ func TestQueryAndInsertToDoris(t *testing.T) {
76 77
 	fmt.Println("Checking Doris table structure...")
77 78
 
78 79
 	// 获取Doris配置
79
-	cfg, err := config.GetConfig()
80
-	if err != nil {
81
-		fmt.Println("failed to load config: %v", err)
82
-		return
83
-	}
80
+	cfg := config.GetConfig()
81
+
84 82
 	database := "X6_STOCK_DEV"
85 83
 	table := "A3_CLOTHING"
86 84
 	skipHeader := false // 改为true,跳过CSV头行
87
-	url := fmt.Sprintf("http://%s:%d/api/%s/%s/_stream_load", cfg.GetDoris().FEHost, cfg.GetDoris().FEPort, database, table)
85
+	url := fmt.Sprintf("http://%s:%d/api/%s/%s/_stream_load", cfg.GetDorisConfig().FEHost, cfg.GetDorisConfig().FEPort, database, table)
88 86
 	fmt.Printf("Doris stream load URL: %s\n", url)
89 87
 
90 88
 	// 计算需要的页数
@@ -122,7 +120,7 @@ func TestQueryAndInsertToDoris(t *testing.T) {
122 120
 				queryStartTime := time.Now()
123 121
 
124 122
 				// 准备查询请求
125
-				queryRequest := types.QueryRequest{
123
+				queryRequest := request.QueryRequest{
126 124
 					SQL:              task.QuerySQL,
127 125
 					PositionalParams: task.QueryParams,
128 126
 					WriterHeader:     false,

+ 3
- 3
test/my_post_query_test.go Näytä tiedosto

@@ -5,7 +5,7 @@ import (
5 5
 	"log"
6 6
 	"testing"
7 7
 
8
-	"git.x2erp.com/qdy/go-base/types"
8
+	"git.x2erp.com/qdy/go-base/model/request"
9 9
 	"git.x2erp.com/qdy/go-db/factory/http"
10 10
 )
11 11
 
@@ -36,7 +36,7 @@ func TestQuery(t *testing.T) {
36 36
 
37 37
 	sql, queryParams := getSQLWithPaginationSQL(0, 10)
38 38
 	// 准备查询请求
39
-	queryRequest := types.QueryRequest{
39
+	queryRequest := request.QueryRequest{
40 40
 		SQL:              sql,
41 41
 		PositionalParams: queryParams,
42 42
 		WriterHeader:     false,
@@ -45,7 +45,7 @@ func TestQuery(t *testing.T) {
45 45
 	httpClient := httpFactory.CreateClient()
46 46
 	// 发送POST请求到 /api/query/csv 获取CSV格式数据
47 47
 	resp, err := httpClient.PostWithAuth(
48
-		"http://localhost:8080/api/query/csv/param",
48
+		"http://localhost:8080/api/query/csv/param/bills",
49 49
 		queryRequest,
50 50
 		"123", // Bearer Token
51 51
 		nil,

+ 11
- 17
test/mycsv_test.go Näytä tiedosto

@@ -5,14 +5,14 @@ import (
5 5
 	"log"
6 6
 	"testing"
7 7
 
8
+	"git.x2erp.com/qdy/go-base/config"
8 9
 	"git.x2erp.com/qdy/go-db/factory/database"
9 10
 )
10 11
 
11 12
 func TestNamedParamsQueryCSV(t *testing.T) {
12
-	factory, err := database.GetDBFactory()
13
-	if err != nil {
14
-		t.Fatalf("Failed to get DB factory: %v", err)
15
-	}
13
+	cfg := config.GetConfig()
14
+	factory := database.CreateDBFactory(cfg)
15
+
16 16
 	defer factory.Close()
17 17
 
18 18
 	// 简化的SQL,只测试3个参数
@@ -50,20 +50,14 @@ func TestNamedParamsQueryCSV(t *testing.T) {
50 50
 	// 执行查询
51 51
 	result := factory.QueryPositionalToJSON(sql, params, nil)
52 52
 
53
-	// 检查结果 - 根据你的错误信息,result.Error 是 string 类型
54
-	if err != nil { // 改成检查空字符串
55
-		t.Errorf("Named parameters query failed: %s", err)
53
+	// 将请求参数输出为格式化的 JSON 日志
54
+	reqJSON, err := json.MarshalIndent(result, "", "  ")
55
+	if err != nil {
56
+		log.Printf("无法序列化请求参数: %v", err)
56 57
 	} else {
57
-		// 将请求参数输出为格式化的 JSON 日志
58
-		reqJSON, err := json.MarshalIndent(result, "", "  ")
59
-		if err != nil {
60
-			log.Printf("无法序列化请求参数: %v", err)
61
-		} else {
62
-			log.Printf("QueryRequest 参数:\n%s", string(reqJSON))
63
-		}
64
-
65
-		// 或者使用 fmt 输出
66
-		//fmt.Printf("=== QueryRequest ===\n%s\n====================\n", string(reqJSON))
58
+		log.Printf("QueryRequest 参数:\n%s", string(reqJSON))
67 59
 	}
68 60
 
61
+	// 或者使用 fmt 输出
62
+	//fmt.Printf("=== QueryRequest ===\n%s\n====================\n", string(reqJSON))
69 63
 }

+ 4
- 5
test/mysql_test.go Näytä tiedosto

@@ -5,16 +5,15 @@ import (
5 5
 	"log"
6 6
 	"testing"
7 7
 
8
+	"git.x2erp.com/qdy/go-base/config"
8 9
 	"git.x2erp.com/qdy/go-db/factory/database"
9 10
 )
10 11
 
11 12
 func TestNamedParamsQuery(t *testing.T) {
12
-	factory, err := database.GetDBFactory()
13
-	if err != nil {
14
-		t.Fatalf("Failed to get DB factory: %v", err)
15
-	}
16
-	defer factory.Close()
13
+	cfg := config.GetConfig()
14
+	factory := database.CreateDBFactory(cfg)
17 15
 
16
+	defer factory.Close()
18 17
 	// 简化的SQL,只测试3个参数
19 18
 	sql := `
20 19
 		SELECT * FROM (

Loading…
Peruuta
Tallenna