...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_database.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  
    13  	"go.mongodb.org/mongo-driver/event"
    14  	"go.mongodb.org/mongo-driver/internal/driverutil"
    15  	"go.mongodb.org/mongo-driver/mongo/description"
    16  	"go.mongodb.org/mongo-driver/mongo/writeconcern"
    17  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    18  	"go.mongodb.org/mongo-driver/x/mongo/driver"
    19  	"go.mongodb.org/mongo-driver/x/mongo/driver/session"
    20  )
    21  
    22  // DropDatabase performs a dropDatabase operation
    23  type DropDatabase struct {
    24  	session      *session.Client
    25  	clock        *session.ClusterClock
    26  	monitor      *event.CommandMonitor
    27  	crypt        driver.Crypt
    28  	database     string
    29  	deployment   driver.Deployment
    30  	selector     description.ServerSelector
    31  	writeConcern *writeconcern.WriteConcern
    32  	serverAPI    *driver.ServerAPIOptions
    33  }
    34  
    35  // NewDropDatabase constructs and returns a new DropDatabase.
    36  func NewDropDatabase() *DropDatabase {
    37  	return &DropDatabase{}
    38  }
    39  
    40  // Execute runs this operations and returns an error if the operation did not execute successfully.
    41  func (dd *DropDatabase) Execute(ctx context.Context) error {
    42  	if dd.deployment == nil {
    43  		return errors.New("the DropDatabase operation must have a Deployment set before Execute can be called")
    44  	}
    45  
    46  	return driver.Operation{
    47  		CommandFn:      dd.command,
    48  		Client:         dd.session,
    49  		Clock:          dd.clock,
    50  		CommandMonitor: dd.monitor,
    51  		Crypt:          dd.crypt,
    52  		Database:       dd.database,
    53  		Deployment:     dd.deployment,
    54  		Selector:       dd.selector,
    55  		WriteConcern:   dd.writeConcern,
    56  		ServerAPI:      dd.serverAPI,
    57  		Name:           driverutil.DropDatabaseOp,
    58  	}.Execute(ctx)
    59  
    60  }
    61  
    62  func (dd *DropDatabase) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
    63  
    64  	dst = bsoncore.AppendInt32Element(dst, "dropDatabase", 1)
    65  	return dst, nil
    66  }
    67  
    68  // Session sets the session for this operation.
    69  func (dd *DropDatabase) Session(session *session.Client) *DropDatabase {
    70  	if dd == nil {
    71  		dd = new(DropDatabase)
    72  	}
    73  
    74  	dd.session = session
    75  	return dd
    76  }
    77  
    78  // ClusterClock sets the cluster clock for this operation.
    79  func (dd *DropDatabase) ClusterClock(clock *session.ClusterClock) *DropDatabase {
    80  	if dd == nil {
    81  		dd = new(DropDatabase)
    82  	}
    83  
    84  	dd.clock = clock
    85  	return dd
    86  }
    87  
    88  // CommandMonitor sets the monitor to use for APM events.
    89  func (dd *DropDatabase) CommandMonitor(monitor *event.CommandMonitor) *DropDatabase {
    90  	if dd == nil {
    91  		dd = new(DropDatabase)
    92  	}
    93  
    94  	dd.monitor = monitor
    95  	return dd
    96  }
    97  
    98  // Crypt sets the Crypt object to use for automatic encryption and decryption.
    99  func (dd *DropDatabase) Crypt(crypt driver.Crypt) *DropDatabase {
   100  	if dd == nil {
   101  		dd = new(DropDatabase)
   102  	}
   103  
   104  	dd.crypt = crypt
   105  	return dd
   106  }
   107  
   108  // Database sets the database to run this operation against.
   109  func (dd *DropDatabase) Database(database string) *DropDatabase {
   110  	if dd == nil {
   111  		dd = new(DropDatabase)
   112  	}
   113  
   114  	dd.database = database
   115  	return dd
   116  }
   117  
   118  // Deployment sets the deployment to use for this operation.
   119  func (dd *DropDatabase) Deployment(deployment driver.Deployment) *DropDatabase {
   120  	if dd == nil {
   121  		dd = new(DropDatabase)
   122  	}
   123  
   124  	dd.deployment = deployment
   125  	return dd
   126  }
   127  
   128  // ServerSelector sets the selector used to retrieve a server.
   129  func (dd *DropDatabase) ServerSelector(selector description.ServerSelector) *DropDatabase {
   130  	if dd == nil {
   131  		dd = new(DropDatabase)
   132  	}
   133  
   134  	dd.selector = selector
   135  	return dd
   136  }
   137  
   138  // WriteConcern sets the write concern for this operation.
   139  func (dd *DropDatabase) WriteConcern(writeConcern *writeconcern.WriteConcern) *DropDatabase {
   140  	if dd == nil {
   141  		dd = new(DropDatabase)
   142  	}
   143  
   144  	dd.writeConcern = writeConcern
   145  	return dd
   146  }
   147  
   148  // ServerAPI sets the server API version for this operation.
   149  func (dd *DropDatabase) ServerAPI(serverAPI *driver.ServerAPIOptions) *DropDatabase {
   150  	if dd == nil {
   151  		dd = new(DropDatabase)
   152  	}
   153  
   154  	dd.serverAPI = serverAPI
   155  	return dd
   156  }
   157  

View as plain text