|
| 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\BulkWriteCommand; |
| 21 | +use MongoDB\Driver\BulkWriteCommandResult; |
| 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 array_filter; |
| 30 | +use function count; |
| 31 | + |
| 32 | +/** |
| 33 | + * Operation for executing multiple write operations via the bulkWrite command. |
| 34 | + * |
| 35 | + * @see \MongoDB\Client::bulkWrite() |
| 36 | + */ |
| 37 | +final class ClientBulkWrite |
| 38 | +{ |
| 39 | + /** |
| 40 | + * Constructs a client-level bulk write operation. |
| 41 | + * |
| 42 | + * Most options for the bulkWrite command and its write operations are |
| 43 | + * specified when building the extension's BulkWriteCommand object. This |
| 44 | + * operation only accepts options for Server::executeBulkWriteCommand(). |
| 45 | + * |
| 46 | + * Supported options: |
| 47 | + * |
| 48 | + * * session (MongoDB\Driver\Session): Client session. |
| 49 | + * |
| 50 | + * * writeConcern (MongoDB\Driver\WriteConcern): Write concern. |
| 51 | + * |
| 52 | + * @param BulkWriteCommand $bulkWriteCommand Assembled bulk write command |
| 53 | + * @param array $options Command options |
| 54 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 55 | + */ |
| 56 | + public function __construct(private BulkWriteCommand $bulkWriteCommand, private array $options = []) |
| 57 | + { |
| 58 | + if (count($bulkWriteCommand) === 0) { |
| 59 | + throw new InvalidArgumentException('$bulkWriteCommand is empty'); |
| 60 | + } |
| 61 | + |
| 62 | + if (isset($options['session']) && ! $options['session'] instanceof Session) { |
| 63 | + throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class); |
| 64 | + } |
| 65 | + |
| 66 | + if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) { |
| 67 | + throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class); |
| 68 | + } |
| 69 | + |
| 70 | + if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) { |
| 71 | + unset($options['writeConcern']); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Execute the operation. |
| 77 | + * |
| 78 | + * @throws UnsupportedException if write concern is used and unsupported |
| 79 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 80 | + */ |
| 81 | + public function execute(Server $server): ?BulkWriteCommandResult |
| 82 | + { |
| 83 | + $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction(); |
| 84 | + if ($inTransaction && isset($this->options['writeConcern'])) { |
| 85 | + throw UnsupportedException::writeConcernNotSupportedInTransaction(); |
| 86 | + } |
| 87 | + |
| 88 | + $options = array_filter($this->options, fn ($value) => isset($value)); |
| 89 | + |
| 90 | + return $server->executeBulkWriteCommand($this->bulkWriteCommand, $options); |
| 91 | + } |
| 92 | +} |
0 commit comments