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 "datashare_itypes_utils.h"
17 #include "datashare_log.h"
18 
19 namespace OHOS::ITypesUtil {
20 using namespace OHOS::DataShare;
21 template<>
Marshalling(const Predicates & predicates,MessageParcel & parcel)22 bool Marshalling(const Predicates &predicates, MessageParcel &parcel)
23 {
24     const auto &operations = predicates.GetOperationList();
25     int16_t mode = predicates.GetSettingMode();
26     return ITypesUtil::Marshal(parcel, operations, predicates.GetWhereClause(), predicates.GetWhereArgs(),
27                                predicates.GetOrder(), mode);
28 }
29 
30 template<>
Unmarshalling(Predicates & predicates,MessageParcel & parcel)31 bool Unmarshalling(Predicates &predicates, MessageParcel &parcel)
32 {
33     std::vector<Operation> operations{};
34     std::string whereClause = "";
35     std::vector<std::string> whereArgs;
36     std::string order = "";
37     int16_t mode = DataShare::INVALID_MODE;
38     if (!ITypesUtil::Unmarshal(parcel, operations, whereClause, whereArgs, order, mode)) {
39         return false;
40     }
41     DataShare::DataSharePredicates tmpPredicates(operations);
42     tmpPredicates.SetWhereClause(whereClause);
43     tmpPredicates.SetWhereArgs(whereArgs);
44     tmpPredicates.SetOrder(order);
45     tmpPredicates.SetSettingMode(mode);
46     predicates = tmpPredicates;
47     return true;
48 }
49 
50 template<>
Marshalling(const BatchUpdateResult & result,MessageParcel & parcel)51 bool Marshalling(const BatchUpdateResult &result, MessageParcel &parcel)
52 {
53     return ITypesUtil::Marshal(parcel, result.uri, result.codes);
54 }
55 
56 template<>
Unmarshalling(BatchUpdateResult & result,MessageParcel & parcel)57 bool Unmarshalling(BatchUpdateResult &result, MessageParcel &parcel)
58 {
59     return ITypesUtil::Unmarshal(parcel, result.uri, result.codes);
60 }
61 
62 template<>
Marshalling(const UpdateOperation & operation,MessageParcel & parcel)63 bool Marshalling(const UpdateOperation &operation, MessageParcel &parcel)
64 {
65     return ITypesUtil::Marshal(parcel, operation.valuesBucket, operation.predicates);
66 }
67 
68 template<>
Unmarshalling(UpdateOperation & operation,MessageParcel & parcel)69 bool Unmarshalling(UpdateOperation &operation, MessageParcel &parcel)
70 {
71     return ITypesUtil::Unmarshal(parcel, operation.valuesBucket, operation.predicates);
72 }
73 
74 template<>
Marshalling(const Operation & operation,MessageParcel & parcel)75 bool Marshalling(const Operation &operation, MessageParcel &parcel)
76 {
77     return ITypesUtil::Marshal(parcel, operation.operation, operation.singleParams, operation.multiParams);
78 }
79 
80 template<>
Unmarshalling(Operation & operation,MessageParcel & parcel)81 bool Unmarshalling(Operation &operation, MessageParcel &parcel)
82 {
83     return ITypesUtil::Unmarshal(parcel, operation.operation, operation.singleParams, operation.multiParams);
84 }
85 
86 template<>
Unmarshalling(PublishedDataItem & dataItem,MessageParcel & parcel)87 bool Unmarshalling(PublishedDataItem &dataItem, MessageParcel &parcel)
88 {
89     return ITypesUtil::Unmarshal(parcel, dataItem.key_, dataItem.subscriberId_, dataItem.value_);
90 }
91 
92 template<>
Marshalling(const PublishedDataItem & dataItem,MessageParcel & parcel)93 bool Marshalling(const PublishedDataItem &dataItem, MessageParcel &parcel)
94 {
95     return ITypesUtil::Marshal(parcel, dataItem.key_, dataItem.subscriberId_, dataItem.value_);
96 }
97 
98 template<>
Marshalling(const Data & data,MessageParcel & parcel)99 bool Marshalling(const Data &data, MessageParcel &parcel)
100 {
101     return ITypesUtil::Marshal(parcel, data.datas_, data.version_);
102 }
103 
104 template<>
Unmarshalling(Data & data,MessageParcel & parcel)105 bool Unmarshalling(Data &data, MessageParcel &parcel)
106 {
107     return ITypesUtil::Unmarshal(parcel, data.datas_, data.version_);
108 }
109 
110 template<>
Unmarshalling(TemplateId & templateId,MessageParcel & parcel)111 bool Unmarshalling(TemplateId &templateId, MessageParcel &parcel)
112 {
113     return ITypesUtil::Unmarshal(parcel, templateId.subscriberId_, templateId.bundleName_);
114 }
115 
116 template<>
Marshalling(const TemplateId & templateId,MessageParcel & parcel)117 bool Marshalling(const TemplateId &templateId, MessageParcel &parcel)
118 {
119     return ITypesUtil::Marshal(parcel, templateId.subscriberId_, templateId.bundleName_);
120 }
121 
122 template<>
Marshalling(const PredicateTemplateNode & predicateTemplateNode,MessageParcel & parcel)123 bool Marshalling(const PredicateTemplateNode &predicateTemplateNode, MessageParcel &parcel)
124 {
125     return ITypesUtil::Marshal(parcel, predicateTemplateNode.key_, predicateTemplateNode.selectSql_);
126 }
127 
128 template<>
Unmarshalling(PredicateTemplateNode & predicateTemplateNode,MessageParcel & parcel)129 bool Unmarshalling(PredicateTemplateNode &predicateTemplateNode, MessageParcel &parcel)
130 {
131     return ITypesUtil::Unmarshal(parcel, predicateTemplateNode.key_, predicateTemplateNode.selectSql_);
132 }
133 
134 template<>
Marshalling(const RdbChangeNode & changeNode,MessageParcel & parcel)135 bool Marshalling(const RdbChangeNode &changeNode, MessageParcel &parcel)
136 {
137     return ITypesUtil::Marshal(parcel, changeNode.uri_, changeNode.templateId_, changeNode.data_);
138 }
139 
140 template<>
Unmarshalling(RdbChangeNode & changeNode,MessageParcel & parcel)141 bool Unmarshalling(RdbChangeNode &changeNode, MessageParcel &parcel)
142 {
143     return ITypesUtil::Unmarshal(parcel, changeNode.uri_, changeNode.templateId_, changeNode.data_);
144 }
145 
146 template<>
Marshalling(const PublishedDataChangeNode & changeNode,MessageParcel & parcel)147 bool Marshalling(const PublishedDataChangeNode &changeNode, MessageParcel &parcel)
148 {
149     return ITypesUtil::Marshal(parcel, changeNode.ownerBundleName_, changeNode.datas_);
150 }
151 
152 template<>
Unmarshalling(PublishedDataChangeNode & changeNode,MessageParcel & parcel)153 bool Unmarshalling(PublishedDataChangeNode &changeNode, MessageParcel &parcel)
154 {
155     return ITypesUtil::Unmarshal(parcel, changeNode.ownerBundleName_, changeNode.datas_);
156 }
157 
158 template<>
Marshalling(const OperationResult & operationResult,MessageParcel & parcel)159 bool Marshalling(const OperationResult &operationResult, MessageParcel &parcel)
160 {
161     return ITypesUtil::Marshal(parcel, operationResult.key_, operationResult.errCode_);
162 }
163 
164 template<>
Unmarshalling(OperationResult & predicateTemplateNode,MessageParcel & parcel)165 bool Unmarshalling(OperationResult &predicateTemplateNode, MessageParcel &parcel)
166 {
167     return ITypesUtil::Unmarshal(parcel, predicateTemplateNode.key_, predicateTemplateNode.errCode_);
168 }
169 
170 template<>
Unmarshalling(AshmemNode & node,MessageParcel & parcel)171 bool Unmarshalling(AshmemNode &node, MessageParcel &parcel)
172 {
173     node.isManaged = true;
174     node.ashmem = parcel.ReadAshmem();
175     return true;
176 }
177 
178 template<>
Marshalling(const AshmemNode & node,MessageParcel & parcel)179 bool Marshalling(const AshmemNode &node, MessageParcel &parcel)
180 {
181     return parcel.WriteAshmem(node.ashmem);
182 }
183 
184 template<>
Marshalling(const Uri & node,MessageParcel & parcel)185 bool Marshalling(const Uri &node, MessageParcel &parcel)
186 {
187     return parcel.WriteParcelable(&node);
188 }
189 
190 template<>
Unmarshalling(Uri & node,MessageParcel & parcel)191 bool Unmarshalling(Uri &node, MessageParcel &parcel)
192 {
193     auto uri = std::shared_ptr<Uri>(parcel.ReadParcelable<Uri>());
194     if (uri == nullptr) {
195         return false;
196     }
197     node = *uri;
198     return true;
199 }
200 
201 template<>
Marshalling(const DataShareValuesBucket & bucket,MessageParcel & parcel)202 bool Marshalling(const DataShareValuesBucket &bucket, MessageParcel &parcel)
203 {
204     return ITypesUtil::Marshal(parcel, bucket.valuesMap);
205 }
206 
207 template<>
Unmarshalling(DataShareValuesBucket & bucket,MessageParcel & parcel)208 bool Unmarshalling(DataShareValuesBucket &bucket, MessageParcel &parcel)
209 {
210     return ITypesUtil::Unmarshal(parcel, bucket.valuesMap);
211 }
212 
213 template<>
Marshalling(const OperationStatement & operationStatement,MessageParcel & parcel)214 bool Marshalling(const OperationStatement &operationStatement, MessageParcel &parcel)
215 {
216     return ITypesUtil::Marshal(parcel, static_cast<int32_t>(operationStatement.operationType),
217         operationStatement.uri, operationStatement.valuesBucket, operationStatement.predicates);
218 }
219 
220 template<>
Unmarshalling(OperationStatement & operationStatement,MessageParcel & parcel)221 bool Unmarshalling(OperationStatement &operationStatement, MessageParcel &parcel)
222 {
223     int type;
224     if (!ITypesUtil::Unmarshalling(type, parcel)) {
225         return false;
226     }
227     operationStatement.operationType = static_cast<DataShare::Operation>(type);
228     return ITypesUtil::Unmarshal(parcel, operationStatement.uri,
229         operationStatement.valuesBucket, operationStatement.predicates);
230 }
231 
232 template<>
Marshalling(const ExecResult & execResult,MessageParcel & parcel)233 bool Marshalling(const ExecResult &execResult, MessageParcel &parcel)
234 {
235     return ITypesUtil::Marshal(parcel, static_cast<int32_t>(execResult.operationType), execResult.code,
236         execResult.message);
237 }
238 
239 template<>
Unmarshalling(ExecResult & execResult,MessageParcel & parcel)240 bool Unmarshalling(ExecResult &execResult, MessageParcel &parcel)
241 {
242     int type;
243     if (!ITypesUtil::Unmarshalling(type, parcel)) {
244         return false;
245     }
246     execResult.operationType = static_cast<DataShare::Operation>(type);
247     return ITypesUtil::Unmarshal(parcel, execResult.code, execResult.message);
248 }
249 
250 template<>
Marshalling(const ExecResultSet & execResultSet,MessageParcel & parcel)251 bool Marshalling(const ExecResultSet &execResultSet, MessageParcel &parcel)
252 {
253     return ITypesUtil::Marshal(parcel, static_cast<int32_t>(execResultSet.errorCode), execResultSet.results);
254 }
255 
256 template<>
Unmarshalling(ExecResultSet & execResultSet,MessageParcel & parcel)257 bool Unmarshalling(ExecResultSet &execResultSet, MessageParcel &parcel)
258 {
259     int errorCode;
260     if (!ITypesUtil::Unmarshalling(errorCode, parcel)) {
261         return false;
262     }
263     execResultSet.errorCode = static_cast<DataShare::ExecErrorCode>(errorCode);
264     return ITypesUtil::Unmarshal(parcel, execResultSet.results);
265 }
266 } // namespace OHOS::ITypesUtil
267