• 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     DataQuery();
32 
33     ~DataQuery() = default;
34 
35     // Reset the query.
36     // Return:
37     //     This Query.
38     DataQuery &Reset();
39 
40     // Equal to int value.
41     // Parameters:
42     //     field: the field name.
43     //     value: the field value.
44     // Return:
45     //     This Query.
46     DataQuery &EqualTo(const std::string &field, const int value);
47 
48     // Equal to long value.
49     // Parameters:
50     //     field: the field name.
51     //     value: the field value.
52     // Return:
53     //     This Query.
54     DataQuery &EqualTo(const std::string &field, const int64_t value);
55 
56     // Equal to double value.
57     // Parameters:
58     //     field: the field name.
59     //     value: the field value.
60     // Return:
61     //     This Query.
62     DataQuery &EqualTo(const std::string &field, const double value);
63 
64     // Equal to String value.
65     // Parameters:
66     //     field: the field name.
67     //     value: the field value.
68     // Return:
69     //     This Query.
70     DataQuery &EqualTo(const std::string &field, const std::string &value);
71 
72     // Equal to boolean value.
73     // Parameters:
74     //     field: the field name.
75     //     value: the field value.
76     // Return:
77     //     This Query.
78     DataQuery &EqualTo(const std::string &field, const bool value);
79 
80     // Not equal to int value.
81     // Parameters:
82     //     field: the field name.
83     //     value: the field value.
84     // Return:
85     //     This Query.
86     DataQuery &NotEqualTo(const std::string &field, const int value);
87 
88     // Not equal to long value.
89     // Parameters:
90     //     field: the field name.
91     //     value: the field value.
92     // Return:
93     //     This Query.
94     DataQuery &NotEqualTo(const std::string &field, const int64_t value);
95 
96     // Not equal to double value.
97     // Parameters:
98     //     field: the field name.
99     //     value: the field value.
100     // Return:
101     //     This Query.
102     DataQuery &NotEqualTo(const std::string &field, const double value);
103 
104     // Not equal to String value.
105     // Parameters:
106     //     field: the field name.
107     //     value: the field value.
108     // Return:
109     //     This Query.
110     DataQuery &NotEqualTo(const std::string &field, const std::string &value);
111 
112     // Not equal to boolean value.
113     // Parameters:
114     //     field: the field name.
115     //     value: the field value.
116     // Return:
117     //     This Query.
118     DataQuery &NotEqualTo(const std::string &field, const bool value);
119 
120     // Greater than int value.
121     // Parameters:
122     //     field: the field name.
123     //     value: the field value.
124     // Return:
125     //     This Query.
126     DataQuery &GreaterThan(const std::string &field, const int value);
127 
128     // Greater than long value.
129     // Parameters:
130     //     field: the field name.
131     //     value: the field value.
132     // Return:
133     //     This Query.
134     DataQuery &GreaterThan(const std::string &field, const int64_t value);
135 
136     // Greater than double value.
137     // Parameters:
138     //     field: the field name.
139     //     value: the field value.
140     // Return:
141     //     This Query.
142     DataQuery &GreaterThan(const std::string &field, const double value);
143 
144     // Greater than String value.
145     // Parameters:
146     //     field: the field name.
147     //     value: the field value.
148     // Return:
149     //     This Query.
150     DataQuery &GreaterThan(const std::string &field, const std::string &value);
151 
152     // Less than int value.
153     // Parameters:
154     //     field: the field name.
155     //     value: the field value.
156     // Return:
157     //     This Query.
158     DataQuery &LessThan(const std::string &field, const int value);
159 
160     // Less than long value.
161     // Parameters:
162     //     field: the field name.
163     //     value: the field value.
164     // Return:
165     //     This Query.
166     DataQuery &LessThan(const std::string &field, const int64_t value);
167 
168     // Less than double value.
169     // Parameters:
170     //     field: the field name.
171     //     value: the field value.
172     // Return:
173     //     This Query.
174     DataQuery &LessThan(const std::string &field, const double value);
175 
176     // Less than String value.
177     // Parameters:
178     //     field: the field name.
179     //     value: the field value.
180     // Return:
181     //     This Query.
182     DataQuery &LessThan(const std::string &field, const std::string &value);
183 
184     // Greater than or equal to int value.
185     // Parameters:
186     //     field: the field name.
187     //     value: the field value.
188     // Return:
189     //     This Query.
190     DataQuery &GreaterThanOrEqualTo(const std::string &field, const int value);
191 
192     // Greater than or equal to long value.
193     // Parameters:
194     //     field: the field name.
195     //     value: the field value.
196     // Return:
197     //     This Query.
198     DataQuery &GreaterThanOrEqualTo(const std::string &field, const int64_t value);
199 
200     // Greater than or equal to double value.
201     // Parameters:
202     //     field: the field name.
203     //     value: the field value.
204     // Return:
205     //     This Query.
206     DataQuery &GreaterThanOrEqualTo(const std::string &field, const double value);
207 
208     // Greater than or equal to String value.
209     // Parameters:
210     //     field: the field name.
211     //     value: the field value.
212     // Return:
213     //     This Query.
214     DataQuery &GreaterThanOrEqualTo(const std::string &field, const std::string &value);
215 
216     // Less than or equal to int value.
217     // Parameters:
218     //     field: the field name.
219     //     value: the field value.
220     // Return:
221     //     This Query.
222     DataQuery &LessThanOrEqualTo(const std::string &field, const int value);
223 
224     // Less than or equal to long value.
225     // Parameters:
226     //     field: the field name.
227     //     value: the field value.
228     // Return:
229     //     This Query.
230     DataQuery &LessThanOrEqualTo(const std::string &field, const int64_t value);
231 
232     // Less than or equal to double value.
233     // Parameters:
234     //     field: the field name.
235     //     value: the field value.
236     // Return:
237     //     This Query.
238     DataQuery &LessThanOrEqualTo(const std::string &field, const double value);
239 
240     // Less than or equal to String value.
241     // Parameters:
242     //     field: the field name.
243     //     value: the field value.
244     // Return:
245     //     This Query.
246     DataQuery &LessThanOrEqualTo(const std::string &field, const std::string &value);
247 
248     // Is null field value.
249     // Parameters:
250     //     field: the field name.
251     // Return:
252     //     This Query.
253     DataQuery &IsNull(const std::string &field);
254 
255     // Is not null field value.
256     // Parameters:
257     //     field: the field name.
258     // Return:
259     //     This Query.
260     DataQuery &IsNotNull(const std::string &field);
261 
262     // In int value list.
263     // Parameters:
264     //     field: the field name.
265     //     value: the field value list.
266     // Return:
267     //     This Query.
268     DataQuery &In(const std::string &field, const std::vector<int> &valueList);
269 
270     // In long value list.
271     // Parameters:
272     //     field: the field name.
273     //     value: the field value list.
274     // Return:
275     //     This Query.
276     DataQuery &In(const std::string &field, const std::vector<int64_t> &valueList);
277 
278     // In Double value list.
279     // Parameters:
280     //     field: the field name.
281     //     value: the field value list.
282     // Return:
283     //     This Query.
284     DataQuery &In(const std::string &field, const std::vector<double> &valueList);
285 
286     // In String value list.
287     // Parameters:
288     //     field: the field name.
289     //     value: the field value list.
290     // Return:
291     //     This Query.
292     DataQuery &In(const std::string &field, const std::vector<std::string> &valueList);
293 
294     // Not in int value list.
295     // Parameters:
296     //     field: the field name.
297     //     value: the field value list.
298     // Return:
299     //     This Query.
300     DataQuery &NotIn(const std::string &field, const std::vector<int> &valueList);
301 
302     // Not in long value list.
303     // Parameters:
304     //     field: the field name.
305     //     value: the field value list.
306     // Return:
307     //     This Query.
308     DataQuery &NotIn(const std::string &field, const std::vector<int64_t> &valueList);
309 
310     // Not in Double value list.
311     // Parameters:
312     //     field: the field name.
313     //     value: the field value list.
314     // Return:
315     //     This Query.
316     DataQuery &NotIn(const std::string &field, const std::vector<double> &valueList);
317 
318     // Not in String value list.
319     // Parameters:
320     //     field: the field name.
321     //     value: the field value list.
322     // Return:
323     //     This Query.
324     DataQuery &NotIn(const std::string &field, const std::vector<std::string> &valueList);
325 
326     // Like String value.
327     // Parameters:
328     //     field: the field name.
329     //     value: the field value list.
330     // Return:
331     //     This Query.
332     DataQuery &Like(const std::string &field, const std::string &value);
333 
334     // Unlike String value.
335     // Parameters:
336     //     field: the field name.
337     //     value: the field value list.
338     // Return:
339     //     This Query.
340     DataQuery &Unlike(const std::string &field, const std::string &value);
341 
342     // And operator.
343     // Return:
344     //     This Query.
345     DataQuery &And();
346 
347     // Or operator.
348     // Return:
349     //     This Query.
350     DataQuery &Or();
351 
352     // Order by ascent.
353     // Parameters:
354     //     field: the field name.
355     // Return:
356     //     This Query.
357     DataQuery &OrderByAsc(const std::string &field);
358 
359     // Order by descent.
360     // Parameters:
361     //     field: the field name.
362     // Return:
363     //     This Query.
364     DataQuery &OrderByDesc(const std::string &field);
365 
366     // Order by write time.
367     // Parameters:
368     //     isAsc: isAsc.
369     // Return:
370     //     This Query.
371     DataQuery &OrderByWriteTime(bool isAsc);
372 
373     // Limit result size.
374     // Parameters:
375     //     number: the number of results.
376     //     offset: the start position.
377     // Return:
378     //     This Query.
379     DataQuery &Limit(const int number, const int offset);
380 
381     // Begin group.
382     // Return:
383     //     This Query.
384     DataQuery &BeginGroup();
385 
386     // End group.
387     // Return:
388     //     This Query.
389     DataQuery &EndGroup();
390 
391     // Select results with specified key prefix.
392     // Parameters:
393     //     prefix: key prefix.
394     // Return:
395     //     This Query.
396     DataQuery &KeyPrefix(const std::string &prefix);
397 
398     // Select results with specified device Identifier.
399     // Parameters:
400     //     deviceId: device Identifier.
401     // Return:
402     //     This Query.
403     DataQuery &DeviceId(const std::string &deviceId);
404 
405     // Select results with suggested index.
406     // Parameters:
407     //     index: suggested index.
408     // Return:
409     //     This Query.
410     DataQuery &SetSuggestIndex(const std::string &index);
411 
412     // Select results with many keys.
413     // Parameters:
414     //     keys: the vector of keys for query
415     // Return:
416     //     This Query.
417     DataQuery &InKeys(const std::vector<std::string> &keys);
418 
419     // Get string representation
420     // Return:
421     //     String representation of this query.
422     std::string ToString() const;
423 
424 private:
425 
426     friend class QueryHelper;
427     friend class DeviceConvertor;
428     friend class Convertor;
429     // equal to
430     static const char * const EQUAL_TO;
431 
432     // not equal to
433     static const char * const NOT_EQUAL_TO;
434 
435     // greater than
436     static const char * const GREATER_THAN;
437 
438     // less than
439     static const char * const LESS_THAN;
440 
441     // greater than or equal to
442     static const char * const GREATER_THAN_OR_EQUAL_TO;
443 
444     // less than or equal to
445     static const char * const LESS_THAN_OR_EQUAL_TO;
446 
447     // is null
448     static const char * const IS_NULL;
449 
450     // in
451     static const char * const IN;
452 
453     // not in
454     static const char * const NOT_IN;
455 
456     // like
457     static const char * const LIKE;
458 
459     // not like
460     static const char * const NOT_LIKE;
461 
462     // and
463     static const char * const AND;
464 
465     // or
466     static const char * const OR;
467 
468     // order by asc
469     static const char * const ORDER_BY_ASC;
470 
471     // order by desc
472     static const char * const ORDER_BY_DESC;
473 
474     // order by write time
475     static const char * const ORDER_BY_WRITE_TIME;
476 
477     // order by write time asc
478     static const char * const IS_ASC;
479 
480     // order by write time desc
481     static const char * const IS_DESC;
482 
483     // limit
484     static const char * const LIMIT;
485 
486     // space
487     static const char * const SPACE;
488 
489     // '^'
490     static const char * const SPECIAL;
491 
492     // '^' escape
493     static const char * const SPECIAL_ESCAPE;
494 
495     // space escape
496     static const char * const SPACE_ESCAPE;
497 
498     // empty string
499     static const char * const EMPTY_STRING;
500 
501     // start in
502     static const char * const START_IN;
503 
504     // end in
505     static const char * const END_IN;
506 
507     // begin group
508     static const char * const BEGIN_GROUP;
509 
510     // end group
511     static const char * const END_GROUP;
512 
513     // key prefix
514     static const char * const KEY_PREFIX;
515 
516     // device id
517     static const char * const DEVICE_ID;
518 
519     // is not null
520     static const char * const IS_NOT_NULL;
521 
522     // type string
523     static const char * const TYPE_STRING;
524 
525     // type integer
526     static const char * const TYPE_INTEGER;
527 
528     // type long
529     static const char * const TYPE_LONG;
530 
531     // type double
532     static const char * const TYPE_DOUBLE;
533 
534     // type boolean
535     static const char * const TYPE_BOOLEAN;
536 
537     // value true
538     static const char * const VALUE_TRUE;
539 
540     // value false
541     static const char * const VALUE_FALSE;
542 
543     // suggested index
544     static const char * const SUGGEST_INDEX;
545 
546     // in keys
547     static const char * const IN_KEYS;
548 
549     std::string str_;
550 
551     bool hasKeys_ = false;
552     bool hasPrefix_ = false;
553     std::shared_ptr<DistributedDB::Query> query_;
554     std::string deviceId_;
555     std::string prefix_;
556     std::vector<std::string> keys_;
557 
558     template<typename T>
559     void AppendCommon(const std::string &keyword, const std::string &fieldType, std::string &field, const T &value);
560 
561     void AppendCommonString(const std::string &keyword, const std::string &fieldType, std::string &field,
562                             std::string &value);
563 
564     void AppendCommonBoolean(const std::string &keyword, const std::string &fieldType, std::string &field,
565                              const bool &value);
566 
567     void AppendCommonString(const std::string &keyword, std::string &field, std::string &value);
568 
569     template<typename T>
570     void AppendCommonList(const std::string &keyword, const std::string &fieldType, std::string &field,
571                           const std::vector<T> &valueList);
572 
573     void AppendCommonListString(const std::string &keyword, const std::string &fieldType, std::string &field,
574                                 std::vector<std::string> &valueList);
575 
576     void EscapeSpace(std::string &input);
577 
578     bool ValidateField(const std::string &field);
579 
580     template<typename T>
581     std::string BasicToString(const T &value);
582 };
583 }  // namespace DistributedKv
584 }  // namespace OHOS
585 
586 #endif  // DISTRIBUTED_DATA_QUERY_H
587