Differences Between: [Versions 400 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\BulkWrite as Bulk; 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 MongoDB\InsertOneResult; 28 use function is_array; 29 use function is_bool; 30 use function is_object; 31 use function MongoDB\server_supports_feature; 32 33 /** 34 * Operation for inserting a single document with the insert command. 35 * 36 * @api 37 * @see \MongoDB\Collection::insertOne() 38 * @see http://docs.mongodb.org/manual/reference/command/insert/ 39 */ 40 class InsertOne implements Executable 41 { 42 /** @var integer */ 43 private static $wireVersionForDocumentLevelValidation = 4; 44 45 /** @var string */ 46 private $databaseName; 47 48 /** @var string */ 49 private $collectionName; 50 51 /** @var array|object */ 52 private $document; 53 54 /** @var array */ 55 private $options; 56 57 /** 58 * Constructs an insert command. 59 * 60 * Supported options: 61 * 62 * * bypassDocumentValidation (boolean): If true, allows the write to 63 * circumvent document level validation. 64 * 65 * For servers < 3.2, this option is ignored as document level validation 66 * is not available. 67 * 68 * * session (MongoDB\Driver\Session): Client session. 69 * 70 * Sessions are not supported for server versions < 3.6. 71 * 72 * * writeConcern (MongoDB\Driver\WriteConcern): Write concern. 73 * 74 * @param string $databaseName Database name 75 * @param string $collectionName Collection name 76 * @param array|object $document Document to insert 77 * @param array $options Command options 78 * @throws InvalidArgumentException for parameter/option parsing errors 79 */ 80 public function __construct($databaseName, $collectionName, $document, array $options = []) 81 { 82 if (! is_array($document) && ! is_object($document)) { 83 throw InvalidArgumentException::invalidType('$document', $document, 'array or object'); 84 } 85 86 if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) { 87 throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean'); 88 } 89 90 if (isset($options['session']) && ! $options['session'] instanceof Session) { 91 throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class); 92 } 93 94 if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) { 95 throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class); 96 } 97 98 if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) { 99 unset($options['writeConcern']); 100 } 101 102 $this->databaseName = (string) $databaseName; 103 $this->collectionName = (string) $collectionName; 104 $this->document = $document; 105 $this->options = $options; 106 } 107 108 /** 109 * Execute the operation. 110 * 111 * @see Executable::execute() 112 * @param Server $server 113 * @return InsertOneResult 114 * @throws DriverRuntimeException for other driver errors (e.g. connection errors) 115 */ 116 public function execute(Server $server) 117 { 118 $options = []; 119 120 $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction(); 121 if (isset($this->options['writeConcern']) && $inTransaction) { 122 throw UnsupportedException::writeConcernNotSupportedInTransaction(); 123 } 124 125 if (! empty($this->options['bypassDocumentValidation']) && 126 server_supports_feature($server, self::$wireVersionForDocumentLevelValidation) 127 ) { 128 $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation']; 129 } 130 131 $bulk = new Bulk($options); 132 $insertedId = $bulk->insert($this->document); 133 134 $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createOptions()); 135 136 return new InsertOneResult($writeResult, $insertedId); 137 } 138 139 /** 140 * Create options for executing the bulk write. 141 * 142 * @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php 143 * @return array 144 */ 145 private function createOptions() 146 { 147 $options = []; 148 149 if (isset($this->options['session'])) { 150 $options['session'] = $this->options['session']; 151 } 152 153 if (isset($this->options['writeConcern'])) { 154 $options['writeConcern'] = $this->options['writeConcern']; 155 } 156 157 return $options; 158 } 159 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body