• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "rdb_utils.h"
17 
18 #include "rdb_logger.h"
19 
20 using namespace OHOS::RdbDataShareAdapter;
21 using namespace OHOS::DataShare;
22 using namespace OHOS::NativeRdb;
23 
24 constexpr RdbUtils::OperateHandler RdbUtils::HANDLERS[LAST_TYPE];
25 
ToValuesBucket(const DataShareValuesBucket & valuesBucket)26 ValuesBucket RdbUtils::ToValuesBucket(const DataShareValuesBucket &valuesBucket)
27 {
28     std::map<std::string, ValueObject> valuesMap;
29     auto values = valuesBucket.valuesMap;
30     for (auto &[key, value] : values) {
31         if (value.type == DataShareValueObjectType::TYPE_BOOL) {
32             valuesMap.insert(std::pair<std::string, ValueObject>(key, ValueObject(value.operator bool())));
33         } else if (value.type == DataShareValueObjectType::TYPE_INT) {
34             valuesMap.insert(std::pair<std::string, ValueObject>(key, ValueObject(value.operator int())));
35         } else if (value.type == DataShareValueObjectType::TYPE_INT64) {
36             valuesMap.insert(std::pair<std::string, ValueObject>(key, ValueObject(value.operator int64_t())));
37         } else if (value.type == DataShareValueObjectType::TYPE_DOUBLE) {
38             valuesMap.insert(std::pair<std::string, ValueObject>(key, ValueObject(value.operator double())));
39         } else if (value.type == DataShareValueObjectType::TYPE_STRING) {
40             valuesMap.insert(std::pair<std::string, ValueObject>(key, ValueObject(value.operator std::string())));
41         } else if (value.type == DataShareValueObjectType::TYPE_BLOB) {
42             valuesMap.insert(
43                 std::pair<std::string, ValueObject>(key, ValueObject(value.operator std::vector<uint8_t>())));
44         } else {
45             LOG_INFO("Convert ValueBucket successful.");
46         }
47     }
48     return ValuesBucket(valuesMap);
49 }
50 
ToPredicates(const DataShareAbsPredicates & predicates,const std::string & table)51 RdbPredicates RdbUtils::ToPredicates(const DataShareAbsPredicates &predicates, const std::string &table)
52 {
53     RdbPredicates rdbPredicates(table);
54     if (predicates.GetSettingMode() == QUERY_LANGUAGE) {
55         rdbPredicates.SetWhereClause(predicates.GetWhereClause());
56         rdbPredicates.SetWhereArgs(predicates.GetWhereArgs());
57         rdbPredicates.SetOrder(predicates.GetOrder());
58     }
59 
60     const auto &operations = predicates.GetOperationList();
61     for (const auto &oper : operations) {
62         if (oper.operation >= 0 && oper.operation < LAST_TYPE) {
63             (*HANDLERS[oper.operation])(oper, rdbPredicates);
64         }
65     }
66     return rdbPredicates;
67 }
68 
ToString(const DataSharePredicatesObject & predicatesObject)69 std::string RdbUtils::ToString(const DataSharePredicatesObject &predicatesObject)
70 {
71     std::string str = " ";
72     switch (predicatesObject.type) {
73         case DataSharePredicatesObjectType::TYPE_INT:
74             str = std::to_string(predicatesObject.operator int());
75             break;
76         case DataSharePredicatesObjectType::TYPE_DOUBLE:
77             str = std::to_string(predicatesObject.operator double());
78             break;
79         case DataSharePredicatesObjectType::TYPE_STRING:
80             str = predicatesObject.operator std::string();
81             break;
82         case DataSharePredicatesObjectType::TYPE_BOOL:
83             str = std::to_string(predicatesObject.operator bool());
84             break;
85         case DataSharePredicatesObjectType::TYPE_LONG:
86             str = std::to_string(predicatesObject.operator int64_t());
87             break;
88         default:
89             LOG_INFO("RdbUtils::ToString No matching type");
90             return str;
91     }
92     return str;
93 }
94 
ToResultSetBridge(std::shared_ptr<ResultSet> resultSet)95 std::shared_ptr<ResultSetBridge> RdbUtils::ToResultSetBridge(std::shared_ptr<ResultSet> resultSet)
96 {
97     if (resultSet == nullptr) {
98         LOG_ERROR("resultSet is null");
99         return nullptr;
100     }
101     return std::make_shared<RdbResultSetBridge>(resultSet);
102 }
103 
NoSupport(const OperationItem & item,RdbPredicates & query)104 void RdbUtils::NoSupport(const OperationItem &item, RdbPredicates &query)
105 {
106     LOG_ERROR("invalid operation:%{public}d", item.operation);
107 }
108 
EqualTo(const OperationItem & item,RdbPredicates & predicates)109 void RdbUtils::EqualTo(const OperationItem &item, RdbPredicates &predicates)
110 {
111     predicates.EqualTo(item.singleParams[0], ToString(item.singleParams[1]));
112 }
113 
NotEqualTo(const OperationItem & item,RdbPredicates & predicates)114 void RdbUtils::NotEqualTo(const OperationItem &item, RdbPredicates &predicates)
115 {
116     predicates.NotEqualTo(item.singleParams[0], ToString(item.singleParams[1]));
117 }
118 
GreaterThan(const OperationItem & item,RdbPredicates & predicates)119 void RdbUtils::GreaterThan(const OperationItem &item, RdbPredicates &predicates)
120 {
121     predicates.GreaterThan(item.singleParams[0], ToString(item.singleParams[1]));
122 }
123 
LessThan(const OperationItem & item,RdbPredicates & predicates)124 void RdbUtils::LessThan(const OperationItem &item, RdbPredicates &predicates)
125 {
126     predicates.LessThan(item.singleParams[0], ToString(item.singleParams[1]));
127 }
128 
GreaterThanOrEqualTo(const OperationItem & item,RdbPredicates & predicates)129 void RdbUtils::GreaterThanOrEqualTo(const OperationItem &item, RdbPredicates &predicates)
130 {
131     predicates.GreaterThanOrEqualTo(item.singleParams[0], ToString(item.singleParams[1]));
132 }
133 
LessThanOrEqualTo(const OperationItem & item,RdbPredicates & predicates)134 void RdbUtils::LessThanOrEqualTo(const OperationItem &item, RdbPredicates &predicates)
135 {
136     predicates.LessThanOrEqualTo(item.singleParams[0], ToString(item.singleParams[1]));
137 }
138 
And(const DataShare::OperationItem & item,RdbPredicates & predicates)139 void RdbUtils::And(const DataShare::OperationItem &item, RdbPredicates &predicates)
140 {
141     predicates.And();
142 }
143 
Or(const DataShare::OperationItem & item,RdbPredicates & predicates)144 void RdbUtils::Or(const DataShare::OperationItem &item, RdbPredicates &predicates)
145 {
146     predicates.Or();
147 }
148 
IsNull(const OperationItem & item,RdbPredicates & predicates)149 void RdbUtils::IsNull(const OperationItem &item, RdbPredicates &predicates)
150 {
151     predicates.IsNull(item.singleParams[0]);
152 }
153 
IsNotNull(const DataShare::OperationItem & item,RdbPredicates & predicates)154 void RdbUtils::IsNotNull(const DataShare::OperationItem &item, RdbPredicates &predicates)
155 {
156     predicates.IsNotNull(item.singleParams[0]);
157 }
158 
In(const DataShare::OperationItem & item,RdbPredicates & predicates)159 void RdbUtils::In(const DataShare::OperationItem &item, RdbPredicates &predicates)
160 {
161     predicates.In(item.singleParams[0], item.multiParams[0]);
162 }
163 
NotIn(const DataShare::OperationItem & item,RdbPredicates & predicates)164 void RdbUtils::NotIn(const DataShare::OperationItem &item, RdbPredicates &predicates)
165 {
166     predicates.NotIn(item.singleParams[0], item.multiParams[0]);
167 }
168 
Like(const DataShare::OperationItem & item,RdbPredicates & predicates)169 void RdbUtils::Like(const DataShare::OperationItem &item, RdbPredicates &predicates)
170 {
171     predicates.Like(item.singleParams[0], ToString(item.singleParams[1]));
172 }
173 
OrderByAsc(const DataShare::OperationItem & item,RdbPredicates & predicates)174 void RdbUtils::OrderByAsc(const DataShare::OperationItem &item, RdbPredicates &predicates)
175 {
176     predicates.OrderByAsc(item.singleParams[0]);
177 }
178 
OrderByDesc(const DataShare::OperationItem & item,RdbPredicates & predicates)179 void RdbUtils::OrderByDesc(const DataShare::OperationItem &item, RdbPredicates &predicates)
180 {
181     predicates.OrderByDesc(item.singleParams[0]);
182 }
183 
Limit(const DataShare::OperationItem & item,RdbPredicates & predicates)184 void RdbUtils::Limit(const DataShare::OperationItem &item, RdbPredicates &predicates)
185 {
186     predicates.Limit(item.singleParams[0].operator int());
187     predicates.Offset(item.singleParams[1].operator int());
188 }
189 
Offset(const DataShare::OperationItem & item,RdbPredicates & predicates)190 void RdbUtils::Offset(const DataShare::OperationItem &item, RdbPredicates &predicates)
191 {
192     predicates.Offset(item.singleParams[0].operator int());
193 }
194 
BeginWrap(const DataShare::OperationItem & item,RdbPredicates & predicates)195 void RdbUtils::BeginWrap(const DataShare::OperationItem &item, RdbPredicates &predicates)
196 {
197     predicates.BeginWrap();
198 }
199 
EndWrap(const DataShare::OperationItem & item,RdbPredicates & predicates)200 void RdbUtils::EndWrap(const DataShare::OperationItem &item, RdbPredicates &predicates)
201 {
202     predicates.EndWrap();
203 }
204 
BeginsWith(const DataShare::OperationItem & item,RdbPredicates & predicates)205 void RdbUtils::BeginsWith(const DataShare::OperationItem &item, RdbPredicates &predicates)
206 {
207     predicates.BeginsWith(item.singleParams[0], ToString(item.singleParams[1]));
208 }
209 
EndsWith(const DataShare::OperationItem & item,RdbPredicates & predicates)210 void RdbUtils::EndsWith(const DataShare::OperationItem &item, RdbPredicates &predicates)
211 {
212     predicates.EndsWith(item.singleParams[0], ToString(item.singleParams[1]));
213 }
214 
Distinct(const DataShare::OperationItem & item,RdbPredicates & predicates)215 void RdbUtils::Distinct(const DataShare::OperationItem &item, RdbPredicates &predicates)
216 {
217     predicates.Distinct();
218 }
219 
GroupBy(const DataShare::OperationItem & item,RdbPredicates & predicates)220 void RdbUtils::GroupBy(const DataShare::OperationItem &item, RdbPredicates &predicates)
221 {
222     predicates.GroupBy(item.multiParams[0]);
223 }
224 
IndexedBy(const DataShare::OperationItem & item,RdbPredicates & predicates)225 void RdbUtils::IndexedBy(const DataShare::OperationItem &item, RdbPredicates &predicates)
226 {
227     predicates.IndexedBy(item.singleParams[0]);
228 }
229 
Contains(const DataShare::OperationItem & item,RdbPredicates & predicates)230 void RdbUtils::Contains(const DataShare::OperationItem &item, RdbPredicates &predicates)
231 {
232     predicates.Contains(item.singleParams[0], ToString(item.singleParams[1]));
233 }
234 
Glob(const DataShare::OperationItem & item,RdbPredicates & predicates)235 void RdbUtils::Glob(const DataShare::OperationItem &item, RdbPredicates &predicates)
236 {
237     predicates.Glob(item.singleParams[0], ToString(item.singleParams[1]));
238 }
239 
Between(const DataShare::OperationItem & item,RdbPredicates & predicates)240 void RdbUtils::Between(const DataShare::OperationItem &item, RdbPredicates &predicates)
241 {
242     predicates.Between(item.singleParams[0], ToString(item.singleParams[1]), ToString(item.singleParams[2]));
243 }
244 
NotBetween(const DataShare::OperationItem & item,RdbPredicates & predicates)245 void RdbUtils::NotBetween(const DataShare::OperationItem &item, RdbPredicates &predicates)
246 {
247     predicates.NotBetween(item.singleParams[0], ToString(item.singleParams[1]), ToString(item.singleParams[2]));
248 }
249 
RdbUtils()250 RdbUtils::RdbUtils()
251 {
252 }
253 
~RdbUtils()254 RdbUtils::~RdbUtils()
255 {
256 }
257