github.com/ravendb/ravendb-go-client@v0.0.0-20240229102137-4474ee7aa0fa/delete_by_query_operation.go (about) 1 package ravendb 2 3 import ( 4 "fmt" 5 "net/http" 6 "strconv" 7 ) 8 9 var ( 10 _ IOperation = &DeleteByQueryOperation{} 11 ) 12 13 type DeleteByQueryOperation struct { 14 Command *DeleteByIndexCommand 15 16 queryToDelete *IndexQuery 17 options *QueryOperationOptions 18 } 19 20 func NewDeleteByQueryOperation(queryToDelete *IndexQuery, options *QueryOperationOptions) (*DeleteByQueryOperation, error) { 21 if queryToDelete == nil { 22 return nil, newIllegalArgumentError("QueryToDelete cannot be null") 23 } 24 return &DeleteByQueryOperation{ 25 queryToDelete: queryToDelete, 26 options: options, 27 }, nil 28 } 29 30 func (o *DeleteByQueryOperation) GetCommand(store *DocumentStore, conventions *DocumentConventions, cache *httpCache) (RavenCommand, error) { 31 var err error 32 o.Command, err = NewDeleteByIndexCommand(conventions, o.queryToDelete, o.options) 33 return o.Command, err 34 } 35 36 var _ RavenCommand = &DeleteByIndexCommand{} 37 38 type DeleteByIndexCommand struct { 39 RavenCommandBase 40 41 conventions *DocumentConventions 42 queryToDelete *IndexQuery 43 options *QueryOperationOptions 44 45 Result *OperationIDResult 46 } 47 48 func NewDeleteByIndexCommand(conventions *DocumentConventions, queryToDelete *IndexQuery, options *QueryOperationOptions) (*DeleteByIndexCommand, error) { 49 if options == nil { 50 options = &QueryOperationOptions{} 51 } 52 cmd := &DeleteByIndexCommand{ 53 RavenCommandBase: NewRavenCommandBase(), 54 55 conventions: conventions, 56 queryToDelete: queryToDelete, 57 options: options, 58 } 59 return cmd, nil 60 } 61 62 func (c *DeleteByIndexCommand) CreateRequest(node *ServerNode) (*http.Request, error) { 63 options := c.options 64 65 url := node.URL + "/databases/" + node.Database + fmt.Sprintf("/queries?allowStale=%v", options.AllowStale) 66 67 if options.MaxOpsPerSecond != 0 { 68 url += "&maxOpsPerSec=" + strconv.Itoa(options.MaxOpsPerSecond) 69 } 70 71 url += fmt.Sprintf("&details=%v", options.RetrieveDetails) 72 73 if options.StaleTimeout != 0 { 74 url += "&staleTimeout=" + durationToTimeSpan(options.StaleTimeout) 75 } 76 77 m := jsonExtensionsWriteIndexQuery(c.conventions, c.queryToDelete) 78 d, err := jsonMarshal(m) 79 // TODO: return error instead? 80 panicIf(err != nil, "jsonMarshal failed with %s", err) 81 82 request, err := newHttpDelete(url, d) 83 if err != nil { 84 return nil, err 85 } 86 return request, nil 87 } 88 89 func (c *DeleteByIndexCommand) SetResponse(response []byte, fromCache bool) error { 90 if len(response) == 0 { 91 return throwInvalidResponse() 92 } 93 94 return jsonUnmarshal(response, &c.Result) 95 }