Esempio n. 1
0
    MongoShellResult ScriptEngine::prepareResult(const std::string &type, const std::string &output,
                                                 const std::vector<MongoDocumentPtr> &objects, qint64 elapsedms)
    {
        const char *script =
            "__robomongoQuery = false; \n"
            "__robomongoDbName = '[invalid database]'; \n"
            "__robomongoServerAddress = '[invalid connection]'; \n"
            "__robomongoCollectionName = '[invalid collection]'; \n"
            "if (typeof __robomongoLastRes == 'object' && __robomongoLastRes != null && __robomongoLastRes instanceof DBQuery) { \n"
            "    __robomongoQuery = true; \n"
            "    __robomongoDbName = __robomongoLastRes._db.getName();\n "
            "    __robomongoServerAddress = __robomongoLastRes._mongo.host; \n"
            "    __robomongoCollectionName = __robomongoLastRes._collection._shortName; \n"
            "    __robomongoQuery = __robomongoLastRes._query; \n"
            "    __robomongoFields = __robomongoLastRes._fields; \n"
            "    __robomongoLimit = __robomongoLastRes._limit; \n"
            "    __robomongoSkip = __robomongoLastRes._skip; \n"
            "    __robomongoBatchSize = __robomongoLastRes._batchSize; \n"
            "    __robomongoOptions = __robomongoLastRes._options; \n"
            "    __robomongoSpecial = __robomongoLastRes._special; \n"
            "} \n";

        _scope->exec(script, "(getresultinfo)", false, false, false);

        bool isQuery = _scope->getBoolean("__robomongoQuery");

        if (isQuery) {
            std::string serverAddress = getString("__robomongoServerAddress");
            std::string dbName = getString("__robomongoDbName");
            std::string collectionName = getString("__robomongoCollectionName");
            mongo::BSONObj query = _scope->getObject("__robomongoQuery");
            mongo::BSONObj fields = _scope->getObject("__robomongoFields");

            int limit = _scope->getNumberInt("__robomongoLimit");
            int skip = _scope->getNumberInt("__robomongoSkip");
            int batchSize = _scope->getNumberInt("__robomongoBatchSize");
            int options = _scope->getNumberInt("__robomongoOptions");

            bool special = _scope->getBoolean("__robomongoSpecial");

            MongoQueryInfo info = MongoQueryInfo(CollectionInfo(serverAddress, dbName, collectionName),
                                       query, fields, limit, skip, batchSize, options, special);
            return MongoShellResult(type, output, objects, info, elapsedms);
        }

        return MongoShellResult(type, output, objects, MongoQueryInfo(), elapsedms);
    }
Esempio n. 2
0
    MongoShellResult ScriptEngine::prepareResult(const std::string &type, const std::string &output,
                                                 const std::vector<MongoDocumentPtr> &objects, qint64 elapsedms,
                                                 const std::string &statement, AggrInfo aggrInfo /*= AggrInfo()*/)
    {
        const char *script =
            "__robomongoQuery = false; \n"
            "__robomongoIsAggregate = false; \n"
            "__robomongoDbName = '[invalid database]'; \n"
            "__robomongoServerAddress = '[invalid connection]'; \n"
            "__robomongoCollectionName = '[invalid collection]'; \n"
            "if (typeof __robomongoLastRes == 'object' && __robomongoLastRes != null \n"
            "    && __robomongoLastRes instanceof DBQuery) { \n"
            "    __robomongoQuery = true; \n"
            "    __robomongoDbName = __robomongoLastRes._db.getName();\n "
            "    __robomongoServerAddress = __robomongoLastRes._mongo.host; \n"
            "    __robomongoCollectionName = __robomongoLastRes._collection._shortName; \n"
            "    __robomongoQuery = __robomongoLastRes._query; \n"
            "    __robomongoFields = __robomongoLastRes._fields; \n"
            "    __robomongoLimit = __robomongoLastRes._limit; \n"
            "    __robomongoSkip = __robomongoLastRes._skip; \n"
            "    __robomongoBatchSize = __robomongoLastRes._batchSize; \n"
            "    __robomongoOptions = __robomongoLastRes._options; \n"
            "    __robomongoSpecial = __robomongoLastRes._special; \n"
            "} \n"
            "else if (typeof __robomongoLastRes == 'object' && __robomongoLastRes != null \n"
            "         && __robomongoLastRes instanceof DBCommandCursor \n"
            "         && __robomongoAggregateUsed) { \n"
            "    __robomongoAggregateUsed = false; \n"    
            "    __robomongoIsAggregate = true; \n"
            "    __robomongoDbName = __robomongoLastRes._db.getName();\n "
            "    __robomongoServerAddress = __robomongoLastRes._db._mongo.host; \n"
            "    __robomongoCollectionName = __robomongoLastRes._collName; \n"
            "} \n"
            ;

        _scope->exec(script, "(getresultinfo)", false, false, false);

        bool const isQuery = _scope->getBoolean("__robomongoQuery");
        bool const isAggregate = _scope->getBoolean("__robomongoIsAggregate");

        if (isQuery) {
            std::string serverAddress = getString("__robomongoServerAddress");
            std::string dbName = getString("__robomongoDbName");
            std::string collectionName = getString("__robomongoCollectionName");
               
            mongo::BSONObj query = _scope->getObject("__robomongoQuery");
            mongo::BSONObj fields = _scope->getObject("__robomongoFields");

            int limit = _scope->getNumberInt("__robomongoLimit");
            int skip = _scope->getNumberInt("__robomongoSkip");
            int batchSize = _scope->getNumberInt("__robomongoBatchSize");
            int options = _scope->getNumberInt("__robomongoOptions");

            bool special = _scope->getBoolean("__robomongoSpecial");

            MongoQueryInfo info = MongoQueryInfo(CollectionInfo(serverAddress, dbName, collectionName),
                                       query, fields, limit, skip, batchSize, options, special);
            return MongoShellResult(type, output, objects, info, elapsedms);
        }
        else if (isAggregate) {
            std::string const serverAddress = getString("__robomongoServerAddress");
            std::string const dbName = getString("__robomongoDbName");
            std::string const collectionName = getString("__robomongoCollectionName");
            mongo::BSONObj const pipeline = _scope->getObject("__robomongoAggregatePipeline");
            mongo::BSONObj const options = _scope->getObject("__robomongoAggregateOptions");

            // This query can be paging of an original aggr. query, we store the original/unpaged 
            // pipeline object here.
            mongo::BSONObj const origPipeline = aggrInfo.isValid ? aggrInfo.pipeline : pipeline;
            int const skip = aggrInfo.isValid ? aggrInfo.skip : 0;
            int const batchSize = aggrInfo.isValid ? aggrInfo.batchSize : 50;
            int const resultIndex = aggrInfo.isValid ? aggrInfo.resultIndex : -1;

            AggrInfo const newAggrInfo { collectionName, skip, batchSize, origPipeline, options, resultIndex };
            return MongoShellResult(type, output, objects, MongoQueryInfo(), elapsedms, newAggrInfo);
        }

        return MongoShellResult(type, output, objects, MongoQueryInfo(), elapsedms);
    }