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 * Set the param of whereClause and whereArgs of the specified Predicates.
25 */
SetWhereClauseAndArgs(AbsPredicates * predicates,std::string whereClause,std::vector<std::string> whereArgs)26 void PredicatesUtils::SetWhereClauseAndArgs(AbsPredicates *predicates, std::string whereClause,
27 std::vector<std::string> whereArgs)
28 {
29 predicates->SetWhereClause(whereClause);
30 predicates->SetWhereArgs(whereArgs);
31 }
32
33 /**
34 * Sets params of the specified Predicates including distinct, index, group, order, limit and offset.
35 */
SetAttributes(AbsPredicates * predicates,bool isDistinct,std::string index,std::string group,std::string order,int limit,int offset)36 void PredicatesUtils::SetAttributes(AbsPredicates *predicates, bool isDistinct, std::string index, std::string group,
37 std::string order, int limit, int offset)
38 {
39 if (isDistinct) {
40 predicates->Distinct();
41 }
42 if (!index.empty()) {
43 predicates->IndexedBy(index);
44 }
45 if (!group.empty()) {
46 std::vector<std::string> groupArray;
47 std::string::size_type startpos = 0;
48 while (startpos != std::string::npos) {
49 startpos = group.find('`');
50 if (startpos != std::string::npos) {
51 group.replace(startpos, 1, "");
52 }
53 }
54 std::istringstream iss(group);
55 std::string temp;
56 while (getline(iss, temp, ',')) {
57 groupArray.push_back(temp);
58 }
59 predicates->GroupBy(groupArray);
60 }
61 if (!order.empty()) {
62 predicates->SetOrder(order);
63 }
64 if (limit != -1) {
65 predicates->Limit(limit);
66 }
67 if (offset != -1) {
68 predicates->Offset(offset);
69 }
70 }
71 } // namespace NativeRdb
72 } // namespace OHOS