Commit ff9d54c2 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #69

parents 006f3000 880f45be
......@@ -37,6 +37,8 @@ use Traversable;
class Collection
{
private static $wireVersionForFindAndModifyWriteConcern = 4;
private $collectionName;
private $databaseName;
private $manager;
......@@ -394,9 +396,14 @@ class Collection
*/
public function findOneAndDelete($filter, array $options = [])
{
$operation = new FindOneAndDelete($this->databaseName, $this->collectionName, $filter, $options);
$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);
}
......@@ -417,9 +424,14 @@ class Collection
*/
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));
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);
}
......@@ -440,9 +452,14 @@ class Collection
*/
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));
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);
}
......
......@@ -4,6 +4,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\UnexpectedValueException;
......@@ -20,6 +21,7 @@ use MongoDB\Exception\UnexpectedValueException;
class FindAndModify implements Executable
{
private static $wireVersionForDocumentLevelValidation = 4;
private static $wireVersionForWriteConcern = 4;
private $databaseName;
private $collectionName;
......@@ -58,6 +60,9 @@ class FindAndModify implements Executable
* matches the query. This option is ignored for remove operations. 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 $collectionName Collection name
* @param array $options Command options
......@@ -103,6 +108,10 @@ class FindAndModify implements Executable
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'])) {
throw new InvalidArgumentTypeException('"upsert" option', $options['upsert'], 'boolean');
}
......@@ -181,6 +190,10 @@ class FindAndModify implements Executable
$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);
}
}
......@@ -32,6 +32,9 @@ class FindOneAndDelete implements Executable
* * sort (document): Determines which document the operation modifies if
* 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 $collectionName Collection name
* @param array|object $filter Query by which to filter documents
......
......@@ -45,6 +45,9 @@ class FindOneAndReplace implements Executable
* * upsert (boolean): When true, a new document is created if no document
* 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 $collectionName Collection name
* @param array|object $filter Query by which to filter documents
......
......@@ -45,6 +45,9 @@ class FindOneAndUpdate implements Executable
* * upsert (boolean): When true, a new document is created if no document
* 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 $collectionName Collection name
* @param array|object $filter Query by which to filter documents
......
......@@ -55,6 +55,10 @@ class FindAndModifyTest extends TestCase
$options[][] = ['upsert' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
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