summaryrefslogtreecommitdiff
path: root/src/mongo/client/query.h
blob: af85022fb0768bfe99d864fbad1c89921a8e5deb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#include "mongo/bson/json.h"
#include "mongo/client/read_preference.h"
#include "mongo/rpc/message.h"

namespace mongo {

/**
 * Represents a subset of query settings, such as sort, hint, etc. It is only used in the context of
 * the deprecated query API in 'DBClientBase', which has been superseded by `DBClientBase::find()`
 * and friends. Additional uses of this class should not be added to the code base!
 */
class Query {
public:
    static const BSONField<BSONObj> ReadPrefField;
    static const BSONField<std::string> ReadPrefModeField;
    static const BSONField<BSONArray> ReadPrefTagsField;

    /**
     * Creating a Query object from raw BSON is on its way out. Please don't add new callers under
     * any circumstances.
     */
    static Query fromBSONDeprecated(const BSONObj& b) {
        Query q;
        q.obj = b;
        return q;
    }

    Query() : obj(BSONObj()) {}

    /** Add a sort (ORDER BY) criteria to the query expression.
        @param sortPattern the sort order template.  For example to order by name ascending, time
            descending:
          { name : 1, ts : -1 }
        i.e.
          BSON( "name" << 1 << "ts" << -1 )
        or
          fromjson(" name : 1, ts : -1 ")
    */
    Query& sort(const BSONObj& sortPattern);

    /** Provide a hint to the query.
        @param keyPattern Key pattern for the index to use.
        Example:
          hint("{ts:1}")
    */
    Query& hint(BSONObj keyPattern);

    /**
     * Sets the read preference for this query.
     *
     * @param pref the read preference mode for this query.
     * @param tags the set of tags to use for this query.
     */
    Query& readPref(ReadPreference pref, const BSONArray& tags);

    BSONObj getFilter() const;

    /**
     * A temporary accessor that returns a reference to the internal BSON object. No new callers
     * should be introduced!
     * NB: must be implemented in the header because db/query/query_request cannot link against
     * client/client_query.
     */
    const BSONObj& getFullSettingsDeprecated() const {
        return obj;
    }

    /**
     * The setters below were added to make the contents of the Query's settings internal BSON
     * explicit. They will be reviewed and deprecated/removed as appropriate.
     */
    Query& appendElements(BSONObj elements);
    Query& requestResumeToken(bool enable);
    Query& resumeAfter(BSONObj point);
    Query& maxTimeMS(long long timeout);
    Query& term(long long value);
    Query& readConcern(BSONObj rc);
    Query& readOnce(bool enable);

private:
    BSONObj obj;

    /**
     * @return true if this query has an orderby, hint, or some other field
     */
    bool isComplex(bool* hasDollar = nullptr) const;
    static bool isComplex(const BSONObj& obj, bool* hasDollar = nullptr);

    void makeComplex();
    template <class T>
    void appendComplex(const char* fieldName, const T& val) {
        makeComplex();
        BSONObjBuilder b(std::move(obj));
        b.append(fieldName, val);
        obj = b.obj();
    }
};

inline std::ostream& operator<<(std::ostream& s, const Query& q) {
    return s << q.getFullSettingsDeprecated().toString();
}

}  // namespace mongo