1
2
3
4
5
6
7 package operation
8
9 import (
10 "context"
11 "errors"
12
13 "go.mongodb.org/mongo-driver/event"
14 "go.mongodb.org/mongo-driver/mongo/description"
15 "go.mongodb.org/mongo-driver/mongo/writeconcern"
16 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
17 "go.mongodb.org/mongo-driver/x/mongo/driver"
18 "go.mongodb.org/mongo-driver/x/mongo/driver/session"
19 )
20
21
22 type Create struct {
23 capped *bool
24 collation bsoncore.Document
25 changeStreamPreAndPostImages bsoncore.Document
26 collectionName *string
27 indexOptionDefaults bsoncore.Document
28 max *int64
29 pipeline bsoncore.Document
30 size *int64
31 storageEngine bsoncore.Document
32 validationAction *string
33 validationLevel *string
34 validator bsoncore.Document
35 viewOn *string
36 session *session.Client
37 clock *session.ClusterClock
38 monitor *event.CommandMonitor
39 crypt driver.Crypt
40 database string
41 deployment driver.Deployment
42 selector description.ServerSelector
43 writeConcern *writeconcern.WriteConcern
44 serverAPI *driver.ServerAPIOptions
45 expireAfterSeconds *int64
46 timeSeries bsoncore.Document
47 encryptedFields bsoncore.Document
48 clusteredIndex bsoncore.Document
49 }
50
51
52 func NewCreate(collectionName string) *Create {
53 return &Create{
54 collectionName: &collectionName,
55 }
56 }
57
58 func (c *Create) processResponse(driver.ResponseInfo) error {
59 return nil
60 }
61
62
63 func (c *Create) Execute(ctx context.Context) error {
64 if c.deployment == nil {
65 return errors.New("the Create operation must have a Deployment set before Execute can be called")
66 }
67
68 return driver.Operation{
69 CommandFn: c.command,
70 ProcessResponseFn: c.processResponse,
71 Client: c.session,
72 Clock: c.clock,
73 CommandMonitor: c.monitor,
74 Crypt: c.crypt,
75 Database: c.database,
76 Deployment: c.deployment,
77 Selector: c.selector,
78 WriteConcern: c.writeConcern,
79 ServerAPI: c.serverAPI,
80 }.Execute(ctx)
81 }
82
83 func (c *Create) command(dst []byte, desc description.SelectedServer) ([]byte, error) {
84 if c.collectionName != nil {
85 dst = bsoncore.AppendStringElement(dst, "create", *c.collectionName)
86 }
87 if c.capped != nil {
88 dst = bsoncore.AppendBooleanElement(dst, "capped", *c.capped)
89 }
90 if c.changeStreamPreAndPostImages != nil {
91 dst = bsoncore.AppendDocumentElement(dst, "changeStreamPreAndPostImages", c.changeStreamPreAndPostImages)
92 }
93 if c.collation != nil {
94 if desc.WireVersion == nil || !desc.WireVersion.Includes(5) {
95 return nil, errors.New("the 'collation' command parameter requires a minimum server wire version of 5")
96 }
97 dst = bsoncore.AppendDocumentElement(dst, "collation", c.collation)
98 }
99 if c.indexOptionDefaults != nil {
100 dst = bsoncore.AppendDocumentElement(dst, "indexOptionDefaults", c.indexOptionDefaults)
101 }
102 if c.max != nil {
103 dst = bsoncore.AppendInt64Element(dst, "max", *c.max)
104 }
105 if c.pipeline != nil {
106 dst = bsoncore.AppendArrayElement(dst, "pipeline", c.pipeline)
107 }
108 if c.size != nil {
109 dst = bsoncore.AppendInt64Element(dst, "size", *c.size)
110 }
111 if c.storageEngine != nil {
112 dst = bsoncore.AppendDocumentElement(dst, "storageEngine", c.storageEngine)
113 }
114 if c.validationAction != nil {
115 dst = bsoncore.AppendStringElement(dst, "validationAction", *c.validationAction)
116 }
117 if c.validationLevel != nil {
118 dst = bsoncore.AppendStringElement(dst, "validationLevel", *c.validationLevel)
119 }
120 if c.validator != nil {
121 dst = bsoncore.AppendDocumentElement(dst, "validator", c.validator)
122 }
123 if c.viewOn != nil {
124 dst = bsoncore.AppendStringElement(dst, "viewOn", *c.viewOn)
125 }
126 if c.expireAfterSeconds != nil {
127 dst = bsoncore.AppendInt64Element(dst, "expireAfterSeconds", *c.expireAfterSeconds)
128 }
129 if c.timeSeries != nil {
130 dst = bsoncore.AppendDocumentElement(dst, "timeseries", c.timeSeries)
131 }
132 if c.encryptedFields != nil {
133 dst = bsoncore.AppendDocumentElement(dst, "encryptedFields", c.encryptedFields)
134 }
135 if c.clusteredIndex != nil {
136 dst = bsoncore.AppendDocumentElement(dst, "clusteredIndex", c.clusteredIndex)
137 }
138 return dst, nil
139 }
140
141
142 func (c *Create) Capped(capped bool) *Create {
143 if c == nil {
144 c = new(Create)
145 }
146
147 c.capped = &capped
148 return c
149 }
150
151
152 func (c *Create) Collation(collation bsoncore.Document) *Create {
153 if c == nil {
154 c = new(Create)
155 }
156
157 c.collation = collation
158 return c
159 }
160
161
162
163 func (c *Create) ChangeStreamPreAndPostImages(csppi bsoncore.Document) *Create {
164 if c == nil {
165 c = new(Create)
166 }
167
168 c.changeStreamPreAndPostImages = csppi
169 return c
170 }
171
172
173 func (c *Create) CollectionName(collectionName string) *Create {
174 if c == nil {
175 c = new(Create)
176 }
177
178 c.collectionName = &collectionName
179 return c
180 }
181
182
183 func (c *Create) IndexOptionDefaults(indexOptionDefaults bsoncore.Document) *Create {
184 if c == nil {
185 c = new(Create)
186 }
187
188 c.indexOptionDefaults = indexOptionDefaults
189 return c
190 }
191
192
193 func (c *Create) Max(max int64) *Create {
194 if c == nil {
195 c = new(Create)
196 }
197
198 c.max = &max
199 return c
200 }
201
202
203 func (c *Create) Pipeline(pipeline bsoncore.Document) *Create {
204 if c == nil {
205 c = new(Create)
206 }
207
208 c.pipeline = pipeline
209 return c
210 }
211
212
213 func (c *Create) Size(size int64) *Create {
214 if c == nil {
215 c = new(Create)
216 }
217
218 c.size = &size
219 return c
220 }
221
222
223 func (c *Create) StorageEngine(storageEngine bsoncore.Document) *Create {
224 if c == nil {
225 c = new(Create)
226 }
227
228 c.storageEngine = storageEngine
229 return c
230 }
231
232
233 func (c *Create) ValidationAction(validationAction string) *Create {
234 if c == nil {
235 c = new(Create)
236 }
237
238 c.validationAction = &validationAction
239 return c
240 }
241
242
243
244 func (c *Create) ValidationLevel(validationLevel string) *Create {
245 if c == nil {
246 c = new(Create)
247 }
248
249 c.validationLevel = &validationLevel
250 return c
251 }
252
253
254 func (c *Create) Validator(validator bsoncore.Document) *Create {
255 if c == nil {
256 c = new(Create)
257 }
258
259 c.validator = validator
260 return c
261 }
262
263
264 func (c *Create) ViewOn(viewOn string) *Create {
265 if c == nil {
266 c = new(Create)
267 }
268
269 c.viewOn = &viewOn
270 return c
271 }
272
273
274 func (c *Create) Session(session *session.Client) *Create {
275 if c == nil {
276 c = new(Create)
277 }
278
279 c.session = session
280 return c
281 }
282
283
284 func (c *Create) ClusterClock(clock *session.ClusterClock) *Create {
285 if c == nil {
286 c = new(Create)
287 }
288
289 c.clock = clock
290 return c
291 }
292
293
294 func (c *Create) CommandMonitor(monitor *event.CommandMonitor) *Create {
295 if c == nil {
296 c = new(Create)
297 }
298
299 c.monitor = monitor
300 return c
301 }
302
303
304 func (c *Create) Crypt(crypt driver.Crypt) *Create {
305 if c == nil {
306 c = new(Create)
307 }
308
309 c.crypt = crypt
310 return c
311 }
312
313
314 func (c *Create) Database(database string) *Create {
315 if c == nil {
316 c = new(Create)
317 }
318
319 c.database = database
320 return c
321 }
322
323
324 func (c *Create) Deployment(deployment driver.Deployment) *Create {
325 if c == nil {
326 c = new(Create)
327 }
328
329 c.deployment = deployment
330 return c
331 }
332
333
334 func (c *Create) ServerSelector(selector description.ServerSelector) *Create {
335 if c == nil {
336 c = new(Create)
337 }
338
339 c.selector = selector
340 return c
341 }
342
343
344 func (c *Create) WriteConcern(writeConcern *writeconcern.WriteConcern) *Create {
345 if c == nil {
346 c = new(Create)
347 }
348
349 c.writeConcern = writeConcern
350 return c
351 }
352
353
354 func (c *Create) ServerAPI(serverAPI *driver.ServerAPIOptions) *Create {
355 if c == nil {
356 c = new(Create)
357 }
358
359 c.serverAPI = serverAPI
360 return c
361 }
362
363
364 func (c *Create) ExpireAfterSeconds(eas int64) *Create {
365 if c == nil {
366 c = new(Create)
367 }
368
369 c.expireAfterSeconds = &eas
370 return c
371 }
372
373
374 func (c *Create) TimeSeries(timeSeries bsoncore.Document) *Create {
375 if c == nil {
376 c = new(Create)
377 }
378
379 c.timeSeries = timeSeries
380 return c
381 }
382
383
384 func (c *Create) EncryptedFields(ef bsoncore.Document) *Create {
385 if c == nil {
386 c = new(Create)
387 }
388
389 c.encryptedFields = ef
390 return c
391 }
392
393
394 func (c *Create) ClusteredIndex(ci bsoncore.Document) *Create {
395 if c == nil {
396 c = new(Create)
397 }
398
399 c.clusteredIndex = ci
400 return c
401 }
402
View as plain text