Commit d070cf84 authored by Jeremy Mikola's avatar Jeremy Mikola

Extract DropIndexes operation class

parent 3c28b7d9
...@@ -19,6 +19,7 @@ use MongoDB\Operation\CreateIndexes; ...@@ -19,6 +19,7 @@ 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\DropCollection;
use MongoDB\Operation\DropIndexes;
use MongoDB\Operation\FindOneAndDelete; use MongoDB\Operation\FindOneAndDelete;
use MongoDB\Operation\FindOneAndReplace; use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Operation\FindOneAndUpdate; use MongoDB\Operation\FindOneAndUpdate;
...@@ -366,43 +367,35 @@ class Collection ...@@ -366,43 +367,35 @@ class Collection
/** /**
* Drop a single index in the collection. * Drop a single index in the collection.
* *
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/ * @param string $indexName Index name
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/ * @return object Command result document
* @param string $indexName
* @return Cursor
* @throws InvalidArgumentException if $indexName is an empty string or "*" * @throws InvalidArgumentException if $indexName is an empty string or "*"
*/ */
public function dropIndex($indexName) public function dropIndex($indexName)
{ {
$indexName = (string) $indexName; $indexName = (string) $indexName;
if ($indexName === '') {
throw new InvalidArgumentException('Index name cannot be empty');
}
if ($indexName === '*') { if ($indexName === '*') {
throw new InvalidArgumentException('dropIndexes() must be used to drop multiple indexes'); throw new InvalidArgumentException('dropIndexes() must be used to drop multiple indexes');
} }
$command = new Command(array('dropIndexes' => $this->collname, 'index' => $indexName)); $operation = new DropIndexes($this->dbname, $this->collname, $indexName);
$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);
} }
/** /**
* Drop all indexes in the collection. * Drop all indexes in the collection.
* *
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/ * @return object Command result document
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndexes/
* @return Cursor
*/ */
public function dropIndexes() public function dropIndexes()
{ {
$command = new Command(array('dropIndexes' => $this->collname, 'index' => '*')); $operation = new DropIndexes($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);
} }
/** /**
......
<?php
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\RuntimeException;
/**
* Operation for the dropIndexes command.
*
* @api
* @see MongoDB\Collection::dropIndexes()
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
*/
class DropIndexes implements Executable
{
private $databaseName;
private $collectionName;
private $indexName;
/**
* Constructs a dropIndexes command.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param string $indexName Index name (use "*" to drop all indexes)
* @throws InvalidArgumentException
*/
public function __construct($databaseName, $collectionName, $indexName)
{
$indexName = (string) $indexName;
if ($indexName === '') {
throw new InvalidArgumentException('$indexName cannot be empty');
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->indexName = $indexName;
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return object Command result document
*/
public function execute(Server $server)
{
$cmd = array(
'dropIndexes' => $this->collectionName,
'index' => $this->indexName,
);
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
$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