Browse Source

添加ds支持

yjp 1 year ago
parent
commit
d02081bc34

+ 71 - 0
framework/core/infrastructure/database/data_service/data_service.go

@@ -0,0 +1,71 @@
+package data_service
+
+import (
+	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
+	"git.sxidc.com/go-tools/utils/template"
+	"git.sxidc.com/service-supports/ds-sdk/sdk"
+	"github.com/pkg/errors"
+	"time"
+)
+
+type Config struct {
+	Token          string `json:"token" yaml:"token"`
+	Address        string `json:"address" yaml:"address"`
+	HttpPort       string `json:"http_port" yaml:"http_port"`
+	GrpcPort       string `json:"grpc_port" yaml:"grpc_port"`
+	Namespace      string `json:"namespace" yaml:"namespace"`
+	DataSource     string `json:"data_source" yaml:"data_source"`
+	HttpTimeoutSec int    `json:"http_timeout_sec" yaml:"http_timeout_sec"`
+}
+
+type Executor struct{}
+
+func NewExecutor(conf *Config) (*Executor, error) {
+	if conf.HttpTimeoutSec == 0 {
+		conf.HttpTimeoutSec = 30
+	}
+
+	err := sdk.InitInstance(conf.Token, conf.Address, conf.HttpPort, conf.GrpcPort, conf.Namespace, conf.DataSource,
+		sdk.WithTimeout(time.Duration(conf.HttpTimeoutSec)*time.Second))
+	if err != nil {
+		return nil, errors.New(err.Error())
+	}
+
+	return &Executor{}, nil
+}
+
+func DestroyExecutor(executor *Executor) error {
+	if executor == nil {
+		return nil
+	}
+
+	err := sdk.DestroyInstance()
+	if err != nil {
+		return errors.New(err.Error())
+	}
+
+	return nil
+}
+
+func (executor *Executor) ExecuteRawSql(sqlStr string, args ...any) ([]sql.Result, error) {
+	return executor.ExecuteRawSqlTemplate(sqlStr, nil, args...)
+}
+
+func (executor *Executor) ExecuteRawSqlTemplate(sqlStr string, executeParams map[string]any, args ...any) ([]sql.Result, error) {
+	parsedSql, err := template.ParseTemplateStringToString(sqlStr, executeParams)
+	if err != nil {
+		return nil, errors.New(err.Error())
+	}
+
+	tableRows, err := sdk.GetInstance().ExecuteRawSql(parsedSql, args...)
+	if err != nil {
+		return nil, errors.New(err.Error())
+	}
+
+	results := make([]sql.Result, len(tableRows))
+	for i, row := range tableRows {
+		results[i] = row
+	}
+
+	return results, nil
+}

+ 0 - 12
framework/core/infrastructure/database/operations/operations.go

