12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package sdk
- import (
- "git.sxidc.com/go-tools/utils/strutils"
- "git.sxidc.com/service-supports/ds-sdk/client"
- "git.sxidc.com/service-supports/ds-sdk/grpc_client"
- "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1/request"
- "github.com/pkg/errors"
- "io"
- )
- type SDK struct {
- options *Options
- client *client.Client
- grpcClient *grpc_client.Client
- }
- func (s *SDK) ExecuteRawSql(sql string, values ...any) ([]map[string]any, error) {
- if strutils.IsStringEmpty(sql) {
- return make([]map[string]any, 0), nil
- }
- options := s.options
- tableRows, err := s.client.ExecuteRawSql(options.token, options.baseUrl,
- options.namespace, options.dataSource, sql, values...)
- if err != nil {
- return nil, err
- }
- results := make([]map[string]any, len(tableRows))
- for i, row := range tableRows {
- results[i] = row
- }
- return results, nil
- }
- func (s *SDK) Transaction(txFunc TxFunc) error {
- stream, err := s.grpcClient.Transaction()
- if err != nil {
- return err
- }
- defer func() {
- innerErr := stream.CloseSend()
- if innerErr != nil {
- panic(innerErr)
- }
- }()
- err = stream.Send(&request.TransactionOperation{
- Request: &request.TransactionOperation_TransactionBeginRequest{
- TransactionBeginRequest: &request.TransactionBeginRequest{
- Token: s.options.token,
- Namespace: s.options.namespace,
- DataSource: s.options.dataSource,
- },
- }})
- if err != nil {
- return err
- }
- resp, err := stream.Recv()
- if err != nil {
- return err
- }
- if !resp.Success {
- return errors.New(resp.Msg)
- }
- err = txFunc(&Transaction{
- stream: stream,
- })
- if err != nil {
- return err
- }
- err = stream.Send(&request.TransactionOperation{
- Request: &request.TransactionOperation_TransactionEndRequest{
- TransactionEndRequest: &request.TransactionEndRequest{},
- }})
- if err != nil {
- return err
- }
- _, err = stream.Recv()
- if err != nil && err != io.EOF {
- return err
- }
- return nil
- }
|