Commit 142d24ec authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-302: Support session option for operations

parent 8b808043
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-common-option.yaml
ref: typeMap
......
source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-common-option.yaml
ref: session
...
......@@ -68,6 +68,10 @@ source:
post: |
This option will be ignored when using the :ref:`$out <agg-out>` stage.
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: typeMap
......
......@@ -17,6 +17,10 @@ interface: phpmethod
operation: ~
optional: true
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
......@@ -38,6 +38,10 @@ source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: readPreference
---
source:
file: apiargs-common-option.yaml
ref: session
---
arg_name: option
name: skip
type: integer
......
......@@ -75,6 +75,10 @@ interface: phpmethod
operation: ~
optional: true
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
......@@ -2,6 +2,10 @@ source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
......@@ -2,6 +2,10 @@ source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: collation
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
......@@ -2,6 +2,10 @@ source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: collation
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
......@@ -13,4 +13,8 @@ source:
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: readPreference
---
source:
file: apiargs-common-option.yaml
ref: session
...
......@@ -4,6 +4,10 @@ source:
post: |
This will be used for the returned command result document.
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
......@@ -2,6 +2,10 @@ source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: typeMap
......
......@@ -2,6 +2,10 @@ source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: typeMap
......
......@@ -125,6 +125,10 @@ source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: readPreference
---
source:
file: apiargs-common-option.yaml
ref: session
---
arg_name: option
name: max
type: array|object
......
......@@ -34,6 +34,10 @@ source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: readPreference
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: typeMap
......
......@@ -14,6 +14,10 @@ source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: typeMap
......
......@@ -31,6 +31,10 @@ interface: phpmethod
operation: ~
optional: true
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: typeMap
......
......@@ -35,6 +35,10 @@ interface: phpmethod
operation: ~
optional: true
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: typeMap
......
......@@ -6,6 +6,10 @@ source:
file: apiargs-MongoDBCollection-method-bulkWrite-option.yaml
ref: ordered
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
......@@ -2,6 +2,10 @@ source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: bypassDocumentValidation
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-common-option.yaml
ref: session
...
......@@ -70,6 +70,10 @@ interface: phpmethod
operation: ~
optional: true
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-method-find-option.yaml
ref: sort
......
......@@ -10,6 +10,10 @@ source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: collation
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
......@@ -14,6 +14,10 @@ source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: collation
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
......@@ -10,6 +10,10 @@ source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: collation
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
......
......@@ -5,6 +5,10 @@ description: |
:manual:`Read preference </reference/read-preference>` to use for the
operation. Defaults to the database's read preference.
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBDatabase-common-option.yaml
ref: typeMap
......
......@@ -93,6 +93,10 @@ source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-common-option.yaml
ref: session
---
arg_name: option
name: size
type: integer
......
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBDatabase-common-option.yaml
ref: typeMap
......
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBDatabase-common-option.yaml
ref: typeMap
......
......@@ -12,4 +12,8 @@ optional: true
source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-common-option.yaml
ref: session
...
......@@ -36,6 +36,17 @@ replacement:
parent: "client"
---
arg_name: option
name: session
type: :php:`MongoDB\\Driver\\Session <class.mongodb-driver-session>`
description: |
Client session to associate with the operation.
Sessions are not supported for server versions prior to 3.6.
interface: phpmethod
operation: ~
optional: true
---
arg_name: option
name: typeMap
type: array
description: |
......
......@@ -21,6 +21,7 @@ use MongoDB\Driver\Command;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -93,6 +94,10 @@ class Aggregate implements Executable
*
* This option is ignored if the $out stage is specified.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * typeMap (array): Type map for BSON deserialization. This will be
* applied to the returned Cursor (it is not sent to the server).
*
......@@ -175,6 +180,10 @@ class Aggregate implements Executable
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
}
......@@ -330,6 +339,10 @@ class Aggregate implements Executable
$options['readPreference'] = $this->options['readPreference'];
}
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if ($hasOutStage && isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
......
......@@ -20,6 +20,7 @@ namespace MongoDB\Operation;
use MongoDB\BulkWriteResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -106,6 +107,10 @@ class BulkWrite implements Executable
* performing the remaining writes. If false, when a write fails,
* continue with the remaining writes, if any. The default is true.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
......@@ -280,6 +285,10 @@ class BulkWrite implements Executable
throw InvalidArgumentException::invalidType('"ordered" option', $options['ordered'], 'boolean');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
......@@ -343,9 +352,29 @@ class BulkWrite implements Executable
}
}
$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createOptions());
return new BulkWriteResult($writeResult, $insertedIds);
}
/**
* Create options for executing the bulk write.
*
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
return $options;
}
}
......@@ -21,6 +21,7 @@ use MongoDB\Driver\Command;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
......@@ -69,6 +70,10 @@ class Count implements Executable
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * skip (integer): The number of documents to skip before returning the
* documents.
*
......@@ -108,6 +113,10 @@ class Count implements Executable
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['skip']) && ! is_integer($options['skip'])) {
throw InvalidArgumentException::invalidType('"skip" option', $options['skip'], 'integer');
}
......@@ -201,6 +210,10 @@ class Count implements Executable
$options['readPreference'] = $this->options['readPreference'];
}
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
return $options;
}
}
......@@ -19,6 +19,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -74,6 +75,10 @@ class CreateCollection implements Executable
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * size (integer): The maximum number of bytes for a capped collection.
*
* * storageEngine (document): Storage engine options.
......@@ -129,6 +134,10 @@ class CreateCollection implements Executable
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['size']) && ! is_integer($options['size'])) {
throw InvalidArgumentException::invalidType('"size" option', $options['size'], 'integer');
}
......@@ -228,6 +237,10 @@ class CreateCollection implements Executable
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
......
......@@ -20,6 +20,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -53,6 +54,10 @@ class CreateIndexes implements Executable
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* This is not supported for server versions < 3.4 and will result in an
......@@ -98,6 +103,10 @@ class CreateIndexes implements Executable
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
......@@ -145,6 +154,10 @@ class CreateIndexes implements Executable
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
......
......@@ -21,6 +21,7 @@ use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Exception\InvalidArgumentException;
/**
......@@ -46,6 +47,10 @@ class DatabaseCommand implements Executable
* the wire protocol accordingly or adds the read preference to the
* command document, respectively.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * typeMap (array): Type map for BSON deserialization. This will be
* applied to the returned Cursor (it is not sent to the server).
*
......@@ -64,6 +69,10 @@ class DatabaseCommand implements Executable
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
}
......@@ -82,9 +91,7 @@ class DatabaseCommand implements Executable
*/
public function execute(Server $server)
{
$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
$cursor = $server->executeCommand($this->databaseName, $this->command, $readPreference);
$cursor = $server->executeCommand($this->databaseName, $this->command, $this->createOptions());
if (isset($this->options['typeMap'])) {
$cursor->setTypeMap($this->options['typeMap']);
......@@ -92,4 +99,25 @@ class DatabaseCommand implements Executable
return $cursor;
}
/**
* Create options for executing the command.
*
* @see http://php.net/manual/en/mongodb-driver-server.executecommand.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['readPreference'])) {
$options['readPreference'] = $this->options['readPreference'];
}
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
return $options;
}
}
......@@ -20,6 +20,7 @@ namespace MongoDB\Operation;
use MongoDB\DeleteResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -54,6 +55,10 @@ class Delete implements Executable
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
......@@ -79,6 +84,10 @@ class Delete implements Executable
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
......@@ -117,9 +126,29 @@ class Delete implements Executable
$bulk = new Bulk();
$bulk->delete($this->filter, $deleteOptions);
$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createOptions());
return new DeleteResult($writeResult);
}
/**
* Create options for executing the bulk write.
*
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
return $options;
}
}
......@@ -44,6 +44,10 @@ class DeleteMany implements Executable
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
......
......@@ -44,6 +44,10 @@ class DeleteOne implements Executable
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
......
......@@ -21,6 +21,7 @@ use MongoDB\Driver\Command;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
......@@ -64,6 +65,10 @@ class Distinct implements Executable
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param string $fieldName Field for which to return distinct values
......@@ -93,6 +98,10 @@ class Distinct implements Executable
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['readConcern']) && $options['readConcern']->isDefault()) {
unset($options['readConcern']);
}
......@@ -179,6 +188,10 @@ class Distinct implements Executable
$options['readPreference'] = $this->options['readPreference'];
}
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
return $options;
}
}
......@@ -19,6 +19,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -46,6 +47,10 @@ class DropCollection implements Executable
*
* Supported options:
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * typeMap (array): Type map for BSON deserialization. This will be used
* for the returned command result document.
*
......@@ -61,6 +66,10 @@ class DropCollection implements Executable
*/
public function __construct($databaseName, $collectionName, array $options = [])
{
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
}
......@@ -126,6 +135,10 @@ class DropCollection implements Executable
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
......
......@@ -19,6 +19,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -44,6 +45,10 @@ class DropDatabase implements Executable
*
* Supported options:
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * typeMap (array): Type map for BSON deserialization. This will be used
* for the returned command result document.
*
......@@ -58,6 +63,10 @@ class DropDatabase implements Executable
*/
public function __construct($databaseName, array $options = [])
{
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
}
......@@ -109,6 +118,10 @@ class DropDatabase implements Executable
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
......
......@@ -19,6 +19,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -48,6 +49,10 @@ class DropIndexes implements Executable
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * typeMap (array): Type map for BSON deserialization. This will be used
* for the returned command result document.
*
......@@ -74,6 +79,10 @@ class DropIndexes implements Executable
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
}
......@@ -145,6 +154,10 @@ class DropIndexes implements Executable
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
......
......@@ -22,6 +22,7 @@ use MongoDB\Driver\Query;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
......@@ -114,6 +115,10 @@ class Find implements Executable
* * returnKey (boolean): If true, returns only the index keys in the
* resulting documents.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * showRecordId (boolean): Determines whether to return the record
* identifier for each document. If true, adds a field $recordId to the
* returned documents.
......@@ -226,6 +231,10 @@ class Find implements Executable
throw InvalidArgumentException::invalidType('"returnKey" option', $options['returnKey'], 'boolean');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['showRecordId']) && ! is_bool($options['showRecordId'])) {
throw InvalidArgumentException::invalidType('"showRecordId" option', $options['showRecordId'], 'boolean');
}
......@@ -275,9 +284,7 @@ class Find implements Executable
throw UnsupportedException::readConcernNotSupported();
}
$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
$cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, $this->createQuery(), $readPreference);
$cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, $this->createQuery(), $this->createOptions());
if (isset($this->options['typeMap'])) {
$cursor->setTypeMap($this->options['typeMap']);
......@@ -286,6 +293,27 @@ class Find implements Executable
return $cursor;
}
/**
* Create options for executing the command.
*
* @see http://php.net/manual/en/mongodb-driver-server.executequery.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['readPreference'])) {
$options['readPreference'] = $this->options['readPreference'];
}
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
return $options;
}
/**
* Create the find query.
*
......
......@@ -19,6 +19,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -82,6 +83,10 @@ class FindAndModify implements Executable
* * remove (boolean): When true, removes the matched document. This option
* cannot be true if the update option is set. The default is false.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * sort (document): Determines which document the operation modifies if
* the query selects multiple documents.
*
......@@ -144,6 +149,10 @@ class FindAndModify implements Executable
throw InvalidArgumentException::invalidType('"remove" option', $options['remove'], 'boolean');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['sort']) && ! is_array($options['sort']) && ! is_object($options['sort'])) {
throw InvalidArgumentException::invalidType('"sort" option', $options['sort'], 'array or object');
}
......@@ -278,6 +287,10 @@ class FindAndModify implements Executable
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
......
......@@ -78,6 +78,10 @@ class FindOne implements Executable
* * returnKey (boolean): If true, returns only the index keys in the
* resulting documents.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * showRecordId (boolean): Determines whether to return the record
* identifier for each document. If true, adds a field $recordId to the
* returned documents.
......
......@@ -49,6 +49,10 @@ class FindOneAndDelete implements Executable
* * projection (document): Limits the fields to return for the matching
* document.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * sort (document): Determines which document the operation modifies if
* the query selects multiple documents.
*
......
......@@ -64,6 +64,10 @@ class FindOneAndReplace implements Executable
* FindOneAndReplace::RETURN_DOCUMENT_AFTER. The default is
* FindOneAndReplace::RETURN_DOCUMENT_BEFORE.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * sort (document): Determines which document the operation modifies if
* the query selects multiple documents.
*
......
......@@ -67,6 +67,10 @@ class FindOneAndUpdate implements Executable
* FindOneAndUpdate::RETURN_DOCUMENT_AFTER. The default is
* FindOneAndUpdate::RETURN_DOCUMENT_BEFORE.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * sort (document): Determines which document the operation modifies if
* the query selects multiple documents.
*
......
......@@ -20,6 +20,7 @@ namespace MongoDB\Operation;
use MongoDB\InsertManyResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -55,6 +56,10 @@ class InsertMany implements Executable
* performing the remaining writes. If false, when a write fails,
* continue with the remaining writes, if any. The default is true.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
......@@ -93,6 +98,10 @@ class InsertMany implements Executable
throw InvalidArgumentException::invalidType('"ordered" option', $options['ordered'], 'boolean');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
......@@ -130,9 +139,29 @@ class InsertMany implements Executable
$insertedIds[$i] = $bulk->insert($document);
}
$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createOptions());
return new InsertManyResult($writeResult, $insertedIds);
}
/**
* Create options for executing the bulk write.
*
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
return $options;
}
}
......@@ -20,6 +20,7 @@ namespace MongoDB\Operation;
use MongoDB\InsertOneResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -51,6 +52,10 @@ class InsertOne implements Executable
* For servers < 3.2, this option is ignored as document level validation
* is not available.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
......@@ -69,6 +74,10 @@ class InsertOne implements Executable
throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
......@@ -102,9 +111,29 @@ class InsertOne implements Executable
$bulk = new Bulk($options);
$insertedId = $bulk->insert($this->document);
$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createOptions());
return new InsertOneResult($writeResult, $insertedId);
}
/**
* Create options for executing the bulk write.
*
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
return $options;
}
}
......@@ -20,6 +20,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Query;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\CachingIterator;
......@@ -51,6 +52,10 @@ class ListCollections implements Executable
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* @param string $databaseName Database name
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
......@@ -65,6 +70,10 @@ class ListCollections implements Executable
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
$this->databaseName = (string) $databaseName;
$this->options = $options;
}
......@@ -85,6 +94,26 @@ class ListCollections implements Executable
: $this->executeLegacy($server);
}
/**
* Create options for executing the command.
*
* Note: read preference is intentionally omitted, as the spec requires that
* the command be executed on the primary.
*
* @see http://php.net/manual/en/mongodb-driver-server.executecommand.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
return $options;
}
/**
* Returns information for all collections in this database using the
* listCollections command.
......@@ -105,7 +134,7 @@ class ListCollections implements Executable
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
$cursor = $server->executeCommand($this->databaseName, new Command($cmd), $this->createOptions());
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
return new CollectionInfoCommandIterator(new CachingIterator($cursor));
......
......@@ -19,6 +19,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
......@@ -44,6 +45,10 @@ class ListDatabases implements Executable
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
......@@ -53,6 +58,10 @@ class ListDatabases implements Executable
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
$this->options = $options;
}
......@@ -73,7 +82,7 @@ class ListDatabases implements Executable
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}
$cursor = $server->executeCommand('admin', new Command($cmd));
$cursor = $server->executeCommand('admin', new Command($cmd), $this->createOptions());
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
$result = current($cursor->toArray());
......@@ -89,4 +98,24 @@ class ListDatabases implements Executable
*/
return new DatabaseInfoLegacyIterator($result['databases']);
}
/**
* Create options for executing the command.
*
* Note: read preference is intentionally omitted, as the spec requires that
* the command be executed on the primary.
*
* @see http://php.net/manual/en/mongodb-driver-server.executecommand.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
return $options;
}
}
......@@ -20,6 +20,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Query;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\CachingIterator;
......@@ -52,6 +53,10 @@ class ListIndexes implements Executable
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array $options Command options
......@@ -63,6 +68,10 @@ class ListIndexes implements Executable
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->options = $options;
......@@ -83,6 +92,26 @@ class ListIndexes implements Executable
: $this->executeLegacy($server);
}
/**
* Create options for executing the command.
*
* Note: read preference is intentionally omitted, as the spec requires that
* the command be executed on the primary.
*
* @see http://php.net/manual/en/mongodb-driver-server.executecommand.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
return $options;
}
/**
* Returns information for all indexes for this collection using the
* listIndexes command.
......@@ -100,7 +129,7 @@ class ListIndexes implements Executable
}
try {
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
$cursor = $server->executeCommand($this->databaseName, new Command($cmd), $this->createOptions());
} catch (DriverRuntimeException $e) {
/* The server may return an error if the collection does not exist.
* Check for possible error codes (see: SERVER-20463) and return an
......
......@@ -22,6 +22,7 @@ use MongoDB\Driver\Command;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -112,6 +113,10 @@ class MapReduce implements Executable
* * scope (document): Specifies global variables that are accessible in
* the map, reduce and finalize functions.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * sort (document): Sorts the input documents. This option is useful for
* optimization. For example, specify the sort key to be the same as the
* emit key so that there are fewer reduce operations. The sort key must
......@@ -183,6 +188,10 @@ class MapReduce implements Executable
throw InvalidArgumentException::invalidType('"scope" option', $options['scope'], 'array or object');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if (isset($options['sort']) && ! is_array($options['sort']) && ! is_object($options['sort'])) {
throw InvalidArgumentException::invalidType('"sort" option', $options['sort'], 'array or object');
}
......@@ -347,6 +356,10 @@ class MapReduce implements Executable
$options['readPreference'] = $this->options['readPreference'];
}
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if ($hasOutputCollection && isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
......
......@@ -50,6 +50,10 @@ class ReplaceOne implements Executable
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * upsert (boolean): When true, a new document is created if no document
* matches the query. The default is false.
*
......
......@@ -20,6 +20,7 @@ namespace MongoDB\Operation;
use MongoDB\UpdateResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
......@@ -72,6 +73,10 @@ class Update implements Executable
* This option cannot be true if the $update argument is a replacement
* document (i.e. contains no update operators). The default is false.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * upsert (boolean): When true, a new document is created if no document
* matches the query. The default is false.
*
......@@ -120,6 +125,10 @@ class Update implements Executable
throw new InvalidArgumentException('"multi" option cannot be true if $update is a replacement document');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
}
if ( ! is_bool($options['upsert'])) {
throw InvalidArgumentException::invalidType('"upsert" option', $options['upsert'], 'boolean');
}
......@@ -180,9 +189,29 @@ class Update implements Executable
$bulk = new Bulk($bulkOptions);
$bulk->update($this->filter, $this->update, $updateOptions);
$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createOptions());
return new UpdateResult($writeResult);
}
/**
* Create options for executing the bulk write.
*
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
if (isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}
return $options;
}
}
......@@ -56,6 +56,10 @@ class UpdateMany implements Executable
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * upsert (boolean): When true, a new document is created if no document
* matches the query. The default is false.
*
......
......@@ -56,6 +56,10 @@ class UpdateOne implements Executable
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * upsert (boolean): When true, a new document is created if no document
* matches the query. The default is false.
*
......
......@@ -23,6 +23,7 @@ use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
......@@ -65,6 +66,10 @@ class Watch implements Executable
* Insert and replace operations always include the "fullDocument" field
* and delete operations omit the field as the document no longer exists.
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * maxAwaitTimeMS (integer): The maximum amount of time for the server to
* wait on new documents to satisfy a change stream query.
*
......
......@@ -72,6 +72,29 @@ class AggregateFunctionalTest extends FunctionalTestCase
$operation->execute($this->getPrimaryServer());
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new Aggregate(
$this->getDatabaseName(),
$this->getCollectionName(),
[],
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
*/
......
......@@ -64,6 +64,10 @@ class AggregateTest extends TestCase
$options[][] = ['readPreference' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidArrayValues() as $value) {
$options[][] = ['typeMap' => $value];
}
......
......@@ -8,6 +8,8 @@ use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\WriteConcern;
use MongoDB\Model\BSONDocument;
use MongoDB\Operation\BulkWrite;
use MongoDB\Tests\CommandObserver;
use stdClass;
class BulkWriteFunctionalTest extends FunctionalTestCase
{
......@@ -286,6 +288,29 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
]);
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new BulkWrite(
$this->getDatabaseName(),
$this->getCollectionName(),
[['insertOne' => [['_id' => 1]]]],
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
/**
* Create data fixtures.
*
......
......@@ -447,6 +447,10 @@ class BulkWriteTest extends TestCase
$options[][] = ['ordered' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
......
......@@ -69,4 +69,27 @@ class CountFunctionalTest extends FunctionalTestCase
$this->assertEquals(3, $operation->execute($this->getPrimaryServer()));
}
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new Count(
$this->getDatabaseName(),
$this->getCollectionName(),
[],
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
}
......@@ -52,6 +52,10 @@ class CountTest extends TestCase
$options[][] = ['readPreference' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = ['skip' => $value];
}
......
......@@ -25,4 +25,26 @@ class CreateCollectionFunctionalTest extends FunctionalTestCase
}
);
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new CreateCollection(
$this->getDatabaseName(),
$this->getCollectionName(),
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
}
......@@ -47,6 +47,10 @@ class CreateCollectionTest extends TestCase
$options[][] = ['maxTimeMS' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = ['size' => $value];
}
......
......@@ -146,6 +146,29 @@ class CreateIndexesFunctionalTest extends FunctionalTestCase
);
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new CreateIndexes(
$this->getDatabaseName(),
$this->getCollectionName(),
[['key' => ['x' => 1]]],
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
/**
* Asserts that an index with the given name exists for the collection.
*
......
......@@ -32,6 +32,10 @@ class CreateIndexesTest extends TestCase
$options[][] = ['maxTimeMS' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
......
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\DatabaseCommand;
use MongoDB\Tests\CommandObserver;
use stdClass;
class DatabaseCommandFunctionalTest extends FunctionalTestCase
{
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new DatabaseCommand(
$this->getDatabaseName(),
['ping' => 1],
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
}
......@@ -32,6 +32,10 @@ class DatabaseCommandTest extends TestCase
$options[][] = ['readPreference' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidArrayValues() as $value) {
$options[][] = ['typeMap' => $value];
}
......
......@@ -7,6 +7,8 @@ use MongoDB\Collection;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\Delete;
use MongoDB\Tests\CommandObserver;
use stdClass;
class DeleteFunctionalTest extends FunctionalTestCase
{
......@@ -58,6 +60,30 @@ class DeleteFunctionalTest extends FunctionalTestCase
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new Delete(
$this->getDatabaseName(),
$this->getCollectionName(),
[],
0,
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
public function testUnacknowledgedWriteConcern()
{
$filter = ['_id' => 1];
......
......@@ -47,6 +47,10 @@ class DeleteTest extends TestCase
$options[][] = ['collation' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
......
......@@ -27,4 +27,28 @@ class DistinctFunctionalTest extends FunctionalTestCase
}
);
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new Distinct(
$this->getDatabaseName(),
$this->getCollectionName(),
'x',
[],
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
}
......@@ -44,6 +44,10 @@ class DistinctTest extends TestCase
$options[][] = ['readPreference' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
return $options;
}
}
......@@ -53,6 +53,28 @@ class DropCollectionFunctionalTest extends FunctionalTestCase
$operation->execute($this->getPrimaryServer());
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new DropCollection(
$this->getDatabaseName(),
$this->getCollectionName(),
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
/**
* Asserts that a collection with the given name does not exist on the
* server.
......
......@@ -19,6 +19,10 @@ class DropCollectionTest extends TestCase
{
$options = [];
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidArrayValues() as $value) {
$options[][] = ['typeMap' => $value];
}
......
......@@ -58,6 +58,27 @@ class DropDatabaseFunctionalTest extends FunctionalTestCase
$operation->execute($server);
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new DropDatabase(
$this->getDatabaseName(),
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
/**
* Asserts that a database with the given name does not exist on the server.
*
......
......@@ -19,6 +19,10 @@ class DropDatabaseTest extends TestCase
{
$options = [];
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidArrayValues() as $value) {
$options[][] = ['typeMap' => $value];
}
......
......@@ -88,6 +88,32 @@ class DropIndexesFunctionalTest extends FunctionalTestCase
}
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
$operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [['key' => ['x' => 1]]]);
$operation->execute($this->getPrimaryServer());
(new CommandObserver)->observe(
function() {
$operation = new DropIndexes(
$this->getDatabaseName(),
$this->getCollectionName(),
'*',
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
/**
* Asserts that an index with the given name exists for the collection.
*
......
......@@ -31,6 +31,10 @@ class DropIndexesTest extends TestCase
$options[][] = ['maxTimeMS' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidArrayValues() as $value) {
$options[][] = ['typeMap' => $value];
}
......
......@@ -28,6 +28,28 @@ class FindAndModifyFunctionalTest extends FunctionalTestCase
);
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new FindAndModify(
$this->getDatabaseName(),
$this->getCollectionName(),
['remove' => true, 'session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocument
*/
......
......@@ -51,6 +51,10 @@ class FindAndModifyTest extends TestCase
$options[][] = ['remove' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['sort' => $value];
}
......
......@@ -82,6 +82,29 @@ class FindFunctionalTest extends FunctionalTestCase
}
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new Find(
$this->getDatabaseName(),
$this->getCollectionName(),
[],
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
*/
......
......@@ -100,6 +100,10 @@ class FindTest extends TestCase
$options[][] = ['returnKey' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['showRecordId' => $value];
}
......
......@@ -41,4 +41,9 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
return new WriteConcern(-2);
}
protected function createSession()
{
return $this->manager->startSession();
}
}
......@@ -7,6 +7,8 @@ use MongoDB\InsertManyResult;
use MongoDB\Driver\WriteConcern;
use MongoDB\Model\BSONDocument;
use MongoDB\Operation\InsertMany;
use MongoDB\Tests\CommandObserver;
use stdClass;
class InsertManyFunctionalTest extends FunctionalTestCase
{
......@@ -50,6 +52,29 @@ class InsertManyFunctionalTest extends FunctionalTestCase
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new InsertMany(
$this->getDatabaseName(),
$this->getCollectionName(),
[['_id' => 1], ['_id' => 2]],
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
public function testUnacknowledgedWriteConcern()
{
$documents = [['x' => 11]];
......
......@@ -55,6 +55,10 @@ class InsertManyTest extends TestCase
$options[][] = ['ordered' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
......
......@@ -7,6 +7,8 @@ use MongoDB\InsertOneResult;
use MongoDB\Driver\WriteConcern;
use MongoDB\Model\BSONDocument;
use MongoDB\Operation\InsertOne;
use MongoDB\Tests\CommandObserver;
use stdClass;
class InsertOneFunctionalTest extends FunctionalTestCase
{
......@@ -65,6 +67,29 @@ class InsertOneFunctionalTest extends FunctionalTestCase
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new InsertOne(
$this->getDatabaseName(),
$this->getCollectionName(),
['_id' => 1],
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
public function testUnacknowledgedWriteConcern()
{
$document = ['x' => 11];
......
......@@ -32,6 +32,10 @@ class InsertOneTest extends TestCase
$options[][] = ['bypassDocumentValidation' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
......
......@@ -5,6 +5,8 @@ namespace MongoDB\Tests\Operation;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\InsertOne;
use MongoDB\Operation\ListCollections;
use MongoDB\Tests\CommandObserver;
use stdClass;
class ListCollectionsFunctionalTest extends FunctionalTestCase
{
......@@ -44,4 +46,25 @@ class ListCollectionsFunctionalTest extends FunctionalTestCase
$this->assertCount(0, $collections);
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new ListCollections(
$this->getDatabaseName(),
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
}
......@@ -27,6 +27,10 @@ class ListCollectionsTest extends TestCase
$options[][] = ['maxTimeMS' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
return $options;
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\ListDatabases;
use MongoDB\Tests\CommandObserver;
use stdClass;
class ListDatabasesFunctionalTest extends FunctionalTestCase
{
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new ListDatabases(
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
}
......@@ -23,6 +23,10 @@ class ListDatabasesTest extends TestCase
$options[][] = ['maxTimeMS' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
return $options;
}
}
......@@ -5,6 +5,8 @@ namespace MongoDB\Tests\Operation;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\InsertOne;
use MongoDB\Operation\ListIndexes;
use MongoDB\Tests\CommandObserver;
use stdClass;
class ListIndexesFunctionalTest extends FunctionalTestCase
{
......@@ -40,4 +42,26 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
$this->assertCount(0, $indexes);
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new ListIndexes(
$this->getDatabaseName(),
$this->getCollectionName(),
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
}
......@@ -23,6 +23,10 @@ class ListIndexesTest extends TestCase
$options[][] = ['maxTimeMS' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
return $options;
}
}
......@@ -129,6 +129,33 @@ class MapReduceFunctionalTest extends FunctionalTestCase
$this->assertEmpty($result->getTiming());
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
$this->createFixtures(3);
(new CommandObserver)->observe(
function() {
$operation = new MapReduce(
$this->getDatabaseName(),
$this->getCollectionName(),
new Javascript('function() { emit(this.x, this.y); }'),
new Javascript('function(key, values) { return Array.sum(values); }'),
['inline' => 1],
['session' => $this->createSession()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
}
);
}
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
*/
......
......@@ -83,6 +83,10 @@ class MapReduceTest extends TestCase
$options[][] = ['scope' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['sort' => $value];
}
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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