1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- package client
- import (
- "fmt"
- "git.sxidc.com/go-tools/utils/reflectutils"
- "github.com/pkg/errors"
- "net/url"
- "reflect"
- )
- const (
- executeRawSqlUrl = "/ds/api/v1/rawSql/execute"
- )
- func (c *Client) ExecuteRawSql(token string, baseUrl string,
- namespace string, dataSource string, sql string, values ...any) ([]map[string]any, error) {
- fullUrl, err := url.JoinPath(baseUrl, executeRawSqlUrl)
- if err != nil {
- return nil, err
- }
- resp := new(struct {
- MsgResponse
- Results []map[string]any `json:"results"`
- })
- rawSqlValues := make([]map[string]any, 0)
- for _, value := range values {
- typedValueReflectValue := reflect.ValueOf(value)
- if !typedValueReflectValue.IsValid() {
- return nil, errors.New("无效值")
- }
- typedValueReflectValueElem := reflectutils.PointerValueElem(typedValueReflectValue)
- rawSqlValues = append(rawSqlValues, map[string]any{
- "kind": typedValueReflectValueElem.Kind(),
- "value": typedValueReflectValueElem.Interface(),
- })
- }
- err = c.post(token, fullUrl, map[string]any{
- "namespace": namespace,
- "dataSource": dataSource,
- "sql": sql,
- "values": rawSqlValues,
- }, resp)
- if err != nil {
- return nil, err
- }
- if !resp.Success {
- return nil, fmt.Errorf(resp.Msg)
- }
- return resp.Results, nil
- }
|