Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 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 MongoDB\server_supports_feature;
  30  
  31  /**
  32   * Operation for the dropDatabase command.
  33   *
  34   * @api
  35   * @see \MongoDB\Client::dropDatabase()
  36   * @see \MongoDB\Database::drop()
  37   * @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
  38   */
  39  class DropDatabase implements Executable
  40  {
  41      /** @var integer */
  42      private static $wireVersionForWriteConcern = 5;
  43  
  44      /** @var string */
  45      private $databaseName;
  46  
  47      /** @var array */
  48      private $options;
  49  
  50      /**
  51       * Constructs a dropDatabase command.
  52       *
  53       * Supported options:
  54       *
  55       *  * session (MongoDB\Driver\Session): Client session.
  56       *
  57       *    Sessions are not supported for server versions < 3.6.
  58       *
  59       *  * typeMap (array): Type map for BSON deserialization. This will be used
  60       *    for the returned command result document.
  61       *
  62       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  63       *
  64       *    This is not supported for server versions < 3.4 and will result in an
  65       *    exception at execution time if used.
  66       *
  67       * @param string $databaseName Database name
  68       * @param array  $options      Command options
  69       * @throws InvalidArgumentException for parameter/option parsing errors
  70       */
  71      public function __construct($databaseName, array $options = [])
  72      {
  73          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  74              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  75          }
  76  
  77          if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
  78              throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
  79          }
  80  
  81          if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
  82              throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
  83          }
  84  
  85          if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
  86              unset($options['writeConcern']);
  87          }
  88  
  89          $this->databaseName = (string) $databaseName;
  90          $this->options = $options;
  91      }
  92  
  93      /**
  94       * Execute the operation.
  95       *
  96       * @see Executable::execute()
  97       * @param Server $server
  98       * @return array|object Command result document
  99       * @throws UnsupportedException if writeConcern is used and unsupported
 100       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 101       */
 102      public function execute(Server $server)
 103      {
 104          if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
 105              throw UnsupportedException::writeConcernNotSupported();
 106          }
 107  
 108          $command = new Command(['dropDatabase' => 1]);
 109          $cursor = $server->executeWriteCommand($this->databaseName, $command, $this->createOptions());
 110  
 111          if (isset($this->options['typeMap'])) {
 112              $cursor->setTypeMap($this->options['typeMap']);
 113          }
 114  
 115          return current($cursor->toArray());
 116      }
 117  
 118      /**
 119       * Create options for executing the command.
 120       *
 121       * @see http://php.net/manual/en/mongodb-driver-server.executewritecommand.php
 122       * @return array
 123       */
 124      private function createOptions()
 125      {
 126          $options = [];
 127  
 128          if (isset($this->options['session'])) {
 129              $options['session'] = $this->options['session'];
 130          }
 131  
 132          if (isset($this->options['writeConcern'])) {
 133              $options['writeConcern'] = $this->options['writeConcern'];
 134          }
 135  
 136          return $options;
 137      }
 138  }