PHP_METHOD(MongoDB, createCollection) { zval *data = NULL, *temp, *options = NULL; char *collection; int collection_len; zend_bool capped = 0; long size = 0, max = 0; if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &collection, &collection_len, &capped, &size, &max) == SUCCESS) { MAKE_STD_ZVAL(data); array_init(data); add_assoc_stringl(data, "create", collection, collection_len, 1); if (size) { add_assoc_long(data, "size", size); } if (capped) { php_error_docref(NULL TSRMLS_CC, MONGO_E_DEPRECATED, "This method now accepts arguments as an options array instead of the three optional arguments for capped, size and max elements"); add_assoc_bool(data, "capped", 1); if (max) { add_assoc_long(data, "max", max); } } } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &collection, &collection_len, &options) == SUCCESS) { zval *tmp_copy; /* We create a new array here, instead of just tagging "create" => * <name> at the end of the array. This is because MongoDB wants the * name of the command as first element in the array. */ MAKE_STD_ZVAL(data); array_init(data); add_assoc_stringl(data, "create", collection, collection_len, 1); if (options) { zend_hash_merge(Z_ARRVAL_P(data), Z_ARRVAL_P(options), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0); } } else { return; } MAKE_STD_ZVAL(temp); MONGO_METHOD1(MongoDB, command, temp, getThis(), data); zval_ptr_dtor(&temp); zval_ptr_dtor(&data); if (!EG(exception)) { zval *zcollection; /* get the collection we just created */ MAKE_STD_ZVAL(zcollection); ZVAL_STRINGL(zcollection, collection, collection_len, 1); MONGO_METHOD1(MongoDB, selectCollection, return_value, getThis(), zcollection); zval_ptr_dtor(&zcollection); } }
ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, int overwrite) { begin_read(source); begin_write(target); zend_hash_merge(TS_HASH(target), TS_HASH(source), pCopyConstructor, overwrite); end_write(target); end_read(source); }
PHP_METHOD(MongoDB, createCollection) { zval *cmd = NULL, *temp, *options = NULL; char *collection; int collection_len; zend_bool capped = 0; long size = 0, max = 0; mongo_db *db; if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &collection, &collection_len, &capped, &size, &max) == SUCCESS) { MAKE_STD_ZVAL(cmd); array_init(cmd); add_assoc_stringl(cmd, "create", collection, collection_len, 1); if (size) { add_assoc_long(cmd, "size", size); } if (capped) { php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "This method now accepts arguments as an options array instead of the three optional arguments for capped, size and max elements"); add_assoc_bool(cmd, "capped", 1); if (max) { add_assoc_long(cmd, "max", max); } } } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &collection, &collection_len, &options) == SUCCESS) { zval *tmp_copy; /* We create a new array here, instead of just tagging "create" => * <name> at the end of the array. This is because MongoDB wants the * name of the command as first element in the array. */ MAKE_STD_ZVAL(cmd); array_init(cmd); add_assoc_stringl(cmd, "create", collection, collection_len, 1); if (options) { zend_hash_merge(Z_ARRVAL_P(cmd), Z_ARRVAL_P(options), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0); } } else { return; } PHP_MONGO_GET_DB(getThis()); temp = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, options, 0, NULL TSRMLS_CC); zval_ptr_dtor(&cmd); if (temp) { zval_ptr_dtor(&temp); } if (!EG(exception)) { zval *zcollection; /* get the collection we just created */ zcollection = php_mongo_db_selectcollection(getThis(), collection, collection_len TSRMLS_CC); if (zcollection) { /* Only copy the zval into return_value if it worked. If * zcollection is NULL here, an exception is set */ RETURN_ZVAL(zcollection, 0, 1); } } }