Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-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 command |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | |
| 12 | "github.com/mongodb/mongo-go-driver/bson" |
| 13 | "github.com/mongodb/mongo-go-driver/mongo/writeconcern" |
| 14 | "github.com/mongodb/mongo-go-driver/x/bsonx" |
| 15 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/session" |
| 16 | "github.com/mongodb/mongo-go-driver/x/network/description" |
| 17 | "github.com/mongodb/mongo-go-driver/x/network/wiremessage" |
| 18 | ) |
| 19 | |
| 20 | // DropDatabase represents the DropDatabase command. |
| 21 | // |
| 22 | // The DropDatabases command drops database. |
| 23 | type DropDatabase struct { |
| 24 | DB string |
| 25 | WriteConcern *writeconcern.WriteConcern |
| 26 | Clock *session.ClusterClock |
| 27 | Session *session.Client |
| 28 | |
| 29 | result bson.Raw |
| 30 | err error |
| 31 | } |
| 32 | |
| 33 | // Encode will encode this command into a wire message for the given server description. |
| 34 | func (dd *DropDatabase) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) { |
| 35 | cmd, err := dd.encode(desc) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | |
| 40 | return cmd.Encode(desc) |
| 41 | } |
| 42 | |
| 43 | func (dd *DropDatabase) encode(desc description.SelectedServer) (*Write, error) { |
| 44 | cmd := bsonx.Doc{{"dropDatabase", bsonx.Int32(1)}} |
| 45 | |
| 46 | write := &Write{ |
| 47 | Clock: dd.Clock, |
| 48 | DB: dd.DB, |
| 49 | Command: cmd, |
| 50 | Session: dd.Session, |
| 51 | } |
| 52 | if desc.WireVersion != nil && desc.WireVersion.Max >= 5 { |
| 53 | write.WriteConcern = dd.WriteConcern |
| 54 | } |
| 55 | return write, nil |
| 56 | } |
| 57 | |
| 58 | // Decode will decode the wire message using the provided server description. Errors during decoding |
| 59 | // are deferred until either the Result or Err methods are called. |
| 60 | func (dd *DropDatabase) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *DropDatabase { |
| 61 | rdr, err := (&Write{}).Decode(desc, wm).Result() |
| 62 | if err != nil { |
| 63 | dd.err = err |
| 64 | return dd |
| 65 | } |
| 66 | |
| 67 | return dd.decode(desc, rdr) |
| 68 | } |
| 69 | |
| 70 | func (dd *DropDatabase) decode(desc description.SelectedServer, rdr bson.Raw) *DropDatabase { |
| 71 | dd.result = rdr |
| 72 | return dd |
| 73 | } |
| 74 | |
| 75 | // Result returns the result of a decoded wire message and server description. |
| 76 | func (dd *DropDatabase) Result() (bson.Raw, error) { |
| 77 | if dd.err != nil { |
| 78 | return nil, dd.err |
| 79 | } |
| 80 | |
| 81 | return dd.result, nil |
| 82 | } |
| 83 | |
| 84 | // Err returns the error set on this command. |
| 85 | func (dd *DropDatabase) Err() error { return dd.err } |
| 86 | |
| 87 | // RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter. |
| 88 | func (dd *DropDatabase) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (bson.Raw, error) { |
| 89 | cmd, err := dd.encode(desc) |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | |
| 94 | rdr, err := cmd.RoundTrip(ctx, desc, rw) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | |
| 99 | return dd.decode(desc, rdr).Result() |
| 100 | } |