sdk.go 2.7 KB

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