Skip to content

PHPLIB-229: Operations should not accept unsupported read concern options #295

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 1, 2016
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
3 changes: 3 additions & 0 deletions docs/includes/apiargs-MongoDBCollection-common-option.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type: :php:`MongoDB\\Driver\\ReadConcern <class.mongodb-driver-readconcern>`
description: |
:manual:`Read concern </reference/read-concern>` to use for the operation.
Defaults to the collection's read concern.

This is not supported for server versions prior to 3.2 and will result in an
exception at execution time if used.
interface: phpmethod
operation: ~
optional: true
Expand Down
3 changes: 2 additions & 1 deletion docs/includes/extracts-error.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ content: |
ref: error-unsupportedexception
content: |
:phpclass:`MongoDB\\Exception\\UnsupportedException` if options are used and
not supported by the selected server (e.g. ``collation``, ``writeConcern``).
not supported by the selected server (e.g. ``collation``, ``readConcern``,
``writeConcern``).
...
10 changes: 10 additions & 0 deletions src/Exception/UnsupportedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ public static function collationNotSupported()
return new static('Collations are not supported by the server executing this operation');
}

/**
* Thrown when a command's readConcern option is not supported by a server.
*
* @return self
*/
public static function readConcernNotSupported()
{
return new static('Read concern is not supported by the server executing this command');
}

/**
* Thrown when a command's writeConcern option is not supported by a server.
*
Expand Down
12 changes: 8 additions & 4 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class Aggregate implements Executable
* * readConcern (MongoDB\Driver\ReadConcern): Read concern. Note that a
* "majority" read concern is not compatible with the $out stage.
*
* For servers < 3.2, this option is ignored as read concern is not
* available.
* This is not supported for server versions < 3.2 and will result in an
* exception at execution time if used.
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
Expand Down Expand Up @@ -182,7 +182,7 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
* @param Server $server
* @return Traversable
* @throws UnexpectedValueException if the command response was malformed
* @throws UnsupportedException if collation or write concern is used and unsupported
* @throws UnsupportedException if collation, read concern, or write concern is used and unsupported
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function execute(Server $server)
Expand All @@ -191,6 +191,10 @@ public function execute(Server $server)
throw UnsupportedException::collationNotSupported();
}

if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
throw UnsupportedException::readConcernNotSupported();
}

if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
throw UnsupportedException::writeConcernNotSupported();
}
Expand Down Expand Up @@ -254,7 +258,7 @@ private function createCommand(Server $server, $isCursorSupported)
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}

if (isset($this->options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
if (isset($this->options['readConcern'])) {
$cmd['readConcern'] = \MongoDB\read_concern_as_document($this->options['readConcern']);
}

Expand Down
12 changes: 8 additions & 4 deletions src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class Count implements Executable
*
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
*
* For servers < 3.2, this option is ignored as read concern is not
* available.
* This is not supported for server versions < 3.2 and will result in an
* exception at execution time if used.
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
Expand Down Expand Up @@ -115,7 +115,7 @@ public function __construct($databaseName, $collectionName, $filter = [], array
* @param Server $server
* @return integer
* @throws UnexpectedValueException if the command response was malformed
* @throws UnsupportedException if collation is used and unsupported
* @throws UnsupportedException if collation or read concern is used and unsupported
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function execute(Server $server)
Expand All @@ -124,6 +124,10 @@ public function execute(Server $server)
throw UnsupportedException::collationNotSupported();
}

if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
throw UnsupportedException::readConcernNotSupported();
}

$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;

$cursor = $server->executeCommand($this->databaseName, $this->createCommand($server), $readPreference);
Expand Down Expand Up @@ -161,7 +165,7 @@ private function createCommand(Server $server)
}
}

if (isset($this->options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
if (isset($this->options['readConcern'])) {
$cmd['readConcern'] = \MongoDB\read_concern_as_document($this->options['readConcern']);
}

Expand Down
10 changes: 7 additions & 3 deletions src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class Distinct implements Executable
*
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
*
* For servers < 3.2, this option is ignored as read concern is not
* available.
* This is not supported for server versions < 3.2 and will result in an
* exception at execution time if used.
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
Expand Down Expand Up @@ -92,7 +92,7 @@ public function __construct($databaseName, $collectionName, $fieldName, $filter
* @param Server $server
* @return mixed[]
* @throws UnexpectedValueException if the command response was malformed
* @throws UnsupportedException if collation is used and unsupported
* @throws UnsupportedException if collation or read concern is used and unsupported
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function execute(Server $server)
Expand All @@ -101,6 +101,10 @@ public function execute(Server $server)
throw UnsupportedException::collationNotSupported();
}

if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
throw UnsupportedException::readConcernNotSupported();
}

$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;

$cursor = $server->executeCommand($this->databaseName, $this->createCommand($server), $readPreference);
Expand Down
10 changes: 7 additions & 3 deletions src/Operation/Find.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class Find implements Executable
*
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
*
* For servers < 3.2, this option is ignored as read concern is not
* available.
* This is not supported for server versions < 3.2 and will result in an
* exception at execution time if used.
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
Expand Down Expand Up @@ -185,7 +185,7 @@ public function __construct($databaseName, $collectionName, $filter, array $opti
* @see Executable::execute()
* @param Server $server
* @return Cursor
* @throws UnsupportedException if collation is used and unsupported
* @throws UnsupportedException if collation or read concern is used and unsupported
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function execute(Server $server)
Expand All @@ -194,6 +194,10 @@ public function execute(Server $server)
throw UnsupportedException::collationNotSupported();
}

if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
throw UnsupportedException::readConcernNotSupported();
}

$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;

$cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, $this->createQuery(), $readPreference);
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/FindOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class FindOne implements Executable
*
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
*
* For servers < 3.2, this option is ignored as read concern is not
* available.
* This is not supported for server versions < 3.2 and will result in an
* exception at execution time if used.
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
Expand Down Expand Up @@ -82,7 +82,7 @@ public function __construct($databaseName, $collectionName, $filter, array $opti
* @see Executable::execute()
* @param Server $server
* @return array|object|null
* @throws UnsupportedException if collation is used and unsupported
* @throws UnsupportedException if collation or read concern is used and unsupported
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function execute(Server $server)
Expand Down