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 #include "abs_rdb_predicates.h"
17
18 #include <variant>
19
20 #include "logger.h"
21 #include "rdb_trace.h"
22
23 namespace OHOS::NativeRdb {
24 using namespace OHOS::Rdb;
25
AbsRdbPredicates(const std::string & tableName)26 AbsRdbPredicates::AbsRdbPredicates(const std::string &tableName)
27 {
28 if (tableName.empty()) {
29 tableName_ = "";
30 LOG_DEBUG("no tableName specified.");
31 return;
32 }
33 tableName_ = std::move(tableName);
34 predicates_.tables_.push_back(tableName_);
35 }
36
AbsRdbPredicates(const std::vector<std::string> & tables)37 AbsRdbPredicates::AbsRdbPredicates(const std::vector<std::string> &tables)
38 {
39 if (tables.empty()) {
40 tableName_ = "";
41 LOG_DEBUG("no tableName specified.");
42 return;
43 }
44 tableName_ = *(tables.begin());
45 predicates_.tables_ = std::move(tables);
46 }
47
Clear()48 void AbsRdbPredicates::Clear()
49 {
50 AbsPredicates::Clear();
51 InitialParam();
52 }
53
InitialParam()54 void AbsRdbPredicates::InitialParam()
55 {
56 joinTypes.clear();
57 joinTableNames.clear();
58 joinConditions.clear();
59 joinCount = 0;
60 }
61
GetJoinTypes()62 std::vector<std::string> AbsRdbPredicates::GetJoinTypes()
63 {
64 return joinTypes;
65 }
66
67 /**
68 * Sets the join types in the predicates. The value can be {@code INNER JOIN}, {@code LEFT OUTER JOIN},
69 * and {@code CROSS JOIN}.
70 */
SetJoinTypes(const std::vector<std::string> & joinTypes)71 void AbsRdbPredicates::SetJoinTypes(const std::vector<std::string> &joinTypes)
72 {
73 this->joinTypes = joinTypes;
74 }
75
76 /**
77 * Obtains the database table names of the joins in the predicates.
78 */
GetJoinTableNames()79 std::vector<std::string> AbsRdbPredicates::GetJoinTableNames()
80 {
81 return joinTableNames;
82 }
83
84 /**
85 * Sets the database table names of the joins in the predicates.
86 */
SetJoinTableNames(const std::vector<std::string> & joinTableNames)87 void AbsRdbPredicates::SetJoinTableNames(const std::vector<std::string> &joinTableNames)
88 {
89 this->joinTableNames = joinTableNames;
90 }
91
92 /**
93 * Obtains the join conditions in the predicates.
94 */
GetJoinConditions()95 std::vector<std::string> AbsRdbPredicates::GetJoinConditions()
96 {
97 return joinConditions;
98 }
99
100 /**
101 * Sets the join conditions required in the predicates.
102 */
SetJoinConditions(const std::vector<std::string> & joinConditions)103 void AbsRdbPredicates::SetJoinConditions(const std::vector<std::string> &joinConditions)
104 {
105 this->joinConditions = joinConditions;
106 }
107
108 /**
109 * Obtains the join clause in the predicates.
110 */
GetJoinClause() const111 std::string AbsRdbPredicates::GetJoinClause() const
112 {
113 return tableName_;
114 }
115
116 /**
117 * Obtains the number of joins in the predicates.
118 */
GetJoinCount() const119 int AbsRdbPredicates::GetJoinCount() const
120 {
121 return joinCount;
122 }
123
124 /**
125 * Sets the number of joins in the predicates.
126 */
SetJoinCount(int joinCount)127 void AbsRdbPredicates::SetJoinCount(int joinCount)
128 {
129 this->joinCount = joinCount;
130 }
131
132 /**
133 * Obtains the table name.
134 */
GetTableName() const135 std::string AbsRdbPredicates::GetTableName() const
136 {
137 return tableName_;
138 }
139
ToString() const140 std::string AbsRdbPredicates::ToString() const
141 {
142 std::string args;
143 for (const auto& item : GetWhereArgs()) {
144 args += item + ", ";
145 }
146 return "TableName = " + GetTableName() + ", {WhereClause:" + GetWhereClause() + ", bindArgs:{" + args + "}"
147 + ", order:" + GetOrder() + ", group:" + GetGroup() + ", index:" + GetIndex()
148 + ", limit:" + std::to_string(GetLimit()) + ", offset:" + std::to_string(GetOffset())
149 + ", distinct:" + std::to_string(IsDistinct()) + ", isNeedAnd:" + std::to_string(IsNeedAnd())
150 + ", isSorted:" + std::to_string(IsSorted()) + "}";
151 }
152
InDevices(std::vector<std::string> & devices)153 AbsRdbPredicates* AbsRdbPredicates::InDevices(std::vector<std::string> &devices)
154 {
155 predicates_.devices_ = devices;
156 return this;
157 }
158
InAllDevices()159 AbsRdbPredicates* AbsRdbPredicates::InAllDevices()
160 {
161 predicates_.devices_.clear();
162 return this;
163 }
164
GetDistributedPredicates() const165 const DistributedRdb::PredicatesMemo& AbsRdbPredicates::GetDistributedPredicates() const
166 {
167 int limit = GetLimit();
168 if (limit >= 0) {
169 predicates_.AddOperation(DistributedRdb::RdbPredicateOperator::LIMIT,
170 std::to_string(limit), std::to_string(GetOffset()));
171 }
172 return predicates_;
173 }
174
EqualTo(const std::string & field,const ValueObject & value)175 AbsRdbPredicates* AbsRdbPredicates::EqualTo(const std::string &field, const ValueObject &value)
176 {
177 DISTRIBUTED_DATA_HITRACE("AbsRdbPredicates::EqualTo");
178 if (auto pval = std::get_if<std::string>(&value.value)) {
179 predicates_.AddOperation(DistributedRdb::EQUAL_TO, field, *pval);
180 }
181 return (AbsRdbPredicates *)AbsPredicates::EqualTo(field, value);
182 }
183
NotEqualTo(const std::string & field,const ValueObject & value)184 AbsRdbPredicates* AbsRdbPredicates::NotEqualTo(const std::string &field, const ValueObject &value)
185 {
186 if (auto pval = std::get_if<std::string>(&value.value)) {
187 predicates_.AddOperation(DistributedRdb::NOT_EQUAL_TO, field, *pval);
188 }
189 return (AbsRdbPredicates *)AbsPredicates::NotEqualTo(field, value);
190 }
191
And()192 AbsRdbPredicates* AbsRdbPredicates::And()
193 {
194 std::string field;
195 std::string value;
196 predicates_.AddOperation(DistributedRdb::AND, field, value);
197 return (AbsRdbPredicates *)AbsPredicates::And();
198 }
199
Or()200 AbsRdbPredicates* AbsRdbPredicates::Or()
201 {
202 std::string field;
203 std::string value;
204 predicates_.AddOperation(DistributedRdb::OR, field, value);
205 return (AbsRdbPredicates *)AbsPredicates::Or();
206 }
207
OrderByAsc(const std::string & field)208 AbsRdbPredicates* AbsRdbPredicates::OrderByAsc(const std::string &field)
209 {
210 std::string isAsc = "true";
211 predicates_.AddOperation(DistributedRdb::ORDER_BY, field, isAsc);
212 return (AbsRdbPredicates *)AbsPredicates::OrderByAsc(field);
213 }
214
OrderByDesc(const std::string & field)215 AbsRdbPredicates* AbsRdbPredicates::OrderByDesc(const std::string &field)
216 {
217 std::string isAsc = "false";
218 predicates_.AddOperation(DistributedRdb::ORDER_BY, field, isAsc);
219 return (AbsRdbPredicates *)AbsPredicates::OrderByDesc(field);
220 }
221 } // namespace OHOS::NativeRdb