Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit 256de29

Browse files
committed
PHP-1285: Add MongoDB::getCollectionInfo as per SPEC-54.
1 parent 387d1dc commit 256de29

File tree

4 files changed

+92
-17
lines changed

4 files changed

+92
-17
lines changed

db.c

+46-17
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ PHP_METHOD(MongoDB, dropCollection)
575575
}
576576
/* }}} */
577577

578-
void mongo_db_list_collections_command(zval *this_ptr, int include_system_collections, int full_collection_object, zval *return_value TSRMLS_DC)
578+
void mongo_db_list_collections_command(zval *this_ptr, int include_system_collections, int return_type, zval *return_value TSRMLS_DC)
579579
{
580580
zval *z_cmd, *list, **collections;
581581
mongo_db *db;
@@ -634,11 +634,20 @@ void mongo_db_list_collections_command(zval *this_ptr, int include_system_collec
634634
continue;
635635
}
636636

637-
if (full_collection_object) {
638-
c = php_mongo_db_selectcollection(this_ptr, Z_STRVAL_PP(collection_name), Z_STRLEN_PP(collection_name) TSRMLS_CC);
639-
add_next_index_zval(list, c);
640-
} else {
641-
add_next_index_string(list, Z_STRVAL_PP(collection_name), 1);
637+
switch (return_type) {
638+
case MONGO_COLLECTION_RETURN_TYPE_NAME:
639+
add_next_index_string(list, Z_STRVAL_PP(collection_name), 1);
640+
break;
641+
642+
case MONGO_COLLECTION_RETURN_TYPE_OBJECT:
643+
c = php_mongo_db_selectcollection(this_ptr, Z_STRVAL_PP(collection_name), Z_STRLEN_PP(collection_name) TSRMLS_CC);
644+
add_next_index_zval(list, c);
645+
break;
646+
647+
case MONGO_COLLECTION_RETURN_TYPE_INFO_ARRAY:
648+
Z_ADDREF_P(*collection_doc);
649+
add_assoc_zval(list, Z_STRVAL_PP(collection_name), *collection_doc);
650+
break;
642651
}
643652
}
644653
}
@@ -648,7 +657,7 @@ void mongo_db_list_collections_command(zval *this_ptr, int include_system_collec
648657
RETURN_ZVAL(list, 0, 1);
649658
}
650659

