]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
mod_mongo: add support for query options
authorChris Rienzo <chris.rienzo@grasshopper.com>
Wed, 13 Nov 2013 16:42:30 +0000 (11:42 -0500)
committerChris Rienzo <chris.rienzo@grasshopper.com>
Wed, 13 Nov 2013 16:42:39 +0000 (11:42 -0500)
src/mod/applications/mod_mongo/mod_mongo.cpp

index 8c3536def2e9fb24353615c3725d1896a226ff92..8bfe724a632f855af183fc1d633d550bf2d4a087 100644 (file)
@@ -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());