Browse Source

修改bug

yjp 2 months ago
parent
commit
033e44b2f5

+ 8 - 7
client/raw_sql.go

@@ -13,22 +13,23 @@ const (
 )
 
 func (c *Client) ExecuteRawSql(token string, baseUrl string,
-	namespace string, dataSource string, sql string, values ...any) ([]map[string]any, error) {
+	namespace string, dataSource string, sql string, values ...any) ([]map[string]any, int64, error) {
 	fullUrl, err := url.JoinPath(baseUrl, executeRawSqlUrl)
 	if err != nil {
-		return nil, err
+		return nil, 0, err
 	}
 
 	resp := new(struct {
 		MsgResponse
-		Results []map[string]any `json:"results"`
+		Results      []map[string]any `json:"results"`
+		RowsAffected int64            `json:"rowsAffected"`
 	})
 
 	rawSqlValues := make([]map[string]any, 0)
 	for _, value := range values {
 		typedValueReflectValue := reflect.ValueOf(value)
 		if !typedValueReflectValue.IsValid() {
-			return nil, errors.New("无效值")
+			return nil, 0, errors.New("无效值")
 		}
 
 		typedValueReflectValueElem := reflectutils.PointerValueElem(typedValueReflectValue)
@@ -45,12 +46,12 @@ func (c *Client) ExecuteRawSql(token string, baseUrl string,
 		"values":     rawSqlValues,
 	}, resp)
 	if err != nil {
-		return nil, err
+		return nil, 0, err
 	}
 
 	if !resp.Success {
-		return nil, fmt.Errorf(resp.Msg)
+		return nil, 0, fmt.Errorf(resp.Msg)
 	}
 
-	return resp.Results, nil
+	return resp.Results, resp.RowsAffected, nil
 }

+ 1 - 1
grpc_client/v1/request/sql.pb.go

@@ -1,6 +1,6 @@
 // Code generated by protoc-gen-go. DO NOT EDIT.
 // versions:
-// 	protoc-gen-go v1.28.1
+// 	protoc-gen-go v1.34.1
 // 	protoc        v3.12.4
 // source: v1/request/sql.proto
 

+ 1 - 1
grpc_client/v1/request/sql.validator.pb.go

@@ -5,10 +5,10 @@ package request
 
 import (
 	fmt "fmt"
-	math "math"
 	proto "github.com/golang/protobuf/proto"
 	_ "github.com/mwitkow/go-proto-validators"
 	github_com_mwitkow_go_proto_validators "github.com/mwitkow/go-proto-validators"
+	math "math"
 )
 
 // Reference imports to suppress errors if they are not otherwise used.

+ 19 - 9
grpc_client/v1/response/sql.pb.go

@@ -1,6 +1,6 @@
 // Code generated by protoc-gen-go. DO NOT EDIT.
 // versions:
-// 	protoc-gen-go v1.28.1
+// 	protoc-gen-go v1.34.1
 // 	protoc        v3.12.4
 // source: v1/response/sql.proto
 
