Skip to content

Commit 8dcc778

Browse files
committed
PHPLIB-229: Operations should not accept unsupported read concern options
1 parent 89742f3 commit 8dcc778

File tree

8 files changed

+48
-18
lines changed

8 files changed

+48
-18
lines changed

docs/includes/apiargs-MongoDBCollection-common-option.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type: :php:`MongoDB\\Driver\\ReadConcern <class.mongodb-driver-readconcern>`
3636
description: |
3737
:manual:`Read concern </reference/read-concern>` to use for the operation.
3838
Defaults to the collection's read concern.
39+
40+
This is not supported for server versions prior to 3.2 and will result in an
41+
exception at execution time if used.
3942
interface: phpmethod
4043
operation: ~
4144
optional: true

docs/includes/extracts-error.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ content: |
2929
ref: error-unsupportedexception
3030
content: |
3131
:phpclass:`MongoDB\\Exception\\UnsupportedException` if options are used and
32-
not supported by the selected server (e.g. ``collation``, ``writeConcern``).
32+
not supported by the selected server (e.g. ``collation``, ``readConcern``,
33+
``writeConcern``).
3334
...

src/Exception/UnsupportedException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ public static function collationNotSupported()
1414
return new static('Collations are not supported by the server executing this operation');
1515
}
1616

17+
/**
18+
* Thrown when a command's readConcern option is not supported by a server.
19+
*
20+
* @return self
21+
*/
22+
public static function readConcernNotSupported()
23+
{
24+
return new static('Read concern is not supported by the server executing this command');
25+
}
26+
1727
/**
1828
* Thrown when a command's writeConcern option is not supported by a server.
1929
*

src/Operation/Aggregate.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class Aggregate implements Executable
6464
* * readConcern (MongoDB\Driver\ReadConcern): Read concern. Note that a
6565
* "majority" read concern is not compatible with the $out stage.
6666
*
67-
* For servers < 3.2, this option is ignored as read concern is not
68-
* available.
67+
* This is not supported for server versions < 3.2 and will result in an
68+
* exception at execution time if used.
6969
*
7070
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
7171
*
@@ -182,7 +182,7 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
182182
* @param Server $server
183183
* @return Traversable
184184
* @throws UnexpectedValueException if the command response was malformed
185-
* @throws UnsupportedException if collation or write concern is used and unsupported
185+
* @throws UnsupportedException if collation, read concern, or write concern is used and unsupported
186186
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
187187
*/
188188
public function execute(Server $server)
@@ -191,6 +191,10 @@ public function execute(Server $server)
191191
throw UnsupportedException::collationNotSupported();
192192
}
193193

194+
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
195+
throw UnsupportedException::readConcernNotSupported();
196+
}
197+
194198
if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
195199
throw UnsupportedException::writeConcernNotSupported();
196200
}
@@ -254,7 +258,7 @@ private function createCommand(Server $server, $isCursorSupported)
254258
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
255259
}
256260

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

src/Operation/Count.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class Count implements Executable
4848
*
4949
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
5050
*
51-
* For servers < 3.2, this option is ignored as read concern is not
52-
* available.
51+
* This is not supported for server versions < 3.2 and will result in an
52+
* exception at execution time if used.
5353
*
5454
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
5555
*
@@ -115,7 +115,7 @@ public function __construct($databaseName, $collectionName, $filter = [], array
115115
* @param Server $server
116116
* @return integer
117117
* @throws UnexpectedValueException if the command response was malformed
118-
* @throws UnsupportedException if collation is used and unsupported
118+
* @throws UnsupportedException if collation or read concern is used and unsupported
119119
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
120120
*/
121121
public function execute(Server $server)
@@ -124,6 +124,10 @@ public function execute(Server $server)
124124
throw UnsupportedException::collationNotSupported();
125125
}
126126

127+
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
128+
throw UnsupportedException::readConcernNotSupported();
129+
}
130+
127131
$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
128132

129133
$cursor = $server->executeCommand($this->databaseName, $this->createCommand($server), $readPreference);
@@ -161,7 +165,7 @@ private function createCommand(Server $server)
161165
}
162166
}
163167

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

src/Operation/Distinct.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class Distinct implements Executable
4444
*
4545
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
4646
*
47-
* For servers < 3.2, this option is ignored as read concern is not
48-
* available.
47+
* This is not supported for server versions < 3.2 and will result in an
48+
* exception at execution time if used.
4949
*
5050
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
5151
*
@@ -92,7 +92,7 @@ public function __construct($databaseName, $collectionName, $fieldName, $filter
9292
* @param Server $server
9393
* @return mixed[]
9494
* @throws UnexpectedValueException if the command response was malformed
95-
* @throws UnsupportedException if collation is used and unsupported
95+
* @throws UnsupportedException if collation or read concern is used and unsupported
9696
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
9797
*/
9898
public function execute(Server $server)
@@ -101,6 +101,10 @@ public function execute(Server $server)
101101
throw UnsupportedException::collationNotSupported();
102102
}
103103

104+
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
105+
throw UnsupportedException::readConcernNotSupported();
106+
}
107+
104108
$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
105109

106110
$cursor = $server->executeCommand($this->databaseName, $this->createCommand($server), $readPreference);

src/Operation/Find.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class Find implements Executable
7575
*
7676
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
7777
*
78-
* For servers < 3.2, this option is ignored as read concern is not
79-
* available.
78+
* This is not supported for server versions < 3.2 and will result in an
79+
* exception at execution time if used.
8080
*
8181
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
8282
*
@@ -185,7 +185,7 @@ public function __construct($databaseName, $collectionName, $filter, array $opti
185185
* @see Executable::execute()
186186
* @param Server $server
187187
* @return Cursor
188-
* @throws UnsupportedException if collation is used and unsupported
188+
* @throws UnsupportedException if collation or read concern is used and unsupported
189189
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
190190
*/
191191
public function execute(Server $server)
@@ -194,6 +194,10 @@ public function execute(Server $server)
194194
throw UnsupportedException::collationNotSupported();
195195
}
196196

197+
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
198+
throw UnsupportedException::readConcernNotSupported();
199+
}
200+
197201
$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
198202

199203
$cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, $this->createQuery(), $readPreference);

src/Operation/FindOne.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class FindOne implements Executable
4545
*
4646
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
4747
*
48-
* For servers < 3.2, this option is ignored as read concern is not
49-
* available.
48+
* This is not supported for server versions < 3.2 and will result in an
49+
* exception at execution time if used.
5050
*
5151
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
5252
*
@@ -82,7 +82,7 @@ public function __construct($databaseName, $collectionName, $filter, array $opti
8282
* @see Executable::execute()
8383
* @param Server $server
8484
* @return array|object|null
85-
* @throws UnsupportedException if collation is used and unsupported
85+
* @throws UnsupportedException if collation or read concern is used and unsupported
8686
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
8787
*/
8888
public function execute(Server $server)

0 commit comments

Comments
 (0)