forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_by_query_operation.go
95 lines (74 loc) · 2.38 KB
/
delete_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
package ravendb
import (
"fmt"
"net/http"
"strconv"
)
var (
_ IOperation = &DeleteByQueryOperation{}
)
type DeleteByQueryOperation struct {
Command *DeleteByIndexCommand
queryToDelete *IndexQuery
options *QueryOperationOptions
}
func NewDeleteByQueryOperation(queryToDelete *IndexQuery, options *QueryOperationOptions) (*DeleteByQueryOperation, error) {
if queryToDelete == nil {
return nil, newIllegalArgumentError("QueryToDelete cannot be null")
}
return &DeleteByQueryOperation{
queryToDelete: queryToDelete,
options: options,
}, nil
}
func (o *DeleteByQueryOperation) GetCommand(store *DocumentStore, conventions *DocumentConventions, cache *httpCache) (RavenCommand, error) {
var err error
o.Command, err = NewDeleteByIndexCommand(conventions, o.queryToDelete, o.options)
return o.Command, err
}
var _ RavenCommand = &DeleteByIndexCommand{}
type DeleteByIndexCommand struct {
RavenCommandBase
conventions *DocumentConventions
queryToDelete *IndexQuery
options *QueryOperationOptions
Result *OperationIDResult
}
func NewDeleteByIndexCommand(conventions *DocumentConventions, queryToDelete *IndexQuery, options *QueryOperationOptions) (*DeleteByIndexCommand, error) {
if options == nil {
options = &QueryOperationOptions{}
}
cmd := &DeleteByIndexCommand{
RavenCommandBase: NewRavenCommandBase(),
conventions: conventions,
queryToDelete: queryToDelete,
options: options,
}
return cmd, nil
}
func (c *DeleteByIndexCommand) 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)
}
m := jsonExtensionsWriteIndexQuery(c.conventions, c.queryToDelete)
d, err := jsonMarshal(m)
// TODO: return error instead?
panicIf(err != nil, "jsonMarshal failed with %s", err)
request, err := newHttpDelete(url, d)
if err != nil {
return nil, err
}
return request, nil
}
func (c *DeleteByIndexCommand) SetResponse(response []byte, fromCache bool) error {
if len(response) == 0 {
return throwInvalidResponse()
}
return jsonUnmarshal(response, &c.Result)
}