sdk.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package sdk
  2. import (
  3. "errors"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "git.sxidc.com/service-supports/ds-sdk/client"
  6. "git.sxidc.com/service-supports/ds-sdk/grpc_client"
  7. "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1/request"
  8. "io"
  9. )
  10. type SDK struct {
  11. options *Options
  12. client *client.Client
  13. grpcClient *grpc_client.Client
  14. }
  15. func (s *SDK) ExecuteRawSql(sql string, executeParams map[string]any) ([]SqlResult, error) {
  16. if strutils.IsStringEmpty(sql) {
  17. return make([]SqlResult, 0), nil
  18. }
  19. options := s.options
  20. tableRows, err := s.client.ExecuteRawSql(options.token, options.baseUrl,
  21. options.namespace, options.dataSource, sql, executeParams)
  22. if err != nil {
  23. return nil, err
  24. }
  25. results := make([]SqlResult, len(tableRows))
  26. for i, row := range tableRows {
  27. results[i] = row
  28. }
  29. return results, nil
  30. }
  31. func (s *SDK) CreateSQL(name string, spec map[string]any) error {
  32. if strutils.IsStringEmpty(name) {
  33. return errors.New("没有传递SQL资源名称")
  34. }
  35. options := s.options
  36. err := s.client.CreateSql(options.token, options.baseUrl,
  37. options.namespace, options.dataSource, name, spec)
  38. if err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. func (s *SDK) DeleteSQL(name string) error {
  44. if strutils.IsStringEmpty(name) {
  45. return errors.New("没有传递SQL资源名称")
  46. }
  47. options := s.options
  48. err := s.client.DeleteSql(options.token, options.baseUrl,
  49. options.namespace, options.dataSource, name)
  50. if err != nil {
  51. return err
  52. }
  53. return nil
  54. }
  55. func (s *SDK) ExecuteSql(name string, executeParams map[string]any) ([]SqlResult, error) {
  56. if strutils.IsStringEmpty(name) {
  57. return nil, errors.New("没有传递SQL资源名称")
  58. }
  59. options := s.options
  60. tableRows, err := s.client.ExecuteSql(options.token, options.baseUrl,
  61. options.namespace, options.dataSource, name, executeParams)
  62. if err != nil {
  63. return nil, err
  64. }
  65. results := make([]SqlResult, len(tableRows))
  66. for i, row := range tableRows {
  67. results[i] = row
  68. }
  69. return results, nil
  70. }
  71. func (s *SDK) Transaction(txFunc TxFunc) error {
  72. stream, err := s.grpcClient.Transaction()
  73. if err != nil {
  74. return err
  75. }
  76. defer func() {
  77. innerErr := stream.CloseSend()
  78. if innerErr != nil {
  79. panic(innerErr)
  80. }
  81. }()
  82. err = stream.Send(&request.TransactionOperation{
  83. Request: &request.TransactionOperation_TransactionBeginRequest{
  84. TransactionBeginRequest: &request.TransactionBeginRequest{
  85. Token: s.options.token,
  86. Namespace: s.options.namespace,
  87. DataSource: s.options.dataSource,
  88. },
  89. }})
  90. if err != nil {
  91. return err
  92. }
  93. resp, err := stream.Recv()
  94. if err != nil {
  95. return err
  96. }
  97. if !resp.Success {
  98. return errors.New(resp.Msg)
  99. }
  100. err = txFunc(&Transaction{
  101. stream: stream,
  102. })
  103. if err != nil {
  104. return err
  105. }
  106. err = stream.Send(&request.TransactionOperation{
  107. Request: &request.TransactionOperation_TransactionEndRequest{
  108. TransactionEndRequest: &request.TransactionEndRequest{},
  109. }})
  110. if err != nil {
  111. return err
  112. }
  113. _, err = stream.Recv()
  114. if err != nil && err != io.EOF {
  115. return err
  116. }
  117. return nil
  118. }