Skip to content

PHPLIB-70: Database and collection management #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Apr 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c035bae
Create test bootstrap for autoloading
jmikola Mar 17, 2015
24d34dc
Create base classes for unit and functional tests
jmikola Mar 17, 2015
b85b91f
PHPLIB-73: Inherit default write concern and read preferences
jmikola Mar 24, 2015
10ea91c
PHPLIB-65: Database drop methods
jmikola Mar 24, 2015
d1b7a48
Cast expected string arguments
jmikola Mar 24, 2015
7ada7c2
Dump server buildInfo before running tests on Travis CI
jmikola Mar 25, 2015
46d0767
Use 127.0.0.1 instead of localhost for default URI
jmikola Mar 25, 2015
e9a5e8a
Restructure CollectionTest and fixture generation functions
jmikola Mar 25, 2015
6c1d6ac
PHPLIB-72: Database enumeration method
jmikola Mar 26, 2015
28da2e1
PHPLIB-71: Collection drop methods
jmikola Mar 26, 2015
da4fdf1
Setup Database object in DatabaseFunctionalTest
jmikola Mar 27, 2015
92ee3c5
PHPLIB-45: Collection enumeration methods
jmikola Mar 27, 2015
0ffbae8
Handle new Cursor and toArray() API in extension
jmikola Apr 6, 2015
fde9d8e
PHPLIB-64: Collection creation method
jmikola Apr 7, 2015
9e62cab
Allow any extension version for development
jmikola Apr 7, 2015
a20fa27
PHPLIB-45: Construct CollectionInfoLegacyIterator from Traversable
jmikola Apr 10, 2015
a32ac4f
PHPLIB-45: List collections according to wire protocol version
jmikola Apr 10, 2015
ccf1eba
PHPLIB-45: Support stdClass listCollections() filter option
jmikola Apr 10, 2015
e574c2d
PHPLIB-45: Test listCollections with filter option
jmikola Apr 10, 2015
fd0b861
Install latest PECL extension when testing
jmikola Apr 10, 2015
192d950
Don't be quiet when compiling the extension
jmikola Apr 10, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ php:
- 5.6

env:
- MONGODB_VERSION=0.2.0
- MONGODB_VERSION=alpha

services: mongodb

before_script:
- pecl -q install -f mongodb-${MONGODB_VERSION}
- mongo --eval 'tojson(db.runCommand({buildInfo:1}))'
- pecl install -f mongodb-${MONGODB_VERSION}
- php --ri mongodb
- composer install --dev --no-interaction --prefer-source
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{ "name": "Derick Rethans", "email": "github@derickrethans.nl" }
],
"require": {
"ext-mongodb": "^0.2"
"ext-mongodb": "*"
},
"require-dev": {
"fzaninotto/faker": "~1.0"
Expand Down
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
convertWarningsToExceptions="true"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
>

<php>
<ini name="error_reporting" value="-1"/>
<env name="MONGODB_URI" value="mongodb://127.0.0.1:27017"/>
<env name="MONGODB_DATABASE" value="phplib_test"/>
</php>

<testsuites>
Expand Down
118 changes: 85 additions & 33 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,125 @@

namespace MongoDB;

use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Result;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use ArrayIterator;
use stdClass;
use UnexpectedValueException;

class Client
{
private $manager;
private $wc;
private $rp;

private $readPreference;
private $writeConcern;

/**
* Constructs new Client instance
* Constructs a new Client instance.
*
* This is the suggested main entry point using phongo.
* It acts as a bridge to access individual databases and collection tools
* which are provided in this namespace.
* This is the preferred class for connecting to a MongoDB server or
* cluster of servers. It serves as a gateway for accessing individual
* databases and collections.
*
* @param Manager $uri The MongoDB URI to connect to
* @param WriteConcern $options URI Options
* @param ReadPreference $driverOptions Driver specific options
* @see http://docs.mongodb.org/manual/reference/connection-string/
* @param string $uri MongoDB connection string
* @param array $options Additional connection string options
* @param array $driverOptions Driver-specific options
*/
public function __construct($uri, $options, $driverOptions)
public function __construct($uri, array $options = array(), array $driverOptions = array())
{
$this->manager = new Manager($uri, $options, $driverOptions);
}

/**
* Drop a database.
*
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
* @param string $databaseName
* @return Result
* @return Cursor
*/
public function dropDatabase($databaseName)
{
// TODO
$databaseName = (string) $databaseName;
$command = new Command(array('dropDatabase' => 1));
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);

return $this->manager->executeCommand($databaseName, $command, $readPreference);
}

/**
* Select a database
* List databases.
*
* It acts as a bridge to access specific database commands
*
* @param string $databaseName The database to select
* @param WriteConcern $writeConcern Default Write Concern to apply
* @param ReadPreference $readPreferences Default Read Preferences to apply
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/
* @return Traversable
* @throws UnexpectedValueException if the command result is malformed
*/
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreferences = null)
public function listDatabases()
{
return new Database($this->manager, $databaseName, $writeConcern, $readPreferences);
$command = new Command(array('listDatabases' => 1));

$cursor = $this->manager->executeCommand('admin', $command);
$result = current($cursor->toArray());

if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
}

$databases = array_map(
function(stdClass $database) { return (array) $database; },
$result['databases']
);

/* Return a Traversable instead of an array in case listDatabases is
* eventually changed to return a command cursor, like the collection
* and index enumeration commands. This makes the "totalSize" command
* field inaccessible, but users can manually invoke the command if they
* need that value.
*/
return new ArrayIterator($databases);
}

