sdk.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package sdk
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "git.sxidc.com/go-tools/utils/strutils"
  6. "git.sxidc.com/service-supports/ds-sdk/client"
  7. "git.sxidc.com/service-supports/ds-sdk/grpc_client"
  8. v1 "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1"
  9. "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1/request"
  10. "io"
  11. )
  12. type SDK struct {
  13. options *Options
  14. client *client.Client
  15. grpcClient *grpc_client.Client
  16. }
  17. func (s *SDK) CreateDataContainer(name string, spec map[string]any) error {
  18. if strutils.IsStringEmpty(name) {
  19. return errors.New("没有传递数据容器名称")
  20. }
  21. options := s.options
  22. err := s.client.CreateDataContainer(options.token, options.baseUrl,
  23. options.namespace, options.dataSource, name, spec)
  24. if err != nil {
  25. return err
  26. }
  27. return nil
  28. }
  29. func (s *SDK) DeleteDataContainer(name string) error {
  30. if strutils.IsStringEmpty(name) {
  31. return errors.New("没有传递数据容器名称")
  32. }
  33. options := s.options
  34. err := s.client.DeleteDataContainer(options.token, options.baseUrl,
  35. options.namespace, options.dataSource, name)
  36. if err != nil {
  37. return err
  38. }
  39. return nil
  40. }
  41. func (s *SDK) GetDataContainers(name string, pageNo int, pageSize int) ([]client.DataContainerInfo, error) {
  42. options := s.options
  43. return s.client.GetDataContainers(options.token, options.baseUrl,
  44. options.namespace, options.dataSource, name, pageNo, pageSize)
  45. }
  46. // TODO 增加Event版本的Container创建
  47. func (s *SDK) ExecuteRawSql(sql string, executeParams map[string]any) ([]map[string]any, error) {
  48. if strutils.IsStringEmpty(sql) {
  49. return make([]map[string]any, 0), nil
  50. }
  51. options := s.options
  52. results, err := s.client.ExecuteRawSql(options.token, options.baseUrl,
  53. options.namespace, options.dataSource, sql, executeParams)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return results, nil
  58. }
  59. func (s *SDK) CreateSQL(name string, spec map[string]any) error {
  60. if strutils.IsStringEmpty(name) {
  61. return errors.New("没有传递SQL资源名称")
  62. }
  63. options := s.options
  64. err := s.client.CreateSql(options.token, options.baseUrl,
  65. options.namespace, options.dataSource, name, spec)
  66. if err != nil {
  67. return err
  68. }
  69. return nil
  70. }
  71. func (s *SDK) DeleteSQL(name string) error {
  72. if strutils.IsStringEmpty(name) {
  73. return errors.New("没有传递SQL资源名称")
  74. }
  75. options := s.options
  76. err := s.client.DeleteSql(options.token, options.baseUrl,
  77. options.namespace, options.dataSource, name)
  78. if err != nil {
  79. return err
  80. }
  81. return nil
  82. }
  83. func (s *SDK) ExecuteSql(name string, executeParams map[string]any) ([]map[string]any, error) {
  84. if strutils.IsStringEmpty(name) {
  85. return nil, errors.New("没有传递SQL资源名称")
  86. }
  87. options := s.options
  88. results, err := s.client.ExecuteSql(options.token, options.baseUrl,
  89. options.namespace, options.dataSource, name, executeParams)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return results, nil
  94. }
  95. type TxFunc func(tx *Transaction) error
  96. type Transaction struct {
  97. stream v1.SqlService_TransactionClient
  98. }
  99. func (tx *Transaction) ExecuteRawSql(sql string, executeParams map[string]any) error {
  100. var retErr error
  101. defer func() {
  102. if retErr != nil {
  103. innerErr := tx.stream.CloseSend()
  104. if innerErr != nil {
  105. panic(innerErr)
  106. }
  107. }
  108. }()
  109. executeParamsJsonBytes, err := json.Marshal(executeParams)
  110. if err != nil {
  111. retErr = err
  112. return retErr
  113. }
  114. err = tx.stream.SendMsg(&request.TransactionOperation{
  115. Request: &request.TransactionOperation_ExecuteRawSqlRequest{
  116. ExecuteRawSqlRequest: &request.ExecuteRawSqlRequest{
  117. SQL: sql,
  118. ExecuteParams: string(executeParamsJsonBytes),
  119. },
  120. },
  121. })
  122. if err != nil {
  123. retErr = err
  124. return retErr
  125. }
  126. _, err = tx.stream.Recv()
  127. if err != nil {
  128. retErr = err
  129. return retErr
  130. }
  131. return nil
  132. }
  133. func (tx *Transaction) ExecuteSql(name string, executeParams map[string]any) error {
  134. var retErr error
  135. defer func() {
  136. if retErr != nil {
  137. innerErr := tx.stream.CloseSend()
  138. if innerErr != nil {
  139. panic(innerErr)
  140. }
  141. }
  142. }()
  143. executeParamsJsonBytes, err := json.Marshal(executeParams)
  144. if err != nil {
  145. retErr = err
  146. return retErr
  147. }
  148. err = tx.stream.Send(&request.TransactionOperation{
  149. Request: &request.TransactionOperation_ExecuteSqlRequest{
  150. ExecuteSqlRequest: &request.ExecuteSqlRequest{
  151. Name: name,
  152. ExecuteParams: string(executeParamsJsonBytes),
  153. },
  154. },
  155. })
  156. if err != nil {
  157. retErr = err
  158. return retErr
  159. }
  160. _, err = tx.stream.Recv()
  161. if err != nil {
  162. retErr = err
  163. return retErr
  164. }
  165. return nil
  166. }
  167. func (s *SDK) Transaction(txFunc TxFunc) error {
  168. stream, err := s.grpcClient.Transaction()
  169. if err != nil {
  170. return err
  171. }
  172. defer func() {
  173. innerErr := stream.CloseSend()
  174. if innerErr != nil {
  175. panic(innerErr)
  176. }
  177. }()
  178. err = stream.Send(&request.TransactionOperation{
  179. Request: &request.TransactionOperation_TransactionBeginRequest{
  180. TransactionBeginRequest: &request.TransactionBeginRequest{
  181. Token: s.options.token,
  182. Namespace: s.options.namespace,
  183. DataSource: s.options.dataSource,
  184. },
  185. }})
  186. if err != nil {
  187. return err
  188. }
  189. err = txFunc(&Transaction{
  190. stream: stream,
  191. })
  192. if err != nil {
  193. return err
  194. }
  195. err = stream.Send(&request.TransactionOperation{
  196. Request: &request.TransactionOperation_TransactionEndRequest{
  197. TransactionEndRequest: &request.TransactionEndRequest{},
  198. }})
  199. if err != nil {
  200. return err
  201. }
  202. _, err = stream.Recv()
  203. if err != nil && err != io.EOF {
  204. return err
  205. }
  206. return nil
  207. }