@@ -25,9 +25,10 @@ type TransactionResponse struct {
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
 
-	Success bool   `protobuf:"varint,1,opt,name=Success,proto3" json:"Success,omitempty"`
-	Msg     string `protobuf:"bytes,2,opt,name=Msg,proto3" json:"Msg,omitempty"`
-	Results string `protobuf:"bytes,3,opt,name=Results,proto3" json:"Results,omitempty"`
+	Success      bool   `protobuf:"varint,1,opt,name=Success,proto3" json:"Success,omitempty"`
+	Msg          string `protobuf:"bytes,2,opt,name=Msg,proto3" json:"Msg,omitempty"`
+	Results      string `protobuf:"bytes,3,opt,name=Results,proto3" json:"Results,omitempty"`
+	RowsAffected int64  `protobuf:"varint,4,opt,name=RowsAffected,proto3" json:"RowsAffected,omitempty"`
 }
 
 func (x *TransactionResponse) Reset() {
@@ -83,20 +84,29 @@ func (x *TransactionResponse) GetResults() string {
 	return ""
 }
 
+func (x *TransactionResponse) GetRowsAffected() int64 {
+	if x != nil {
+		return x.RowsAffected
+	}
+	return 0
+}
+
 var File_v1_response_sql_proto protoreflect.FileDescriptor
 
 var file_v1_response_sql_proto_rawDesc = []byte{
 	0x0a, 0x15, 0x76, 0x31, 0x2f, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2f, 0x73, 0x71,
 	0x6c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
-	0x65, 0x22, 0x5b, 0x0a, 0x13, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e,
+	0x65, 0x22, 0x7f, 0x0a, 0x13, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e,
 	0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x53, 0x75, 0x63, 0x63,
 	0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x53, 0x75, 0x63, 0x63, 0x65,
 	0x73, 0x73, 0x12, 0x10, 0x0a, 0x03, 0x4d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
 	0x03, 0x4d, 0x73, 0x67, 0x12, 0x18, 0x0a, 0x07, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x18,
-	0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x42, 0x1c,
-	0x5a, 0x1a, 0x64, 0x73, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x70, 0x69, 0x2f, 0x70, 0x62,
-	0x2f, 0x76, 0x31, 0x2f, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x62, 0x06, 0x70, 0x72,
-	0x6f, 0x74, 0x6f, 0x33,
+	0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x12, 0x22,
+	0x0a, 0x0c, 0x52, 0x6f, 0x77, 0x73, 0x41, 0x66, 0x66, 0x65, 0x63, 0x74, 0x65, 0x64, 0x18, 0x04,
+	0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x52, 0x6f, 0x77, 0x73, 0x41, 0x66, 0x66, 0x65, 0x63, 0x74,
+	0x65, 0x64, 0x42, 0x1c, 0x5a, 0x1a, 0x64, 0x73, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x70,
+	0x69, 0x2f, 0x70, 0x62, 0x2f, 0x76, 0x31, 0x2f, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
+	0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
 }
 
 var (

+ 1 - 1
grpc_client/v1/sql.pb.go

@@ -1,6 +1,6 @@
 // Code generated by protoc-gen-go. DO NOT EDIT.
 // versions:
-// 	protoc-gen-go v1.28.1
+// 	protoc-gen-go v1.34.1
 // 	protoc        v3.12.4
 // source: v1/sql.proto
 

+ 35 - 57
grpc_client/v1/sql_grpc.pb.go

@@ -1,6 +1,6 @@
 // Code generated by protoc-gen-go-grpc. DO NOT EDIT.
 // versions:
-// - protoc-gen-go-grpc v1.2.0
+// - protoc-gen-go-grpc v1.5.1
 // - protoc             v3.12.4
 // source: v1/sql.proto
 
@@ -8,8 +8,8 @@ package v1
 
 import (
 	context "context"
-	request "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1/request"
 	response "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1/response"
+	request "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1/request"
 	grpc "google.golang.org/grpc"
 	codes "google.golang.org/grpc/codes"
 	status "google.golang.org/grpc/status"
@@ -17,14 +17,18 @@ import (
 
 // This is a compile-time assertion to ensure that this generated file
 // is compatible with the grpc package it is being compiled against.
-// Requires gRPC-Go v1.32.0 or later.
-const _ = grpc.SupportPackageIsVersion7
+// Requires gRPC-Go v1.64.0 or later.
+const _ = grpc.SupportPackageIsVersion9
+
+const (
+	SqlService_Transaction_FullMethodName = "/v1.SqlService/Transaction"
+)
 
 // SqlServiceClient is the client API for SqlService service.
 //
 // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
 type SqlServiceClient interface {
-	Transaction(ctx context.Context, opts ...grpc.CallOption) (SqlService_TransactionClient, error)
+	Transaction(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[request.TransactionOperation, response.TransactionResponse], error)
 }
 
 type sqlServiceClient struct {
@@ -35,53 +39,39 @@ func NewSqlServiceClient(cc grpc.ClientConnInterface) SqlServiceClient {
 	return &sqlServiceClient{cc}
 }
 
-func (c *sqlServiceClient) Transaction(ctx context.Context, opts ...grpc.CallOption) (SqlService_TransactionClient, error) {
-	stream, err := c.cc.NewStream(ctx, &SqlService_ServiceDesc.Streams[0], "/v1.SqlService/Transaction", opts...)
+func (c *sqlServiceClient) Transaction(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[request.TransactionOperation, response.TransactionResponse], error) {
+	cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
+	stream, err := c.cc.NewStream(ctx, &SqlService_ServiceDesc.Streams[0], SqlService_Transaction_FullMethodName, cOpts...)
 	if err != nil {
 		return nil, err
 	}
-	x := &sqlServiceTransactionClient{stream}
+	x := &grpc.GenericClientStream[request.TransactionOperation, response.TransactionResponse]{ClientStream: stream}
 	return x, nil
 }
 
-type SqlService_TransactionClient interface {
-	Send(*request.TransactionOperation) error
-	Recv() (*response.TransactionResponse, error)
-	grpc.ClientStream
-}
-
-type sqlServiceTransactionClient struct {
-	grpc.ClientStream
-}
-
-func (x *sqlServiceTransactionClient) Send(m *request.TransactionOperation) error {
-	return x.ClientStream.SendMsg(m)
-}
-
-func (x *sqlServiceTransactionClient) Recv() (*response.TransactionResponse, error) {
-	m := new(response.TransactionResponse)
-	if err := x.ClientStream.RecvMsg(m); err != nil {
-		return nil, err
-	}
-	return m, nil
-}
+// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
+type SqlService_TransactionClient = grpc.BidiStreamingClient[request.TransactionOperation, response.TransactionResponse]
 
 // SqlServiceServer is the server API for SqlService service.
 // All implementations must embed UnimplementedSqlServiceServer
-// for forward compatibility
+// for forward compatibility.
 type SqlServiceServer interface {
-	Transaction(SqlService_TransactionServer) error
+	Transaction(grpc.BidiStreamingServer[request.TransactionOperation, response.TransactionResponse]) error
 	mustEmbedUnimplementedSqlServiceServer()
 }
 
-// UnimplementedSqlServiceServer must be embedded to have forward compatible implementations.
-type UnimplementedSqlServiceServer struct {
-}
+// UnimplementedSqlServiceServer must be embedded to have
+// forward compatible implementations.
+//
+// NOTE: this should be embedded by value instead of pointer to avoid a nil
+// pointer dereference when methods are called.
+type UnimplementedSqlServiceServer struct{}
 
-func (UnimplementedSqlServiceServer) Transaction(SqlService_TransactionServer) error {
+func (UnimplementedSqlServiceServer) Transaction(grpc.BidiStreamingServer[request.TransactionOperation, response.TransactionResponse]) error {
 	return status.Errorf(codes.Unimplemented, "method Transaction not implemented")
 }
 func (UnimplementedSqlServiceServer) mustEmbedUnimplementedSqlServiceServer() {}
+func (UnimplementedSqlServiceServer) testEmbeddedByValue()                    {}
 
 // UnsafeSqlServiceServer may be embedded to opt out of forward compatibility for this service.
 // Use of this interface is not recommended, as added methods to SqlServiceServer will
@@ -91,34 +81,22 @@ type UnsafeSqlServiceServer interface {
 }
 
 func RegisterSqlServiceServer(s grpc.ServiceRegistrar, srv SqlServiceServer) {
+	// If the following call pancis, it indicates UnimplementedSqlServiceServer was
+	// embedded by pointer and is nil.  This will cause panics if an
+	// unimplemented method is ever invoked, so we test this at initialization
+	// time to prevent it from happening at runtime later due to I/O.
+	if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
+		t.testEmbeddedByValue()
+	}
 	s.RegisterService(&SqlService_ServiceDesc, srv)
 }
 
 func _SqlService_Transaction_Handler(srv interface{}, stream grpc.ServerStream) error {
-	return srv.(SqlServiceServer).Transaction(&sqlServiceTransactionServer{stream})
-}
-
-type SqlService_TransactionServer interface {
-	Send(*response.TransactionResponse) error
-	Recv() (*request.TransactionOperation, error)
-	grpc.ServerStream
-}
-
-type sqlServiceTransactionServer struct {
-	grpc.ServerStream
-}
-
-func (x *sqlServiceTransactionServer) Send(m *response.TransactionResponse) error {
-	return x.ServerStream.SendMsg(m)
+	return srv.(SqlServiceServer).Transaction(&grpc.GenericServerStream[request.TransactionOperation, response.TransactionResponse]{ServerStream: stream})
 }
 
-func (x *sqlServiceTransactionServer) Recv() (*request.TransactionOperation, error) {
-	m := new(request.TransactionOperation)
-	if err := x.ServerStream.RecvMsg(m); err != nil {
-		return nil, err
-	}
-	return m, nil
-}
+// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
+type SqlService_TransactionServer = grpc.BidiStreamingServer[request.TransactionOperation, response.TransactionResponse]
 
 // SqlService_ServiceDesc is the grpc.ServiceDesc for SqlService service.
 // It's only intended for direct use with grpc.RegisterService,

+ 8 - 8
sdk/transaction.go

@@ -15,7 +15,7 @@ type Transaction struct {
 	stream v1.SqlService_TransactionClient
 }
 
-func (tx *Transaction) ExecuteRawSql(sql string, values ...any) ([]map[string]any, error) {
+func (tx *Transaction) ExecuteRawSql(sql string, values ...any) ([]map[string]any, int64, error) {
 	var retErr error
 
 	defer func() {
@@ -31,7 +31,7 @@ func (tx *Transaction) ExecuteRawSql(sql string, values ...any) ([]map[string]an
 	for _, value := range values {
 		typedValueReflectValue := reflect.ValueOf(value)
 		if !typedValueReflectValue.IsValid() {
-			return nil, errors.New("无效值")
+			return nil, 0, errors.New("无效值")
 		}
 
 		typedValueReflectValueElem := reflectutils.PointerValueElem(typedValueReflectValue)
@@ -49,7 +49,7 @@ func (tx *Transaction) ExecuteRawSql(sql string, values ...any) ([]map[string]an
 	requestJsonBytes, err := json.Marshal(requestMap)
 	if err != nil {
 		retErr = err
-		return nil, retErr
+		return nil, 0, retErr
 	}
 
 	err = tx.stream.SendMsg(&request.TransactionOperation{
@@ -59,25 +59,25 @@ func (tx *Transaction) ExecuteRawSql(sql string, values ...any) ([]map[string]an
 	})
 	if err != nil {
 		retErr = err
-		return nil, retErr
+		return nil, 0, retErr
 	}
 
 	resp, err := tx.stream.Recv()
 	if err != nil {
 		retErr = err
-		return nil, retErr
+		return nil, 0, retErr
 	}
 
 	if !resp.Success {
 		retErr = errors.New(resp.Msg)
-		return nil, retErr
+		return nil, 0, retErr
 	}
 
 	tableRows := make([]map[string]any, 0)
 	err = json.Unmarshal([]byte(resp.Results), &tableRows)
 	if err != nil {
 		retErr = err
-		return nil, retErr
+		return nil, 0, retErr
 	}
 
 	results := make([]map[string]any, len(tableRows))
@@ -85,5 +85,5 @@ func (tx *Transaction) ExecuteRawSql(sql string, values ...any) ([]map[string]an
 		results[i] = row
 	}
 
-	return results, nil
+	return results, resp.RowsAffected, nil
 }