Skip to content

PHPLIB-283: Support filter option for ListDatabases #485

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 2 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
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:
file: apiargs-common-option.yaml
ref: maxTimeMS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ type: array|object
description: |
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
operation: ~
optional: true
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/ListCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class ListCollections implements Executable
*
* * 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
* run.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Operation/ListDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class ListDatabases implements Executable
*
* 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
* run.
*
Expand All @@ -54,6 +58,10 @@ class ListDatabases implements Executable
*/
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'])) {
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
Expand All @@ -78,6 +86,10 @@ public function execute(Server $server)
{
$cmd = ['listDatabases' => 1];

if ( ! empty($this->options['filter'])) {
$cmd['filter'] = (object) $this->options['filter'];
}

if (isset($this->options['maxTimeMS'])) {
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}
Expand Down
44 changes: 44 additions & 0 deletions tests/Operation/ListDatabasesFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,56 @@

namespace MongoDB\Tests\Operation;

use MongoDB\Operation\InsertOne;
use MongoDB\Operation\ListDatabases;
use MongoDB\Tests\CommandObserver;
use stdClass;

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()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Operation/ListDatabasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function provideInvalidConstructorOptions()
{
$options = [];

foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['filter' => $value];
}

foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = ['maxTimeMS' => $value];
}
Expand Down