raw_sql.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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, error) {
  14. fullUrl, err := url.JoinPath(baseUrl, executeRawSqlUrl)
  15. if err != nil {
  16. return nil, err
  17. }
  18. resp := new(struct {
  19. MsgResponse
  20. Results []map[string]any `json:"results"`
  21. })
  22. rawSqlValues := make([]map[string]any, 0)
  23. for _, value := range values {
  24. typedValueReflectValue := reflect.ValueOf(value)
  25. if !typedValueReflectValue.IsValid() {
  26. return nil, errors.New("无效值")
  27. }
  28. typedValueReflectValueElem := reflectutils.PointerValueElem(typedValueReflectValue)
  29. rawSqlValues = append(rawSqlValues, map[string]any{
  30. "kind": typedValueReflectValueElem.Kind(),
  31. "value": typedValueReflectValueElem.Interface(),
  32. })
  33. }
  34. err = c.post(token, fullUrl, map[string]any{
  35. "namespace": namespace,
  36. "dataSource": dataSource,
  37. "sql": sql,
  38. "values": rawSqlValues,
  39. }, resp)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if !resp.Success {
  44. return nil, fmt.Errorf(resp.Msg)
  45. }
  46. return resp.Results, nil
  47. }