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
return (integer) $result->n;
}
public function getCommandDocument()
public function getCommandDocument(Server $server)
{
return $this->createCommandDocument();
}
......
......@@ -125,7 +125,7 @@ class Delete implements Executable, Explainable
return new DeleteResult($writeResult);
}
public function getCommandDocument()
public function getCommandDocument(Server $server)
{
$cmd = ['delete' => $this->collectionName, 'deletes' => [['q' => $this->filter] + $this->createDeleteOptions()]];
......
......@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::deleteOne()
* @see http://docs.mongodb.org/manual/reference/command/delete/
*/
class DeleteMany implements Executable
class DeleteMany implements Executable, Explainable
{
private $delete;
......@@ -74,4 +74,9 @@ class DeleteMany implements Executable
{
return $this->delete->execute($server);
}
public function getCommandDocument(Server $server)
{
return $this->delete->getCommandDocument($server);
}
}
......@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::deleteOne()
* @see http://docs.mongodb.org/manual/reference/command/delete/
*/
class DeleteOne implements Executable
class DeleteOne implements Executable, Explainable
{
private $delete;
......@@ -74,4 +74,9 @@ class DeleteOne implements Executable
{
return $this->delete->execute($server);
}
public function getCommandDocument(Server $server)
{
return $this->delete->getCommandDocument($server);
}
}
......@@ -143,7 +143,7 @@ class Distinct implements Executable, Explainable
return $result->values;
}
public function getCommandDocument()
public function getCommandDocument(Server $server)
{
return $this->createCommandDocument();
}
......
......@@ -88,7 +88,7 @@ class Explain implements Executable
throw UnsupportedException::explainNotSupported();
}
$cmd = ['explain' => $this->explainable->getCommandDocument()];
$cmd = ['explain' => $this->explainable->getCommandDocument($server)];
if (isset($this->options['verbosity'])) {
$cmd['verbosity'] = $this->options['verbosity'];
......
......@@ -17,6 +17,8 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Server;
/**
* Explainable interface for explainable operations (count, distinct, find,
* findAndModify, delete, and update).
......@@ -25,5 +27,5 @@ namespace MongoDB\Operation;
*/
interface Explainable extends Executable
{
function getCommandDocument();
function getCommandDocument(Server $server);
}
......@@ -293,7 +293,7 @@ class Find implements Executable, Explainable
return $cursor;
}
public function getCommandDocument()
public function getCommandDocument(Server $server)
{
return $this->createCommandDocument();
}
......
......@@ -210,7 +210,7 @@ class FindAndModify implements Executable, Explainable
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());
if ( ! isset($result->value)) {
......@@ -239,9 +239,9 @@ class FindAndModify implements Executable, Explainable
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
*
* @return array
*/
private function createCommandDocument()
private function createCommandDocument(Server $server)
{
$cmd = ['findAndModify' => $this->collectionName];
......
......@@ -127,8 +127,8 @@ class FindOne implements Executable, Explainable
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
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
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
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
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()]];
......
......@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::updateMany()
* @see http://docs.mongodb.org/manual/reference/command/update/
*/
class UpdateMany implements Executable
class UpdateMany implements Executable, Explainable
{
private $update;
......@@ -104,4 +104,9 @@ class UpdateMany implements Executable
{
return $this->update->execute($server);
}
public function getCommandDocument(Server $server)
{
return $this->update->getCommandDocument($server);
}
}
......@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::updateOne()
* @see http://docs.mongodb.org/manual/reference/command/update/
*/
class UpdateOne implements Executable
class UpdateOne implements Executable, Explainable
{
private $update;
......@@ -104,4 +104,9 @@ class UpdateOne implements Executable
{
return $this->update->execute($server);
}
public function getCommandDocument(Server $server)
{
return $this->update->getCommandDocument($server);
}
}
......@@ -8,6 +8,8 @@ use MongoDB\Operation\Count;
use MongoDB\Operation\CreateCollection;
use MongoDB\Operation\Distinct;
use MongoDB\Operation\Delete;
use MongoDB\Operation\DeleteMany;
use MongoDB\Operation\DeleteOne;
use MongoDB\Operation\Explain;
use MongoDB\Operation\Find;
use MongoDB\Operation\FindAndModify;
......@@ -16,6 +18,8 @@ use MongoDB\Operation\FindOneAndDelete;
use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Operation\FindOneAndUpdate;
use MongoDB\Operation\Update;
use MongoDB\Operation\UpdateMany;
use MongoDB\Operation\UpdateOne;
use MongoDB\Tests\CommandObserver;
use stdClass;
......@@ -49,8 +53,6 @@ class ExplainFunctionalTest extends FunctionalTestCase
*/
public function testDelete($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
$this->createFixtures(3);
$filter = ['_id' => 1];
......@@ -63,6 +65,40 @@ class ExplainFunctionalTest extends FunctionalTestCase
$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
*/
......@@ -262,6 +298,42 @@ class ExplainFunctionalTest extends FunctionalTestCase
$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()
{
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