sdk.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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) ([]map[string]any, error) {
  16. if strutils.IsStringEmpty(sql) {
  17. return make([]map[string]any, 0), nil
  18. }
  19. options := s.options
  20. results, 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. return results, nil
  26. }
  27. func (s *SDK) CreateSQL(name string, spec map[string]any) error {
  28. if strutils.IsStringEmpty(name) {
  29. return errors.New("没有传递SQL资源名称")
  30. }
  31. options := s.options
  32. err := s.client.CreateSql(options.token, options.baseUrl,
  33. options.namespace, options.dataSource, name, spec)
  34. if err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. func (s *SDK) DeleteSQL(name string) error {
  40. if strutils.IsStringEmpty(name) {
  41. return errors.New("没有传递SQL资源名称")
  42. }
  43. options := s.options
  44. err := s.client.DeleteSql(options.token, options.baseUrl,
  45. options.namespace, options.dataSource, name)
  46. if err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func (s *SDK) ExecuteSql(name string, executeParams map[string]any) ([]map[string]any, error) {
  52. if strutils.IsStringEmpty(name) {
  53. return nil, errors.New("没有传递SQL资源名称")
  54. }
  55. options := s.options
  56. results, err := s.client.ExecuteSql(options.token, options.baseUrl,
  57. options.namespace, options.dataSource, name, executeParams)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return results, nil
  62. }
  63. func (s *SDK) Transaction(txFunc TxFunc) error {
  64. stream, err := s.grpcClient.Transaction()
  65. if err != nil {
  66. return err
  67. }
  68. defer func() {
  69. innerErr := stream.CloseSend()
  70. if innerErr != nil {
  71. panic(innerErr)
  72. }
  73. }()
  74. err = stream.Send(&request.TransactionOperation{
  75. Request: &request.TransactionOperation_TransactionBeginRequest{
  76. TransactionBeginRequest: &request.TransactionBeginRequest{
  77. Token: s.options.token,
  78. Namespace: s.options.namespace,
  79. DataSource: s.options.dataSource,
  80. },
  81. }})
  82. if err != nil {
  83. return err
  84. }
  85. resp, err := stream.Recv()
  86. if err != nil {
  87. return err
  88. }
  89. if !resp.Success {
  90. return errors.New(resp.Msg)
  91. }
  92. err = txFunc(&Transaction{
  93. stream: stream,
  94. })
  95. if err != nil {
  96. return err
  97. }
  98. err = stream.Send(&request.TransactionOperation{
  99. Request: &request.TransactionOperation_TransactionEndRequest{
  100. TransactionEndRequest: &request.TransactionEndRequest{},
  101. }})
  102. if err != nil {
  103. return err
  104. }
  105. _, err = stream.Recv()
  106. if err != nil && err != io.EOF {
  107. return err
  108. }
  109. return nil
  110. }