/**
* Select a specific collection in a database
* Select a database.
*
* It acts as a bridge to access specific collection commands
* If a write concern or read preference is not specified, the write concern
* or read preference of the Client will be applied, respectively.
*
* @param string $databaseName The database where the $collectionName exists
* @param string $collectionName The collection to select
* @param WriteConcern $writeConcern Default Write Concern to apply
* @param ReadPreference $readPreferences Default Read Preferences to apply
* @param string $databaseName Name of the database to select
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
* @return Database
*/
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreferences = null)
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{
return new Collection($this->manager, "{$databaseName}.{$collectionName}", $writeConcern, $readPreferences);
// TODO: inherit from Manager options once PHPC-196 is implemented
$writeConcern = $writeConcern ?: $this->writeConcern;
$readPreference = $readPreference ?: $this->readPreference;

return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
}

}
/**
* Select a collection.
*
* If a write concern or read preference is not specified, the write concern
* or read preference of the Client will be applied, respectively.
*
* @param string $databaseName Name of the database containing the collection
* @param string $collectionName Name of the collection to select
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
* @return Collection
*/
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{
$namespace = $databaseName . '.' . $collectionName;
// TODO: inherit from Manager options once PHPC-196 is implemented
$writeConcern = $writeConcern ?: $this->writeConcern;
$readPreference = $readPreference ?: $this->readPreference;

return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
}
}
69 changes: 37 additions & 32 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace MongoDB;

use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Query;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Result;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\WriteConcern;

Expand Down Expand Up @@ -43,25 +43,24 @@ class Collection


/**
* Constructs new Collection instance
* Constructs new Collection instance.
*
* This is the suggested CRUD interface when using phongo.
* It implements the MongoDB CRUD specification which is an interface all MongoDB
* supported drivers follow.
* This class provides methods for collection-specific operations, such as
* CRUD (i.e. create, read, update, and delete) and index management.
*
* @param Manager $manager The phongo Manager instance
* @param string $ns Fully Qualified Namespace (dbname.collname)
* @param WriteConcern $wc The WriteConcern to apply to writes
* @param ReadPreference $rp The ReadPreferences to apply to reads
* @param Manager $manager Manager instance from the driver
* @param string $namespace Collection namespace (e.g. "db.collection")
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
*/
public function __construct(Manager $manager, $ns, WriteConcern $wc = null, ReadPreference $rp = null)
public function __construct(Manager $manager, $namespace, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{
$this->manager = $manager;
$this->ns = $ns;
$this->wc = $wc;
$this->rp = $rp;
$this->ns = (string) $namespace;
$this->wc = $writeConcern;
$this->rp = $readPreference;

list($this->dbname, $this->collname) = explode(".", $ns, 2);
list($this->dbname, $this->collname) = explode(".", $namespace, 2);
}

/**
Expand All @@ -87,14 +86,16 @@ public function aggregate(array $pipeline, array $options = array())
"pipeline" => $pipeline,
) + $options;

$result = $this->_runCommand($this->dbname, $cmd);
$doc = $result->toArray();
$cursor = $this->_runCommand($this->dbname, $cmd);

if (isset($cmd["cursor"]) && $cmd["cursor"]) {
return $result;
} else {
if ($doc["ok"]) {
return new \ArrayIterator($doc["result"]);
}
return $cursor;
}

$doc = current($cursor->toArray());

if ($doc["ok"]) {
return new \ArrayIterator($doc["result"]);
}

throw $this->_generateCommandException($doc);
Expand Down Expand Up @@ -235,7 +236,7 @@ public function count(array $filter = array(), array $options = array())
"query" => $filter,
) + $options;

$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["n"];
}
Expand Down Expand Up @@ -325,7 +326,7 @@ public function distinct($fieldName, array $filter = array(), array $options = a
"query" => $filter,
) + $options;

$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["values"];
}
Expand All @@ -335,11 +336,15 @@ public function distinct($fieldName, array $filter = array(), array $options = a
/**
* Drop this collection.
*
* @return Result
* @see http://docs.mongodb.org/manual/reference/command/drop/
* @return Cursor
*/
public function drop()
{
// TODO
$command = new Command(array('drop' => $this->collname));
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);

return $this->manager->executeCommand($this->dbname, $command, $readPreference);
}

/**
Expand All @@ -348,7 +353,7 @@ public function drop()
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
* @param string $indexName
* @return Result
* @return Cursor
* @throws InvalidArgumentException if "*" is specified
*/
public function dropIndex($indexName)
Expand All @@ -361,7 +366,7 @@ public function dropIndex($indexName)
*
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndexes/
* @return Result
* @return Cursor
*/
public function dropIndexes()
{
Expand All @@ -376,7 +381,7 @@ public function dropIndexes()
*
* @param array $filter The find query to execute
* @param array $options Additional options
* @return Result
* @return Cursor
*/
public function find(array $filter = array(), array $options = array())
{
Expand Down Expand Up @@ -434,7 +439,7 @@ public function findOneAndDelete(array $filter, array $options = array())
"query" => $filter,
) + $options;

$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["value"];
}
Expand Down Expand Up @@ -471,7 +476,7 @@ public function findOneAndReplace(array $filter, array $replacement, array $opti
"query" => $filter,
) + $options;

$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["value"];
}
Expand Down Expand Up @@ -509,7 +514,7 @@ public function findOneAndUpdate(array $filter, array $update, array $options =
"query" => $filter,
) + $options;

$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["value"];
}
Expand Down Expand Up @@ -948,7 +953,7 @@ public function insertOne(array $document)
*
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
* @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
* @return Result
* @return Cursor
*/
public function listIndexes()
{
Expand Down
Loading