forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_by_query_operation.go
98 lines (75 loc) · 2.34 KB
/
patch_by_query_operation.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package ravendb
import (
"fmt"
"net/http"
"strconv"
)
var (
_ IOperation = &PatchByQueryOperation{}
)
type PatchByQueryOperation struct {
Command *PatchByQueryCommand
_queryToUpdate *IndexQuery
_options *QueryOperationOptions
}
func NewPatchByQueryOperation(queryToUpdate string) *PatchByQueryOperation {
return &PatchByQueryOperation{
_queryToUpdate: NewIndexQuery(queryToUpdate),
}
}
func (o *PatchByQueryOperation) GetCommand(store *DocumentStore, conventions *DocumentConventions, cache *httpCache) (RavenCommand, error) {
var err error
o.Command, err = NewPatchByQueryCommand(conventions, o._queryToUpdate, o._options)
return o.Command, err
}
var _ RavenCommand = &PatchByQueryCommand{}
type PatchByQueryCommand struct {
RavenCommandBase
_conventions *DocumentConventions
_queryToUpdate *IndexQuery
_options *QueryOperationOptions
Result *OperationIDResult
}
func NewPatchByQueryCommand(conventions *DocumentConventions, queryToUpdate *IndexQuery, options *QueryOperationOptions) (*PatchByQueryCommand, error) {
if queryToUpdate == nil {
return nil, newIllegalArgumentError("QueryToUpdate cannot be null")
}
if options == nil {
options = &QueryOperationOptions{}
}
cmd := &PatchByQueryCommand{
RavenCommandBase: NewRavenCommandBase(),
_conventions: conventions,
_queryToUpdate: queryToUpdate,
_options: options,
}
return cmd, nil
}
func (c *PatchByQueryCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
_options := c._options
url := node.URL + "/databases/" + node.Database + fmt.Sprintf("/queries?allowStale=%v", _options.allowStale)
if _options.maxOpsPerSecond != 0 {
url += "&maxOpsPerSec=" + strconv.Itoa(_options.maxOpsPerSecond)
}
url += fmt.Sprintf("&details=%v", _options.retrieveDetails)
if _options.staleTimeout != 0 {
url += "&staleTimeout=" + durationToTimeSpan(_options.staleTimeout)
}
q := jsonExtensionsWriteIndexQuery(c._conventions, c._queryToUpdate)
m := map[string]interface{}{
"Query": q,
}
d, err := jsonMarshal(m)
panicIf(err != nil, "jsonMarshal failed with %s", err)
request, err := newHttpPatch(url, d)
if err != nil {
return nil, err
}
return request, nil
}
func (c *PatchByQueryCommand) SetResponse(response []byte, fromCache bool) error {
if len(response) == 0 {
return throwInvalidResponse()
}
return jsonUnmarshal(response, &c.Result)
}