Realm C++ SDK Version v2.2.0

query.hpp

1
2//
3// Copyright 2024 Realm Inc.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
18
19#ifndef CPPREALM_BRIDGE_QUERY_HPP
20#define CPPREALM_BRIDGE_QUERY_HPP
21
22#include <cpprealm/internal/bridge/col_key.hpp>
23#include <cpprealm/internal/bridge/obj.hpp>
24#include <cpprealm/internal/bridge/utils.hpp>
25
26#include <optional>
27#include <string_view>
28
29namespace realm {
30 struct object_id;
31 struct decimal128;
32 struct uuid;
33 class LinkChain;
34 class Query;
35 class Subexpr;
36}
37namespace realm::internal::bridge {
38 struct table;
39 struct col_key;
40 struct timestamp;
41 struct binary;
42 struct object_id;
43 struct decimal128;
44 struct uuid;
45 struct mixed;
46
47 struct subexpr {
48 subexpr(subexpr&& other) = default;
49 subexpr& operator=(subexpr&& other) = default;
50 ~subexpr() = default;
51 subexpr(std::unique_ptr<Subexpr> other);
52
53 query equal(const std::optional<int64_t>& rhs) const;
54 query not_equal(const std::optional<int64_t>& rhs) const;
55 query greater(const std::optional<int64_t>& rhs) const;
56 query less(const std::optional<int64_t>& rhs) const;
57 query greater_equal(const std::optional<int64_t>& rhs) const;
58 query less_equal(const std::optional<int64_t>& rhs) const;
59
60 query equal(const std::optional<bool>& rhs) const;
61 query not_equal(const std::optional<bool>& rhs) const;
62
63 query equal(const std::optional<double>& rhs) const;
64 query not_equal(const std::optional<double>& rhs) const;
65 query greater(const std::optional<double>& rhs) const;
66 query less(const std::optional<double>& rhs) const;
67 query greater_equal(const std::optional<double>& rhs) const;
68 query less_equal(const std::optional<double>& rhs) const;
69
70 query equal(const std::optional<binary>& rhs) const;
71 query not_equal(const std::optional<binary>& rhs) const;
72
73 query equal(const std::optional<timestamp>& rhs) const;
74 query not_equal(const std::optional<timestamp>& rhs) const;
75 query greater(const std::optional<timestamp>& rhs) const;
76 query less(const std::optional<timestamp>& rhs) const;
77 query greater_equal(const std::optional<timestamp>& rhs) const;
78 query less_equal(const std::optional<timestamp>& rhs) const;
79
80 query equal(const std::optional<std::string>& rhs) const;
81 query not_equal(const std::optional<std::string>& rhs) const;
82 query contains(const std::optional<std::string>& rhs, bool case_sensitive = true) const;
83
84 query equal(const std::optional<internal::bridge::uuid>& rhs) const;
85 query not_equal(const std::optional<internal::bridge::uuid>& rhs) const;
86
87 query equal(const std::optional<internal::bridge::object_id>& rhs) const;
88 query not_equal(const std::optional<internal::bridge::object_id>& rhs) const;
89
90 query equal(const std::optional<internal::bridge::decimal128>& rhs) const;
91 query not_equal(const std::optional<internal::bridge::decimal128>& rhs) const;
92 query greater(const std::optional<internal::bridge::decimal128>& rhs) const;
93 query less(const std::optional<internal::bridge::decimal128>& rhs) const;
94 query greater_equal(const std::optional<internal::bridge::decimal128>& rhs) const;
95 query less_equal(const std::optional<internal::bridge::decimal128>& rhs) const;
96
97 query mixed_equal(const internal::bridge::mixed& rhs) const;
98 query mixed_not_equal(const internal::bridge::mixed& rhs) const;
99
100 query equal(const std::optional<obj>&) const;
101 query not_equal(const std::optional<obj>&) const;
102
103 private:
104 std::shared_ptr<Subexpr> m_subexpr;
105 };
106
107 struct link_chain {
108 link_chain();
109 link_chain(const link_chain& other) ;
110 link_chain& operator=(const link_chain& other) ;
111 link_chain(link_chain&& other);
112 link_chain& operator=(link_chain&& other);
113 ~link_chain() = default;
114 link_chain(const LinkChain& other);
115
116 link_chain& link(col_key);
117 link_chain& link(std::string col_name);
118 link_chain& backlink(const table& origin, col_key origin_col_key);
119
120 template<typename>
121 subexpr column(col_key);
122 subexpr column_mixed(col_key);
123
124 subexpr subquery(query subquery);
125 table get_table();
126
127 private:
128 std::shared_ptr<LinkChain> m_link_chain;
129 };
130
131 struct query {
132 query();
133 query(const query& other) ;
134 query& operator=(const query& other) ;
135 query(query&& other);
136 query& operator=(query&& other);
137 ~query();
138 query(const table& table); //NOLINT(google-explicit-constructor)
139 table get_table();
140 query and_query(const query&);
141 query& negate();
142
143 query(const Query&); //NOLINT(google-explicit-constructor)
144 operator Query() const; //NOLINT(google-explicit-constructor)
145
146 // Conditions: null
147 query& equal(col_key column_key, std::nullopt_t);
148 query& not_equal(col_key column_key, std::nullopt_t);
149
150 query& equal(col_key column_key, int64_t value);
151 query& not_equal(col_key column_key, int64_t value);
152 query& greater(col_key column_key, int64_t value);
153 query& greater_equal(col_key column_key, int64_t value);
154 query& less(col_key column_key, int64_t value);
155 query& less_equal(col_key column_key, int64_t value);
156 query& between(col_key column_key, int64_t from, int64_t to);
157
158 // Conditions: double
159 query& equal(col_key column_key, double value);
160 query& not_equal(col_key column_key, double value);
161 query& greater(col_key column_key, double value);
162 query& greater_equal(col_key column_key, double value);
163 query& less(col_key column_key, double value);
164 query& less_equal(col_key column_key, double value);
165 query& between(col_key column_key, double from, double to);
166
167 // Conditions: timestamp
168 query& equal(col_key column_key, timestamp value);
169 query& not_equal(col_key column_key, timestamp value);
170 query& greater(col_key column_key, timestamp value);
171 query& greater_equal(col_key column_key, timestamp value);
172 query& less_equal(col_key column_key, timestamp value);
173 query& less(col_key column_key, timestamp value);
174
175 // Conditions: UUID
176 query& equal(col_key column_key, uuid value);
177 query& not_equal(col_key column_key, uuid value);
178 query& greater(col_key column_key, uuid value);
179 query& greater_equal(col_key column_key, uuid value);
180 query& less_equal(col_key column_key, uuid value);
181 query& less(col_key column_key, uuid value);
182
183 // Conditions: ObjectId
184 query& equal(col_key column_key, object_id value);
185 query& not_equal(col_key column_key, object_id value);
186 query& greater(col_key column_key, object_id value);
187 query& greater_equal(col_key column_key, object_id value);
188 query& less_equal(col_key column_key, object_id value);
189 query& less(col_key column_key, object_id value);
190
191 // Conditions: Decimal128
192 query& equal(col_key column_key, decimal128 value);
193 query& not_equal(col_key column_key, decimal128 value);
194 query& greater(col_key column_key, decimal128 value);
195 query& greater_equal(col_key column_key, decimal128 value);
196 query& less_equal(col_key column_key, decimal128 value);
197 query& less(col_key column_key, decimal128 value);
198
199 // Conditions: string
200 query& equal(col_key column_key, std::string_view value, bool case_sensitive = true);
201 query& not_equal(col_key column_key, std::string_view value, bool case_sensitive = true);
202 query& begins_with(col_key column_key, const std::string& value, bool case_sensitive = true);
203 query& ends_with(col_key column_key, const std::string& value, bool case_sensitive = true);
204 query& contains(col_key column_key, std::string_view value, bool case_sensitive = true);
205 query& like(col_key column_key, const std::string& value, bool case_sensitive = true);
206
207 // Conditions: binary
208 query& equal(col_key column_key, binary value, bool case_sensitive = true);
209 query& not_equal(col_key column_key, binary value, bool case_sensitive = true);
210 query& begins_with(col_key column_key, binary value, bool case_sensitive = true);
211 query& ends_with(col_key column_key, binary value, bool case_sensitive = true);
212 query& contains(col_key column_key, binary value, bool case_sensitive = true);
213 query& like(col_key column_key, binary b, bool case_sensitive = true);
214
215 // Conditions: Mixed
216 query& equal(col_key column_key, mixed value, bool case_sensitive = true);
217 query& not_equal(col_key column_key, mixed value, bool case_sensitive = true);
218 query& greater(col_key column_key, mixed value);
219 query& greater_equal(col_key column_key, mixed value);
220 query& less(col_key column_key, mixed value);
221 query& less_equal(col_key column_key, mixed value);
222 query& begins_with(col_key column_key, mixed value, bool case_sensitive = true);
223 query& ends_with(col_key column_key, mixed value, bool case_sensitive = true);
224 query& contains(col_key column_key, mixed value, bool case_sensitive = true);
225 query& like(col_key column_key, mixed value, bool case_sensitive = true);
226
227 // Conditions: bool
228 query& equal(col_key column_key, bool value);
229 query& not_equal(col_key column_key, bool value);
230
231 // Conditions: links
232 query& links_to(col_key column_key, const internal::bridge::obj& o);
233 query& not_links_to(col_key column_key, const internal::bridge::obj& o);
234
235 query& dictionary_has_value_for_key_equals(col_key column_key, const std::string& key, const mixed& value);
236 query& dictionary_has_value_for_key_not_equals(col_key column_key, const std::string& key, const mixed& value);
237 query& dictionary_has_value_for_key_greater_than(col_key column_key, const std::string& key, const mixed& value);
238 query& dictionary_has_value_for_key_less_than(col_key column_key, const std::string& key, const mixed& value);
239 query& dictionary_has_value_for_key_greater_than_equals(col_key column_key, const std::string& key, const mixed& value);
240 query& dictionary_has_value_for_key_less_than_equals(col_key column_key, const std::string& key, const mixed& value);
241 query& dictionary_contains_string_for_key(col_key column_key, const std::string& key, const std::string& value);
242 query& dictionary_contains_key(col_key column_key, const std::string& key);
243 subexpr dictionary_link_subexpr(col_key column_key, col_key link_column_key, const std::string& key);
244
245 // Expressions
246 static query falsepredicate();
247
248 std::string description() const;
249 private:
250 inline Query* get_query();
251#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES
252 storage::Query m_query[1];
253#else
254 std::shared_ptr<Query> m_query;
255#endif
256
257 };
258
259 query operator || (const query& lhs, const query& rhs);
260}
261
262#endif //CPPREALM_BRIDGE_QUERY_HPP
Definition: binary.hpp:30
Definition: col_key.hpp:28
Definition: decimal128.hpp:30
Definition: mixed.hpp:69
Definition: obj.hpp:123
Definition: object_id.hpp:31
Definition: query.hpp:131
Definition: query.hpp:47
Definition: table.hpp:40
Definition: timestamp.hpp:30
Definition: uuid.hpp:32