raw_sql.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package client
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-tools/utils/reflectutils"
  5. "github.com/pkg/errors"
  6. "net/url"
  7. "reflect"
  8. )
  9. const (
  10. executeRawSqlUrl = "/ds/api/v1/rawSql/execute"
  11. )
  12. func (c *Client) ExecuteRawSql(token string, baseUrl string,
  13. namespace string, dataSource string, sql string, values ...any) ([]map[string]any, int64, error) {
  14. fullUrl, err := url.JoinPath(baseUrl, executeRawSqlUrl)
  15. if err != nil {
  16. return nil, 0, err
  17. }
  18. resp := new(struct {
  19. MsgResponse
  20. Results []map[string]any `json:"results"`
  21. RowsAffected int64 `json:"rowsAffected"`
  22. })
  23. rawSqlValues := make([]map[string]any, 0)
  24. for _, value := range values {
  25. typedValueReflectValue := reflect.ValueOf(value)
  26. if !typedValueReflectValue.IsValid() {
  27. return nil, 0, errors.New("无效值")
  28. }
  29. typedValueReflectValueElem := reflectutils.PointerValueElem(typedValueReflectValue)
  30. rawSqlValues = append(rawSqlValues, map[string]any{
  31. "kind": typedValueReflectValueElem.Kind(),
  32. "value": typedValueReflectValueElem.Interface(),
  33. })
  34. }
  35. err = c.post(token, fullUrl, map[string]any{
  36. "namespace": namespace,
  37. "dataSource": dataSource,
  38. "sql": sql,
  39. "values": rawSqlValues,
  40. }, resp)
  41. if err != nil {
  42. return nil, 0, err
  43. }
  44. if !resp.Success {
  45. return nil, 0, fmt.Errorf(resp.Msg)
  46. }
  47. return resp.Results, resp.RowsAffected, nil
  48. }