DropIndexes.php 5.93 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 30
use function current;
use function is_array;
use function is_integer;
use function MongoDB\server_supports_feature;
31 32 33 34 35

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

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 58

    /**
     * Constructs a dropIndexes command.
     *
59 60
     * Supported options:
     *
61 62 63
     *  * maxTimeMS (integer): The maximum amount of time to allow the query to
     *    run.
     *
64 65 66 67
     *  * session (MongoDB\Driver\Session): Client session.
     *
     *    Sessions are not supported for server versions < 3.6.
     *
68 69 70
     *  * typeMap (array): Type map for BSON deserialization. This will be used
     *    for the returned command result document.
     *
71 72 73 74 75
     *  * 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.
     *
76 77 78
     * @param string $databaseName   Database name
     * @param string $collectionName Collection name
     * @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 85 86 87 88 89
    {
        $indexName = (string) $indexName;

        if ($indexName === '') {
            throw new InvalidArgumentException('$indexName cannot be empty');
        }

90 91 92 93
        if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
            throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
        }

94
        if (isset($options['session']) && ! $options['session'] instanceof Session) {
95
            throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
96 97
        }

98 99 100 101
        if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
            throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
        }

102
        if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
103
            throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
104 105
        }

106 107 108 109
        if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
            unset($options['writeConcern']);
        }

110 111 112
        $this->databaseName = (string) $databaseName;
        $this->collectionName = (string) $collectionName;
        $this->indexName = $indexName;
113
        $this->options = $options;
114 115 116 117 118 119 120
    }

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

131 132 133 134 135
        $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
        if ($inTransaction && isset($this->options['writeConcern'])) {
            throw UnsupportedException::writeConcernNotSupportedInTransaction();
        }

136
        $cursor = $server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
137

138 139 140 141
        if (isset($this->options['typeMap'])) {
            $cursor->setTypeMap($this->options['typeMap']);
        }

142
        return current($cursor->toArray());
143
    }
144 145 146 147 148 149 150 151 152 153 154 155 156

    /**
     * Create the dropIndexes command.
     *
     * @return Command
     */
    private function createCommand()
    {
        $cmd = [
            'dropIndexes' => $this->collectionName,
            'index' => $this->indexName,
        ];

157 158 159 160
        if (isset($this->options['maxTimeMS'])) {
            $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
        }

161 162 163 164 165 166 167 168 169 170 171 172 173
        return new Command($cmd);
    }

    /**
     * Create options for executing the command.
     *
     * @see http://php.net/manual/en/mongodb-driver-server.executewritecommand.php
     * @return array
     */
    private function createOptions()
    {
        $options = [];

174 175 176 177
        if (isset($this->options['session'])) {
            $options['session'] = $this->options['session'];
        }

178
        if (isset($this->options['writeConcern'])) {
179
            $options['writeConcern'] = $this->options['writeConcern'];
180 181
        }

182
        return $options;
183
    }
184
}