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