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