Skip to content

PHPLIB-129: Support writeConcern option for findAndModify #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

class Collection
{
private static $wireVersionForFindAndModifyWriteConcern = 4;

private $collectionName;
private $databaseName;
private $manager;
Expand Down Expand Up @@ -394,9 +396,14 @@ public function findOne($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));

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);
}

Expand All @@ -417,9 +424,14 @@ public function findOneAndDelete($filter, 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));

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);
}

Expand All @@ -440,9 +452,14 @@ public function findOneAndReplace($filter, $replacement, 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));

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);
}

Expand Down
13 changes: 13 additions & 0 deletions src/Operation/FindAndModify.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\UnexpectedValueException;
Expand All @@ -20,6 +21,7 @@
class FindAndModify implements Executable
{
private static $wireVersionForDocumentLevelValidation = 4;
private static $wireVersionForWriteConcern = 4;

private $databaseName;
private $collectionName;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -103,6 +108,10 @@ public function __construct($databaseName, $collectionName, array $options)
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');
}
Expand Down Expand Up @@ -181,6 +190,10 @@ private function createCommand(Server $server)
$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);
}
}
3 changes: 3 additions & 0 deletions src/Operation/FindOneAndDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Operation/FindOneAndReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Operation/FindOneAndUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/Operation/FindAndModifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public function provideInvalidConstructorOptions()
$options[][] = ['upsert' => $value];
}

foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}

return $options;
}

Expand Down