Hazelcast C++ Client
Hazelcast C++ Client Library
predicates.h
1 /*
2  * Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include "hazelcast/client/serialization/serialization.h"
19 
20 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
21 #pragma warning(push)
22 #pragma warning(disable: 4251) //for dll export
23 #endif
24 
25 namespace hazelcast {
26  namespace client {
27  class hazelcast_client;
28 
29  namespace query {
30  struct HAZELCAST_API query_constants {
31  static constexpr const char *KEY_ATTRIBUTE_NAME = "__key";
32  static constexpr const char *THIS_ATTRIBUTE_NAME = "this";
33  };
34 
39  class HAZELCAST_API predicate {
40  };
41 
42  struct HAZELCAST_API base_predicate : public predicate {
43  explicit base_predicate(hazelcast_client &client);
44 
46  };
47 
48  class HAZELCAST_API named_predicate : public base_predicate {
49  protected:
50  explicit named_predicate(hazelcast_client &client, const std::string &attribute_name);
51  };
52 
54  public:
55  template<typename ...Args>
56  multi_predicate(hazelcast_client &client, const Args &...values) : base_predicate(client) {
57  out_stream.write<int32_t>(static_cast<int32_t>(sizeof...(values)));
58  out_stream.write_objects(values...);
59  }
60 
61  template<typename ...Args>
62  multi_predicate(const std::string attribute_name, hazelcast_client &client, const Args &...values) : base_predicate(client) {
63  out_stream.write(attribute_name);
64  out_stream.write<int32_t>(static_cast<int32_t>(sizeof...(values)));
65  out_stream.write_objects(values...);
66  }
67  };
68 
69  enum struct predicate_data_serializer_hook {
70  F_ID = -20,
71 
72  SQL_PREDICATE = 0,
73 
74  AND_PREDICATE = 1,
75 
76  BETWEEN_PREDICATE = 2,
77 
78  EQUAL_PREDICATE = 3,
79 
80  GREATERLESS_PREDICATE = 4,
81 
82  LIKE_PREDICATE = 5,
83 
84  ILIKE_PREDICATE = 6,
85 
86  IN_PREDICATE = 7,
87 
88  INSTANCEOF_PREDICATE = 8,
89 
90  NOTEQUAL_PREDICATE = 9,
91 
92  NOT_PREDICATE = 10,
93 
94  OR_PREDICATE = 11,
95 
96  REGEX_PREDICATE = 12,
97 
98  FALSE_PREDICATE = 13,
99 
100  TRUE_PREDICATE = 14,
101 
102  PAGING_PREDICATE = 15
103  };
104 
106  public:
111  template<typename T>
112  equal_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value)
113  : named_predicate(client, attribute_name) {
114  out_stream.write_object(value);
115  }
116  };
117 
119  public:
124  template<typename T>
125  not_equal_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value)
126  : named_predicate(client, attribute_name) {
127  out_stream.write_object(value);
128  }
129  };
130 
132  public:
139  template<typename T>
140  greater_less_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value,
141  bool is_equal, bool is_less)
142  : named_predicate(client, attribute_name) {
143  out_stream.write_object(value);
144  out_stream.write(is_equal);
145  out_stream.write(is_less);
146  }
147  };
148 
150  public:
156  template<typename FROM_TYPE, typename TO_TYPE>
157  between_predicate(hazelcast_client &client, const std::string &attribute_name, const FROM_TYPE &from,
158  const TO_TYPE &to)
159  : named_predicate(client, attribute_name) {
160  out_stream.write_object(to);
161  out_stream.write_object(from);
162  }
163  };
164 
165  class HAZELCAST_API false_predicate : public base_predicate {
166  public:
168  };
169 
170  class HAZELCAST_API true_predicate : public base_predicate {
171  public:
173  };
174 
175  class HAZELCAST_API instance_of_predicate : public base_predicate {
176  public:
180  instance_of_predicate(hazelcast_client &client, const std::string &java_class_name);
181  };
182 
183  class HAZELCAST_API sql_predicate : public base_predicate {
184  public:
190  sql_predicate(hazelcast_client &client, const std::string &sql);
191  };
192 
193  class HAZELCAST_API like_predicate : public named_predicate {
194  public:
200  like_predicate(hazelcast_client &client, const std::string &attribute, const std::string &expression);
201  };
202 
203  class HAZELCAST_API ilike_predicate : public named_predicate {
204  public:
210  ilike_predicate(hazelcast_client &client, const std::string &attribute, const std::string &expression);
211  };
212 
213  class HAZELCAST_API regex_predicate : public named_predicate {
214  public:
220  regex_predicate(hazelcast_client &client, const std::string &attribute, const std::string &expression);
221  };
222 
223  class in_predicate : public multi_predicate {
224  public:
230  template<typename ...Args>
231  in_predicate(hazelcast_client &client, const std::string &attribute_name, const Args &...values)
232  : multi_predicate(attribute_name, client, values...) {}
233  };
234 
236  public:
237  template<typename ...Args>
238  and_predicate(hazelcast_client &client, const Args &...values) : multi_predicate(client, values...) {}
239  };
240 
241  class or_predicate : public multi_predicate {
242  public:
243  template<typename ...PredicateTypes>
244  or_predicate(hazelcast_client &client, const PredicateTypes &...values) : multi_predicate(client, values...) {}
245  };
246 
247  class not_predicate : public base_predicate {
248  public:
249  template<typename T>
250  not_predicate(hazelcast_client &client, const T &predicate) : base_predicate(client) {
251  out_stream.write_object(predicate);
252  }
253  };
254  }
255 
256  namespace serialization {
257  template<typename T>
262  static constexpr int32_t get_factory_id() noexcept {
263  return static_cast<int32_t>(query::predicate_data_serializer_hook::F_ID);
264  }
265 
270  static void write_data(const T &object, object_data_output &out) {
271  out.append_bytes(object.out_stream.to_byte_array());
272  }
273 
277  static T read_data(object_data_input &in) {
278  // Not need to read at the client side
279  BOOST_THROW_EXCEPTION(exception::hazelcast_serialization("readData",
280  "Client should not need to use readdata method!!!"));
281  }
282  };
283 
284  template<>
285  struct hz_serializer<query::between_predicate> : public BasePredicateSerializer<query::between_predicate> {
289  static constexpr int32_t get_class_id() noexcept {
290  return static_cast<int32_t>(query::predicate_data_serializer_hook::BETWEEN_PREDICATE);
291  }
292  };
293 
294  template<>
295  struct hz_serializer<query::equal_predicate> : public BasePredicateSerializer<query::equal_predicate> {
299  static constexpr int32_t get_class_id() noexcept {
300  return static_cast<int32_t>(query::predicate_data_serializer_hook::EQUAL_PREDICATE);
301  }
302  };
303 
304  template<>
305  struct hz_serializer<query::not_equal_predicate> : public BasePredicateSerializer<query::not_equal_predicate> {
309  static constexpr int32_t get_class_id() noexcept {
310  return static_cast<int32_t>(query::predicate_data_serializer_hook::NOTEQUAL_PREDICATE);
311  }
312  };
313 
314  template<>
315  struct hz_serializer<query::greater_less_predicate>
316  : public BasePredicateSerializer<query::greater_less_predicate> {
320  static constexpr int32_t get_class_id() noexcept {
321  return static_cast<int32_t>(query::predicate_data_serializer_hook::GREATERLESS_PREDICATE);
322  }
323  };
324 
325  template<>
326  struct hz_serializer<query::false_predicate> : public BasePredicateSerializer<query::false_predicate> {
330  static constexpr int32_t get_class_id() noexcept {
331  return static_cast<int32_t>(query::predicate_data_serializer_hook::FALSE_PREDICATE);
332  }
333  };
334 
335  template<>
336  struct hz_serializer<query::true_predicate> : public BasePredicateSerializer<query::true_predicate> {
340  static constexpr int32_t get_class_id() noexcept {
341  return static_cast<int32_t>(query::predicate_data_serializer_hook::TRUE_PREDICATE);
342  }
343  };
344 
345  template<>
346  struct hz_serializer<query::like_predicate> : public BasePredicateSerializer<query::like_predicate> {
350  static constexpr int32_t get_class_id() noexcept {
351  return static_cast<int32_t>(query::predicate_data_serializer_hook::LIKE_PREDICATE);
352  }
353  };
354 
355  template<>
356  struct hz_serializer<query::instance_of_predicate>
357  : public BasePredicateSerializer<query::instance_of_predicate> {
361  static constexpr int32_t get_class_id() noexcept {
362  return static_cast<int32_t>(query::predicate_data_serializer_hook::INSTANCEOF_PREDICATE);
363  }
364  };
365 
366  template<>
367  struct hz_serializer<query::sql_predicate>
368  : public BasePredicateSerializer<query::sql_predicate> {
372  static constexpr int32_t get_class_id() noexcept {
373  return static_cast<int32_t>(query::predicate_data_serializer_hook::SQL_PREDICATE);
374  }
375  };
376 
377  template<>
378  struct hz_serializer<query::ilike_predicate> : public BasePredicateSerializer<query::ilike_predicate> {
382  static constexpr int32_t get_class_id() noexcept {
383  return static_cast<int32_t>(query::predicate_data_serializer_hook::ILIKE_PREDICATE);
384  }
385  };
386 
387  template<>
388  struct hz_serializer<query::regex_predicate> : public BasePredicateSerializer<query::regex_predicate> {
392  static constexpr int32_t get_class_id() noexcept {
393  return static_cast<int32_t>(query::predicate_data_serializer_hook::REGEX_PREDICATE);
394  }
395  };
396 
397  template<>
398  struct hz_serializer<query::in_predicate> : public BasePredicateSerializer<query::in_predicate> {
402  static constexpr int32_t get_class_id() noexcept {
403  return static_cast<int32_t>(query::predicate_data_serializer_hook::IN_PREDICATE);
404  }
405  };
406 
407  template<>
408  struct hz_serializer<query::and_predicate> : public BasePredicateSerializer<query::and_predicate> {
412  static constexpr int32_t get_class_id() noexcept {
413  return static_cast<int32_t>(query::predicate_data_serializer_hook::AND_PREDICATE);
414  }
415  };
416 
417  template<>
418  struct hz_serializer<query::or_predicate> : public BasePredicateSerializer<query::or_predicate> {
422  static constexpr int32_t get_class_id() noexcept {
423  return static_cast<int32_t>(query::predicate_data_serializer_hook::OR_PREDICATE);
424  }
425  };
426 
427  template<>
428  struct hz_serializer<query::not_predicate> : public BasePredicateSerializer<query::not_predicate> {
432  static constexpr int32_t get_class_id() noexcept {
433  return static_cast<int32_t>(query::predicate_data_serializer_hook::NOT_PREDICATE);
434  }
435  };
436  };
437  }
438 }
439 
440 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
441 #pragma warning(pop)
442 #endif
between_predicate(hazelcast_client &client, const std::string &attribute_name, const FROM_TYPE &from, const TO_TYPE &to)
Definition: predicates.h:157
equal_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value)
Definition: predicates.h:112
greater_less_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value, bool is_equal, bool is_less)
Definition: predicates.h:140
in_predicate(hazelcast_client &client, const std::string &attribute_name, const Args &...values)
The type of Args should be able to be serialized.
Definition: predicates.h:231
not_equal_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value)
Definition: predicates.h:125
This is a marker class for Predicate classes.
Definition: predicates.h:39
static constexpr int32_t get_factory_id() noexcept
Definition: predicates.h:262
static void write_data(const T &object, object_data_output &out)
Defines how this class will be written.
Definition: predicates.h:270
static T read_data(object_data_input &in)
Should not be called at the client side!
Definition: predicates.h:277
Classes derived from this class should implement the following static methods: static int32_t getClas...