...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_collection.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/operation

     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  package operation
     8  
     9  import (
    10  	"context"
    11  	"errors"
    12  	"fmt"
    13  	"time"
    14  
    15  	"go.mongodb.org/mongo-driver/event"
    16  	"go.mongodb.org/mongo-driver/internal/driverutil"
    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  // DropCollection performs a drop operation.
    25  type DropCollection struct {
    26  	session      *session.Client
    27  	clock        *session.ClusterClock
    28  	collection   string
    29  	monitor      *event.CommandMonitor
    30  	crypt        driver.Crypt
    31  	database     string
    32  	deployment   driver.Deployment
    33  	selector     description.ServerSelector
    34  	writeConcern *writeconcern.WriteConcern
    35  	result       DropCollectionResult
    36  	serverAPI    *driver.ServerAPIOptions
    37  	timeout      *time.Duration
    38  }
    39  
    40  // DropCollectionResult represents a dropCollection result returned by the server.
    41  type DropCollectionResult struct {
    42  	// The number of indexes in the dropped collection.
    43  	NIndexesWas int32
    44  	// The namespace of the dropped collection.
    45  	Ns string
    46  }
    47  
    48  func buildDropCollectionResult(response bsoncore.Document) (DropCollectionResult, error) {
    49  	elements, err := response.Elements()
    50  	if err != nil {
    51  		return DropCollectionResult{}, err
    52  	}
    53  	dcr := DropCollectionResult{}
    54  	for _, element := range elements {
    55  		switch element.Key() {
    56  		case "nIndexesWas":
    57  			var ok bool
    58  			dcr.NIndexesWas, ok = element.Value().AsInt32OK()
    59  			if !ok {
    60  				return dcr, fmt.Errorf("response field 'nIndexesWas' is type int32, but received BSON type %s", element.Value().Type)
    61  			}
    62  		case "ns":
    63  			var ok bool
    64  			dcr.Ns, ok = element.Value().StringValueOK()
    65  			if !ok {
    66  				return dcr, fmt.Errorf("response field 'ns' is type string, but received BSON type %s", element.Value().Type)
    67  			}
    68  		}
    69  	}
    70  	return dcr, nil
    71  }
    72  
    73  // NewDropCollection constructs and returns a new DropCollection.
    74  func NewDropCollection() *DropCollection {
    75  	return &DropCollection{}
    76  }
    77  
    78  // Result returns the result of executing this operation.
    79  func (dc *DropCollection) Result() DropCollectionResult { return dc.result }
    80  
    81  func (dc *DropCollection) processResponse(info driver.ResponseInfo) error {
    82  	var err error
    83  	dc.result, err = buildDropCollectionResult(info.ServerResponse)
    84  	return err
    85  }
    86  
    87  // Execute runs this operations and returns an error if the operation did not execute successfully.
    88  func (dc *DropCollection) Execute(ctx context.Context) error {
    89  	if dc.deployment == nil {
    90  		return errors.New("the DropCollection operation must have a Deployment set before Execute can be called")
    91  	}
    92  
    93  	return driver.Operation{
    94  		CommandFn:         dc.command,
    95  		ProcessResponseFn: dc.processResponse,
    96  		Client:            dc.session,
    97  		Clock:             dc.clock,
    98  		CommandMonitor:    dc.monitor,
    99  		Crypt:             dc.crypt,
   100  		Database:          dc.database,
   101  		Deployment:        dc.deployment,
   102  		Selector:          dc.selector,
   103  		WriteConcern:      dc.writeConcern,
   104  		ServerAPI:         dc.serverAPI,
   105  		Timeout:           dc.timeout,
   106  		Name:              driverutil.DropOp,
   107  	}.Execute(ctx)
   108  
   109  }
   110  
   111  func (dc *DropCollection) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
   112  	dst = bsoncore.AppendStringElement(dst, "drop", dc.collection)
   113  	return dst, nil
   114  }
   115  
   116  // Session sets the session for this operation.
   117  func (dc *DropCollection) Session(session *session.Client) *DropCollection {
   118  	if dc == nil {
   119  		dc = new(DropCollection)
   120  	}
   121  
   122  	dc.session = session
   123  	return dc
   124  }
   125  
   126  // ClusterClock sets the cluster clock for this operation.
   127  func (dc *DropCollection) ClusterClock(clock *session.ClusterClock) *DropCollection {
   128  	if dc == nil {
   129  		dc = new(DropCollection)
   130  	}
   131  
   132  	dc.clock = clock
   133  	return dc
   134  }
   135  
   136  // Collection sets the collection that this command will run against.
   137  func (dc *DropCollection) Collection(collection string) *DropCollection {
   138  	if dc == nil {
   139  		dc = new(DropCollection)
   140  	}
   141  
   142  	dc.collection = collection
   143  	return dc
   144  }
   145  
   146  // CommandMonitor sets the monitor to use for APM events.
   147  func (dc *DropCollection) CommandMonitor(monitor *event.CommandMonitor) *DropCollection {
   148  	if dc == nil {
   149  		dc = new(DropCollection)
   150  	}
   151  
   152  	dc.monitor = monitor
   153  	return dc
   154  }
   155  
   156  // Crypt sets the Crypt object to use for automatic encryption and decryption.
   157  func (dc *DropCollection) Crypt(crypt driver.Crypt) *DropCollection {
   158  	if dc == nil {
   159  		dc = new(DropCollection)
   160  	}
   161  
   162  	dc.crypt = crypt
   163  	return dc
   164  }
   165  
   166  // Database sets the database to run this operation against.
   167  func (dc *DropCollection) Database(database string) *DropCollection {
   168  	if dc == nil {
   169  		dc = new(DropCollection)
   170  	}
   171  
   172  	dc.database = database
   173  	return dc
   174  }
   175  
   176  // Deployment sets the deployment to use for this operation.
   177  func (dc *DropCollection) Deployment(deployment driver.Deployment) *DropCollection {
   178  	if dc == nil {
   179  		dc = new(DropCollection)
   180  	}
   181  
   182  	dc.deployment = deployment
   183  	return dc
   184  }
   185  
   186  // ServerSelector sets the selector used to retrieve a server.
   187  func (dc *DropCollection) ServerSelector(selector description.ServerSelector) *DropCollection {
   188  	if dc == nil {
   189  		dc = new(DropCollection)
   190  	}
   191  
   192  	dc.selector = selector
   193  	return dc
   194  }
   195  
   196  // WriteConcern sets the write concern for this operation.
   197  func (dc *DropCollection) WriteConcern(writeConcern *writeconcern.WriteConcern) *DropCollection {
   198  	if dc == nil {
   199  		dc = new(DropCollection)
   200  	}
   201  
   202  	dc.writeConcern = writeConcern
   203  	return dc
   204  }
   205  
   206  // ServerAPI sets the server API version for this operation.
   207  func (dc *DropCollection) ServerAPI(serverAPI *driver.ServerAPIOptions) *DropCollection {
   208  	if dc == nil {
   209  		dc = new(DropCollection)
   210  	}
   211  
   212  	dc.serverAPI = serverAPI
   213  	return dc
   214  }
   215  
   216  // Timeout sets the timeout for this operation.
   217  func (dc *DropCollection) Timeout(timeout *time.Duration) *DropCollection {
   218  	if dc == nil {
   219  		dc = new(DropCollection)
   220  	}
   221  
   222  	dc.timeout = timeout
   223  	return dc
   224  }
   225  

View as plain text