Commit ad4f3e6c authored by Katherine Walker's avatar Katherine Walker

Add getCommandDocument for Delete and Update

parent fefdb978
......@@ -35,7 +35,7 @@ use MongoDB\Exception\UnsupportedException;
* @internal
* @see http://docs.mongodb.org/manual/reference/command/delete/
*/
class Delete implements Executable
class Delete implements Executable, Explainable
{
private static $wireVersionForCollation = 5;
......@@ -117,18 +117,38 @@ class Delete implements Executable
throw UnsupportedException::collationNotSupported();
}
$deleteOptions = $this->createDeleteOptions();
$bulk = new Bulk();
$bulk->delete($this->filter, $this->createDeleteOptions());
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createExecuteOptions());
return new DeleteResult($writeResult);
}
public function getCommandDocument()
{
return ['delete' => $this->collectionName, 'deletes' => [['q' => $this->filter] + $this->createDeleteOptions()]];
}
/**
* Create options for the delete command.
*
* Note that these options are different from the bulk write options, which
* are created in createOptions().
*
* @return array
*/
private function createDeleteOptions()
{
$deleteOptions = ['limit' => $this->limit];
if (isset($this->options['collation'])) {
$deleteOptions['collation'] = (object) $this->options['collation'];
}
$bulk = new Bulk();
$bulk->delete($this->filter, $deleteOptions);
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createOptions());
return new DeleteResult($writeResult);
return $deleteOptions;
}
/**
......@@ -137,7 +157,7 @@ class Delete implements Executable
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @return array
*/
private function createOptions()
private function createExecuteOptions()
{
$options = [];
......
......@@ -35,7 +35,7 @@ use MongoDB\Exception\UnsupportedException;
* @internal
* @see http://docs.mongodb.org/manual/reference/command/update/
*/
class Update implements Executable
class Update implements Executable, Explainable
{
private static $wireVersionForArrayFilters = 6;
private static $wireVersionForCollation = 5;
......@@ -167,19 +167,6 @@ class Update implements Executable
throw UnsupportedException::collationNotSupported();
}
$updateOptions = [
'multi' => $this->options['multi'],
'upsert' => $this->options['upsert'],
];
if (isset($this->options['arrayFilters'])) {
$updateOptions['arrayFilters'] = $this->options['arrayFilters'];
}
if (isset($this->options['collation'])) {
$updateOptions['collation'] = (object) $this->options['collation'];
}
$bulkOptions = [];
if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
......@@ -187,20 +174,25 @@ class Update implements Executable
}
$bulk = new Bulk($bulkOptions);
$bulk->update($this->filter, $this->update, $updateOptions);
$bulk->update($this->filter, $this->update, $this->createUpdateOptions());
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createOptions());
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createExecuteOptions());
return new UpdateResult($writeResult);
}
public function getCommandDocument()
{
return ['update' => $this->collectionName, 'updates' => [['q' => $this->filter] + ['u' => $this->update] + $this->createUpdateOptions()]];
}
/**
* Create options for executing the bulk write.
*
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @return array
*/
private function createOptions()
private function createExecuteOptions()
{
$options = [];
......@@ -214,4 +206,30 @@ class Update implements Executable
return $options;
}
/**
* Create options for the update command.
*
* Note that these options are different from the bulk write options, which
* are created in createOptions().
*
* @return array
*/
private function createUpdateOptions()
{
$updateOptions = [
'multi' => $this->options['multi'],
'upsert' => $this->options['upsert'],
];
if (isset($this->options['arrayFilters'])) {
$updateOptions['arrayFilters'] = $this->options['arrayFilters'];
}
if (isset($this->options['collation'])) {
$updateOptions['collation'] = (object) $this->options['collation'];
}
return $updateOptions;
}
}
......@@ -3,14 +3,17 @@
namespace MongoDB\Tests\Operation;
use MongoDB\Driver\BulkWrite;
use MongoDB\Collection;
use MongoDB\Operation\Count;
use MongoDB\Operation\CreateCollection;
use MongoDB\Operation\Distinct;
use MongoDB\Operation\Delete;
use MongoDB\Operation\Explain;
use MongoDB\Operation\Find;
use MongoDB\Operation\FindAndModify;
use MongoDB\Operation\FindOne;
use MongoDB\Operation\InsertMany;
use MongoDB\Operation\Update;
class ExplainFunctionalTest extends FunctionalTestCase
{
......@@ -18,7 +21,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
{
parent::setUp();
if (version_compare($this->getServerVersion(), '3.0.0', '<')) {
$this->markTestSkipped('Explain is not supported');
$this->markTestSkipped('Explain command is not supported');
}
}
......@@ -36,6 +39,26 @@ class ExplainFunctionalTest extends FunctionalTestCase
$insertMany->execute($this->getPrimaryServer());
$operation = new Count($this->getDatabaseName(), $this->getCollectionName(), ['x' => ['$gte' => 1]], []);
$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 testDelete($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
$this->createFixtures(3);
$filter = ['_id' => 1];
$operation = new Delete($this->getDatabaseName(), $this->getCollectionName(), $filter, 1);
$explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
$result = $explainOperation->execute($this->getPrimaryServer());
......@@ -48,7 +71,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
public function testDistinct($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
$this->markTestSkipped('Distinct is not supported on servers with version < 3.2');
$this->markTestSkipped('Explaining distinct command requires server version >= 3.2');
}
$operation = new Distinct($this->getDatabaseName(), $this->getCollectionName(), 'x', []);
......@@ -65,7 +88,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
public function testFindAndModify($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
$this->markTestSkipped('FindAndModify is not supported on servers with version < 3.2');
$this->markTestSkipped('Explaining findAndModify command requires server version >= 3.2');
}
$operation = new FindAndModify($this->getDatabaseName(), $this->getCollectionName(), ['remove' => true]);
......@@ -148,6 +171,24 @@ class ExplainFunctionalTest extends FunctionalTestCase
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
}
/**
* @dataProvider provideVerbosityInformation
*/
public function testUpdate($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
$this->createFixtures(3);
$filter = ['_id' => ['$gt' => 1]];
$update = ['$inc' => ['x' => 1]];
$operation = new Update($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 [
......@@ -184,7 +225,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
for ($i = 1; $i <= $n; $i++) {
$bulkWrite->insert([
'_id' => $i,
'x' => (object) ['foo' => 'bar'],
'x' => (integer) ($i . $i),
]);
}
......
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