Commit 70c6e6e4 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-109: Extract DeleteOne and DeleteMany operation classes

parent 15af4606
...@@ -16,6 +16,8 @@ use MongoDB\Model\IndexInput; ...@@ -16,6 +16,8 @@ use MongoDB\Model\IndexInput;
use MongoDB\Operation\Aggregate; use MongoDB\Operation\Aggregate;
use MongoDB\Operation\CreateIndexes; use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\Count; use MongoDB\Operation\Count;
use MongoDB\Operation\DeleteMany;
use MongoDB\Operation\DeleteOne;
use MongoDB\Operation\Distinct; use MongoDB\Operation\Distinct;
use MongoDB\Operation\DropCollection; use MongoDB\Operation\DropCollection;
use MongoDB\Operation\DropIndexes; use MongoDB\Operation\DropIndexes;
...@@ -289,35 +291,45 @@ class Collection ...@@ -289,35 +291,45 @@ class Collection
} }
/** /**
* Deletes a document matching the $filter criteria. * Deletes all documents matching the filter.
* NOTE: Will delete ALL documents matching $filter
* *
* @see DeleteMany::__construct() for supported options
* @see http://docs.mongodb.org/manual/reference/command/delete/ * @see http://docs.mongodb.org/manual/reference/command/delete/
* * @param array|object $filter Query by which to delete documents
* @param array $filter The $filter criteria to delete * @param array $options Command options
* @return DeleteResult * @return DeleteResult
*/ */
public function deleteMany(array $filter) public function deleteMany($filter, array $options = array())
{ {
$wr = $this->_delete($filter, 0); if ( ! isset($options['writeConcern']) && isset($this->wc)) {
$options['writeConcern'] = $this->wc;
}
$operation = new DeleteMany($this->dbname, $this->collname, $filter, $options);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
return new DeleteResult($wr); return $operation->execute($server);
} }
/** /**
* Deletes a document matching the $filter criteria. * Deletes at most one document matching the filter.
* NOTE: Will delete at most ONE document matching $filter
* *
* @see DeleteOne::__construct() for supported options
* @see http://docs.mongodb.org/manual/reference/command/delete/ * @see http://docs.mongodb.org/manual/reference/command/delete/
* * @param array|object $filter Query by which to delete documents
* @param array $filter The $filter criteria to delete * @param array $options Command options
* @return DeleteResult * @return DeleteResult
*/ */
public function deleteOne(array $filter) public function deleteOne($filter, array $options = array())
{ {
$wr = $this->_delete($filter); if ( ! isset($options['writeConcern']) && isset($this->wc)) {
$options['writeConcern'] = $this->wc;
}
return new DeleteResult($wr); $operation = new DeleteOne($this->dbname, $this->collname, $filter, $options);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
return $operation->execute($server);
} }
/** /**
...@@ -660,19 +672,6 @@ class Collection ...@@ -660,19 +672,6 @@ class Collection
return new UpdateResult($wr); return new UpdateResult($wr);
} }
/**
* Internal helper for delete one/many documents
* @internal
*/
final protected function _delete($filter, $limit = 1)
{
$options = array_merge($this->getWriteOptions(), array("limit" => $limit));
$bulk = new BulkWrite($options["ordered"]);
$bulk->delete($filter, $options);
return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
}
/** /**
* Internal helper for replacing/updating one/many documents * Internal helper for replacing/updating one/many documents
* @internal * @internal
......
<?php
namespace MongoDB\Operation;
use MongoDB\DeleteResult;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
/**
* Operation for the delete command.
*
* This class is used internally by the DeleteMany and DeleteOne operation
* classes.
*
* @internal
* @see http://docs.mongodb.org/manual/reference/command/delete/
*/
class Delete implements Executable
{
private $databaseName;
private $collectionName;
private $filter;
private $limit;
private $options;
/**
* Constructs a delete command.
*
* Supported options:
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array|object $filter Query by which to delete documents
* @param integer $limit The number of matching documents to
* delete. Must be 0 or 1, for all or a
* single document, respectively.
* @param array $options Command options
* @throws InvalidArgumentException
*/
public function __construct($databaseName, $collectionName, $filter, $limit, array $options = array())
{
if ( ! is_array($filter) && ! is_object($filter)) {
throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
}
if ($limit !== 0 && $limit !== 1) {
throw new InvalidArgumentException('$limit must be 0 or 1');
}
if (array_key_exists('writeConcern', $options) && ! $options['writeConcern'] instanceof WriteConcern) {
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->filter = $filter;
$this->limit = $limit;
$this->options = $options;
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return DeleteResult
*/
public function execute(Server $server)
{
$bulk = new BulkWrite();
$bulk->delete($this->filter, array('limit' => $this->limit));
$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
return new DeleteResult($writeResult);
}
}
<?php
namespace MongoDB\Operation;
use MongoDB\DeleteResult;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
/**
* Operation for deleting multiple document with the delete command.
*
* @api
* @see MongoDB\Collection::deleteOne()
* @see http://docs.mongodb.org/manual/reference/command/delete/
*/
class DeleteMany implements Executable
{
private $delete;
/**
* Constructs a delete command.
*
* Supported options:
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array|object $filter Query by which to delete documents
* @param array $options Command options
* @throws InvalidArgumentException
*/
public function __construct($databaseName, $collectionName, $filter, array $options = array())
{
$this->delete = new Delete($databaseName, $collectionName, $filter, 0, $options);
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return DeleteResult
*/
public function execute(Server $server)
{
return $this->delete->execute($server);
}
}
<?php
namespace MongoDB\Operation;
use MongoDB\DeleteResult;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
/**
* Operation for deleting a single document with the delete command.
*
* @api
* @see MongoDB\Collection::deleteOne()
* @see http://docs.mongodb.org/manual/reference/command/delete/
*/
class DeleteOne implements Executable
{
private $delete;
/**
* Constructs a delete command.
*
* Supported options:
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array|object $filter Query by which to delete documents
* @param array $options Command options
* @throws InvalidArgumentException
*/
public function __construct($databaseName, $collectionName, $filter, array $options = array())
{
$this->delete = new Delete($databaseName, $collectionName, $filter, 1, $options);
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return DeleteResult
*/
public function execute(Server $server)
{
return $this->delete->execute($server);
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\Delete;
class DeleteTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidDocumentArguments
*/
public function testConstructorFilterArgumentType($filter)
{
new Delete($this->getDatabaseName(), $this->getCollectionName(), $filter, 0);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidDocumentArguments
*/
public function testConstructorLimitArgumentMustBeOneOrZero()
{
new Delete($this->getDatabaseName(), $this->getCollectionName(), array(), 2);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
*/
public function testConstructorWriteConcernOptionType()
{
new Delete($this->getDatabaseName(), $this->getCollectionName(), array(), 1, array('writeConcern' => null));
}
}
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