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