forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplain_query_command.go
60 lines (49 loc) · 1.35 KB
/
explain_query_command.go
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
package ravendb
import (
"net/http"
)
var (
_ RavenCommand = &ExplainQueryCommand{}
)
type ExplainQueryResult struct {
Index string `json:"Index"`
Reason string `json:"Reason"`
}
type ExplainQueryCommand struct {
RavenCommandBase
_conventions *DocumentConventions
_indexQuery *IndexQuery
Result []*ExplainQueryResult
}
func NewExplainQueryCommand(conventions *DocumentConventions, indexQuery *IndexQuery) *ExplainQueryCommand {
panicIf(conventions == nil, "Conventions cannot be null")
panicIf(indexQuery == nil, "IndexQuery cannot be null")
cmd := &ExplainQueryCommand{
RavenCommandBase: NewRavenCommandBase(),
_conventions: conventions,
_indexQuery: indexQuery,
}
cmd.IsReadRequest = true
return cmd
}
func (c *ExplainQueryCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
url := node.URL + "/databases/" + node.Database + "/queries?debug=explain"
v := jsonExtensionsWriteIndexQuery(c._conventions, c._indexQuery)
d, err := jsonMarshal(v)
panicIf(err != nil, "jsonMarshal() failed with %s", err)
return NewHttpPost(url, d)
}
func (c *ExplainQueryCommand) SetResponse(response []byte, fromCache bool) error {
var res struct {
Results []*ExplainQueryResult
}
err := jsonUnmarshal(response, &res)
if err != nil {
return err
}
if res.Results == nil {
return throwInvalidResponse()
}
c.Result = res.Results
return nil
}