Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
99650bb3
Commit
99650bb3
authored
Jun 16, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract Database::listCollections() to an operation class
parent
1269542c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
127 additions
and
62 deletions
+127
-62
Database.php
src/Database.php
+5
-62
ListCollections.php
src/Operation/ListCollections.php
+122
-0
No files found.
src/Database.php
View file @
99650bb3
...
@@ -12,8 +12,7 @@ use MongoDB\Driver\Server;
...
@@ -12,8 +12,7 @@ use MongoDB\Driver\Server;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Model\CollectionInfoIterator
;
use
MongoDB\Model\CollectionInfoIterator
;
use
MongoDB\Model\CollectionInfoCommandIterator
;
use
MongoDB\Operation\ListCollections
;
use
MongoDB\Model\CollectionInfoLegacyIterator
;
class
Database
class
Database
{
{
...
@@ -112,18 +111,16 @@ class Database
...
@@ -112,18 +111,16 @@ class Database
/**
/**
* Returns information for all collections in this database.
* Returns information for all collections in this database.
*
*
* @see
http://docs.mongodb.org/manual/reference/command/listCollections/
* @see
ListCollections::__construct() for supported options
* @param array $options
* @param array $options
* @return CollectionInfoIterator
* @return CollectionInfoIterator
*/
*/
public
function
listCollections
(
array
$options
=
array
())
public
function
listCollections
(
array
$options
=
array
())
{
{
$
readPreference
=
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
);
$
operation
=
new
ListCollections
(
$this
->
databaseName
,
$options
);
$server
=
$this
->
manager
->
selectServer
(
$readPreference
);
$server
=
$this
->
manager
->
selectServer
(
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
)
);
return
(
FeatureDetection
::
isSupported
(
$server
,
FeatureDetection
::
API_LISTCOLLECTIONS_CMD
))
return
$operation
->
execute
(
$server
);
?
$this
->
listCollectionsCommand
(
$server
,
$options
)
:
$this
->
listCollectionsLegacy
(
$server
,
$options
);
}
}
/**
/**
...
@@ -145,58 +142,4 @@ class Database
...
@@ -145,58 +142,4 @@ class Database
return
new
Collection
(
$this
->
manager
,
$namespace
,
$writeConcern
,
$readPreference
);
return
new
Collection
(
$this
->
manager
,
$namespace
,
$writeConcern
,
$readPreference
);
}
}
/**
* Returns information for all collections in this database using the
* listCollections command.
*
* @param Server $server
* @param array $options
* @return CollectionInfoCommandIterator
*/
private
function
listCollectionsCommand
(
Server
$server
,
array
$options
=
array
())
{
$command
=
new
Command
(
array
(
'listCollections'
=>
1
)
+
$options
);
$cursor
=
$server
->
executeCommand
(
$this
->
databaseName
,
$command
);
$cursor
->
setTypeMap
(
array
(
'document'
=>
'array'
));
return
new
CollectionInfoCommandIterator
(
$cursor
);
}
/**
* Returns information for all collections in this database by querying the
* "system.namespaces" collection (MongoDB <2.8).
*
* @param Server $server
* @param array $options
* @return CollectionInfoLegacyIterator
* @throws InvalidArgumentException if the filter option is neither an array
* nor object, or if filter.name is not a
* string.
*/
private
function
listCollectionsLegacy
(
Server
$server
,
array
$options
=
array
())
{
$filter
=
array_key_exists
(
'filter'
,
$options
)
?
$options
[
'filter'
]
:
array
();
if
(
!
is_array
(
$filter
)
&&
!
is_object
(
$filter
))
{
throw
new
InvalidArgumentException
(
sprintf
(
'Expected filter to be array or object, %s given'
,
gettype
(
$filter
)));
}
if
(
array_key_exists
(
'name'
,
(
array
)
$filter
))
{
$filter
=
(
array
)
$filter
;
if
(
!
is_string
(
$filter
[
'name'
]))
{
throw
new
InvalidArgumentException
(
sprintf
(
'Filter "name" must be a string for MongoDB <2.8, %s given'
,
gettype
(
$filter
[
'name'
])));
}
$filter
[
'name'
]
=
$this
->
databaseName
.
'.'
.
$filter
[
'name'
];
}
$namespace
=
$this
->
databaseName
.
'.system.namespaces'
;
$query
=
new
Query
(
$filter
);
$cursor
=
$server
->
executeQuery
(
$namespace
,
$query
);
$cursor
->
setTypeMap
(
array
(
'document'
=>
'array'
));
return
new
CollectionInfoLegacyIterator
(
$cursor
);
}
}
}
src/Operation/ListCollections.php
0 → 100644
View file @
99650bb3
<?php
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\Query
;
use
MongoDB\Driver\Server
;
use
MongoDB\Exception\RuntimeException
;
use
MongoDB\Model\CollectionInfoCommandIterator
;
use
MongoDB\Model\CollectionInfoIterator
;
use
MongoDB\Model\CollectionInfoLegacyIterator
;
/**
* Operation for the listCollections command.
*
* @api
* @see MongoDB\Database::listCollections()
* @see http://docs.mongodb.org/manual/reference/command/listCollections/
*/
class
ListCollections
implements
Executable
{
private
static
$wireVersionForCommand
=
3
;
private
$databaseName
;
private
$options
;
/**
* Constructs a listCollections command.
*
* Supported options:
*
* * filter (document): Query by which to filter collections.
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* @param string $databaseName Database name
* @param array $options Command options
*/
public
function
__construct
(
$databaseName
,
array
$options
=
array
())
{
if
(
isset
(
$options
[
'filter'
])
&&
!
is_array
(
$options
[
'filter'
])
&&
!
is_object
(
$options
[
'filter'
]))
{
throw
new
InvalidArgumentTypeException
(
'"filter" option'
,
$options
[
'filter'
],
'array or object'
);
}
if
(
isset
(
$options
[
'maxTimeMS'
])
&&
!
is_integer
(
$options
[
'maxTimeMS'
]))
{
throw
new
InvalidArgumentTypeException
(
'"maxTimeMS" option'
,
$options
[
'maxTimeMS'
],
'integer'
);
}
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
options
=
$options
;
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return CollectionInfoIterator
*/
public
function
execute
(
Server
$server
)
{
return
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForCommand
)
?
$this
->
executeCommand
(
$server
)
:
$this
->
executeLegacy
(
$server
);
}
/**
* Returns information for all collections in this database using the
* listCollections command.
*
* @param Server $server
* @return CollectionInfoCommandIterator
*/
private
function
executeCommand
(
Server
$server
)
{
$cmd
=
array
(
'listCollections'
=>
1
);
if
(
!
empty
(
$this
->
options
[
'filter'
]))
{
$cmd
[
'filter'
]
=
(
object
)
$this
->
options
[
'filter'
];
}
if
(
isset
(
$this
->
options
[
'maxTimeMS'
]))
{
$cmd
[
'maxTimeMS'
]
=
$this
->
options
[
'maxTimeMS'
];
}
$cursor
=
$server
->
executeCommand
(
$this
->
databaseName
,
new
Command
(
$cmd
));
$cursor
->
setTypeMap
(
array
(
'document'
=>
'array'
));
return
new
CollectionInfoCommandIterator
(
$cursor
);
}
/**
* Returns information for all collections in this database by querying the
* "system.namespaces" collection (MongoDB <3.0).
*
* @param Server $server
* @return CollectionInfoLegacyIterator
* @throws InvalidArgumentException if filter.name is not a string.
*/
private
function
executeLegacy
(
Server
$server
)
{
$filter
=
empty
(
$this
->
options
[
'filter'
])
?
array
()
:
(
array
)
$this
->
options
[
'filter'
];
if
(
array_key_exists
(
'name'
,
$filter
))
{
if
(
!
is_string
(
$filter
[
'name'
]))
{
throw
new
InvalidArgumentTypeException
(
'filter name for MongoDB <3.0'
,
$filter
[
'name'
],
'string'
);
}
$filter
[
'name'
]
=
$this
->
databaseName
.
'.'
.
$filter
[
'name'
];
}
$options
=
isset
(
$this
->
options
[
'maxTimeMS'
])
?
array
(
'modifiers'
=>
array
(
'$maxTimeMS'
=>
$this
->
options
[
'maxTimeMS'
]))
:
array
();
$cursor
=
$server
->
executeQuery
(
$this
->
databaseName
.
'.system.namespaces'
,
new
Query
(
$filter
,
$options
));
$cursor
->
setTypeMap
(
array
(
'document'
=>
'array'
));
return
new
CollectionInfoLegacyIterator
(
$cursor
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment