1// Copyright (C) MongoDB, Inc. 2019-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7// Code generated by operationgen. DO NOT EDIT.
8
9package operation
10
11import (
12	"context"
13	"errors"
14	"fmt"
15
16	"go.mongodb.org/mongo-driver/event"
17	"go.mongodb.org/mongo-driver/mongo/description"
18	"go.mongodb.org/mongo-driver/mongo/writeconcern"
19	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
20	"go.mongodb.org/mongo-driver/x/mongo/driver"
21	"go.mongodb.org/mongo-driver/x/mongo/driver/session"
22)
23
24// DropIndexes performs an dropIndexes operation.
25type DropIndexes struct {
26	index        *string
27	maxTimeMS    *int64
28	session      *session.Client
29	clock        *session.ClusterClock
30	collection   string
31	monitor      *event.CommandMonitor
32	crypt        *driver.Crypt
33	database     string
34	deployment   driver.Deployment
35	selector     description.ServerSelector
36	writeConcern *writeconcern.WriteConcern
37	result       DropIndexesResult
38}
39
40type DropIndexesResult struct {
41	// Number of indexes that existed before the drop was executed.
42	NIndexesWas int32
43}
44
45func buildDropIndexesResult(response bsoncore.Document, srvr driver.Server) (DropIndexesResult, error) {
46	elements, err := response.Elements()
47	if err != nil {
48		return DropIndexesResult{}, err
49	}
50	dir := DropIndexesResult{}
51	for _, element := range elements {
52		switch element.Key() {
53		case "nIndexesWas":
54			var ok bool
55			dir.NIndexesWas, ok = element.Value().AsInt32OK()
56			if !ok {
57				err = fmt.Errorf("response field 'nIndexesWas' is type int32, but received BSON type %s", element.Value().Type)
58			}
59		}
60	}
61	return dir, nil
62}
63
64// NewDropIndexes constructs and returns a new DropIndexes.
65func NewDropIndexes(index string) *DropIndexes {
66	return &DropIndexes{
67		index: &index,
68	}
69}
70
71// Result returns the result of executing this operation.
72func (di *DropIndexes) Result() DropIndexesResult { return di.result }
73
74func (di *DropIndexes) processResponse(response bsoncore.Document, srvr driver.Server, desc description.Server, _ int) error {
75	var err error
76	di.result, err = buildDropIndexesResult(response, srvr)
77	return err
78}
79
80// Execute runs this operations and returns an error if the operaiton did not execute successfully.
81func (di *DropIndexes) Execute(ctx context.Context) error {
82	if di.deployment == nil {
83		return errors.New("the DropIndexes operation must have a Deployment set before Execute can be called")
84	}
85
86	return driver.Operation{
87		CommandFn:         di.command,
88		ProcessResponseFn: di.processResponse,
89		Client:            di.session,
90		Clock:             di.clock,
91		CommandMonitor:    di.monitor,
92		Crypt:             di.crypt,
93		Database:          di.database,
94		Deployment:        di.deployment,
95		Selector:          di.selector,
96		WriteConcern:      di.writeConcern,
97	}.Execute(ctx, nil)
98
99}
100
101func (di *DropIndexes) command(dst []byte, desc description.SelectedServer) ([]byte, error) {
102	dst = bsoncore.AppendStringElement(dst, "dropIndexes", di.collection)
103	if di.index != nil {
104		dst = bsoncore.AppendStringElement(dst, "index", *di.index)
105	}
106	if di.maxTimeMS != nil {
107		dst = bsoncore.AppendInt64Element(dst, "maxTimeMS", *di.maxTimeMS)
108	}
109	return dst, nil
110}
111
112// Index specifies the name of the index to drop. If '*' is specified, all indexes will be dropped.
113//
114func (di *DropIndexes) Index(index string) *DropIndexes {
115	if di == nil {
116		di = new(DropIndexes)
117	}
118
119	di.index = &index
120	return di
121}
122
123// MaxTimeMS specifies the maximum amount of time to allow the query to run.
124func (di *DropIndexes) MaxTimeMS(maxTimeMS int64) *DropIndexes {
125	if di == nil {
126		di = new(DropIndexes)
127	}
128
129	di.maxTimeMS = &maxTimeMS
130	return di
131}
132
133// Session sets the session for this operation.
134func (di *DropIndexes) Session(session *session.Client) *DropIndexes {
135	if di == nil {
136		di = new(DropIndexes)
137	}
138
139	di.session = session
140	return di
141}
142
143// ClusterClock sets the cluster clock for this operation.
144func (di *DropIndexes) ClusterClock(clock *session.ClusterClock) *DropIndexes {
145	if di == nil {
146		di = new(DropIndexes)
147	}
148
149	di.clock = clock
150	return di
151}
152
153// Collection sets the collection that this command will run against.
154func (di *DropIndexes) Collection(collection string) *DropIndexes {
155	if di == nil {
156		di = new(DropIndexes)
157	}
158
159	di.collection = collection
160	return di
161}
162
163// CommandMonitor sets the monitor to use for APM events.
164func (di *DropIndexes) CommandMonitor(monitor *event.CommandMonitor) *DropIndexes {
165	if di == nil {
166		di = new(DropIndexes)
167	}
168
169	di.monitor = monitor
170	return di
171}
172
173// Crypt sets the Crypt object to use for automatic encryption and decryption.
174func (di *DropIndexes) Crypt(crypt *driver.Crypt) *DropIndexes {
175	if di == nil {
176		di = new(DropIndexes)
177	}
178
179	di.crypt = crypt
180	return di
181}
182
183// Database sets the database to run this operation against.
184func (di *DropIndexes) Database(database string) *DropIndexes {
185	if di == nil {
186		di = new(DropIndexes)
187	}
188
189	di.database = database
190	return di
191}
192
193// Deployment sets the deployment to use for this operation.
194func (di *DropIndexes) Deployment(deployment driver.Deployment) *DropIndexes {
195	if di == nil {
196		di = new(DropIndexes)
197	}
198
199	di.deployment = deployment
200	return di
201}
202
203// ServerSelector sets the selector used to retrieve a server.
204func (di *DropIndexes) ServerSelector(selector description.ServerSelector) *DropIndexes {
205	if di == nil {
206		di = new(DropIndexes)
207	}
208
209	di.selector = selector
210	return di
211}
212
213// WriteConcern sets the write concern for this operation.
214func (di *DropIndexes) WriteConcern(writeConcern *writeconcern.WriteConcern) *DropIndexes {
215	if di == nil {
216		di = new(DropIndexes)
217	}
218
219	di.writeConcern = writeConcern
220	return di
221}
222