DropDatabase.php 4.49 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2015-2017 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17 18 19 20

namespace MongoDB\Operation;

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 28 29
use function current;
use function is_array;
use function MongoDB\server_supports_feature;
30 31 32 33 34

/**
 * Operation for the dropDatabase command.
 *
 * @api
35 36
 * @see \MongoDB\Client::dropDatabase()
 * @see \MongoDB\Database::drop()
37 38 39 40
 * @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
 */
class DropDatabase implements Executable
{
41
    /** @var integer */
42 43
    private static $wireVersionForWriteConcern = 5;

44
    /** @var string */
45
    private $databaseName;
46 47

    /** @var array */
48
    private $options;
49 50 51 52

    /**
     * Constructs a dropDatabase command.
     *
53 54
     * Supported options:
     *
55 56 57 58
     *  * session (MongoDB\Driver\Session): Client session.
     *
     *    Sessions are not supported for server versions < 3.6.
     *
59 60 61
     *  * typeMap (array): Type map for BSON deserialization. This will be used
     *    for the returned command result document.
     *
62 63 64 65 66
     *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
     *
     *    This is not supported for server versions < 3.4 and will result in an
     *    exception at execution time if used.
     *
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 78 79 80
        if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
            throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
        }

81
        if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
82
            throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
83 84
        }

85 86 87 88
        if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
            unset($options['writeConcern']);
        }

89
        $this->databaseName = (string) $databaseName;
90
        $this->options = $options;
91 92 93 94 95 96 97
    }

    /**
     * Execute the operation.
     *
     * @see Executable::execute()
     * @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 103
     */
    public function execute(Server $server)
    {
104
        if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
105 106 107
            throw UnsupportedException::writeConcernNotSupported();
        }

108 109
        $command = new Command(['dropDatabase' => 1]);
        $cursor = $server->executeWriteCommand($this->databaseName, $command, $this->createOptions());
110

111 112 113 114
        if (isset($this->options['typeMap'])) {
            $cursor->setTypeMap($this->options['typeMap']);
        }

115
        return current($cursor->toArray());
116
    }
117 118

    /**
119
     * Create options for executing the command.
120
     *
121 122
     * @see http://php.net/manual/en/mongodb-driver-server.executewritecommand.php
     * @return array
123
     */
124
    private function createOptions()
125
    {
126
        $options = [];
127

128 129 130 131
        if (isset($this->options['session'])) {
            $options['session'] = $this->options['session'];
        }

132
        if (isset($this->options['writeConcern'])) {
133
            $options['writeConcern'] = $this->options['writeConcern'];
134 135
        }

136
        return $options;
137
    }
138
}