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 #include "predicates_utils.h"
17 
18 #include <sstream>
19 
20 namespace OHOS {
21 namespace NativeRdb {
PredicatesUtils()22 PredicatesUtils::PredicatesUtils()
23 {
24 }
25 
26 /**
27  * Set the param of whereClause and bindArgs of the specified Predicates.
28  */
SetWhereClauseAndArgs(AbsPredicates * predicates,const std::string & whereClause,const std::vector<std::string> & whereArgs)29 void PredicatesUtils::SetWhereClauseAndArgs(
30     AbsPredicates *predicates, const std::string &whereClause, const std::vector<std::string> &whereArgs)
31 {
32     predicates->SetWhereClause(whereClause);
33     predicates->SetWhereArgs(whereArgs);
34 }
35 
36 /**
37  * Set the param of whereClause and bindArgs of the specified Predicates.
38  */
SetWhereClauseAndArgs(AbsPredicates * predicates,const std::string & whereClause,const std::vector<ValueObject> & bindArgs)39 void PredicatesUtils::SetWhereClauseAndArgs(AbsPredicates *predicates, const std::string &whereClause,
40     const std::vector<ValueObject> &bindArgs)
41 {
42     predicates->SetWhereClause(whereClause);
43     predicates->SetBindArgs(bindArgs);
44 }
45 
46 /**
47  * Sets params of the specified Predicates including distinct, index, group, order, limit and offset.
48  */
SetAttributes(AbsPredicates * predicates,bool isDistinct,const std::string & index,const std::string & group,const std::string & order,const int limit,const int offset)49 void PredicatesUtils::SetAttributes(AbsPredicates *predicates, bool isDistinct, const std::string &index,
50     const std::string &group, const std::string &order, const int limit, const int offset)
51 {
52     if (isDistinct) {
53         predicates->Distinct();
54     }
55     if (!index.empty()) {
56         predicates->IndexedBy(index);
57     }
58     if (!group.empty()) {
59         std::vector<std::string> groupArray;
60         std::istringstream iss(group);
61         std::string temp;
62         while (getline(iss, temp, ',')) {
63             groupArray.push_back(temp);
64         }
65         predicates->GroupBy(groupArray);
66     }
67     if (!order.empty()) {
68         predicates->SetOrder(order);
69     }
70     if (limit != AbsPredicates::INIT_LIMIT_VALUE) {
71         predicates->Limit(limit);
72     }
73     if (offset != AbsPredicates::INIT_OFFSET_VALUE) {
74         predicates->Offset(offset);
75     }
76 }
77 } // namespace NativeRdb
78 } // namespace OHOS