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 #define LOG_TAG "AbsRdbPredicates"
17
18 #include "abs_rdb_predicates.h"
19 #include "logger.h"
20 #include "rdb_manager.h"
21 #include "rdb_service.h"
22
23 namespace OHOS::NativeRdb {
AbsRdbPredicates(std::string tableName)24 AbsRdbPredicates::AbsRdbPredicates(std::string tableName)
25 {
26 if (tableName.empty()) {
27 this->tableName = "";
28 LOG_INFO("no tableName specified.");
29 return;
30 }
31 this->tableName = tableName;
32 predicates_.table_ = tableName;
33 }
34
35 /**
36 * Obtains the table name.
37 */
GetTableName() const38 std::string AbsRdbPredicates::GetTableName() const
39 {
40 return tableName;
41 }
42
ToString() const43 std::string AbsRdbPredicates::ToString() const
44 {
45 std::string args;
46 for (const auto& item : GetWhereArgs()) {
47 args += item + ", ";
48 }
49 return "TableName = " + GetTableName() + ", {WhereClause:" + GetWhereClause() + ", whereArgs:{" + args + "}"
50 + ", order:" + GetOrder() + ", group:" + GetGroup() + ", index:" + GetIndex()
51 + ", limit:" + std::to_string(GetLimit()) + ", offset:" + std::to_string(GetOffset())
52 + ", distinct:" + std::to_string(IsDistinct()) + ", isNeedAnd:" + std::to_string(IsNeedAnd())
53 + ", isSorted:" + std::to_string(IsSorted()) + "}";
54 }
55
InDevices(std::vector<std::string> & devices)56 AbsRdbPredicates* AbsRdbPredicates::InDevices(std::vector<std::string> &devices)
57 {
58 for (const auto& device : devices) {
59 LOG_INFO("%{public}.6s", device.c_str());
60 }
61 predicates_.devices_ = devices;
62 return this;
63 }
64
InAllDevices()65 AbsRdbPredicates* AbsRdbPredicates::InAllDevices()
66 {
67 LOG_INFO("enter");
68 predicates_.devices_.clear();
69 return this;
70 }
71
GetDistributedPredicates() const72 const DistributedRdb::RdbPredicates& AbsRdbPredicates::GetDistributedPredicates() const
73 {
74 int limit = GetLimit();
75 if (limit >= 0) {
76 predicates_.AddOperation(DistributedRdb::RdbPredicateOperator::LIMIT,
77 std::to_string(limit), std::to_string(GetOffset()));
78 }
79 return predicates_;
80 }
81
EqualTo(std::string field,std::string value)82 AbsRdbPredicates* AbsRdbPredicates::EqualTo(std::string field, std::string value)
83 {
84 predicates_.AddOperation(DistributedRdb::EQUAL_TO, field, value);
85 return (AbsRdbPredicates *)AbsPredicates::EqualTo(field, value);
86 }
87
NotEqualTo(std::string field,std::string value)88 AbsRdbPredicates* AbsRdbPredicates::NotEqualTo(std::string field, std::string value)
89 {
90 predicates_.AddOperation(DistributedRdb::NOT_EQUAL_TO, field, value);
91 return (AbsRdbPredicates *)AbsPredicates::NotEqualTo(field, value);
92 }
93
And()94 AbsRdbPredicates* AbsRdbPredicates::And()
95 {
96 std::string field;
97 std::string value;
98 predicates_.AddOperation(DistributedRdb::AND, field, value);
99 return (AbsRdbPredicates *)AbsPredicates::And();
100 }
101
Or()102 AbsRdbPredicates* AbsRdbPredicates::Or()
103 {
104 std::string field;
105 std::string value;
106 predicates_.AddOperation(DistributedRdb::OR, field, value);
107 return (AbsRdbPredicates *)AbsPredicates::Or();
108 }
109
OrderByAsc(std::string field)110 AbsRdbPredicates* AbsRdbPredicates::OrderByAsc(std::string field)
111 {
112 std::string isAsc = "true";
113 predicates_.AddOperation(DistributedRdb::ORDER_BY, field, isAsc);
114 return (AbsRdbPredicates *)AbsPredicates::OrderByAsc(field);
115 }
116
OrderByDesc(std::string field)117 AbsRdbPredicates* AbsRdbPredicates::OrderByDesc(std::string field)
118 {
119 std::string isAsc = "false";
120 predicates_.AddOperation(DistributedRdb::ORDER_BY, field, isAsc);
121 return (AbsRdbPredicates *)AbsPredicates::OrderByDesc(field);
122 }
123 }