@@ -2,7 +2,6 @@ package operations
 
 import (
 	dbSql "database/sql"
-	"encoding/json"
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
 	"git.sxidc.com/go-tools/utils/template"
@@ -160,17 +159,6 @@ func (op *Operations) ExecuteRawSqlTemplate(sqlStr string, executeParams map[str
 		return nil, errors.New(err.Error())
 	}
 
-	// 简化一下类型体系,与DataService保持一致
-	jsonTableRows, err := json.Marshal(tableRows)
-	if err != nil {
-		return nil, errors.New(err.Error())
-	}
-
-	err = json.Unmarshal(jsonTableRows, &tableRows)
-	if err != nil {
-		return nil, errors.New(err.Error())
-	}
-
 	results := make([]sql.Result, len(tableRows))
 	for i, row := range tableRows {
 		results[i] = row

+ 15 - 1
framework/core/infrastructure/infrastructure.go

@@ -5,6 +5,7 @@ import (
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache/local"
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache/redis"
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
+	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/data_service"
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/operations"
 	"git.sxidc.com/go-tools/utils/strutils"
 )
@@ -15,7 +16,8 @@ type Config struct {
 }
 
 type DatabaseConfig struct {
-	Operations *operations.Config `json:"operations" yaml:"operations"`
+	Operations  *operations.Config   `json:"operations" yaml:"operations"`
+	DataService *data_service.Config `json:"data_service" yaml:"data_service"`
 }
 
 type CacheConfig struct {
@@ -46,6 +48,13 @@ func NewInfrastructure(config Config) *Infrastructure {
 		}
 
 		i.dbExecutor = op
+	} else if config.DatabaseConfig.DataService != nil {
+		executor, err := data_service.NewExecutor(config.DatabaseConfig.DataService)
+		if err != nil {
+			panic("创建数据服务执行器失败: " + err.Error())
+		}
+
+		i.dbExecutor = executor
 	}
 
 	// 初始化缓存
@@ -92,6 +101,11 @@ func DestroyInfrastructure(i *Infrastructure) {
 			if err != nil {
 				panic("销毁数据库操作失败: " + err.Error())
 			}
+		case *data_service.Executor:
+			err := data_service.DestroyExecutor(dbExecutor)
+			if err != nil {
+				panic("销毁数据库操作失败: " + err.Error())
+			}
 		default:
 			panic("不支持的数据库执行器类型")
 		}

+ 11 - 7
go.mod

@@ -3,21 +3,17 @@ module git.sxidc.com/go-framework/baize
 go 1.22.3
 
 require (
-	git.sxidc.com/go-tools/utils v1.5.15
-	git.sxidc.com/service-supports/websocket v1.3.1
+	git.sxidc.com/go-tools/utils v1.5.23
+	git.sxidc.com/service-supports/ds-sdk v0.10.4
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/gin-gonic/gin v1.10.0
 	github.com/go-playground/locales v0.14.1
 	github.com/go-playground/universal-translator v0.18.1
 	github.com/go-playground/validator/v10 v10.20.0
-	github.com/golang/protobuf v1.5.4
 	github.com/iancoleman/strcase v0.3.0
-	github.com/mwitkow/go-proto-validators v0.3.2
 	github.com/pkg/errors v0.9.1
 	github.com/redis/go-redis/v9 v9.4.0
 	go.uber.org/zap v1.27.0
-	google.golang.org/grpc v1.64.0
-	google.golang.org/protobuf v1.34.1
 	gopkg.in/natefinch/lumberjack.v2 v2.2.1
 	gopkg.in/yaml.v3 v3.0.1
 	gorm.io/driver/postgres v1.5.7
@@ -25,6 +21,10 @@ require (
 )
 
 require (
+	git.sxidc.com/go-tools/api_binding v1.3.23 // indirect
+	git.sxidc.com/service-supports/fserr v0.3.2 // indirect
+	git.sxidc.com/service-supports/fslog v0.5.9 // indirect
+	git.sxidc.com/service-supports/websocket v1.3.1 // indirect
 	github.com/Masterminds/goutils v1.1.1 // indirect
 	github.com/Masterminds/semver/v3 v3.2.0 // indirect
 	github.com/Masterminds/sprig/v3 v3.2.3 // indirect
@@ -34,11 +34,13 @@ require (
 	github.com/cloudwego/base64x v0.1.4 // indirect
 	github.com/cloudwego/iasm v0.2.0 // indirect
 	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
+	github.com/fatih/structs v1.1.0 // indirect
 	github.com/gabriel-vasile/mimetype v1.4.3 // indirect
 	github.com/gin-contrib/sse v0.1.0 // indirect
 	github.com/go-resty/resty/v2 v2.11.0 // indirect
 	github.com/goccy/go-json v0.10.2 // indirect
 	github.com/gogo/protobuf v1.3.0 // indirect
+	github.com/golang/protobuf v1.5.4 // indirect
 	github.com/google/uuid v1.6.0 // indirect
 	github.com/gorilla/websocket v1.5.0 // indirect
 	github.com/huandu/xstrings v1.3.3 // indirect
@@ -56,9 +58,9 @@ require (
 	github.com/mitchellh/reflectwalk v1.0.0 // indirect
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
+	github.com/mwitkow/go-proto-validators v0.3.2 // indirect
 	github.com/olahol/melody v1.2.1 // indirect
 	github.com/pelletier/go-toml/v2 v2.2.2 // indirect
-	github.com/rogpeppe/go-internal v1.12.0 // indirect
 	github.com/satori/go.uuid v1.2.0 // indirect
 	github.com/shopspring/decimal v1.2.0 // indirect
 	github.com/spf13/cast v1.3.1 // indirect
@@ -71,4 +73,6 @@ require (
 	golang.org/x/sys v0.20.0 // indirect
 	golang.org/x/text v0.15.0 // indirect
 	google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
+	google.golang.org/grpc v1.64.0 // indirect
+	google.golang.org/protobuf v1.34.1 // indirect
 )

+ 14 - 3
go.sum

@@ -1,5 +1,13 @@
-git.sxidc.com/go-tools/utils v1.5.15 h1:7xs/EM8XZyKycrSSHcPZ6wvyYs+v8uWQ7ZmPP/fHyFI=
-git.sxidc.com/go-tools/utils v1.5.15/go.mod h1:fkobAXFpOMTvkZ82TQXWcpsayePcyk/MS5TN6GTlRDg=
+git.sxidc.com/go-tools/api_binding v1.3.23 h1:vgCdYq09aiw7Vs9JeOR0OZLOjezbHugQ/3ABeakvD4g=
+git.sxidc.com/go-tools/api_binding v1.3.23/go.mod h1:SmUnRrMtODonLzWmWCGQN9uAB2TjH8g5yEKFnp4rEgU=
+git.sxidc.com/go-tools/utils v1.5.23 h1:Kbcj+EafFVssRa6i1jnILi+LddLWMDtyvRzuV26C6fU=
+git.sxidc.com/go-tools/utils v1.5.23/go.mod h1:uTDb6QK5JZzK5+Fzsfeng7TwmnRDZiTY6JLYxIX94Qw=
+git.sxidc.com/service-supports/ds-sdk v0.10.4 h1:3eIzVaqxZcWYn3WEyAoLBY0CDTvGPglf67+ndjG1abo=
+git.sxidc.com/service-supports/ds-sdk v0.10.4/go.mod h1:bO711uqrG40hBbgZqxOvRYPCYbB3sRCiH10Zp9mYLL0=
+git.sxidc.com/service-supports/fserr v0.3.2 h1:5/FCr8o2jd1kNsp5tH/ADjB9fr091JZXMMZ15ZvNZzs=
+git.sxidc.com/service-supports/fserr v0.3.2/go.mod h1:W54RoA71mfex+zARuH/iMnQPMnBXQ23qXXOkwUh2sVQ=
+git.sxidc.com/service-supports/fslog v0.5.9 h1:q2XIK2o/fk/qmByy4x5kKLC+k7kolT5LrXHcWRSffXQ=
+git.sxidc.com/service-supports/fslog v0.5.9/go.mod h1:/m03ATmmOle75qtEgvEw8a1+Dcg6iHp08M1bGFXJTBU=
 git.sxidc.com/service-supports/websocket v1.3.1 h1:1mRfUwvpg0QA2JVKVMK8YJ/B33HFpDhY9srqroIuNGc=
 git.sxidc.com/service-supports/websocket v1.3.1/go.mod h1:YqEZXkN8ZFzUp01tDlekgIJJ0Yz+67d6eTXyA0ZIkgM=
 github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
@@ -29,6 +37,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
 github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
 github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
 github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
+github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
+github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
 github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
 github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
 github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
@@ -216,8 +226,9 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
 gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
 gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
-gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
 gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=