-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
70 lines (61 loc) · 2.12 KB
/
database.go
File metadata and controls
70 lines (61 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package archeryapi
import (
"encoding/json"
"errors"
"github.com/fatih/structs"
"github.com/mcuadros/go-defaults"
)
type DatabaseService interface {
Query(sql string, request *QueryRequest) (DatabaseQueryResponse, error)
}
type DatabaseClient struct {
apiClient *Client
}
func (c *DatabaseClient) Query(sql string, request *QueryRequest) (DatabaseQueryResponse, error) {
request.SQLContent = sql
defaults.SetDefaults(request)
params := map[string]string{}
for k, v := range structs.Map(request) {
params[k] = v.(string)
}
r, err := c.apiClient.httpClient.R().
SetFormData(params).
Post("/query/")
if err != nil {
return DatabaseQueryResponse{}, err
}
var result Result
result.Data = &DatabaseQueryResponse{}
if err := json.Unmarshal(r.Body(), &result); err != nil {
return DatabaseQueryResponse{}, err
}
if result.Status != 0 {
return DatabaseQueryResponse{}, errors.New(result.Msg)
}
return *(result.Data.(*DatabaseQueryResponse)), nil
}
type QueryRequest struct {
InstanceName string `structs:"instance_name"`
DBName string `structs:"db_name"`
SchemaName string `structs:"schema_name"`
TBName string `structs:"tb_name"`
SQLContent string `structs:"sql_content"`
LimitNum string `structs:"limit_num" default:"100"`
}
type DatabaseQueryResponse struct {
FullSql string `json:"full_sql"`
IsExecute bool `json:"is_execute"`
Checked interface{} `json:"checked"`
IsMasked bool `json:"is_masked"`
QueryTime float64 `json:"query_time"`
MaskRuleHit bool `json:"mask_rule_hit"`
MaskTime string `json:"mask_time"`
Warning interface{} `json:"warning"`
Error interface{} `json:"error"`
IsCritical bool `json:"is_critical"`
Rows [][]interface{} `json:"rows"`
ColumnList []string `json:"column_list"`
Status interface{} `json:"status"`
AffectedRows int `json:"affected_rows"`
SecondsBehindMaster interface{} `json:"seconds_behind_master"`
}