static mongoc_uri_t *hippo_mongo_driver_manager_make_uri(const char *dsn, const Array options) { mongoc_uri_t *uri = mongoc_uri_new(dsn); if (!uri) { throw MongoDriver::Utils::throwInvalidArgumentException("Failed to parse MongoDB URI: '" + String(dsn) + "'"); } for (ArrayIter iter(options); iter; ++iter) { const Variant& key = iter.first(); const Variant& value = iter.second(); const char *s_key = key.toString().c_str(); if ( !strcasecmp(s_key, "journal") || !strcasecmp(s_key, "readpreference") || !strcasecmp(s_key, "readpreferencetags") || !strcasecmp(s_key, "safe") || !strcasecmp(s_key, "slaveok") || !strcasecmp(s_key, "w") || !strcasecmp(s_key, "wtimeoutms") ) { continue; } if (mongoc_uri_option_is_bool(s_key)) { mongoc_uri_set_option_as_bool(uri, s_key, value.toBoolean()); } else if (mongoc_uri_option_is_int32(s_key) && value.isInteger()) { mongoc_uri_set_option_as_int32(uri, s_key, (int32_t) value.toInt64()); } else if (mongoc_uri_option_is_utf8(s_key) && value.isString()) { mongoc_uri_set_option_as_utf8(uri, s_key, value.toString().c_str()); } else if (value.isString()) { if (!strcasecmp(s_key, "username")) { mongoc_uri_set_username(uri, value.toString().c_str()); } else if (!strcasecmp(s_key, "password")) { mongoc_uri_set_password(uri, value.toString().c_str()); } else if (!strcasecmp(s_key, "database")) { mongoc_uri_set_database(uri, value.toString().c_str()); } else if (!strcasecmp(s_key, "authsource")) { mongoc_uri_set_auth_source(uri, value.toString().c_str()); } } } return uri; }
// Return a new mongoc_uri_t which should be freed with mongoc_uri_destroy mongoc_uri_t *be_mongo_new_uri_from_options() { const char *uristr = p_stab("mongo_uri"); const char *host = p_stab("mongo_host"); const char *port = p_stab("mongo_port"); const char *user = p_stab("mongo_user"); const char *password = p_stab("mongo_password"); const char *authSource = p_stab("mongo_authSource"); mongoc_uri_t *uri; if (uristr) { // URI string trumps everything else. Let the driver parse it. uri = mongoc_uri_new(uristr); } else if (host || port || user || password || authSource) { // Using legacy piecemeal connection options. Assemble the URI. uri = mongoc_uri_new_for_host_port( host ? host : "localhost", (port && atoi(port)) ? atoi(port) : 27017 ); // NB: Option setters require mongo-c-driver >= 1.4.0 (Aug 2016) if (user != NULL) { mongoc_uri_set_username(uri, user); if (password != NULL) { mongoc_uri_set_password(uri, password); } } if (authSource != NULL) { mongoc_uri_set_auth_source(uri, authSource); } } else { // No connection options given at all, use defaults. uri = mongoc_uri_new_for_host_port("localhost", 27017); } return uri; }