• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DISTRIBUTED_DATA_QUERY_H
17 #define DISTRIBUTED_DATA_QUERY_H
18 
19 #include <string>
20 #include <vector>
21 #include <sstream>
22 #include <memory>
23 #include "visibility.h"
24 namespace DistributedDB {
25 class Query;
26 }
27 namespace OHOS {
28 namespace DistributedKv {
29 class API_EXPORT DataQuery {
30 public:
31     /**
32      * @brief Constructor.
33      */
34     DataQuery();
35 
36     /**
37      * @brief Destructor.
38      */
39     ~DataQuery() = default;
40 
41     /**
42      * @brief Reset the query.
43      * @return This query.
44     */
45     DataQuery &Reset();
46 
47     /**
48      * @brief Equal to int value.
49      * @param field The field name.
50      * @param value The field value.
51      * @return This query
52     */
53     DataQuery &EqualTo(const std::string &field, const int value);
54 
55     /**
56      * @brief Equal to long value.
57      * @param field The field name.
58      * @param value The field value.
59      * @return This query
60     */
61     DataQuery &EqualTo(const std::string &field, const int64_t value);
62 
63     /**
64      * @brief Equal to double value.
65      * @param field The field name.
66      * @param value The field value.
67      * @return This query
68     */
69     DataQuery &EqualTo(const std::string &field, const double value);
70 
71     /**
72      * @brief Equal to string value.
73      * @param field The field name.
74      * @param value The field value.
75      * @return This query
76     */
77     DataQuery &EqualTo(const std::string &field, const std::string &value);
78 
79     /**
80      * @brief Equal to boolean value.
81      * @param field The field name.
82      * @param value The field value.
83      * @return This query
84     */
85     DataQuery &EqualTo(const std::string &field, const bool value);
86 
87     /**
88      * @brief Not equal to int value.
89      * @param field The field name.
90      * @param value The field value.
91      * @return This query
92     */
93     DataQuery &NotEqualTo(const std::string &field, const int value);
94 
95     /**
96      * @brief Not equal to long value.
97      * @param field The field name.
98      * @param value The field value.
99      * @return This query
100     */
101     DataQuery &NotEqualTo(const std::string &field, const int64_t value);
102 
103     /**
104      * @brief Not equal to double value.
105      * @param field The field name.
106      * @param value The field value.
107      * @return This query
108     */
109     DataQuery &NotEqualTo(const std::string &field, const double value);
110 
111     /**
112      * @brief Not equal to string value.
113      * @param field The field name.
114      * @param value The field value.
115      * @return This query
116     */
117     DataQuery &NotEqualTo(const std::string &field, const std::string &value);
118 
119     /**
120      * @brief Not equal to boolean value.
121      * @param field The field name.
122      * @param value The field value.
123      * @return This query
124     */
125     DataQuery &NotEqualTo(const std::string &field, const bool value);
126 
127     /**
128      * @brief Greater than int value.
129      * @param field The field name.
130      * @param value The field value.
131      * @return This query
132     */
133     DataQuery &GreaterThan(const std::string &field, const int value);
134 
135     /**
136      * @brief Greater than long value.
137      * @param field The field name.
138      * @param value The field value.
139      * @return This query
140     */
141     DataQuery &GreaterThan(const std::string &field, const int64_t value);
142 
143     /**
144      * @brief Greater than double value.
145      * @param field The field name.
146      * @param value The field value.
147      * @return This query
148     */
149     DataQuery &GreaterThan(const std::string &field, const double value);
150 
151     /**
152      * @brief Greater than string value.
153      * @param field The field name.
154      * @param value The field value.
155      * @return This query
156     */
157     DataQuery &GreaterThan(const std::string &field, const std::string &value);
158 
159     /**
160      * @brief Less than int value.
161      * @param field The field name.
162      * @param value The field value.
163      * @return This query
164     */
165     DataQuery &LessThan(const std::string &field, const int value);
166 
167     /**
168      * @brief Less than long value.
169      * @param field The field name.
170      * @param value The field value.
171      * @return This query
172     */
173     DataQuery &LessThan(const std::string &field, const int64_t value);
174 
175     /**
176      * @brief Less than double value.
177      * @param field The field name.
178      * @param value The field value.
179      * @return This query
180     */
181     DataQuery &LessThan(const std::string &field, const double value);
182 
183     /**
184      * @brief Less than string value.
185      * @param field The field name.
186      * @param value The field value.
187      * @return This query
188     */
189     DataQuery &LessThan(const std::string &field, const std::string &value);
190 
191     /**
192      * @brief Greater than or equal to int value.
193      * @param field The field name.
194      * @param value The field value.
195      * @return This query
196     */
197     DataQuery &GreaterThanOrEqualTo(const std::string &field, const int value);
198 
199     /**
200      * @brief Greater than or equal to long value.
201      * @param field The field name.
202      * @param value The field value.
203      * @return This query
204     */
205     DataQuery &GreaterThanOrEqualTo(const std::string &field, const int64_t value);
206 
207     /**
208      * @brief Greater than or equal to double value.
209      * @param field The field name.
210      * @param value The field value.
211      * @return This query
212     */
213     DataQuery &GreaterThanOrEqualTo(const std::string &field, const double value);
214 
215     /**
216      * @brief Greater than or equal to string value.
217      * @param field The field name.
218      * @param value The field value.
219      * @return This query
220     */
221     DataQuery &GreaterThanOrEqualTo(const std::string &field, const std::string &value);
222 
223     /**
224      * @brief Less than or equal to int value.
225      * @param field The field name.
226      * @param value The field value.
227      * @return This query
228     */
229     DataQuery &LessThanOrEqualTo(const std::string &field, const int value);
230 
231     /**
232      * @brief Less than or equal to long value.
233      * @param field The field name.
234      * @param value The field value.
235      * @return This query
236     */
237     DataQuery &LessThanOrEqualTo(const std::string &field, const int64_t value);
238 
239     /**
240      * @brief Less than or equal to double value.
241      * @param field The field name.
242      * @param value The field value.
243      * @return This query
244     */
245     DataQuery &LessThanOrEqualTo(const std::string &field, const double value);
246 
247     /**
248      * @brief Less than or equal to string value.
249      * @param field The field name.
250      * @param value The field value.
251      * @return This query
252     */
253     DataQuery &LessThanOrEqualTo(const std::string &field, const std::string &value);
254 
255     /**
256      * @brief Is null field value.
257      * @param field The field name.
258      * @return This query
259     */
260     DataQuery &IsNull(const std::string &field);
261 
262     /**
263      * @brief Is not null field value.
264      * @param field The field name.
265      * @return This query
266     */
267     DataQuery &IsNotNull(const std::string &field);
268 
269     /**
270      * @brief In int value list.
271      * @param field The field name.
272      * @param valueList The field value list.
273      * @return This query
274     */
275     DataQuery &In(const std::string &field, const std::vector<int> &valueList);
276 
277     /**
278      * @brief In long value list.
279      * @param field The field name.
280      * @param valueList The field value list.
281      * @return This query
282     */
283     DataQuery &In(const std::string &field, const std::vector<int64_t> &valueList);
284 
285     /**
286      * @brief In double value list.
287      * @param field The field name.
288      * @param valueList The field value list.
289      * @return This query
290     */
291     DataQuery &In(const std::string &field, const std::vector<double> &valueList);
292 
293     /**
294      * @brief In string value list.
295      * @param field The field name.
296      * @param valueList The field value list.
297      * @return This query
298     */
299     DataQuery &In(const std::string &field, const std::vector<std::string> &valueList);
300 
301     /**
302      * @brief Not in int value list.
303      * @param field The field name.
304      * @param valueList The field value list.
305      * @return This query
306     */
307     DataQuery &NotIn(const std::string &field, const std::vector<int> &valueList);
308 
309     /**
310      * @brief Not in long value list.
311      * @param field The field name.
312      * @param valueList The field value list.
313      * @return This query
314     */
315     DataQuery &NotIn(const std::string &field, const std::vector<int64_t> &valueList);
316 
317     /**
318      * @brief Not in double value list.
319      * @param field The field name.
320      * @param valueList The field value list.
321      * @return This query
322     */
323     DataQuery &NotIn(const std::string &field, const std::vector<double> &valueList);
324 
325     /**
326      * @brief Not in string value list.
327      * @param field The field name.
328      * @param valueList The field value list.
329      * @return This query
330     */
331     DataQuery &NotIn(const std::string &field, const std::vector<std::string> &valueList);
332 
333     /**
334      * @brief Like string value.
335      * @param field The field name.
336      * @param value The field value.
337      * @return This query
338     */
339     DataQuery &Like(const std::string &field, const std::string &value);
340 
341     /**
342      * @brief Unlike string value.
343      * @param field The field name.
344      * @param value The field value.
345      * @return This query
346     */
347     DataQuery &Unlike(const std::string &field, const std::string &value);
348 
349     /**
350      * @brief And operator.
351      * @return This query
352     */
353     DataQuery &And();
354 
355     /**
356      * @brief Or operator.
357      * @return This query
358     */
359     DataQuery &Or();
360 
361     /**
362      * @brief Order by ascent.
363      * @param field The field name.
364      * @return This query
365     */
366     DataQuery &OrderByAsc(const std::string &field);
367 
368     /**
369      * @brief Order by descent.
370      * @param field The field name.
371      * @return This query
372     */
373     DataQuery &OrderByDesc(const std::string &field);
374 
375     /**
376      * @brief Order by write time.
377      * @param isAsc Is ascent.
378      * @return This query
379     */
380     DataQuery &OrderByWriteTime(bool isAsc);
381 
382     /**
383      * @brief Limit result size.
384      * @param number The number of results.
385      * @param offset Start position.
386      * @return This query
387     */
388     DataQuery &Limit(const int number, const int offset);
389 
390     /**
391      * @brief Begin group.
392      * @return This query
393     */
394     DataQuery &BeginGroup();
395 
396     /**
397      * @brief End group.
398      * @return This query
399     */
400     DataQuery &EndGroup();
401 
402     /**
403      * @brief Select results with specified key prefix.
404      * @param prefix key prefix.
405      * @return This query
406     */
407     DataQuery &KeyPrefix(const std::string &prefix);
408 
409     /**
410      * @brief Select results with specified device Identifier.
411      * @param deviceId Device Identifier.
412      * @return This query
413     */
414     DataQuery &DeviceId(const std::string &deviceId);
415 
416     /**
417      * @brief Select results with suggested index.
418      * @param index Suggested index.
419      * @return This query
420     */
421     DataQuery &SetSuggestIndex(const std::string &index);
422 
423     /**
424      * @brief Select results with many keys.
425      * @param keys The vector of keys for query.
426      * @return This query
427     */
428     DataQuery &InKeys(const std::vector<std::string> &keys);
429 
430     /**
431      * @brief Get string representation
432      * @return String representation of this query.
433     */
434     std::string ToString() const;
435 
436 private:
437 
438     friend class QueryHelper;
439     friend class DeviceConvertor;
440     friend class Convertor;
441 
442     /**
443      * @brief equal to
444     */
445     static const char * const EQUAL_TO;
446 
447     /**
448      * @brief not equal to
449     */
450     static const char * const NOT_EQUAL_TO;
451 
452     /**
453      * @brief greater than
454     */
455     static const char * const GREATER_THAN;
456 
457     /**
458      * @brief less than
459     */
460     static const char * const LESS_THAN;
461 
462     /**
463      * @brief greater than or equal to
464     */
465     static const char * const GREATER_THAN_OR_EQUAL_TO;
466 
467     /**
468      * @brief less than or equal to
469     */
470     static const char * const LESS_THAN_OR_EQUAL_TO;
471 
472     /**
473      * @brief is null
474     */
475     static const char * const IS_NULL;
476 
477     /**
478      * @brief in
479     */
480     static const char * const IN;
481 
482     /**
483      * @brief not in
484     */
485     static const char * const NOT_IN;
486 
487     /**
488      * @brief like
489     */
490     static const char * const LIKE;
491 
492     /**
493      * @brief not like
494     */
495     static const char * const NOT_LIKE;
496 
497     /**
498      * @brief and
499     */
500     static const char * const AND;
501 
502     /**
503      * @brief or
504     */
505     static const char * const OR;
506 
507     /**
508      * @brief order by asc
509     */
510     static const char * const ORDER_BY_ASC;
511 
512     /**
513      * @brief order by desc
514     */
515     static const char * const ORDER_BY_DESC;
516 
517     /**
518      * @brief order by write time
519     */
520     static const char * const ORDER_BY_WRITE_TIME;
521 
522     /**
523      * @brief order by write time asc
524     */
525     static const char * const IS_ASC;
526 
527     /**
528      * @brief order by write time desc
529     */
530     static const char * const IS_DESC;
531 
532     /**
533      * @brief limit
534     */
535     static const char * const LIMIT;
536 
537     /**
538      * @brief space
539     */
540     static const char * const SPACE;
541 
542     /**
543      * @brief '^'
544     */
545     static const char * const SPECIAL;
546 
547     /**
548      * @brief '^' escape
549     */
550     static const char * const SPECIAL_ESCAPE;
551 
552     /**
553      * @brief space escape
554     */
555     static const char * const SPACE_ESCAPE;
556 
557     /**
558      * @brief empty string
559     */
560     static const char * const EMPTY_STRING;
561 
562     /**
563      * @brief start in
564     */
565     static const char * const START_IN;
566 
567     /**
568      * @brief end in
569     */
570     static const char * const END_IN;
571 
572     /**
573      * @brief begin group
574     */
575     static const char * const BEGIN_GROUP;
576 
577     /**
578      * @brief end group
579     */
580     static const char * const END_GROUP;
581 
582     /**
583      * @brief key prefix
584     */
585     static const char * const KEY_PREFIX;
586 
587     /**
588      * @brief device id
589     */
590     static const char * const DEVICE_ID;
591 
592     /**
593      * @brief is not null
594     */
595     static const char * const IS_NOT_NULL;
596 
597     /**
598      * @brief type string
599     */
600     static const char * const TYPE_STRING;
601 
602     /**
603      * @brief type integer
604     */
605     static const char * const TYPE_INTEGER;
606 
607     /**
608      * @brief type long
609     */
610     static const char * const TYPE_LONG;
611 
612     /**
613      * @brief type double
614     */
615     static const char * const TYPE_DOUBLE;
616 
617     /**
618      * @brief type boolean
619     */
620     static const char * const TYPE_BOOLEAN;
621 
622     /**
623      * @brief value true
624     */
625     static const char * const VALUE_TRUE;
626 
627     /**
628      * @brief value false
629     */
630     static const char * const VALUE_FALSE;
631 
632     /**
633      * @brief suggested index
634     */
635     static const char * const SUGGEST_INDEX;
636 
637     /**
638      * @brief in keys
639     */
640     static const char * const IN_KEYS;
641 
642     std::string str_;
643 
644     bool hasKeys_ = false;
645     bool hasPrefix_ = false;
646     std::shared_ptr<DistributedDB::Query> query_;
647     std::string deviceId_;
648     std::string prefix_;
649     std::vector<std::string> keys_;
650 
651     template<typename T>
652     void AppendCommon(const std::string &keyword, const std::string &fieldType, std::string &field, const T &value);
653 
654     void AppendCommonString(const std::string &keyword, const std::string &fieldType, std::string &field,
655                             std::string &value);
656 
657     void AppendCommonBoolean(const std::string &keyword, const std::string &fieldType, std::string &field,
658                              const bool &value);
659 
660     void AppendCommonString(const std::string &keyword, std::string &field, std::string &value);
661 
662     template<typename T>
663     void AppendCommonList(const std::string &keyword, const std::string &fieldType, std::string &field,
664                           const std::vector<T> &valueList);
665 
666     void AppendCommonListString(const std::string &keyword, const std::string &fieldType, std::string &field,
667                                 std::vector<std::string> &valueList);
668 
669     void EscapeSpace(std::string &input);
670 
671     bool ValidateField(const std::string &field);
672 
673     template<typename T>
674     std::string BasicToString(const T &value);
675 };
676 }  // namespace DistributedKv
677 }  // namespace OHOS
678 
679 #endif  // DISTRIBUTED_DATA_QUERY_H
680