Hazelcast C++ Client
Hazelcast C++ Client Library
predicates.h
1 /*
2  * Copyright (c) 2008-2022, 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 const char * const KEY_ATTRIBUTE_NAME;
32  static const char * const THIS_ATTRIBUTE_NAME;
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  template<typename T>
69  multi_predicate(const std::string attribute_name, hazelcast_client &client, const std::vector<T> &values) : base_predicate(client) {
70  out_stream.write(attribute_name);
71  out_stream.write<int32_t>(static_cast<int32_t>(values.size()));
72  for (const T &value : values) {
73  out_stream.write_object(value);
74  }
75  }
76  };
77 
78  enum struct predicate_data_serializer_hook {
79  F_ID = -20,
80 
81  SQL_PREDICATE = 0,
82 
83  AND_PREDICATE = 1,
84 
85  BETWEEN_PREDICATE = 2,
86 
87  EQUAL_PREDICATE = 3,
88 
89  GREATERLESS_PREDICATE = 4,
90 
91  LIKE_PREDICATE = 5,
92 
93  ILIKE_PREDICATE = 6,
94 
95  IN_PREDICATE = 7,
96 
97  INSTANCEOF_PREDICATE = 8,
98 
99  NOTEQUAL_PREDICATE = 9,
100 
101  NOT_PREDICATE = 10,
102 
103  OR_PREDICATE = 11,
104 
105  REGEX_PREDICATE = 12,
106 
107  FALSE_PREDICATE = 13,
108 
109  TRUE_PREDICATE = 14,
110 
111  PAGING_PREDICATE = 15
112  };
113 
115  public:
120  template<typename T>
121  equal_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value)
122  : named_predicate(client, attribute_name) {
123  out_stream.write_object(value);
124  }
125  };
126 
128  public:
133  template<typename T>
134  not_equal_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value)
135  : named_predicate(client, attribute_name) {
136  out_stream.write_object(value);
137  }
138  };
139 
141  public:
148  template<typename T>
149  greater_less_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value,
150  bool is_equal, bool is_less)
151  : named_predicate(client, attribute_name) {
152  out_stream.write_object(value);
153  out_stream.write(is_equal);
154  out_stream.write(is_less);
155  }
156  };
157 
159  public:
165  template<typename FROM_TYPE, typename TO_TYPE>
166  between_predicate(hazelcast_client &client, const std::string &attribute_name, const FROM_TYPE &from,
167  const TO_TYPE &to)
168  : named_predicate(client, attribute_name) {
169  out_stream.write_object(to);
170  out_stream.write_object(from);
171  }
172  };
173 
174  class HAZELCAST_API false_predicate : public base_predicate {
175  public:
177  };
178 
179  class HAZELCAST_API true_predicate : public base_predicate {
180  public:
182  };
183 
184  class HAZELCAST_API instance_of_predicate : public base_predicate {
185  public:
189  instance_of_predicate(hazelcast_client &client, const std::string &java_class_name);
190  };
191 
192  class HAZELCAST_API sql_predicate : public base_predicate {
193  public:
199  sql_predicate(hazelcast_client &client, const std::string &sql);
200  };
201 
202  class HAZELCAST_API like_predicate : public named_predicate {
203  public:
209  like_predicate(hazelcast_client &client, const std::string &attribute, const std::string &expression);
210  };
211 
212  class HAZELCAST_API ilike_predicate : public named_predicate {
213  public:
219  ilike_predicate(hazelcast_client &client, const std::string &attribute, const std::string &expression);
220  };
221 
222  class HAZELCAST_API regex_predicate : public named_predicate {
223  public:
229  regex_predicate(hazelcast_client &client, const std::string &attribute, const std::string &expression);
230  };
231 
232  class in_predicate : public multi_predicate {
233  public:
239  template<typename ...Args>
240  in_predicate(hazelcast_client &client, const std::string &attribute_name, const Args &...values)
241  : multi_predicate(attribute_name, client, values...) {}
242 
248  template<typename T>
249  in_predicate(hazelcast_client &client, const std::string &attribute_name, const std::vector<T> &values)
250  : multi_predicate(attribute_name, client, values) {}
251  };
252 
254  public:
255  template<typename ...Args>
256  and_predicate(hazelcast_client &client, const Args &...values) : multi_predicate(client, values...) {}
257  };
258 
259  class or_predicate : public multi_predicate {
260  public:
261  template<typename ...PredicateTypes>
262  or_predicate(hazelcast_client &client, const PredicateTypes &...values) : multi_predicate(client, values...) {}
263  };
264 
265  class not_predicate : public base_predicate {
266  public:
267  template<typename T>
268  not_predicate(hazelcast_client &client, const T &predicate) : base_predicate(client) {
269  out_stream.write_object(predicate);
270  }
271  };
272  }
273 
274  namespace serialization {
275  template<typename T>
280  static constexpr int32_t get_factory_id() noexcept {
281  return static_cast<int32_t>(query::predicate_data_serializer_hook::F_ID);
282  }
283 
288  static void write_data(const T &object, object_data_output &out) {
289  out.append_bytes(object.out_stream.to_byte_array());
290  }
291 
295  static T read_data(object_data_input &in) {
296  // Not need to read at the client side
297  BOOST_THROW_EXCEPTION(exception::hazelcast_serialization("readData",
298  "Client should not need to use readdata method!!!"));
299  }
300  };
301 
302  template<>
303  struct hz_serializer<query::between_predicate> : public BasePredicateSerializer<query::between_predicate> {
307  static constexpr int32_t get_class_id() noexcept {
308  return static_cast<int32_t>(query::predicate_data_serializer_hook::BETWEEN_PREDICATE);
309  }
310  };
311 
312  template<>
313  struct hz_serializer<query::equal_predicate> : public BasePredicateSerializer<query::equal_predicate> {
317  static constexpr int32_t get_class_id() noexcept {
318  return static_cast<int32_t>(query::predicate_data_serializer_hook::EQUAL_PREDICATE);
319  }
320  };
321 
322  template<>
323  struct hz_serializer<query::not_equal_predicate> : public BasePredicateSerializer<query::not_equal_predicate> {
327  static constexpr int32_t get_class_id() noexcept {
328  return static_cast<int32_t>(query::predicate_data_serializer_hook::NOTEQUAL_PREDICATE);
329  }
330  };
331 
332  template<>
333  struct hz_serializer<query::greater_less_predicate>
334  : public BasePredicateSerializer<query::greater_less_predicate> {
338  static constexpr int32_t get_class_id() noexcept {
339  return static_cast<int32_t>(query::predicate_data_serializer_hook::GREATERLESS_PREDICATE);
340  }
341  };
342 
343  template<>
344  struct hz_serializer<query::false_predicate> : public BasePredicateSerializer<query::false_predicate> {
348  static constexpr int32_t get_class_id() noexcept {
349  return static_cast<int32_t>(query::predicate_data_serializer_hook::FALSE_PREDICATE);
350  }
351  };
352 
353  template<>
354  struct hz_serializer<query::true_predicate> : public BasePredicateSerializer<query::true_predicate> {
358  static constexpr int32_t get_class_id() noexcept {
359  return static_cast<int32_t>(query::predicate_data_serializer_hook::TRUE_PREDICATE);
360  }
361  };
362 
363  template<>
364  struct hz_serializer<query::like_predicate> : public BasePredicateSerializer<query::like_predicate> {
368  static constexpr int32_t get_class_id() noexcept {
369  return static_cast<int32_t>(query::predicate_data_serializer_hook::LIKE_PREDICATE);
370  }
371  };
372 
373  template<>
374  struct hz_serializer<query::instance_of_predicate>
375  : public BasePredicateSerializer<query::instance_of_predicate> {
379  static constexpr int32_t get_class_id() noexcept {
380  return static_cast<int32_t>(query::predicate_data_serializer_hook::INSTANCEOF_PREDICATE);
381  }
382  };
383 
384  template<>
385  struct hz_serializer<query::sql_predicate>
386  : public BasePredicateSerializer<query::sql_predicate> {
390  static constexpr int32_t get_class_id() noexcept {
391  return static_cast<int32_t>(query::predicate_data_serializer_hook::SQL_PREDICATE);
392  }
393  };
394 
395  template<>
396  struct hz_serializer<query::ilike_predicate> : public BasePredicateSerializer<query::ilike_predicate> {
400  static constexpr int32_t get_class_id() noexcept {
401  return static_cast<int32_t>(query::predicate_data_serializer_hook::ILIKE_PREDICATE);
402  }
403  };
404 
405  template<>
406  struct hz_serializer<query::regex_predicate> : public BasePredicateSerializer<query::regex_predicate> {
410  static constexpr int32_t get_class_id() noexcept {
411  return static_cast<int32_t>(query::predicate_data_serializer_hook::REGEX_PREDICATE);
412  }
413  };
414 
415  template<>
416  struct hz_serializer<query::in_predicate> : public BasePredicateSerializer<query::in_predicate> {
420  static constexpr int32_t get_class_id() noexcept {
421  return static_cast<int32_t>(query::predicate_data_serializer_hook::IN_PREDICATE);
422  }
423  };
424 
425  template<>
426  struct hz_serializer<query::and_predicate> : public BasePredicateSerializer<query::and_predicate> {
430  static constexpr int32_t get_class_id() noexcept {
431  return static_cast<int32_t>(query::predicate_data_serializer_hook::AND_PREDICATE);
432  }
433  };
434 
435  template<>
436  struct hz_serializer<query::or_predicate> : public BasePredicateSerializer<query::or_predicate> {
440  static constexpr int32_t get_class_id() noexcept {
441  return static_cast<int32_t>(query::predicate_data_serializer_hook::OR_PREDICATE);
442  }
443  };
444 
445  template<>
446  struct hz_serializer<query::not_predicate> : public BasePredicateSerializer<query::not_predicate> {
450  static constexpr int32_t get_class_id() noexcept {
451  return static_cast<int32_t>(query::predicate_data_serializer_hook::NOT_PREDICATE);
452  }
453  };
454  };
455  }
456 }
457 
458 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
459 #pragma warning(pop)
460 #endif
between_predicate(hazelcast_client &client, const std::string &attribute_name, const FROM_TYPE &from, const TO_TYPE &to)
Definition: predicates.h:166
equal_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value)
Definition: predicates.h:121
greater_less_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value, bool is_equal, bool is_less)
Definition: predicates.h:149
in_predicate(hazelcast_client &client, const std::string &attribute_name, const std::vector< T > &values)
The type of Args should be able to be serialized.
Definition: predicates.h:249
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:240
not_equal_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value)
Definition: predicates.h:134
This is a marker class for Predicate classes.
Definition: predicates.h:39
static constexpr int32_t get_factory_id() noexcept
Definition: predicates.h:280
static void write_data(const T &object, object_data_output &out)
Defines how this class will be written.
Definition: predicates.h:288
static T read_data(object_data_input &in)
Should not be called at the client side!
Definition: predicates.h:295
Classes derived from this class should implement the following static methods: static int32_t getClas...