Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401]

   1  <?php
   2  /*
   3   * Copyright 2015-2017 MongoDB, Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *   http://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  namespace MongoDB\Operation;
  19  
  20  use MongoDB\Driver\Command;
  21  use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
  22  use MongoDB\Driver\Server;
  23  use MongoDB\Driver\Session;
  24  use MongoDB\Driver\WriteConcern;
  25  use MongoDB\Exception\InvalidArgumentException;
  26  use MongoDB\Exception\UnsupportedException;
  27  use function current;
  28  use function is_array;
  29  use function is_integer;
  30  use function MongoDB\server_supports_feature;
  31  
  32  /**
  33   * Operation for the dropIndexes command.
  34   *
  35   * @api
  36   * @see \MongoDB\Collection::dropIndexes()
  37   * @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
  38   */
  39  class DropIndexes implements Executable
  40  {
  41      /** @var integer */
  42      private static $wireVersionForWriteConcern = 5;
  43  
  44      /** @var string */
  45      private $databaseName;
  46  
  47      /** @var string */
  48      private $collectionName;
  49  
  50      /** @var string */
  51      private $indexName;
  52  
  53      /** @var array */
  54      private $options;
  55  
  56      /**
  57       * Constructs a dropIndexes command.
  58       *
  59       * Supported options:
  60       *
  61       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  62       *    run.
  63       *
  64       *  * session (MongoDB\Driver\Session): Client session.
  65       *
  66       *    Sessions are not supported for server versions < 3.6.
  67       *
  68       *  * typeMap (array): Type map for BSON deserialization. This will be used
  69       *    for the returned command result document.
  70       *
  71       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  72       *
  73       *    This is not supported for server versions < 3.4 and will result in an
  74       *    exception at execution time if used.
  75       *
  76       * @param string $databaseName   Database name
  77       * @param string $collectionName Collection name
  78       * @param string $indexName      Index name (use "*" to drop all indexes)
  79       * @param array  $options        Command options
  80       * @throws InvalidArgumentException for parameter/option parsing errors
  81       */
  82      public function __construct($databaseName, $collectionName, $indexName, array $options = [])
  83      {
  84          $indexName = (string) $indexName;
  85  
  86          if ($indexName === '') {
  87              throw new InvalidArgumentException('$indexName cannot be empty');
  88          }
  89  
  90          if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
  91              throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
  92          }
  93  
  94          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  95              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  96          }
  97  
  98          if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
  99              throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
 100          }
 101  
 102          if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
 103              throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
 104          }
 105  
 106          if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
 107              unset($options['writeConcern']);
 108          }
 109  
 110          $this->databaseName = (string) $databaseName;
 111          $this->collectionName = (string) $collectionName;
 112          $this->indexName = $indexName;
 113          $this->options = $options;
 114      }
 115  
 116      /**
 117       * Execute the operation.
 118       *
 119       * @see Executable::execute()
 120       * @param Server $server
 121       * @return array|object Command result document
 122       * @throws UnsupportedException if writeConcern is used and unsupported
 123       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 124       */
 125      public function execute(Server $server)
 126      {
 127          if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
 128              throw UnsupportedException::writeConcernNotSupported();
 129          }
 130  
 131          $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
 132          if ($inTransaction && isset($this->options['writeConcern'])) {
 133              throw UnsupportedException::writeConcernNotSupportedInTransaction();
 134          }
 135  
 136          $cursor = $server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
 137  
 138          if (isset($this->options['typeMap'])) {
 139              $cursor->setTypeMap($this->options['typeMap']);
 140          }
 141  
 142          return current($cursor->toArray());
 143      }
 144  
 145      /**
 146       * Create the dropIndexes command.
 147       *
 148       * @return Command
 149       */
 150      private function createCommand()
 151      {
 152          $cmd = [
 153              'dropIndexes' => $this->collectionName,
 154              'index' => $this->indexName,
 155          ];
 156  
 157          if (isset($this->options['maxTimeMS'])) {
 158              $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
 159          }
 160  
 161          return new Command($cmd);
 162      }
 163  
 164      /**
 165       * Create options for executing the command.
 166       *
 167       * @see http://php.net/manual/en/mongodb-driver-server.executewritecommand.php
 168       * @return array
 169       */
 170      private function createOptions()
 171      {
 172          $options = [];
 173  
 174          if (isset($this->options['session'])) {
 175              $options['session'] = $this->options['session'];
 176          }
 177  
 178          if (isset($this->options['writeConcern'])) {
 179              $options['writeConcern'] = $this->options['writeConcern'];
 180          }
 181  
 182          return $options;
 183      }
 184  }