From b90f2446d495657f912f130460ff80b09a9108d7 Mon Sep 17 00:00:00 2001 From: Chris Rienzo Date: Wed, 13 Nov 2013 11:42:30 -0500 Subject: [PATCH] mod_mongo: add support for query options --- src/mod/applications/mod_mongo/mod_mongo.cpp | 39 ++++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/mod/applications/mod_mongo/mod_mongo.cpp b/src/mod/applications/mod_mongo/mod_mongo.cpp index 8c3536def2..8bfe724a63 100644 --- a/src/mod/applications/mod_mongo/mod_mongo.cpp +++ b/src/mod/applications/mod_mongo/mod_mongo.cpp @@ -43,6 +43,33 @@ static struct { char *finalize; } globals; +static int parse_query_options(char *query_options_str) +{ + int query_options = 0; + if (strstr(query_options_str, "cursorTailable")) { + query_options |= QueryOption_CursorTailable; + } + if (strstr(query_options_str, "slaveOk")) { + query_options |= QueryOption_SlaveOk; + } + if (strstr(query_options_str, "oplogReplay")) { + query_options |= QueryOption_OplogReplay; + } + if (strstr(query_options_str, "noCursorTimeout")) { + query_options |= QueryOption_NoCursorTimeout; + } + if (strstr(query_options_str, "awaitData")) { + query_options |= QueryOption_AwaitData; + } + if (strstr(query_options_str, "exhaust")) { + query_options |= QueryOption_Exhaust; + } + if (strstr(query_options_str, "partialResults")) { + query_options |= QueryOption_PartialResults; + } + return query_options; +} + SWITCH_STANDARD_API(mongo_mapreduce_function) { switch_status_t status = SWITCH_STATUS_SUCCESS; @@ -101,11 +128,11 @@ SWITCH_STANDARD_API(mongo_mapreduce_function) return status; } - SWITCH_STANDARD_API(mongo_find_one_function) { switch_status_t status = SWITCH_STATUS_SUCCESS; - char *ns = NULL, *json_query = NULL, *json_fields = NULL; + char *ns = NULL, *json_query = NULL, *json_fields = NULL, *query_options_str = NULL; + int query_options = 0; ns = strdup(cmd); switch_assert(ns != NULL); @@ -114,6 +141,12 @@ SWITCH_STANDARD_API(mongo_find_one_function) *json_query++ = '\0'; if ((json_fields = strchr(json_query, DELIMITER))) { *json_fields++ = '\0'; + if ((query_options_str = strchr(json_fields, DELIMITER))) { + *query_options_str++ = '\0'; + if (!zstr(query_options_str)) { + query_options = parse_query_options(query_options_str); + } + } } } @@ -127,7 +160,7 @@ SWITCH_STANDARD_API(mongo_find_one_function) conn = mongo_connection_pool_get(globals.conn_pool); if (conn) { - BSONObj res = conn->findOne(ns, Query(query), &fields); + BSONObj res = conn->findOne(ns, Query(query), &fields, query_options); mongo_connection_pool_put(globals.conn_pool, conn, SWITCH_FALSE); stream->write_function(stream, "-OK\n%s\n", res.jsonString().c_str());