forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_command.go
71 lines (56 loc) · 1.7 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
package ravendb
import (
"net/http"
)
var (
_ RavenCommand = &QueryCommand{}
)
type QueryCommand struct {
RavenCommandBase
conventions *DocumentConventions
indexQuery *IndexQuery
metadataOnly bool
indexEntriesOnly bool
Result *QueryResult
}
func NewQueryCommand(conventions *DocumentConventions, indexQuery *IndexQuery, metadataOnly bool, indexEntriesOnly bool) (*QueryCommand, error) {
if indexQuery == nil {
return nil, newIllegalArgumentError("IndexQuery cannot be null")
}
cmd := &QueryCommand{
RavenCommandBase: NewRavenCommandBase(),
conventions: conventions,
indexQuery: indexQuery,
metadataOnly: metadataOnly,
indexEntriesOnly: indexEntriesOnly,
}
cmd.IsReadRequest = true
return cmd, nil
}
func (c *QueryCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
c.CanCache = !c.indexQuery.disableCaching
// we won't allow aggressive caching of queries with WaitForNonStaleResults
c.CanCacheAggressively = c.CanCache && !c.indexQuery.waitForNonStaleResults
// we need to add a query hash because we are using POST queries
// so we need to unique parameter per query so the query cache will
// work properly
path := node.URL + "/databases/" + node.Database + "/queries?queryHash=" + c.indexQuery.GetQueryHash()
if c.metadataOnly {
path += "&metadataOnly=true"
}
if c.indexEntriesOnly {
path += "&debug=entries"
}
m := jsonExtensionsWriteIndexQuery(c.conventions, c.indexQuery)
d, err := jsonMarshal(m)
if err != nil {
return nil, err
}
return NewHttpPost(path, d)
}
func (c *QueryCommand) SetResponse(response []byte, fromCache bool) error {
if len(response) == 0 {
return nil
}
return jsonUnmarshal(response, &c.Result)
}