See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]
1 <?php 2 /* 3 * Copyright 2015-present 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 * https://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\CommandException; 22 use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; 23 use MongoDB\Driver\Server; 24 use MongoDB\Driver\Session; 25 use MongoDB\Driver\WriteConcern; 26 use MongoDB\Exception\InvalidArgumentException; 27 use MongoDB\Exception\UnsupportedException; 28 29 use function current; 30 use function is_array; 31 32 /** 33 * Operation for the drop command. 34 * 35 * @api 36 * @see \MongoDB\Collection::drop() 37 * @see \MongoDB\Database::dropCollection() 38 * @see https://mongodb.com/docs/manual/reference/command/drop/ 39 */ 40 class DropCollection implements Executable 41 { 42 /** @var integer */ 43 private static $errorCodeNamespaceNotFound = 26; 44 45 /** @var string */ 46 private $databaseName; 47 48 /** @var string */ 49 private $collectionName; 50 51 /** @var array */ 52 private $options; 53 54 /** 55 * Constructs a drop command. 56 * 57 * Supported options: 58 * 59 * * comment (mixed): BSON value to attach as a comment to this command. 60 * 61 * This is not supported for servers versions < 4.4. 62 * 63 * * session (MongoDB\Driver\Session): Client session. 64 * 65 * * typeMap (array): Type map for BSON deserialization. This will be used 66 * for the returned command result document. 67 * 68 * * writeConcern (MongoDB\Driver\WriteConcern): Write concern. 69 * 70 * @param string $databaseName Database name 71 * @param string $collectionName Collection name 72 * @param array $options Command options 73 * @throws InvalidArgumentException for parameter/option parsing errors 74 */ 75 public function __construct(string $databaseName, string $collectionName, array $options = []) 76 { 77 if (isset($options['session']) && ! $options['session'] instanceof Session) { 78 throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class); 79 } 80 81 if (isset($options['typeMap']) && ! is_array($options['typeMap'])) { 82 throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array'); 83 } 84 85 if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) { 86 throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class); 87 } 88 89 if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) { 90 unset($options['writeConcern']); 91 } 92 93 $this->databaseName = $databaseName; 94 $this->collectionName = $collectionName; 95 $this->options = $options; 96 } 97 98 /** 99 * Execute the operation. 100 * 101 * @see Executable::execute() 102 * @return array|object Command result document 103 * @throws UnsupportedException if write concern is used and unsupported 104 * @throws DriverRuntimeException for other driver errors (e.g. connection errors) 105 */ 106 public function execute(Server $server) 107 { 108 $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction(); 109 if ($inTransaction && isset($this->options['writeConcern'])) { 110 throw UnsupportedException::writeConcernNotSupportedInTransaction(); 111 } 112 113 try { 114 $cursor = $server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions()); 115 } catch (CommandException $e) { 116 /* The server may return an error if the collection does not exist. 117 * Check for an error code and return the command reply instead of 118 * throwing. */ 119 if ($e->getCode() === self::$errorCodeNamespaceNotFound) { 120 return $e->getResultDocument(); 121 } 122 123 throw $e; 124 } 125 126 if (isset($this->options['typeMap'])) { 127 $cursor->setTypeMap($this->options['typeMap']); 128 } 129 130 return current($cursor->toArray()); 131 } 132 133 /** 134 * Create the drop command. 135 */ 136 private function createCommand(): Command 137 { 138 $cmd = ['drop' => $this->collectionName]; 139 140 if (isset($this->options['comment'])) { 141 $cmd['comment'] = $this->options['comment']; 142 } 143 144 return new Command($cmd); 145 } 146 147 /** 148 * Create options for executing the command. 149 * 150 * @see https://php.net/manual/en/mongodb-driver-server.executewritecommand.php 151 */ 152 private function createOptions(): array 153 { 154 $options = []; 155 156 if (isset($this->options['session'])) { 157 $options['session'] = $this->options['session']; 158 } 159 160 if (isset($this->options['writeConcern'])) { 161 $options['writeConcern'] = $this->options['writeConcern']; 162 } 163 164 return $options; 165 } 166 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body