...

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

View as plain text