651-
void mongo_db_list_collections_legacy(zval *this_ptr, int include_system_collections, int full_collection_object, zval *return_value TSRMLS_DC)
660+
void mongo_db_list_collections_legacy(zval *this_ptr, int include_system_collections, int return_type, zval *return_value TSRMLS_DC)
652661
{
653662
zval *z_system_collection, *z_cursor, *list;
654663
mongo_cursor *cursor;
@@ -728,13 +737,22 @@ void mongo_db_list_collections_legacy(zval *this_ptr, int include_system_collect
728737
continue;
729738
}
730739

731-
if (full_collection_object) {
732-
c = php_mongo_db_selectcollection(this_ptr, name, strlen(name) TSRMLS_CC);
733-
/* No need to test for c here, as this was already covered in
734-
* system_collection above */
735-
add_next_index_zval(list, c);
736-
} else {
737-
add_next_index_string(list, name, 1);
740+
switch (return_type) {
741+
case MONGO_COLLECTION_RETURN_TYPE_NAME:
742+
add_next_index_string(list, name, 1);
743+
break;
744+
745+
case MONGO_COLLECTION_RETURN_TYPE_OBJECT:
746+
c = php_mongo_db_selectcollection(this_ptr, name, strlen(name) TSRMLS_CC);
747+
/* No need to test for c here, as this was already covered in
748+
* system_collection above */
749+
add_next_index_zval(list, c);
750+
break;
751+
752+
case MONGO_COLLECTION_RETURN_TYPE_INFO_ARRAY:
753+
Z_ADDREF_P(cursor->current);
754+
add_assoc_zval(list, name, cursor->current);
755+
break;
738756
}
739757

740758
php_mongocursor_advance(cursor TSRMLS_CC);
@@ -746,7 +764,12 @@ void mongo_db_list_collections_legacy(zval *this_ptr, int include_system_collect
746764
RETURN_ZVAL(list, 0, 1);
747765
}
748766

749-
static void php_mongo_enumerate_collections(INTERNAL_FUNCTION_PARAMETERS, int full_collection_object)
767+
/* Return types:
768+
* 0: Collection name
769+
* 1: MongoCollection object
770+
* 2: Array containing collection name and options
771+
*/
772+
static void php_mongo_enumerate_collections(INTERNAL_FUNCTION_PARAMETERS, int return_type)
750773
{
751774
zend_bool include_system_collections = 0;
752775
mongo_connection *connection;
@@ -765,9 +788,9 @@ static void php_mongo_enumerate_collections(INTERNAL_FUNCTION_PARAMETERS, int fu
765788
}
766789

767790
if (php_mongo_api_connection_min_server_version(connection, 2, 7, 5)) {
768-
mongo_db_list_collections_command(getThis(), include_system_collections, full_collection_object, return_value TSRMLS_CC);
791+
mongo_db_list_collections_command(getThis(), include_system_collections, return_type, return_value TSRMLS_CC);
769792
} else {
770-
mongo_db_list_collections_legacy(getThis(), include_system_collections, full_collection_object, return_value TSRMLS_CC);
793+
mongo_db_list_collections_legacy(getThis(), include_system_collections, return_type, return_value TSRMLS_CC);
771794
}
772795
}
773796

@@ -776,6 +799,11 @@ PHP_METHOD(MongoDB, listCollections)
776799
php_mongo_enumerate_collections(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
777800
}
778801

802+
PHP_METHOD(MongoDB, getCollectionInfo)
803+
{
804+
php_mongo_enumerate_collections(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
805+
}
806+
779807
PHP_METHOD(MongoDB, getCollectionNames)
780808
{
781809
php_mongo_enumerate_collections(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
@@ -1336,6 +1364,7 @@ static zend_function_entry MongoDB_methods[] = {
13361364
PHP_ME(MongoDB, dropCollection, arginfo_dropCollection, ZEND_ACC_PUBLIC)
13371365
PHP_ME(MongoDB, listCollections, arginfo_systemCollections, ZEND_ACC_PUBLIC)
13381366
PHP_ME(MongoDB, getCollectionNames, arginfo_systemCollections, ZEND_ACC_PUBLIC)
1367+
PHP_ME(MongoDB, getCollectionInfo, arginfo_systemCollections, ZEND_ACC_PUBLIC)
13391368
PHP_ME(MongoDB, createDBRef, arginfo_createDBRef, ZEND_ACC_PUBLIC)
13401369
PHP_ME(MongoDB, getDBRef, arginfo_getDBRef, ZEND_ACC_PUBLIC)
13411370
PHP_ME(MongoDB, execute, arginfo_execute, ZEND_ACC_PUBLIC)

db.h

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#ifndef MONGO_DB_H
1717
#define MONGO_DB_H
1818

19+
#define MONGO_COLLECTION_RETURN_TYPE_NAME 0
20+
#define MONGO_COLLECTION_RETURN_TYPE_OBJECT 1
21+
#define MONGO_COLLECTION_RETURN_TYPE_INFO_ARRAY 2
22+
1923
zend_object_value mongo_init_MongoDB_new(zend_class_entry* TSRMLS_DC);
2024

2125
/* Create a fake cursor that can be used to query the db from C. */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
MongoDB::getCollectionInfo()
3+
--SKIPIF--
4+
<?php require_once "tests/utils/standalone.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/server.inc";
8+
$a = mongo_standalone();
9+
$d = $a->selectDb(dbname());
10+
11+
$d->setProfilingLevel(MongoDB::PROFILING_OFF);
12+
$d->system->profile->drop();
13+
$d->createCollection("system.profile", array('capped' => true, 'size' => 5000));
14+
15+
$d->listcol->drop();
16+
$d->listcol->insert(array('_id' => 'test'));
17+
18+
echo "without flag\n";
19+
$collections = $d->getCollectionInfo();
20+
foreach( $collections as $name => $info )
21+
{
22+
if ($name == 'system.profile' || $name == 'listcol') {
23+
echo $name, "\n";
24+
}
25+
}
26+
27+
echo "with flag\n";
28+
$collections = $d->getCollectionInfo(true);
29+
foreach( $collections as $name => $info )
30+
{
31+
if ($name == 'system.profile' || $name == 'listcol') {
32+
echo $name, "\n";
33+
}
34+
}
35+
--EXPECT--
36+
without flag
37+
listcol
38+
with flag
39+
listcol
40+
system.profile

tests/no-servers/bug00270-arginfo.phpt

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ MongoDB
137137
0: includeSystemCollections (optional)
138138
Method getCollectionNames expects 1 parameters
139139
0: includeSystemCollections (optional)
140+
Method getCollectionInfo expects 1 parameters
141+
0: includeSystemCollections (optional)
140142
Method createDBRef expects 2 parameters
141143
0: collection_name
142144
1: array_with_id_fields_OR_MongoID

0 commit comments

Comments
 (0)