Commit c53cb01a authored by Jeremy Mikola's avatar Jeremy Mikola

Extract DropDatabase operation class

parent 6dc7d316
......@@ -8,6 +8,7 @@ use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Model\DatabaseInfoIterator;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\ListDatabases;
class Client
......@@ -36,17 +37,15 @@ class Client
/**
* Drop a database.
*
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
* @param string $databaseName
* @return Cursor
* @return object Command result document
*/
public function dropDatabase($databaseName)
{
$databaseName = (string) $databaseName;
$command = new Command(array('dropDatabase' => 1));
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
$operation = new DropDatabase($databaseName);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
return $this->manager->executeCommand($databaseName, $command, $readPreference);
return $operation->execute($server);
}
/**
......
......@@ -12,6 +12,7 @@ use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\CollectionInfoIterator;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\ListCollections;
class Database
......@@ -71,15 +72,14 @@ class Database
/**
* Drop this database.
*
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
* @return Cursor
* @return object Command result document
*/
public function drop()
{
$command = new Command(array('dropDatabase' => 1));
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
$operation = new DropDatabase($this->databaseName);
$server = $this->manager->selectServer(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 dropDatabase command.
*
* @api
* @see MongoDB\Client::dropDatabase()
* @see MongoDB\Database::drop()
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
*/
class DropDatabase implements Executable
{
private $databaseName;
/**
* Constructs a dropDatabase command.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
*/
public function __construct($databaseName)
{
$this->databaseName = (string) $databaseName;
}
/**
* 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('dropDatabase' => 1)));
$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