Commit d2fe8b39 authored by Jeremy Mikola's avatar Jeremy Mikola

Extra DropCollection operation class

parent c53cb01a
...@@ -18,6 +18,7 @@ use MongoDB\Operation\Aggregate; ...@@ -18,6 +18,7 @@ use MongoDB\Operation\Aggregate;
use MongoDB\Operation\CreateIndexes; use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\Count; use MongoDB\Operation\Count;
use MongoDB\Operation\Distinct; use MongoDB\Operation\Distinct;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\FindOneAndDelete; use MongoDB\Operation\FindOneAndDelete;
use MongoDB\Operation\FindOneAndReplace; use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Operation\FindOneAndUpdate; use MongoDB\Operation\FindOneAndUpdate;
...@@ -352,15 +353,14 @@ class Collection ...@@ -352,15 +353,14 @@ class Collection
/** /**
* Drop this collection. * Drop this collection.
* *
* @see http://docs.mongodb.org/manual/reference/command/drop/ * @return object Command result document
* @return Cursor
*/ */
public function drop() public function drop()
{ {
$command = new Command(array('drop' => $this->collname)); $operation = new DropCollection($this->dbname, $this->collname);
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY); $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
return $this->manager->executeCommand($this->dbname, $command, $readPreference); return $operation->execute($server);
} }
/** /**
......
...@@ -12,6 +12,7 @@ use MongoDB\Driver\Server; ...@@ -12,6 +12,7 @@ use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern; use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\CollectionInfoIterator; use MongoDB\Model\CollectionInfoIterator;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\DropDatabase; use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\ListCollections; use MongoDB\Operation\ListCollections;
...@@ -85,17 +86,15 @@ class Database ...@@ -85,17 +86,15 @@ class Database
/** /**
* Drop a collection within this database. * Drop a collection within this database.
* *
* @see http://docs.mongodb.org/manual/reference/command/drop/
* @param string $collectionName * @param string $collectionName
* @return Cursor * @return object Command result document
*/ */
public function dropCollection($collectionName) public function dropCollection($collectionName)
{ {
$collectionName = (string) $collectionName; $operation = new DropCollection($this->databaseName, $collectionName);
$command = new Command(array('drop' => $collectionName)); $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
return $this->manager->executeCommand($this->databaseName, $command, $readPreference); return $operation->execute($server);
} }
/** /**
......
<?php
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Exception\RuntimeException;
/**
* Operation for the drop command.
*
* @api
* @see MongoDB\Collection::drop()
* @see MongoDB\Database::dropCollection()
* @see http://docs.mongodb.org/manual/reference/command/drop/
*/
class DropCollection implements Executable
{
private $databaseName;
private $collectionName;
/**
* Constructs a drop command.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
*/
public function __construct($databaseName, $collectionName)
{
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return object Command result document
*/
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
$result = current($cursor->toArray());
if (empty($result['ok'])) {
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
}
return $result;
}
}
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