forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_by_query_operation.go
98 lines (75 loc) · 2.49 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
96
97
98
package ravendb
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)
var (
_ IOperation = &DeleteByQueryOperation{}
)
type DeleteByQueryOperation struct {
Command *DeleteByIndexCommand
_queryToDelete *IndexQuery
_options *QueryOperationOptions
}
func NewDeleteByQueryOperation(queryToDelete *IndexQuery) *DeleteByQueryOperation {
return NewDeleteByQueryOperationWithOptions(queryToDelete, nil)
}
func NewDeleteByQueryOperationWithOptions(queryToDelete *IndexQuery, options *QueryOperationOptions) *DeleteByQueryOperation {
// TODO: validate queryToDelete
return &DeleteByQueryOperation{
_queryToDelete: queryToDelete,
_options: options,
}
}
func (o *DeleteByQueryOperation) GetCommand(store *IDocumentStore, conventions *DocumentConventions, cache *HttpCache) RavenCommand {
o.Command = NewDeleteByIndexCommand(conventions, o._queryToDelete, o._options)
return o.Command
}
var _ RavenCommand = &DeleteByIndexCommand{}
type DeleteByIndexCommand struct {
RavenCommandBase
_conventions *DocumentConventions
_queryToDelete *IndexQuery
_options *QueryOperationOptions
Result *OperationIdResult
}
func NewDeleteByIndexCommand(conventions *DocumentConventions, queryToDelete *IndexQuery, options *QueryOperationOptions) *DeleteByIndexCommand {
if options == nil {
options = NewQueryOperationOptions()
}
cmd := &DeleteByIndexCommand{
RavenCommandBase: NewRavenCommandBase(),
_conventions: conventions,
_queryToDelete: queryToDelete,
_options: options,
}
return cmd
}
func (c *DeleteByIndexCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
_options := c._options
url := node.GetUrl() + "/databases/" + node.GetDatabase() + fmt.Sprintf("/queries?allowStale=%v", _options.isAllowStale())
if _options.getMaxOpsPerSecond() != 0 {
url += "&maxOpsPerSec=" + strconv.Itoa(_options.getMaxOpsPerSecond())
}
url += fmt.Sprintf("&details=%v", _options.isRetrieveDetails())
if _options.getStaleTimeout() != 0 {
url += "&staleTimeout=" + durationToTimeSpan(_options.getStaleTimeout())
}
m := JsonExtensions_writeIndexQuery(c._conventions, c._queryToDelete)
d, err := json.Marshal(m)
// TODO: return error instead?
panicIf(err != nil, "json.Marshal 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 {
throwInvalidResponse()
}
return json.Unmarshal(response, &c.Result)
}