123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- package sdk
- import (
- "encoding/json"
- "errors"
- "git.sxidc.com/go-tools/utils/strutils"
- "git.sxidc.com/service-supports/ds-sdk/client"
- "git.sxidc.com/service-supports/ds-sdk/grpc_client"
- v1 "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1"
- "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1/request"
- "io"
- )
- type SDK struct {
- options *Options
- client *client.Client
- grpcClient *grpc_client.Client
- }
- func (s *SDK) CreateDataContainer(name string, spec map[string]any) error {
- if strutils.IsStringEmpty(name) {
- return errors.New("没有传递数据容器名称")
- }
- options := s.options
- err := s.client.CreateDataContainer(options.token, options.baseUrl,
- options.namespace, options.dataSource, name, spec)
- if err != nil {
- return err
- }
- return nil
- }
- func (s *SDK) DeleteDataContainer(name string) error {
- if strutils.IsStringEmpty(name) {
- return errors.New("没有传递数据容器名称")
- }
- options := s.options
- err := s.client.DeleteDataContainer(options.token, options.baseUrl,
- options.namespace, options.dataSource, name)
- if err != nil {
- return err
- }
- return nil
- }
- func (s *SDK) GetDataContainers(name string, pageNo int, pageSize int) ([]client.DataContainerInfo, error) {
- options := s.options
- return s.client.GetDataContainers(options.token, options.baseUrl,
- options.namespace, options.dataSource, name, pageNo, pageSize)
- }
- func (s *SDK) ExecuteRawSql(sql string, executeParams map[string]any) ([]map[string]any, error) {
- if strutils.IsStringEmpty(sql) {
- return make([]map[string]any, 0), nil
- }
- options := s.options
- results, err := s.client.ExecuteRawSql(options.token, options.baseUrl,
- options.namespace, options.dataSource, sql, executeParams)
- if err != nil {
- return nil, err
- }
- return results, nil
- }
- func (s *SDK) CreateSQL(name string, spec map[string]any) error {
- if strutils.IsStringEmpty(name) {
- return errors.New("没有传递SQL资源名称")
- }
- options := s.options
- err := s.client.CreateSql(options.token, options.baseUrl,
- options.namespace, options.dataSource, name, spec)
- if err != nil {
- return err
- }
- return nil
- }
- func (s *SDK) DeleteSQL(name string) error {
- if strutils.IsStringEmpty(name) {
- return errors.New("没有传递SQL资源名称")
- }
- options := s.options
- err := s.client.DeleteSql(options.token, options.baseUrl,
- options.namespace, options.dataSource, name)
- if err != nil {
- return err
- }
- return nil
- }
- func (s *SDK) ExecuteSql(name string, executeParams map[string]any) ([]map[string]any, error) {
- if strutils.IsStringEmpty(name) {
- return nil, errors.New("没有传递SQL资源名称")
- }
- options := s.options
- results, err := s.client.ExecuteSql(options.token, options.baseUrl,
- options.namespace, options.dataSource, name, executeParams)
- if err != nil {
- return nil, err
- }
- return results, nil
- }
- type TxFunc func(tx *Transaction) error
- type Transaction struct {
- stream v1.SqlService_TransactionClient
- }
- func (tx *Transaction) ExecuteRawSql(sql string, executeParams map[string]any) error {
- var retErr error
- defer func() {
- if retErr != nil {
- innerErr := tx.stream.CloseSend()
- if innerErr != nil {
- panic(innerErr)
- }
- }
- }()
- executeParamsJsonBytes, err := json.Marshal(executeParams)
- if err != nil {
- retErr = err
- return retErr
- }
- err = tx.stream.SendMsg(&request.TransactionOperation{
- Request: &request.TransactionOperation_ExecuteRawSqlRequest{
- ExecuteRawSqlRequest: &request.ExecuteRawSqlRequest{
- SQL: sql,
- ExecuteParams: string(executeParamsJsonBytes),
- },
- },
- })
- if err != nil {
- retErr = err
- return retErr
- }
- _, err = tx.stream.Recv()
- if err != nil {
- retErr = err
- return retErr
- }
- return nil
- }
- func (tx *Transaction) ExecuteSql(name string, executeParams map[string]any) error {
- var retErr error
- defer func() {
- if retErr != nil {
- innerErr := tx.stream.CloseSend()
- if innerErr != nil {
- panic(innerErr)
- }
- }
- }()
- executeParamsJsonBytes, err := json.Marshal(executeParams)
- if err != nil {
- retErr = err
- return retErr
- }
- err = tx.stream.Send(&request.TransactionOperation{
- Request: &request.TransactionOperation_ExecuteSqlRequest{
- ExecuteSqlRequest: &request.ExecuteSqlRequest{
- Name: name,
- ExecuteParams: string(executeParamsJsonBytes),
- },
- },
- })
- if err != nil {
- retErr = err
- return retErr
- }
- _, err = tx.stream.Recv()
- if err != nil {
- retErr = err
- return retErr
- }
- return 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
- }
- 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
- }
|