Commit 880f45be authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-129: Support writeConcern option for findAndModify

parent 006f3000
...@@ -37,6 +37,8 @@ use Traversable; ...@@ -37,6 +37,8 @@ use Traversable;
class Collection class Collection
{ {
private static $wireVersionForFindAndModifyWriteConcern = 4;
private $collectionName; private $collectionName;
private $databaseName; private $databaseName;
private $manager; private $manager;
...@@ -394,9 +396,14 @@ class Collection ...@@ -394,9 +396,14 @@ class Collection
*/ */
public function findOneAndDelete($filter, array $options = []) public function findOneAndDelete($filter, array $options = [])
{ {
$operation = new FindOneAndDelete($this->databaseName, $this->collectionName, $filter, $options);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern)) {
$options['writeConcern'] = $this->writeConcern;
}
$operation = new FindOneAndDelete($this->databaseName, $this->collectionName, $filter, $options);
return $operation->execute($server); return $operation->execute($server);
} }
...@@ -417,9 +424,14 @@ class Collection ...@@ -417,9 +424,14 @@ class Collection
*/ */
public function findOneAndReplace($filter, $replacement, array $options = []) public function findOneAndReplace($filter, $replacement, array $options = [])
{ {
$operation = new FindOneAndReplace($this->databaseName, $this->collectionName, $filter, $replacement, $options);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern)) {
$options['writeConcern'] = $this->writeConcern;
}
$operation = new FindOneAndReplace($this->databaseName, $this->collectionName, $filter, $replacement, $options);
return $operation->execute($server); return $operation->execute($server);
} }
...@@ -440,9 +452,14 @@ class Collection ...@@ -440,9 +452,14 @@ class Collection
*/ */
public function findOneAndUpdate($filter, $update, array $options = []) public function findOneAndUpdate($filter, $update, array $options = [])
{ {
$operation = new FindOneAndUpdate($this->databaseName, $this->collectionName, $filter, $update, $options);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern)) {
$options['writeConcern'] = $this->writeConcern;
}
$operation = new FindOneAndUpdate($this->databaseName, $this->collectionName, $filter, $update, $options);
return $operation->execute($server); return $operation->execute($server);
} }
......
...@@ -4,6 +4,7 @@ namespace MongoDB\Operation; ...@@ -4,6 +4,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command; use MongoDB\Driver\Command;
use MongoDB\Driver\Server; use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException; use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\UnexpectedValueException; use MongoDB\Exception\UnexpectedValueException;
...@@ -20,6 +21,7 @@ use MongoDB\Exception\UnexpectedValueException; ...@@ -20,6 +21,7 @@ use MongoDB\Exception\UnexpectedValueException;
class FindAndModify implements Executable class FindAndModify implements Executable
{ {
private static $wireVersionForDocumentLevelValidation = 4; private static $wireVersionForDocumentLevelValidation = 4;
private static $wireVersionForWriteConcern = 4;
private $databaseName; private $databaseName;
private $collectionName; private $collectionName;
...@@ -58,6 +60,9 @@ class FindAndModify implements Executable ...@@ -58,6 +60,9 @@ class FindAndModify implements Executable
* matches the query. This option is ignored for remove operations. The * matches the query. This option is ignored for remove operations. The
* default is false. * default is false.
* *
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This option
* is only supported for server versions >= 3.2.
*
* @param string $databaseName Database name * @param string $databaseName Database name
* @param string $collectionName Collection name * @param string $collectionName Collection name
* @param array $options Command options * @param array $options Command options
...@@ -103,6 +108,10 @@ class FindAndModify implements Executable ...@@ -103,6 +108,10 @@ class FindAndModify implements Executable
throw new InvalidArgumentTypeException('"update" option', $options['update'], 'array or object'); throw new InvalidArgumentTypeException('"update" option', $options['update'], 'array or object');
} }
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if ( ! is_bool($options['upsert'])) { if ( ! is_bool($options['upsert'])) {
throw new InvalidArgumentTypeException('"upsert" option', $options['upsert'], 'boolean'); throw new InvalidArgumentTypeException('"upsert" option', $options['upsert'], 'boolean');
} }
...@@ -181,6 +190,10 @@ class FindAndModify implements Executable ...@@ -181,6 +190,10 @@ class FindAndModify implements Executable
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation']; $cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
} }
if (isset($this->options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
$cmd['writeConcern'] = $this->options['writeConcern'];
}
return new Command($cmd); return new Command($cmd);
} }
} }
...@@ -32,6 +32,9 @@ class FindOneAndDelete implements Executable ...@@ -32,6 +32,9 @@ class FindOneAndDelete implements Executable
* * sort (document): Determines which document the operation modifies if * * sort (document): Determines which document the operation modifies if
* the query selects multiple documents. * the query selects multiple documents.
* *
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This option
* is only supported for server versions >= 3.2.
*
* @param string $databaseName Database name * @param string $databaseName Database name
* @param string $collectionName Collection name * @param string $collectionName Collection name
* @param array|object $filter Query by which to filter documents * @param array|object $filter Query by which to filter documents
......
...@@ -45,6 +45,9 @@ class FindOneAndReplace implements Executable ...@@ -45,6 +45,9 @@ class FindOneAndReplace implements Executable
* * upsert (boolean): When true, a new document is created if no document * * upsert (boolean): When true, a new document is created if no document
* matches the query. The default is false. * matches the query. The default is false.
* *
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This option
* is only supported for server versions >= 3.2.
*
* @param string $databaseName Database name * @param string $databaseName Database name
* @param string $collectionName Collection name * @param string $collectionName Collection name
* @param array|object $filter Query by which to filter documents * @param array|object $filter Query by which to filter documents
......
...@@ -45,6 +45,9 @@ class FindOneAndUpdate implements Executable ...@@ -45,6 +45,9 @@ class FindOneAndUpdate implements Executable
* * upsert (boolean): When true, a new document is created if no document * * upsert (boolean): When true, a new document is created if no document
* matches the query. The default is false. * matches the query. The default is false.
* *
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This option
* is only supported for server versions >= 3.2.
*
* @param string $databaseName Database name * @param string $databaseName Database name
* @param string $collectionName Collection name * @param string $collectionName Collection name
* @param array|object $filter Query by which to filter documents * @param array|object $filter Query by which to filter documents
......
...@@ -55,6 +55,10 @@ class FindAndModifyTest extends TestCase ...@@ -55,6 +55,10 @@ class FindAndModifyTest extends TestCase
$options[][] = ['upsert' => $value]; $options[][] = ['upsert' => $value];
} }
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
return $options; return $options;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment