Skip to content

Commit c1e85c4

Browse files
committed
PHPLIB-109: Extract DeleteOne and DeleteMany operation classes
1 parent 31ed96c commit c1e85c4

File tree

4 files changed

+203
-27
lines changed

4 files changed

+203
-27
lines changed

src/Collection.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use MongoDB\Operation\Aggregate;
1717
use MongoDB\Operation\CreateIndexes;
1818
use MongoDB\Operation\Count;
19+
use MongoDB\Operation\DeleteMany;
20+
use MongoDB\Operation\DeleteOne;
1921
use MongoDB\Operation\Distinct;
2022
use MongoDB\Operation\DropCollection;
2123
use MongoDB\Operation\DropIndexes;
@@ -289,35 +291,41 @@ public function createIndexes(array $indexes)
289291
}
290292

291293
/**
292-
* Deletes a document matching the $filter criteria.
293-
* NOTE: Will delete ALL documents matching $filter
294+
* Deletes all documents matching the filter.
294295
*
296+
* @see DeleteMany::__construct() for supported options
295297
* @see http://docs.mongodb.org/manual/reference/command/delete/
296-
*
297-
* @param array $filter The $filter criteria to delete
298+
* @param array|object $filter Query by which to delete documents
299+
* @param array $options Command options
298300
* @return DeleteResult
299301
*/
300-
public function deleteMany(array $filter)
302+
public function deleteMany($filter, array $options = array())
301303
{
302-
$wr = $this->_delete($filter, 0);
304+
$options += array('writeConcern' => $this->wc);
303305

304-
return new DeleteResult($wr);
306+
$operation = new DeleteMany($this->dbname, $this->collname, $filter, $options);
307+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
308+
309+
return $operation->execute($server);
305310
}
306311

307312
/**
308-
* Deletes a document matching the $filter criteria.
309-
* NOTE: Will delete at most ONE document matching $filter
313+
* Deletes at most one document matching the filter.
310314
*
315+
* @see DeleteOne::__construct() for supported options
311316
* @see http://docs.mongodb.org/manual/reference/command/delete/
312-
*
313-
* @param array $filter The $filter criteria to delete
317+
* @param array|object $filter Query by which to delete documents
318+
* @param array $options Command options
314319
* @return DeleteResult
315320
*/
316-
public function deleteOne(array $filter)
321+
public function deleteOne($filter, array $options = array())
317322
{
318-
$wr = $this->_delete($filter);
323+
$options += array('writeConcern' => $this->wc);
324+
325+
$operation = new DeleteOne($this->dbname, $this->collname, $filter, $options);
326+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
319327

320-
return new DeleteResult($wr);
328+
return $operation->execute($server);
321329
}
322330

323331
/**
@@ -656,19 +664,6 @@ public function updateOne(array $filter, array $update, array $options = array()
656664
return new UpdateResult($wr);
657665
}
658666

659-
/**
660-
* Internal helper for delete one/many documents
661-
* @internal
662-
*/
663-
final protected function _delete($filter, $limit = 1)
664-
{
665-
$options = array_merge($this->getWriteOptions(), array("limit" => $limit));
666-
667-
$bulk = new BulkWrite($options["ordered"]);
668-
$bulk->delete($filter, $options);
669-
return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
670-
}
671-
672667
/**
673668
* Internal helper for replacing/updating one/many documents
674669
* @internal

src/Operation/Delete.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace MongoDB\Operation;
4+
5+
use MongoDB\DeleteResult;
6+
use MongoDB\Driver\BulkWrite;
7+
use MongoDB\Driver\Server;
8+
use MongoDB\Driver\WriteConcern;
9+
use MongoDB\Exception\InvalidArgumentException;
10+
use MongoDB\Exception\InvalidArgumentTypeException;
11+
12+
/**
13+
* Operation for the delete command.
14+
*
15+
* This class is used internally by the DeleteMany and DeleteOne operation
16+
* classes.
17+
*
18+
* @internal
19+
* @see http://docs.mongodb.org/manual/reference/command/delete/
20+
*/
21+
class Delete implements Executable
22+
{
23+
private $databaseName;
24+
private $collectionName;
25+
private $filter;
26+
private $limit;
27+
private $options;
28+
29+
/**
30+
* Constructs a delete command.
31+
*
32+
* Supported options:
33+
*
34+
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
35+
*
36+
* @param string $databaseName Database name
37+
* @param string $collectionName Collection name
38+
* @param array|object $filter Query by which to delete documents
39+
* @param integer $limit The number of matching documents to
40+
* delete. Must be 0 or 1, for all or a
41+
* single document, respectively.
42+
* @param array $options Command options
43+
* @throws InvalidArgumentException
44+
*/
45+
public function __construct($databaseName, $collectionName, $filter, $limit, array $options = array())
46+
{
47+
if ( ! is_array($filter) && ! is_object($filter)) {
48+
throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
49+
}
50+
51+
if ($limit !== 0 && $limit !== 1) {
52+
throw new InvalidArgumentException('$limit must be 0 or 1');
53+
}
54+
55+
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
56+
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
57+
}
58+
59+
$this->databaseName = (string) $databaseName;
60+
$this->collectionName = (string) $collectionName;
61+
$this->filter = $filter;
62+
$this->limit = $limit;
63+
$this->options = $options;
64+
}
65+
66+
/**
67+
* Execute the operation.
68+
*
69+
* @see Executable::execute()
70+
* @param Server $server
71+
* @return DeleteResult
72+
*/
73+
public function execute(Server $server)
74+
{
75+
$bulk = new BulkWrite();
76+
$bulk->delete($this->filter, array('limit' => $this->limit));
77+
78+
$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
79+
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
80+
81+
return new DeleteResult($writeResult);
82+
}
83+
}

src/Operation/DeleteMany.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace MongoDB\Operation;
4+
5+
use MongoDB\DeleteResult;
6+
use MongoDB\Driver\Server;
7+
use MongoDB\Exception\InvalidArgumentException;
8+
9+
/**
10+
* Operation for deleting multiple document with the delete command.
11+
*
12+
* @api
13+
* @see MongoDB\Collection::deleteOne()
14+
* @see http://docs.mongodb.org/manual/reference/command/delete/
15+
*/
16+
class DeleteMany implements Executable
17+
{
18+
private $delete;
19+
20+
/**
21+
* Constructs a delete command.
22+
*
23+
* Supported options:
24+
*
25+
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
26+
*
27+
* @param string $databaseName Database name
28+
* @param string $collectionName Collection name
29+
* @param array|object $filter Query by which to delete documents
30+
* @param array $options Command options
31+
* @throws InvalidArgumentException
32+
*/
33+
public function __construct($databaseName, $collectionName, $filter, array $options = array())
34+
{
35+
$this->delete = new Delete($databaseName, $collectionName, $filter, 0, $options);
36+
}
37+
38+
/**
39+
* Execute the operation.
40+
*
41+
* @see Executable::execute()
42+
* @param Server $server
43+
* @return DeleteResult
44+
*/
45+
public function execute(Server $server)
46+
{
47+
return $this->delete->execute($server);
48+
}
49+
}

src/Operation/DeleteOne.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace MongoDB\Operation;
4+
5+
use MongoDB\DeleteResult;
6+
use MongoDB\Driver\Server;
7+
use MongoDB\Exception\InvalidArgumentException;
8+
9+
/**
10+
* Operation for deleting a single document with the delete command.
11+
*
12+
* @api
13+
* @see MongoDB\Collection::deleteOne()
14+
* @see http://docs.mongodb.org/manual/reference/command/delete/
15+
*/
16+
class DeleteOne implements Executable
17+
{
18+
private $delete;
19+
20+
/**
21+
* Constructs a delete command.
22+
*
23+
* Supported options:
24+
*
25+
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
26+
*
27+
* @param string $databaseName Database name
28+
* @param string $collectionName Collection name
29+
* @param array|object $filter Query by which to delete documents
30+
* @param array $options Command options
31+
* @throws InvalidArgumentException
32+
*/
33+
public function __construct($databaseName, $collectionName, $filter, array $options = array())
34+
{
35+
$this->delete = new Delete($databaseName, $collectionName, $filter, 1, $options);
36+
}
37+
38+
/**
39+
* Execute the operation.
40+
*
41+
* @see Executable::execute()
42+
* @param Server $server
43+
* @return DeleteResult
44+
*/
45+
public function execute(Server $server)
46+
{
47+
return $this->delete->execute($server);
48+
}
49+
}

0 commit comments

Comments
 (0)