Commit 1eebfe0d authored by Katherine Walker's avatar Katherine Walker

Add getCommandDocument for DeleteOne, DeleteMany, UpdateOne, and UpdateMany

parent 0f91e9f3
...@@ -162,7 +162,7 @@ class Count implements Executable, Explainable ...@@ -162,7 +162,7 @@ class Count implements Executable, Explainable
return (integer) $result->n; return (integer) $result->n;
} }
public function getCommandDocument() public function getCommandDocument(Server $server)
{ {
return $this->createCommandDocument(); return $this->createCommandDocument();
} }
......
...@@ -125,7 +125,7 @@ class Delete implements Executable, Explainable ...@@ -125,7 +125,7 @@ class Delete implements Executable, Explainable
return new DeleteResult($writeResult); return new DeleteResult($writeResult);
} }
public function getCommandDocument() public function getCommandDocument(Server $server)
{ {
$cmd = ['delete' => $this->collectionName, 'deletes' => [['q' => $this->filter] + $this->createDeleteOptions()]]; $cmd = ['delete' => $this->collectionName, 'deletes' => [['q' => $this->filter] + $this->createDeleteOptions()]];
......
...@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException; ...@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::deleteOne() * @see \MongoDB\Collection::deleteOne()
* @see http://docs.mongodb.org/manual/reference/command/delete/ * @see http://docs.mongodb.org/manual/reference/command/delete/
*/ */
class DeleteMany implements Executable class DeleteMany implements Executable, Explainable
{ {
private $delete; private $delete;
...@@ -74,4 +74,9 @@ class DeleteMany implements Executable ...@@ -74,4 +74,9 @@ class DeleteMany implements Executable
{ {
return $this->delete->execute($server); return $this->delete->execute($server);
} }
public function getCommandDocument(Server $server)
{
return $this->delete->getCommandDocument($server);
}
} }
...@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException; ...@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::deleteOne() * @see \MongoDB\Collection::deleteOne()
* @see http://docs.mongodb.org/manual/reference/command/delete/ * @see http://docs.mongodb.org/manual/reference/command/delete/
*/ */
class DeleteOne implements Executable class DeleteOne implements Executable, Explainable
{ {
private $delete; private $delete;
...@@ -74,4 +74,9 @@ class DeleteOne implements Executable ...@@ -74,4 +74,9 @@ class DeleteOne implements Executable
{ {
return $this->delete->execute($server); return $this->delete->execute($server);
} }
public function getCommandDocument(Server $server)
{
return $this->delete->getCommandDocument($server);
}
} }
...@@ -143,7 +143,7 @@ class Distinct implements Executable, Explainable ...@@ -143,7 +143,7 @@ class Distinct implements Executable, Explainable
return $result->values; return $result->values;
} }
public function getCommandDocument() public function getCommandDocument(Server $server)
{ {
return $this->createCommandDocument(); return $this->createCommandDocument();
} }
......
...@@ -88,7 +88,7 @@ class Explain implements Executable ...@@ -88,7 +88,7 @@ class Explain implements Executable
throw UnsupportedException::explainNotSupported(); throw UnsupportedException::explainNotSupported();
} }
$cmd = ['explain' => $this->explainable->getCommandDocument()]; $cmd = ['explain' => $this->explainable->getCommandDocument($server)];
if (isset($this->options['verbosity'])) { if (isset($this->options['verbosity'])) {
$cmd['verbosity'] = $this->options['verbosity']; $cmd['verbosity'] = $this->options['verbosity'];
......
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
namespace MongoDB\Operation; namespace MongoDB\Operation;
use MongoDB\Driver\Server;
/** /**
* Explainable interface for explainable operations (count, distinct, find, * Explainable interface for explainable operations (count, distinct, find,
* findAndModify, delete, and update). * findAndModify, delete, and update).
...@@ -25,5 +27,5 @@ namespace MongoDB\Operation; ...@@ -25,5 +27,5 @@ namespace MongoDB\Operation;
*/ */
interface Explainable extends Executable interface Explainable extends Executable
{ {
function getCommandDocument(); function getCommandDocument(Server $server);
} }
...@@ -293,7 +293,7 @@ class Find implements Executable, Explainable ...@@ -293,7 +293,7 @@ class Find implements Executable, Explainable
return $cursor; return $cursor;
} }
public function getCommandDocument() public function getCommandDocument(Server $server)
{ {
return $this->createCommandDocument(); return $this->createCommandDocument();
} }
......
...@@ -210,7 +210,7 @@ class FindAndModify implements Executable, Explainable ...@@ -210,7 +210,7 @@ class FindAndModify implements Executable, Explainable
throw UnsupportedException::writeConcernNotSupported(); throw UnsupportedException::writeConcernNotSupported();
} }
$cursor = $server->executeReadWriteCommand($this->databaseName, new Command($this->createCommandDocument()), $this->createOptions()); $cursor = $server->executeReadWriteCommand($this->databaseName, new Command($this->createCommandDocument($server)), $this->createOptions());
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if ( ! isset($result->value)) { if ( ! isset($result->value)) {
...@@ -239,9 +239,9 @@ class FindAndModify implements Executable, Explainable ...@@ -239,9 +239,9 @@ class FindAndModify implements Executable, Explainable
return $result->value; return $result->value;
} }
public function getCommandDocument() public function getCommandDocument(Server $server)
{ {
return $this->createCommandDocument(); return $this->createCommandDocument($server);
} }
/** /**
...@@ -249,7 +249,7 @@ class FindAndModify implements Executable, Explainable ...@@ -249,7 +249,7 @@ class FindAndModify implements Executable, Explainable
* *
* @return array * @return array
*/ */
private function createCommandDocument() private function createCommandDocument(Server $server)
{ {
$cmd = ['findAndModify' => $this->collectionName]; $cmd = ['findAndModify' => $this->collectionName];
......
...@@ -127,8 +127,8 @@ class FindOne implements Executable, Explainable ...@@ -127,8 +127,8 @@ class FindOne implements Executable, Explainable
return ($document === false) ? null : $document; return ($document === false) ? null : $document;
} }
public function getCommandDocument() public function getCommandDocument(Server $server)
{ {
return $this->find->getCommandDocument(); return $this->find->getCommandDocument($server);
} }
} }
...@@ -106,8 +106,8 @@ class FindOneAndDelete implements Executable, Explainable ...@@ -106,8 +106,8 @@ class FindOneAndDelete implements Executable, Explainable
return $this->findAndModify->execute($server); return $this->findAndModify->execute($server);
} }
public function getCommandDocument() public function getCommandDocument(Server $server)
{ {
return $this->findAndModify->getCommandDocument(); return $this->findAndModify->getCommandDocument($server);
} }
} }
...@@ -149,8 +149,8 @@ class FindOneAndReplace implements Executable, Explainable ...@@ -149,8 +149,8 @@ class FindOneAndReplace implements Executable, Explainable
return $this->findAndModify->execute($server); return $this->findAndModify->execute($server);
} }
public function getCommandDocument() public function getCommandDocument(Server $server)
{ {
return $this->findAndModify->getCommandDocument(); return $this->findAndModify->getCommandDocument($server);
} }
} }
...@@ -152,8 +152,8 @@ class FindOneAndUpdate implements Executable, Explainable ...@@ -152,8 +152,8 @@ class FindOneAndUpdate implements Executable, Explainable
return $this->findAndModify->execute($server); return $this->findAndModify->execute($server);
} }
public function getCommandDocument() public function getCommandDocument(Server $server)
{ {
return $this->findAndModify->getCommandDocument(); return $this->findAndModify->getCommandDocument($server);
} }
} }
...@@ -181,7 +181,7 @@ class Update implements Executable, Explainable ...@@ -181,7 +181,7 @@ class Update implements Executable, Explainable
return new UpdateResult($writeResult); return new UpdateResult($writeResult);
} }
public function getCommandDocument() public function getCommandDocument(Server $server)
{ {
$cmd = ['update' => $this->collectionName, 'updates' => [['q' => $this->filter, 'u' => $this->update] + $this->createUpdateOptions()]]; $cmd = ['update' => $this->collectionName, 'updates' => [['q' => $this->filter, 'u' => $this->update] + $this->createUpdateOptions()]];
......
...@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException; ...@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::updateMany() * @see \MongoDB\Collection::updateMany()
* @see http://docs.mongodb.org/manual/reference/command/update/ * @see http://docs.mongodb.org/manual/reference/command/update/
*/ */
class UpdateMany implements Executable class UpdateMany implements Executable, Explainable
{ {
private $update; private $update;
...@@ -104,4 +104,9 @@ class UpdateMany implements Executable ...@@ -104,4 +104,9 @@ class UpdateMany implements Executable
{ {
return $this->update->execute($server); return $this->update->execute($server);
} }
public function getCommandDocument(Server $server)
{
return $this->update->getCommandDocument($server);
}
} }
...@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException; ...@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::updateOne() * @see \MongoDB\Collection::updateOne()
* @see http://docs.mongodb.org/manual/reference/command/update/ * @see http://docs.mongodb.org/manual/reference/command/update/
*/ */
class UpdateOne implements Executable class UpdateOne implements Executable, Explainable
{ {
private $update; private $update;
...@@ -104,4 +104,9 @@ class UpdateOne implements Executable ...@@ -104,4 +104,9 @@ class UpdateOne implements Executable
{ {
return $this->update->execute($server); return $this->update->execute($server);
} }
public function getCommandDocument(Server $server)
{
return $this->update->getCommandDocument($server);
}
} }
...@@ -8,6 +8,8 @@ use MongoDB\Operation\Count; ...@@ -8,6 +8,8 @@ use MongoDB\Operation\Count;
use MongoDB\Operation\CreateCollection; use MongoDB\Operation\CreateCollection;
use MongoDB\Operation\Distinct; use MongoDB\Operation\Distinct;
use MongoDB\Operation\Delete; use MongoDB\Operation\Delete;
use MongoDB\Operation\DeleteMany;
use MongoDB\Operation\DeleteOne;
use MongoDB\Operation\Explain; use MongoDB\Operation\Explain;
use MongoDB\Operation\Find; use MongoDB\Operation\Find;
use MongoDB\Operation\FindAndModify; use MongoDB\Operation\FindAndModify;
...@@ -16,6 +18,8 @@ use MongoDB\Operation\FindOneAndDelete; ...@@ -16,6 +18,8 @@ use MongoDB\Operation\FindOneAndDelete;
use MongoDB\Operation\FindOneAndReplace; use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Operation\FindOneAndUpdate; use MongoDB\Operation\FindOneAndUpdate;
use MongoDB\Operation\Update; use MongoDB\Operation\Update;
use MongoDB\Operation\UpdateMany;
use MongoDB\Operation\UpdateOne;
use MongoDB\Tests\CommandObserver; use MongoDB\Tests\CommandObserver;
use stdClass; use stdClass;
...@@ -49,8 +53,6 @@ class ExplainFunctionalTest extends FunctionalTestCase ...@@ -49,8 +53,6 @@ class ExplainFunctionalTest extends FunctionalTestCase
*/ */
public function testDelete($verbosity, $executionStatsExpected, $allPlansExecutionExpected) public function testDelete($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{ {
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
$this->createFixtures(3); $this->createFixtures(3);
$filter = ['_id' => 1]; $filter = ['_id' => 1];
...@@ -63,6 +65,40 @@ class ExplainFunctionalTest extends FunctionalTestCase ...@@ -63,6 +65,40 @@ class ExplainFunctionalTest extends FunctionalTestCase
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected); $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
} }
/**
* @dataProvider provideVerbosityInformation
*/
public function testDeleteMany($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
$this->createFixtures(3);
$filter = ['_id' => ['$gt' => 1]];
$operation = new DeleteMany($this->getDatabaseName(), $this->getCollectionName(), $filter);
$explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
$result = $explainOperation->execute($this->getPrimaryServer());
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
}
/**
* @dataProvider provideVerbosityInformation
*/
public function testDeleteOne($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
$this->createFixtures(3);
$filter = ['_id' => 1];
$operation = new DeleteOne($this->getDatabaseName(), $this->getCollectionName(), $filter);
$explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
$result = $explainOperation->execute($this->getPrimaryServer());
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
}
/** /**
* @dataProvider provideVerbosityInformation * @dataProvider provideVerbosityInformation
*/ */
...@@ -262,6 +298,42 @@ class ExplainFunctionalTest extends FunctionalTestCase ...@@ -262,6 +298,42 @@ class ExplainFunctionalTest extends FunctionalTestCase
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected); $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
} }
/**
* @dataProvider provideVerbosityInformation
*/
public function testUpdateMany($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
$this->createFixtures(3);
$filter = ['_id' => ['$gt' => 1]];
$update = ['$inc' => ['x' => 1]];
$operation = new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), $filter, $update);
$explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
$result = $explainOperation->execute($this->getPrimaryServer());
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
}
/**
* @dataProvider provideVerbosityInformation
*/
public function testUpdateOne($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
$this->createFixtures(3);
$filter = ['_id' => ['$lte' => 1]];
$update = ['$inc' => ['x' => 1]];
$operation = new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), $filter, $update);
$explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
$result = $explainOperation->execute($this->getPrimaryServer());
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
}
public function provideVerbosityInformation() public function provideVerbosityInformation()
{ {
return [ return [
......
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