DropDatabase.php 4.34 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 21

namespace MongoDB\Operation;

use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
22
use MongoDB\Driver\Session;
23
use MongoDB\Driver\WriteConcern;
24
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
25
use MongoDB\Exception\InvalidArgumentException;
26
use MongoDB\Exception\UnsupportedException;
27 28 29 30 31

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

40
    private $databaseName;
41
    private $options;
42 43 44 45

    /**
     * Constructs a dropDatabase command.
     *
46 47
     * Supported options:
     *
48 49 50 51
     *  * session (MongoDB\Driver\Session): Client session.
     *
     *    Sessions are not supported for server versions < 3.6.
     *
52 53 54
     *  * typeMap (array): Type map for BSON deserialization. This will be used
     *    for the returned command result document.
     *
55 56 57 58 59
     *  * 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.
     *
60
     * @param string $databaseName Database name
61
     * @param array  $options      Command options
62
     * @throws InvalidArgumentException for parameter/option parsing errors
63
     */
64
    public function __construct($databaseName, array $options = [])
65
    {
66
        if (isset($options['session']) && ! $options['session'] instanceof Session) {
67
            throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
68 69
        }

70 71 72 73
        if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
            throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
        }

74
        if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
75
            throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
76 77
        }

78 79 80 81
        if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
            unset($options['writeConcern']);
        }

82
        $this->databaseName = (string) $databaseName;
83
        $this->options = $options;
84 85 86 87 88 89 90
    }

    /**
     * Execute the operation.
     *
     * @see Executable::execute()
     * @param Server $server
91
     * @return array|object Command result document
92
     * @throws UnsupportedException if writeConcern is used and unsupported
93
     * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
94 95 96
     */
    public function execute(Server $server)
    {
97 98 99 100
        if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
            throw UnsupportedException::writeConcernNotSupported();
        }

101 102
        $command = new Command(['dropDatabase' => 1]);
        $cursor = $server->executeWriteCommand($this->databaseName, $command, $this->createOptions());
103

104 105 106 107
        if (isset($this->options['typeMap'])) {
            $cursor->setTypeMap($this->options['typeMap']);
        }

108
        return current($cursor->toArray());
109
    }
110 111

    /**
112
     * Create options for executing the command.
113
     *
114 115
     * @see http://php.net/manual/en/mongodb-driver-server.executewritecommand.php
     * @return array
116
     */
117
    private function createOptions()
118
    {
119
        $options = [];
120

121 122 123 124
        if (isset($this->options['session'])) {
            $options['session'] = $this->options['session'];
        }

125
        if (isset($this->options['writeConcern'])) {
126
            $options['writeConcern'] = $this->options['writeConcern'];
127 128
        }

129
        return $options;
130
    }
131
}