• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 DATASHARE_PREDICATES_H
17 #define DATASHARE_PREDICATES_H
18 
19 #include <string>
20 
21 #include "datashare_abs_predicates.h"
22 #include "datashare_errno.h"
23 #include "datashare_predicates_object.h"
24 #include "datashare_predicates_objects.h"
25 #include "message_parcel.h"
26 
27 namespace OHOS {
28 namespace DataShare {
29 class DataSharePredicates : public DataShareAbsPredicates {
30 public:
31 
32     /**
33      * @brief Constructor.
34      */
DataSharePredicates()35     DataSharePredicates()
36     {
37     }
38 
39     /**
40      * @brief Constructor.
41      *
42      * A parameterized constructor used to create an DataSharePredicates instance.
43      *
44      * @param OperationList Indicates the operation list of the database.
45      */
DataSharePredicates(std::vector<OperationItem> operList)46     explicit DataSharePredicates(std::vector<OperationItem> operList) : operations_(std::move(operList))
47     {
48     }
49 
50     /**
51      * @brief Destructor.
52      */
~DataSharePredicates()53     ~DataSharePredicates()
54     {
55     }
56 
57     /**
58      * @brief The EqualTo of the predicate.
59      *
60      * @param field Indicates the target field.
61      * @param value Indicates the queried value.
62      */
EqualTo(const std::string & field,const SingleValue & value)63     DataSharePredicates *EqualTo(const std::string &field, const SingleValue &value)
64     {
65         SetOperationList(EQUAL_TO, field, value);
66         return this;
67     }
68 
69     /**
70      * @brief The NotEqualTo of the predicate.
71      *
72      * @param field Indicates the target field.
73      * @param value Indicates the queried value.
74      */
NotEqualTo(const std::string & field,const SingleValue & value)75     DataSharePredicates *NotEqualTo(const std::string &field, const SingleValue &value)
76     {
77         SetOperationList(NOT_EQUAL_TO, field, value);
78         return this;
79     }
80 
81     /**
82      * @brief The GreaterThan of the predicate.
83      *
84      * @param field Indicates the target field.
85      * @param value Indicates the queried value.
86      */
GreaterThan(const std::string & field,const SingleValue & value)87     DataSharePredicates *GreaterThan(const std::string &field, const SingleValue &value)
88     {
89         SetOperationList(GREATER_THAN, field, value);
90         return this;
91     }
92 
93     /**
94      * @brief The LessThan of the predicate.
95      *
96      * @param field Indicates the target field.
97      * @param value Indicates the queried value.
98      */
LessThan(const std::string & field,const SingleValue & value)99     DataSharePredicates *LessThan(const std::string &field, const SingleValue &value)
100     {
101         SetOperationList(LESS_THAN, field, value);
102         return this;
103     }
104 
105     /**
106      * @brief The GreaterThanOrEqualTo of the predicate.
107      *
108      * @param field Indicates the target field.
109      * @param value Indicates the queried value.
110      */
GreaterThanOrEqualTo(const std::string & field,const SingleValue & value)111     DataSharePredicates *GreaterThanOrEqualTo(const std::string &field, const SingleValue &value)
112     {
113         SetOperationList(GREATER_THAN_OR_EQUAL_TO, field, value);
114         return this;
115     }
116 
117     /**
118      * @brief The LessThanOrEqualTo of the predicate.
119      *
120      * @param field Indicates the target field.
121      * @param value Indicates the queried value.
122      */
LessThanOrEqualTo(const std::string & field,const SingleValue & value)123     DataSharePredicates *LessThanOrEqualTo(const std::string &field, const SingleValue &value)
124     {
125         SetOperationList(LESS_THAN_OR_EQUAL_TO, field, value);
126         return this;
127     }
128 
129     /**
130      * @brief The In of the predicate.
131      *
132      * @param field Indicates the target field.
133      * @param value Indicates the queried value.
134      */
In(const std::string & field,const MutliValue & values)135     DataSharePredicates *In(const std::string &field, const MutliValue &values)
136     {
137         SetOperationList(SQL_IN, field, values);
138         return this;
139     }
140 
141     /**
142      * @brief The NotIn of the predicate.
143      *
144      * @param field Indicates the target field.
145      * @param value Indicates the queried value.
146      */
NotIn(const std::string & field,const MutliValue & values)147     DataSharePredicates *NotIn(const std::string &field, const MutliValue &values)
148     {
149         SetOperationList(NOT_IN, field, values);
150         return this;
151     }
152 
153     /**
154      * @brief BeginWrap.
155      */
BeginWrap()156     DataSharePredicates *BeginWrap()
157     {
158         SetOperationList(BEGIN_WARP);
159         return this;
160     }
161 
162     /**
163      * @brief EndWrap.
164      */
EndWrap()165     DataSharePredicates *EndWrap()
166     {
167         SetOperationList(END_WARP);
168         return this;
169     }
170 
171     /**
172      * @brief Or.
173      */
Or()174     DataSharePredicates *Or()
175     {
176         SetOperationList(OR);
177         return this;
178     }
179 
180     /**
181      * @brief And.
182      */
And()183     DataSharePredicates *And()
184     {
185         SetOperationList(AND);
186         return this;
187     }
188 
189     /**
190      * @brief The Contains of the predicate.
191      *
192      * @param field Indicates the target field.
193      * @param value Indicates the queried value.
194      */
Contains(const std::string & field,const std::string & value)195     DataSharePredicates *Contains(const std::string &field, const std::string &value)
196     {
197         SetOperationList(CONTAINS, field, value);
198         return this;
199     }
200 
201     /**
202      * @brief The BeginsWith of the predicate.
203      *
204      * @param field Indicates the target field.
205      * @param value Indicates the queried value.
206      */
BeginsWith(const std::string & field,const std::string & value)207     DataSharePredicates *BeginsWith(const std::string &field, const std::string &value)
208     {
209         SetOperationList(BEGIN_WITH, field, value);
210         return this;
211     }
212 
213     /**
214      * @brief The EndsWith of the predicate.
215      *
216      * @param field Indicates the target field.
217      * @param value Indicates the queried value.
218      */
EndsWith(const std::string & field,const std::string & value)219     DataSharePredicates *EndsWith(const std::string &field, const std::string &value)
220     {
221         SetOperationList(END_WITH, field, value);
222         return this;
223     }
224 
225     /**
226      * @brief The IsNull of the predicate.
227      *
228      * @param field Indicates the target field.
229      * @param value Indicates the queried value.
230      */
IsNull(const std::string & field)231     DataSharePredicates *IsNull(const std::string &field)
232     {
233         SetOperationList(IS_NULL, field);
234         return this;
235     }
236 
237     /**
238      * @brief The IsNotNull of the predicate.
239      *
240      * @param field Indicates the target field.
241      * @param value Indicates the queried value.
242      */
IsNotNull(const std::string & field)243     DataSharePredicates *IsNotNull(const std::string &field)
244     {
245         SetOperationList(IS_NOT_NULL, field);
246         return this;
247     }
248 
249     /**
250      * @brief The Like of the predicate.
251      *
252      * @param field Indicates the target field.
253      * @param value Indicates the queried value.
254      */
Like(const std::string & field,const std::string & value)255     DataSharePredicates *Like(const std::string &field, const std::string &value)
256     {
257         SetOperationList(LIKE, field, value);
258         return this;
259     }
260 
261     /**
262      * @brief The Unlike of the predicate.
263      *
264      * @param field Indicates the target field.
265      * @param value Indicates the queried value.
266      */
Unlike(const std::string & field,const std::string & value)267     DataSharePredicates *Unlike(const std::string &field, const std::string &value)
268     {
269         SetOperationList(UNLIKE, field, value);
270         return this;
271     }
272 
273     /**
274      * @brief The Glob of the predicate.
275      *
276      * @param field Indicates the target field.
277      * @param value Indicates the queried value.
278      */
Glob(const std::string & field,const std::string & value)279     DataSharePredicates *Glob(const std::string &field, const std::string &value)
280     {
281         SetOperationList(GLOB, field, value);
282         return this;
283     }
284 
285     /**
286      * @brief The Between of the predicate.
287      *
288      * @param field Indicates the target field.
289      * @param value Indicates the queried value.
290      */
Between(const std::string & field,const std::string & low,const std::string & high)291     DataSharePredicates *Between(const std::string &field, const std::string &low, const std::string &high)
292     {
293         SetOperationList(BETWEEN, field, low, high);
294         return this;
295     }
296 
297     /**
298      * @brief The NotBetween of the predicate.
299      *
300      * @param field Indicates the target field.
301      * @param value Indicates the queried value.
302      */
NotBetween(const std::string & field,const std::string & low,const std::string & high)303     DataSharePredicates *NotBetween(const std::string &field, const std::string &low, const std::string &high)
304     {
305         SetOperationList(NOTBETWEEN, field, low, high);
306         return this;
307     }
308 
309     /**
310      * @brief The OrderByAsc of the predicate.
311      *
312      * @param field Indicates the target field.
313      * @param value Indicates the queried value.
314      */
OrderByAsc(const std::string & field)315     DataSharePredicates *OrderByAsc(const std::string &field)
316     {
317         SetOperationList(ORDER_BY_ASC, field);
318         return this;
319     }
320 
321     /**
322      * @brief The OrderByDesc of the predicate.
323      *
324      * @param field Indicates the target field.
325      * @param value Indicates the queried value.
326      */
OrderByDesc(const std::string & field)327     DataSharePredicates *OrderByDesc(const std::string &field)
328     {
329         SetOperationList(ORDER_BY_DESC, field);
330         return this;
331     }
332 
333     /**
334      * @brief Distinct predicate condition.
335      */
Distinct()336     DataSharePredicates *Distinct()
337     {
338         SetOperationList(DISTINCT);
339         return this;
340     }
341 
342     /**
343      * @brief The Limit of the predicate.
344      *
345      * @param number Indicates the target number.
346      * @param offset Indicates the queried value.
347      */
Limit(const int number,const int offset)348     DataSharePredicates *Limit(const int number, const int offset)
349     {
350         SetOperationList(LIMIT, number, offset);
351         return this;
352     }
353 
354     /**
355      * @brief The GroupBy of the predicate.
356      *
357      * @param field Indicates the target field.
358      */
GroupBy(const std::vector<std::string> & fields)359     DataSharePredicates *GroupBy(const std::vector<std::string> &fields)
360     {
361         SetOperationList(GROUP_BY, fields);
362         return this;
363     }
364 
365     /**
366      * @brief The IndexedBy of the predicate.
367      *
368      * @param indexName indicates the query condition.
369      */
IndexedBy(const std::string & indexName)370     DataSharePredicates *IndexedBy(const std::string &indexName)
371     {
372         SetOperationList(INDEXED_BY, indexName);
373         return this;
374     }
375 
376     /**
377      * @brief The KeyPrefix of the predicate.
378      *
379      * @param Search by prefix conditions.
380      */
KeyPrefix(const std::string & prefix)381     DataSharePredicates *KeyPrefix(const std::string &prefix)
382     {
383         SetOperationList(KEY_PREFIX, prefix);
384         return this;
385     }
386 
387     /**
388      * @brief The InKeys of the predicate.
389      *
390      * @param Query based on key conditions.
391      */
InKeys(const std::vector<std::string> & keys)392     DataSharePredicates *InKeys(const std::vector<std::string> &keys)
393     {
394         SetOperationList(IN_KEY, keys);
395         return this;
396     }
397 
398     /**
399      * @brief The CrossJoin of the predicate.
400      *
401      * @param tableName indicates the query condition.
402      */
CrossJoin(const std::string & tableName)403     DataSharePredicates *CrossJoin(const std::string &tableName)
404     {
405         SetOperationList(CROSSJOIN, tableName);
406         return this;
407     }
408 
409     /**
410      * @brief The InnerJoin of the predicate.
411      *
412      * @param tableName indicates the query condition.
413      */
InnerJoin(const std::string & tableName)414     DataSharePredicates *InnerJoin(const std::string &tableName)
415     {
416         SetOperationList(INNERJOIN, tableName);
417         return this;
418     }
419 
420     /**
421      * @brief The LeftOuterJoin of the predicate.
422      *
423      * @param tableName indicates the query condition.
424      */
LeftOuterJoin(const std::string & tableName)425     DataSharePredicates *LeftOuterJoin(const std::string &tableName)
426     {
427         SetOperationList(LEFTOUTERJOIN, tableName);
428         return this;
429     }
430 
431     /**
432      * @brief The Using of the predicate.
433      *
434      * @param field Indicates the target field.
435      */
Using(const std::vector<std::string> & fields)436     DataSharePredicates *Using(const std::vector<std::string> &fields)
437     {
438         SetOperationList(USING, fields);
439         return this;
440     }
441 
442     /**
443      * @brief The On of the predicate.
444      *
445      * @param field Indicates the target field.
446      */
On(const std::vector<std::string> & fields)447     DataSharePredicates *On(const std::vector<std::string> &fields)
448     {
449         SetOperationList(ON, fields);
450         return this;
451     }
452 
453     /**
454      * @brief The GetOperationList of the predicate.
455      */
GetOperationList()456     const std::vector<OperationItem> &GetOperationList() const
457     {
458         return operations_;
459     }
460 
461     /**
462      * @brief Used in the deserialization function to assign values to operations,
463      * Directly assignsing values to operations with Different usage from private methods.
464      * reference to the SetWhereClause, SetWhereArgs, SetOrder.
465      */
SetOperationList(std::vector<OperationItem> operations)466     int SetOperationList(std::vector<OperationItem> operations)
467     {
468         if ((settingMode_ != PREDICATES_METHOD) && (!operations.empty())) {
469             this->operations_ = operations;
470             settingMode_ = QUERY_LANGUAGE;
471             return E_OK;
472         }
473         return E_ERROR;
474     }
475 
476     /**
477      * @brief The GetWhereClause of the predicate.
478      */
GetWhereClause()479     std::string GetWhereClause() const
480     {
481         return whereClause_;
482     }
483 
484     /**
485      * @brief The SetWhereClause of the predicate.
486      *
487      * @param Query based on the whereClause.
488      */
SetWhereClause(const std::string & whereClause)489     int SetWhereClause(const std::string &whereClause)
490     {
491         if ((settingMode_ != PREDICATES_METHOD) && (!whereClause.empty())) {
492             this->whereClause_ = whereClause;
493             settingMode_ = QUERY_LANGUAGE;
494             return E_OK;
495         }
496         return E_ERROR;
497     }
498 
499     /**
500      * @brief The GetWhereArgs of the predicate.
501      */
GetWhereArgs()502     std::vector<std::string> GetWhereArgs() const
503     {
504         return whereArgs_;
505     }
506 
507     /**
508      * @brief The SetWhereArgs of the predicate.
509      *
510      * @param Query based on whereArgs conditions.
511      */
SetWhereArgs(const std::vector<std::string> & whereArgs)512     int SetWhereArgs(const std::vector<std::string> &whereArgs)
513     {
514         if ((settingMode_ != PREDICATES_METHOD) && (!whereArgs.empty())) {
515             if (!whereArgs.empty()) {
516                 this->whereArgs_ = whereArgs;
517                 settingMode_ = QUERY_LANGUAGE;
518                 return E_OK;
519             }
520         }
521         return E_ERROR;
522     }
523 
524     /**
525      * @brief The GetOrder of the predicate.
526      */
GetOrder()527     std::string GetOrder() const
528     {
529         return order_;
530     }
531 
532     /**
533      * @brief The SetOrder of the predicate.
534      *
535      * @param Query based on order conditions..
536      */
SetOrder(const std::string & order)537     int SetOrder(const std::string &order)
538     {
539         if ((settingMode_ != PREDICATES_METHOD) && (!order.empty())) {
540             this->order_ = order;
541             settingMode_ = QUERY_LANGUAGE;
542             return E_OK;
543         }
544         return E_ERROR;
545     }
546 
547     /**
548      * @brief The GetSettingMode of the predicate.
549      */
GetSettingMode()550     int16_t GetSettingMode() const
551     {
552         return settingMode_;
553     }
554 
555     /**
556      * @brief The SetSettingMode of the predicate.
557      */
SetSettingMode(int16_t settingMode)558     void SetSettingMode(int16_t settingMode)
559     {
560         settingMode_ = settingMode;
561     }
562 
563     /**
564      * @brief The following four functions are used for serializing and deserializing objects
565      * to and from shared memory during Query and BatchInsert operations,
566      * which has a 128M upper limit. The upper limit of other method is 200k.
567      * Other methods remain unchanged.
568      */
569     static bool Marshal(const DataSharePredicates &predicates, MessageParcel &parcel);
570 
571     static bool Unmarshal(DataSharePredicates &predicates, MessageParcel &parcel);
572 
573 private:
SetOperationList(OperationType operationType,const MutliValue & param)574     void SetOperationList(OperationType operationType, const MutliValue &param)
575     {
576         OperationItem operationItem {};
577         operationItem.operation = operationType;
578         operationItem.multiParams.push_back(param.value);
579         operations_.push_back(operationItem);
580         if (settingMode_ != PREDICATES_METHOD) {
581             ClearQueryLanguage();
582             settingMode_ = PREDICATES_METHOD;
583         }
584     }
SetOperationList(OperationType operationType,const SingleValue & param1,const MutliValue & param2)585     void SetOperationList(
586         OperationType operationType, const SingleValue &param1, const MutliValue &param2)
587     {
588         OperationItem operationItem {};
589         operationItem.operation = operationType;
590         operationItem.singleParams.push_back(param1.value);
591         operationItem.multiParams.push_back(param2.value);
592         operations_.push_back(operationItem);
593         if (settingMode_ != PREDICATES_METHOD) {
594             ClearQueryLanguage();
595             settingMode_ = PREDICATES_METHOD;
596         }
597     }
598     void SetOperationList(OperationType operationType, const SingleValue &para1 = {},
599         const SingleValue &para2 = {}, const SingleValue &para3 = {})
600     {
601         OperationItem operationItem {};
602         operationItem.operation = operationType;
603         operationItem.singleParams.push_back(para1.value);
604         operationItem.singleParams.push_back(para2.value);
605         operationItem.singleParams.push_back(para3.value);
606         operations_.push_back(operationItem);
607         if (settingMode_ != PREDICATES_METHOD) {
608             ClearQueryLanguage();
609             settingMode_ = PREDICATES_METHOD;
610         }
611     }
ClearQueryLanguage()612     void ClearQueryLanguage()
613     {
614         whereClause_ = "";
615         whereArgs_ = {};
616         order_ = "";
617     }
618     std::vector<OperationItem> operations_;
619     std::string whereClause_;
620     std::vector<std::string> whereArgs_;
621     std::string order_;
622     int16_t settingMode_ = {};
623 };
624 } // namespace DataShare
625 } // namespace OHOS
626 #endif