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
15	"go.mongodb.org/mongo-driver/event"
16	"go.mongodb.org/mongo-driver/mongo/description"
17	"go.mongodb.org/mongo-driver/mongo/readpref"
18	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
19	"go.mongodb.org/mongo-driver/x/mongo/driver"
20	"go.mongodb.org/mongo-driver/x/mongo/driver/session"
21)
22
23// ListCollections performs a listCollections operation.
24type ListCollections struct {
25	filter         bsoncore.Document
26	nameOnly       *bool
27	session        *session.Client
28	clock          *session.ClusterClock
29	monitor        *event.CommandMonitor
30	crypt          *driver.Crypt
31	database       string
32	deployment     driver.Deployment
33	readPreference *readpref.ReadPref
34	selector       description.ServerSelector
35	retry          *driver.RetryMode
36	result         driver.CursorResponse
37	batchSize      *int32
38}
39
40// NewListCollections constructs and returns a new ListCollections.
41func NewListCollections(filter bsoncore.Document) *ListCollections {
42	return &ListCollections{
43		filter: filter,
44	}
45}
46
47// Result returns the result of executing this operation.
48func (lc *ListCollections) Result(opts driver.CursorOptions) (*driver.ListCollectionsBatchCursor, error) {
49	bc, err := driver.NewBatchCursor(lc.result, lc.session, lc.clock, opts)
50	if err != nil {
51		return nil, err
52	}
53	desc := lc.result.Desc
54	if desc.WireVersion == nil || desc.WireVersion.Max < 3 {
55		return driver.NewLegacyListCollectionsBatchCursor(bc)
56	}
57	return driver.NewListCollectionsBatchCursor(bc)
58}
59
60func (lc *ListCollections) processResponse(response bsoncore.Document, srvr driver.Server, desc description.Server, _ int) error {
61	var err error
62	lc.result, err = driver.NewCursorResponse(response, srvr, desc)
63	return err
64}
65
66// Execute runs this operations and returns an error if the operaiton did not execute successfully.
67func (lc *ListCollections) Execute(ctx context.Context) error {
68	if lc.deployment == nil {
69		return errors.New("the ListCollections operation must have a Deployment set before Execute can be called")
70	}
71
72	return driver.Operation{
73		CommandFn:         lc.command,
74		ProcessResponseFn: lc.processResponse,
75		RetryMode:         lc.retry,
76		Type:              driver.Read,
77		Client:            lc.session,
78		Clock:             lc.clock,
79		CommandMonitor:    lc.monitor,
80		Crypt:             lc.crypt,
81		Database:          lc.database,
82		Deployment:        lc.deployment,
83		ReadPreference:    lc.readPreference,
84		Selector:          lc.selector,
85		Legacy:            driver.LegacyListCollections,
86	}.Execute(ctx, nil)
87
88}
89
90func (lc *ListCollections) command(dst []byte, desc description.SelectedServer) ([]byte, error) {
91
92	dst = bsoncore.AppendInt32Element(dst, "listCollections", 1)
93	if lc.filter != nil {
94		dst = bsoncore.AppendDocumentElement(dst, "filter", lc.filter)
95	}
96	if lc.nameOnly != nil {
97		dst = bsoncore.AppendBooleanElement(dst, "nameOnly", *lc.nameOnly)
98	}
99	cursorDoc := bsoncore.NewDocumentBuilder()
100	if lc.batchSize != nil {
101		cursorDoc.AppendInt32("batchSize", *lc.batchSize)
102	}
103	dst = bsoncore.AppendDocumentElement(dst, "cursor", cursorDoc.Build())
104
105	return dst, nil
106}
107
108// Filter determines what results are returned from listCollections.
109func (lc *ListCollections) Filter(filter bsoncore.Document) *ListCollections {
110	if lc == nil {
111		lc = new(ListCollections)
112	}
113
114	lc.filter = filter
115	return lc
116}
117
118// NameOnly specifies whether to only return collection names.
119func (lc *ListCollections) NameOnly(nameOnly bool) *ListCollections {
120	if lc == nil {
121		lc = new(ListCollections)
122	}
123
124	lc.nameOnly = &nameOnly
125	return lc
126}
127
128// Session sets the session for this operation.
129func (lc *ListCollections) Session(session *session.Client) *ListCollections {
130	if lc == nil {
131		lc = new(ListCollections)
132	}
133
134	lc.session = session
135	return lc
136}
137
138// ClusterClock sets the cluster clock for this operation.
139func (lc *ListCollections) ClusterClock(clock *session.ClusterClock) *ListCollections {
140	if lc == nil {
141		lc = new(ListCollections)
142	}
143
144	lc.clock = clock
145	return lc
146}
147
148// CommandMonitor sets the monitor to use for APM events.
149func (lc *ListCollections) CommandMonitor(monitor *event.CommandMonitor) *ListCollections {
150	if lc == nil {
151		lc = new(ListCollections)
152	}
153
154	lc.monitor = monitor
155	return lc
156}
157
158// Crypt sets the Crypt object to use for automatic encryption and decryption.
159func (lc *ListCollections) Crypt(crypt *driver.Crypt) *ListCollections {
160	if lc == nil {
161		lc = new(ListCollections)
162	}
163
164	lc.crypt = crypt
165	return lc
166}
167
168// Database sets the database to run this operation against.
169func (lc *ListCollections) Database(database string) *ListCollections {
170	if lc == nil {
171		lc = new(ListCollections)
172	}
173
174	lc.database = database
175	return lc
176}
177
178// Deployment sets the deployment to use for this operation.
179func (lc *ListCollections) Deployment(deployment driver.Deployment) *ListCollections {
180	if lc == nil {
181		lc = new(ListCollections)
182	}
183
184	lc.deployment = deployment
185	return lc
186}
187
188// ReadPreference set the read prefernce used with this operation.
189func (lc *ListCollections) ReadPreference(readPreference *readpref.ReadPref) *ListCollections {
190	if lc == nil {
191		lc = new(ListCollections)
192	}
193
194	lc.readPreference = readPreference
195	return lc
196}
197
198// ServerSelector sets the selector used to retrieve a server.
199func (lc *ListCollections) ServerSelector(selector description.ServerSelector) *ListCollections {
200	if lc == nil {
201		lc = new(ListCollections)
202	}
203
204	lc.selector = selector
205	return lc
206}
207
208// Retry enables retryable mode for this operation. Retries are handled automatically in driver.Operation.Execute based
209// on how the operation is set.
210func (lc *ListCollections) Retry(retry driver.RetryMode) *ListCollections {
211	if lc == nil {
212		lc = new(ListCollections)
213	}
214
215	lc.retry = &retry
216	return lc
217}
218
219// BatchSize specifies the number of documents to return in every batch.
220func (lc *ListCollections) BatchSize(batchSize int32) *ListCollections {
221	if lc == nil {
222		lc = new(ListCollections)
223	}
224
225	lc.batchSize = &batchSize
226	return lc
227}
228