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 {
32  static const char* const KEY_ATTRIBUTE_NAME;
33  static const char* const THIS_ATTRIBUTE_NAME;
34 };
35 
41 class HAZELCAST_API predicate
42 {};
43 
44 struct HAZELCAST_API base_predicate : public predicate
45 {
46  explicit base_predicate(hazelcast_client& client);
47 
49 };
50 
51 class HAZELCAST_API named_predicate : public base_predicate
52 {
53 protected:
54  explicit named_predicate(hazelcast_client& client,
55  const std::string& attribute_name);
56 };
57 
59 {
60 public:
61  template<typename... Args>
62  multi_predicate(hazelcast_client& client, const Args&... values)
63  : base_predicate(client)
64  {
65  out_stream.write<int32_t>(static_cast<int32_t>(sizeof...(values)));
66  out_stream.write_objects(values...);
67  }
68 
69  template<typename... Args>
70  multi_predicate(const std::string attribute_name,
71  hazelcast_client& client,
72  const Args&... values)
73  : base_predicate(client)
74  {
75  out_stream.write(attribute_name);
76  out_stream.write<int32_t>(static_cast<int32_t>(sizeof...(values)));
77  out_stream.write_objects(values...);
78  }
79 
80  template<typename T>
81  multi_predicate(const std::string attribute_name,
82  hazelcast_client& client,
83  const std::vector<T>& values)
84  : base_predicate(client)
85  {
86  out_stream.write(attribute_name);
87  out_stream.write<int32_t>(static_cast<int32_t>(values.size()));
88  for (const T& value : values) {
89  out_stream.write_object(value);
90  }
91  }
92 };
93 
94 enum struct predicate_data_serializer_hook
95 {
96  F_ID = -20,
97 
98  SQL_PREDICATE = 0,
99 
100  AND_PREDICATE = 1,
101 
102  BETWEEN_PREDICATE = 2,
103 
104  EQUAL_PREDICATE = 3,
105 
106  GREATERLESS_PREDICATE = 4,
107 
108  LIKE_PREDICATE = 5,
109 
110  ILIKE_PREDICATE = 6,
111 
112  IN_PREDICATE = 7,
113 
114  INSTANCEOF_PREDICATE = 8,
115 
116  NOTEQUAL_PREDICATE = 9,
117 
118  NOT_PREDICATE = 10,
119 
120  OR_PREDICATE = 11,
121 
122  REGEX_PREDICATE = 12,
123 
124  FALSE_PREDICATE = 13,
125 
126  TRUE_PREDICATE = 14,
127 
128  PAGING_PREDICATE = 15
129 };
130 
132 {
133 public:
138  template<typename T>
140  const std::string& attribute_name,
141  const T& value)
142  : named_predicate(client, attribute_name)
143  {
144  out_stream.write_object(value);
145  }
146 };
147 
149 {
150 public:
155  template<typename T>
157  const std::string& attribute_name,
158  const T& value)
159  : named_predicate(client, attribute_name)
160  {
161  out_stream.write_object(value);
162  }
163 };
164 
166 {
167 public:
175  template<typename T>
177  const std::string& attribute_name,
178  const T& value,
179  bool is_equal,
180  bool is_less)
181  : named_predicate(client, attribute_name)
182  {
183  out_stream.write_object(value);
184  out_stream.write(is_equal);
185  out_stream.write(is_less);
186  }
187 };
188 
190 {
191 public:
197  template<typename FROM_TYPE, typename TO_TYPE>
199  const std::string& attribute_name,
200  const FROM_TYPE& from,
201  const TO_TYPE& to)
202  : named_predicate(client, attribute_name)
203  {
204  out_stream.write_object(to);
205  out_stream.write_object(from);
206  }
207 };
208 
209 class HAZELCAST_API false_predicate : public base_predicate
210 {
211 public:
213 };
214 
215 class HAZELCAST_API true_predicate : public base_predicate
216 {
217 public:
219 };
220 
221 class HAZELCAST_API instance_of_predicate : public base_predicate
222 {
223 public:
229  const std::string& java_class_name);
230 };
231 
232 class HAZELCAST_API sql_predicate : public base_predicate
233 {
234 public:
240  sql_predicate(hazelcast_client& client, const std::string& sql);
241 };
242 
243 class HAZELCAST_API like_predicate : public named_predicate
244 {
245 public:
252  const std::string& attribute,
253  const std::string& expression);
254 };
255 
256 class HAZELCAST_API ilike_predicate : public named_predicate
257 {
258 public:
265  const std::string& attribute,
266  const std::string& expression);
267 };
268 
269 class HAZELCAST_API regex_predicate : public named_predicate
270 {
271 public:
278  const std::string& attribute,
279  const std::string& expression);
280 };
281 
283 {
284 public:
290  template<typename... Args>
292  const std::string& attribute_name,
293  const Args&... values)
294  : multi_predicate(attribute_name, client, values...)
295  {}
296 
302  template<typename T>
304  const std::string& attribute_name,
305  const std::vector<T>& values)
306  : multi_predicate(attribute_name, client, values)
307  {}
308 };
309 
311 {
312 public:
313  template<typename... Args>
314  and_predicate(hazelcast_client& client, const Args&... values)
315  : multi_predicate(client, values...)
316  {}
317 };
318 
320 {
321 public:
322  template<typename... PredicateTypes>
323  or_predicate(hazelcast_client& client, const PredicateTypes&... values)
324  : multi_predicate(client, values...)
325  {}
326 };
327 
329 {
330 public:
331  template<typename T>
332  not_predicate(hazelcast_client& client, const T& predicate)
333  : base_predicate(client)
334  {
335  out_stream.write_object(predicate);
336  }
337 };
338 } // namespace query
339 
340 namespace serialization {
341 template<typename T>
343 {
347  static constexpr int32_t get_factory_id() noexcept
348  {
349  return static_cast<int32_t>(
350  query::predicate_data_serializer_hook::F_ID);
351  }
352 
357  static void write_data(const T& object, object_data_output& out)
358  {
359  out.append_bytes(object.out_stream.to_byte_array());
360  }
361 
366  {
367  // Not need to read at the client side
368  BOOST_THROW_EXCEPTION(exception::hazelcast_serialization(
369  "readData", "Client should not need to use readdata method!!!"));
370  }
371 };
372 
373 template<>
374 struct hz_serializer<query::between_predicate>
375  : public BasePredicateSerializer<query::between_predicate>
376 {
380  static constexpr int32_t get_class_id() noexcept
381  {
382  return static_cast<int32_t>(
383  query::predicate_data_serializer_hook::BETWEEN_PREDICATE);
384  }
385 };
386 
387 template<>
388 struct hz_serializer<query::equal_predicate>
389  : public BasePredicateSerializer<query::equal_predicate>
390 {
394  static constexpr int32_t get_class_id() noexcept
395  {
396  return static_cast<int32_t>(
397  query::predicate_data_serializer_hook::EQUAL_PREDICATE);
398  }
399 };
400 
401 template<>
402 struct hz_serializer<query::not_equal_predicate>
403  : public BasePredicateSerializer<query::not_equal_predicate>
404 {
408  static constexpr int32_t get_class_id() noexcept
409  {
410  return static_cast<int32_t>(
411  query::predicate_data_serializer_hook::NOTEQUAL_PREDICATE);
412  }
413 };
414 
415 template<>
416 struct hz_serializer<query::greater_less_predicate>
417  : public BasePredicateSerializer<query::greater_less_predicate>
418 {
422  static constexpr int32_t get_class_id() noexcept
423  {
424  return static_cast<int32_t>(
425  query::predicate_data_serializer_hook::GREATERLESS_PREDICATE);
426  }
427 };
428 
429 template<>
430 struct hz_serializer<query::false_predicate>
431  : public BasePredicateSerializer<query::false_predicate>
432 {
436  static constexpr int32_t get_class_id() noexcept
437  {
438  return static_cast<int32_t>(
439  query::predicate_data_serializer_hook::FALSE_PREDICATE);
440  }
441 };
442 
443 template<>
444 struct hz_serializer<query::true_predicate>
445  : public BasePredicateSerializer<query::true_predicate>
446 {
450  static constexpr int32_t get_class_id() noexcept
451  {
452  return static_cast<int32_t>(
453  query::predicate_data_serializer_hook::TRUE_PREDICATE);
454  }
455 };
456 
457 template<>
458 struct hz_serializer<query::like_predicate>
459  : public BasePredicateSerializer<query::like_predicate>
460 {
464  static constexpr int32_t get_class_id() noexcept
465  {
466  return static_cast<int32_t>(
467  query::predicate_data_serializer_hook::LIKE_PREDICATE);
468  }
469 };
470 
471 template<>
472 struct hz_serializer<query::instance_of_predicate>
473  : public BasePredicateSerializer<query::instance_of_predicate>
474 {
478  static constexpr int32_t get_class_id() noexcept
479  {
480  return static_cast<int32_t>(
481  query::predicate_data_serializer_hook::INSTANCEOF_PREDICATE);
482  }
483 };
484 
485 template<>
486 struct hz_serializer<query::sql_predicate>
487  : public BasePredicateSerializer<query::sql_predicate>
488 {
492  static constexpr int32_t get_class_id() noexcept
493  {
494  return static_cast<int32_t>(
495  query::predicate_data_serializer_hook::SQL_PREDICATE);
496  }
497 };
498 
499 template<>
500 struct hz_serializer<query::ilike_predicate>
501  : public BasePredicateSerializer<query::ilike_predicate>
502 {
506  static constexpr int32_t get_class_id() noexcept
507  {
508  return static_cast<int32_t>(
509  query::predicate_data_serializer_hook::ILIKE_PREDICATE);
510  }
511 };
512 
513 template<>
514 struct hz_serializer<query::regex_predicate>
515  : public BasePredicateSerializer<query::regex_predicate>
516 {
520  static constexpr int32_t get_class_id() noexcept
521  {
522  return static_cast<int32_t>(
523  query::predicate_data_serializer_hook::REGEX_PREDICATE);
524  }
525 };
526 
527 template<>
528 struct hz_serializer<query::in_predicate>
529  : public BasePredicateSerializer<query::in_predicate>
530 {
534  static constexpr int32_t get_class_id() noexcept
535  {
536  return static_cast<int32_t>(
537  query::predicate_data_serializer_hook::IN_PREDICATE);
538  }
539 };
540 
541 template<>
542 struct hz_serializer<query::and_predicate>
543  : public BasePredicateSerializer<query::and_predicate>
544 {
548  static constexpr int32_t get_class_id() noexcept
549  {
550  return static_cast<int32_t>(
551  query::predicate_data_serializer_hook::AND_PREDICATE);
552  }
553 };
554 
555 template<>
556 struct hz_serializer<query::or_predicate>
557  : public BasePredicateSerializer<query::or_predicate>
558 {
562  static constexpr int32_t get_class_id() noexcept
563  {
564  return static_cast<int32_t>(
565  query::predicate_data_serializer_hook::OR_PREDICATE);
566  }
567 };
568 
569 template<>
570 struct hz_serializer<query::not_predicate>
571  : public BasePredicateSerializer<query::not_predicate>
572 {
576  static constexpr int32_t get_class_id() noexcept
577  {
578  return static_cast<int32_t>(
579  query::predicate_data_serializer_hook::NOT_PREDICATE);
580  }
581 };
582 }; // namespace serialization
583 } // namespace client
584 } // namespace hazelcast
585 
586 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
587 #pragma warning(pop)
588 #endif
between_predicate(hazelcast_client &client, const std::string &attribute_name, const FROM_TYPE &from, const TO_TYPE &to)
Definition: predicates.h:198
equal_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value)
Definition: predicates.h:139
greater_less_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value, bool is_equal, bool is_less)
Definition: predicates.h:176
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:303
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:291
not_equal_predicate(hazelcast_client &client, const std::string &attribute_name, const T &value)
Definition: predicates.h:156
This is a marker class for Predicate classes.
Definition: predicates.h:42
static constexpr int32_t get_factory_id() noexcept
Definition: predicates.h:347
static void write_data(const T &object, object_data_output &out)
Defines how this class will be written.
Definition: predicates.h:357
static T read_data(object_data_input &in)
Should not be called at the client side!
Definition: predicates.h:365
Classes derived from this class should implement the following static methods: static int32_t get_cla...