-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathquery_spec.h
More file actions
169 lines (147 loc) · 6.13 KB
/
Copy pathquery_spec.h
File metadata and controls
169 lines (147 loc) · 6.13 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2016 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef FIREBASE_DATABASE_SRC_COMMON_QUERY_SPEC_H_
#define FIREBASE_DATABASE_SRC_COMMON_QUERY_SPEC_H_
#include "app/src/include/firebase/variant.h"
#include "app/src/optional.h"
#include "app/src/path.h"
namespace firebase {
namespace database {
class Database;
namespace internal {
// QueryParams are the set of filters and sorting options to apply to a Query.
struct QueryParams {
QueryParams() : order_by(kOrderByPriority), limit_first(0), limit_last(0) {}
// Compare two QueryParams, which are considered the same if all fields are
// the same (except only compare order_by_child if order_by is kOrderByChild).
bool operator==(const QueryParams& other) const {
return order_by == other.order_by &&
(order_by != kOrderByChild ||
order_by_child == other.order_by_child) &&
start_at_value == other.start_at_value &&
start_at_child_key == other.start_at_child_key &&
end_at_value == other.end_at_value &&
end_at_child_key == other.end_at_child_key &&
equal_to_value == other.equal_to_value &&
equal_to_child_key == other.equal_to_child_key &&
limit_first == other.limit_first && limit_last == other.limit_last;
}
template <typename T>
int OptionalCmp(const Optional<T>& a, const Optional<T>& b) const {
if (a.has_value() && b.has_value()) {
if (*a < *b) return -1;
if (*a > *b) return 1;
return 0;
}
if (!a.has_value() && b.has_value()) return -1;
if (a.has_value() && !b.has_value()) return 1;
return 0;
}
// Less-than operator, required so we can place QuerySpec instances in an
// std::map. This is an arbitrary operation, but must be consistent.
bool operator<(const QueryParams& other) const {
if (order_by < other.order_by) return true;
if (order_by > other.order_by) return false;
if (order_by == kOrderByChild && other.order_by == kOrderByChild) {
if (order_by_child < other.order_by_child) return true;
if (order_by_child > other.order_by_child) return false;
}
// clang-format off
switch (OptionalCmp(start_at_value, other.start_at_value)) {
case -1: return true;
case 1: return false;
}
switch (OptionalCmp(start_at_child_key, other.start_at_child_key)) {
case -1: return true;
case 1: return false;
}
switch (OptionalCmp(end_at_value, other.end_at_value)) {
case -1: return true;
case 1: return false;
}
switch (OptionalCmp(end_at_child_key, other.end_at_child_key)) {
case -1: return true;
case 1: return false;
}
switch (OptionalCmp(equal_to_value, other.equal_to_value)) {
case -1: return true;
case 1: return false;
}
switch (OptionalCmp(equal_to_child_key, other.equal_to_child_key)) {
case -1: return true;
case 1: return false;
}
// clang-format on
if (limit_first < other.limit_first) return true;
if (limit_first > other.limit_first) return false;
if (limit_last < other.limit_last) return true;
if (limit_last > other.limit_last) return false;
return false;
}
enum OrderBy { kOrderByPriority, kOrderByChild, kOrderByKey, kOrderByValue };
// Set by Query::OrderByPriority(), Query::OrderByChild(),
// Query::OrderByKey(), and Query::OrderByValue().
// Default is kOrderByPriority.
OrderBy order_by;
// Set by Query::OrderByChild(). Only valid if order_by is kOrderByChild.
std::string order_by_child;
// Set by Query::StartAt(). Variant::Null() if unspecified.
Optional<Variant> start_at_value;
// Set by Query::StartAt() with child specified. Blank if unspecified.
Optional<std::string> start_at_child_key;
// Set by Query::EndAt(). Variant::Null() if unspecified.
Optional<Variant> end_at_value;
// Set by Query::EndAt() with child specified. Blank if unspecified.
Optional<std::string> end_at_child_key;
// Set by Query::EqualTo(). Variant::Null() if unspecified.
Optional<Variant> equal_to_value;
// Set by Query::EqualTo() with child specified. Blank if unspecified.
Optional<std::string> equal_to_child_key;
// Set by Query::LimitToFirst(). 0 means no limit.
size_t limit_first;
// Set by Query::LimitToLast(). 0 means no limit.
size_t limit_last;
};
// Query specifier. When you add a Listener to a query, the Listener is indexed
// not by the Query itself, but by the Query's QuerySpec. This allows you to
// remove a listener from a different (but matching) Query to the original.
struct QuerySpec {
QuerySpec() : path(), params() {}
explicit QuerySpec(const Path& _path) : path(_path), params() {}
explicit QuerySpec(const QueryParams& _params) : path(), params(_params) {}
QuerySpec(const Path& _path, const QueryParams& _params)
: path(_path), params(_params) {}
// Compare two QuerySpecs, which are considered the same if all fields are the
// same.
bool operator==(const QuerySpec& other) const {
return path == other.path && params == other.params;
}
// Less-than operator, required so we can place QuerySpec instances in an
// std::map. This is an arbitrary operation, but must be consistent.
bool operator<(const QuerySpec& other) const {
if (path < other.path) return true;
if (path > other.path) return false;
if (params < other.params) return true;
return false;
}
// Full path this query refers to. Only changes when a DatabaseReference is
// created.
Path path;
// Parameters that define how a query is being filtered.
QueryParams params;
};
} // namespace internal
} // namespace database
} // namespace firebase
#endif // FIREBASE_DATABASE_SRC_COMMON_QUERY_SPEC_H_