Commit 70f8c0a1 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #485

parents d9c277ad 0b511a05
arg_name: option
name: filter
type: array|object
description: |
A query expression to filter the list of databases.
You can specify a query expression for database fields (e.g. ``name``,
``sizeOnDisk``, ``empty``).
.. versionadded:: 1.3
interface: phpmethod
operation: ~
optional: true
---
source: source:
file: apiargs-common-option.yaml file: apiargs-common-option.yaml
ref: maxTimeMS ref: maxTimeMS
......
...@@ -4,7 +4,12 @@ type: array|object ...@@ -4,7 +4,12 @@ type: array|object
description: | description: |
A query expression to filter the list of collections. A query expression to filter the list of collections.
You can specify a query expression on the collection ``name`` and ``options``. You can specify a query expression for collection fields (e.g. ``name``,
``options``).
For server versions < 3.0, the filter can only be used to match the ``name``
field with a string value. More complex filters will result in an exception at
execution time if used.
interface: phpmethod interface: phpmethod
operation: ~ operation: ~
optional: true optional: true
......
...@@ -49,6 +49,10 @@ class ListCollections implements Executable ...@@ -49,6 +49,10 @@ class ListCollections implements Executable
* *
* * filter (document): Query by which to filter collections. * * filter (document): Query by which to filter collections.
* *
* For server versions < 3.0, the filter can only be used to match the
* "name" field with a string value. More complex filters will result in
* an exception at execution time if used.
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to * * maxTimeMS (integer): The maximum amount of time to allow the query to
* run. * run.
* *
......
...@@ -42,6 +42,10 @@ class ListDatabases implements Executable ...@@ -42,6 +42,10 @@ class ListDatabases implements Executable
* *
* Supported options: * Supported options:
* *
* * filter (document): Query by which to filter databases.
*
* For servers < 3.6, this option is ignored.
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to * * maxTimeMS (integer): The maximum amount of time to allow the query to
* run. * run.
* *
...@@ -54,6 +58,10 @@ class ListDatabases implements Executable ...@@ -54,6 +58,10 @@ class ListDatabases implements Executable
*/ */
public function __construct(array $options = []) public function __construct(array $options = [])
{ {
if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) {
throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'array or object');
}
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) { if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer'); throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
} }
...@@ -78,6 +86,10 @@ class ListDatabases implements Executable ...@@ -78,6 +86,10 @@ class ListDatabases implements Executable
{ {
$cmd = ['listDatabases' => 1]; $cmd = ['listDatabases' => 1];
if ( ! empty($this->options['filter'])) {
$cmd['filter'] = (object) $this->options['filter'];
}
if (isset($this->options['maxTimeMS'])) { if (isset($this->options['maxTimeMS'])) {
$cmd['maxTimeMS'] = $this->options['maxTimeMS']; $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
} }
......
...@@ -2,12 +2,56 @@ ...@@ -2,12 +2,56 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\Operation\InsertOne;
use MongoDB\Operation\ListDatabases; use MongoDB\Operation\ListDatabases;
use MongoDB\Tests\CommandObserver; use MongoDB\Tests\CommandObserver;
use stdClass; use stdClass;
class ListDatabasesFunctionalTest extends FunctionalTestCase class ListDatabasesFunctionalTest extends FunctionalTestCase
{ {
public function testListDatabases()
{
$server = $this->getPrimaryServer();
$insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
$writeResult = $insertOne->execute($server);
$this->assertEquals(1, $writeResult->getInsertedCount());
$operation = new ListDatabases();
$databases = $operation->execute($server);
$this->assertInstanceOf('MongoDB\Model\DatabaseInfoIterator', $databases);
foreach ($databases as $database) {
$this->assertInstanceOf('MongoDB\Model\DatabaseInfo', $database);
}
}
public function testFilterOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('listDatabase command "filter" option is not supported');
}
$server = $this->getPrimaryServer();
$insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
$writeResult = $insertOne->execute($server);
$this->assertEquals(1, $writeResult->getInsertedCount());
$operation = new ListDatabases(['filter' => ['name' => $this->getDatabaseName()]]);
$databases = $operation->execute($server);
$this->assertInstanceOf('MongoDB\Model\DatabaseInfoIterator', $databases);
$this->assertCount(1, $databases);
foreach ($databases as $database) {
$this->assertInstanceOf('MongoDB\Model\DatabaseInfo', $database);
$this->assertEquals($this->getDatabaseName(), $database->getName());
}
}
public function testSessionOption() public function testSessionOption()
{ {
if (version_compare($this->getServerVersion(), '3.6.0', '<')) { if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
......
...@@ -19,6 +19,10 @@ class ListDatabasesTest extends TestCase ...@@ -19,6 +19,10 @@ class ListDatabasesTest extends TestCase
{ {
$options = []; $options = [];
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['filter' => $value];
}
foreach ($this->getInvalidIntegerValues() as $value) { foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = ['maxTimeMS' => $value]; $options[][] = ['maxTimeMS' => $value];
} }
......
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