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 Operation & operation,MessageParcel & parcel)51 bool Marshalling(const Operation &operation, MessageParcel &parcel)
52 {
53 return ITypesUtil::Marshal(parcel, operation.operation, operation.singleParams, operation.multiParams);
54 }
55
56 template<>
Unmarshalling(Operation & operation,MessageParcel & parcel)57 bool Unmarshalling(Operation &operation, MessageParcel &parcel)
58 {
59 return ITypesUtil::Unmarshal(parcel, operation.operation, operation.singleParams, operation.multiParams);
60 }
61
62 template<>
Unmarshalling(PublishedDataItem & dataItem,MessageParcel & parcel)63 bool Unmarshalling(PublishedDataItem &dataItem, MessageParcel &parcel)
64 {
65 return ITypesUtil::Unmarshal(parcel, dataItem.key_, dataItem.subscriberId_, dataItem.value_);
66 }
67
68 template<>
Marshalling(const PublishedDataItem & dataItem,MessageParcel & parcel)69 bool Marshalling(const PublishedDataItem &dataItem, MessageParcel &parcel)
70 {
71 return ITypesUtil::Marshal(parcel, dataItem.key_, dataItem.subscriberId_, dataItem.value_);
72 }
73
74 template<>
Marshalling(const Data & data,MessageParcel & parcel)75 bool Marshalling(const Data &data, MessageParcel &parcel)
76 {
77 return ITypesUtil::Marshal(parcel, data.datas_, data.version_);
78 }
79
80 template<>
Unmarshalling(Data & data,MessageParcel & parcel)81 bool Unmarshalling(Data &data, MessageParcel &parcel)
82 {
83 return ITypesUtil::Unmarshal(parcel, data.datas_, data.version_);
84 }
85
86 template<>
Unmarshalling(TemplateId & templateId,MessageParcel & parcel)87 bool Unmarshalling(TemplateId &templateId, MessageParcel &parcel)
88 {
89 return ITypesUtil::Unmarshal(parcel, templateId.subscriberId_, templateId.bundleName_);
90 }
91
92 template<>
Marshalling(const TemplateId & templateId,MessageParcel & parcel)93 bool Marshalling(const TemplateId &templateId, MessageParcel &parcel)
94 {
95 return ITypesUtil::Marshal(parcel, templateId.subscriberId_, templateId.bundleName_);
96 }
97
98 template<>
Marshalling(const PredicateTemplateNode & predicateTemplateNode,MessageParcel & parcel)99 bool Marshalling(const PredicateTemplateNode &predicateTemplateNode, MessageParcel &parcel)
100 {
101 return ITypesUtil::Marshal(parcel, predicateTemplateNode.key_, predicateTemplateNode.selectSql_);
102 }
103
104 template<>
Unmarshalling(PredicateTemplateNode & predicateTemplateNode,MessageParcel & parcel)105 bool Unmarshalling(PredicateTemplateNode &predicateTemplateNode, MessageParcel &parcel)
106 {
107 return ITypesUtil::Unmarshal(parcel, predicateTemplateNode.key_, predicateTemplateNode.selectSql_);
108 }
109
110 template<>
Marshalling(const RdbChangeNode & changeNode,MessageParcel & parcel)111 bool Marshalling(const RdbChangeNode &changeNode, MessageParcel &parcel)
112 {
113 return ITypesUtil::Marshal(parcel, changeNode.uri_, changeNode.templateId_, changeNode.data_);
114 }
115
116 template<>
Unmarshalling(RdbChangeNode & changeNode,MessageParcel & parcel)117 bool Unmarshalling(RdbChangeNode &changeNode, MessageParcel &parcel)
118 {
119 return ITypesUtil::Unmarshal(parcel, changeNode.uri_, changeNode.templateId_, changeNode.data_);
120 }
121
122 template<>
Marshalling(const PublishedDataChangeNode & changeNode,MessageParcel & parcel)123 bool Marshalling(const PublishedDataChangeNode &changeNode, MessageParcel &parcel)
124 {
125 return ITypesUtil::Marshal(parcel, changeNode.ownerBundleName_, changeNode.datas_);
126 }
127
128 template<>
Unmarshalling(PublishedDataChangeNode & changeNode,MessageParcel & parcel)129 bool Unmarshalling(PublishedDataChangeNode &changeNode, MessageParcel &parcel)
130 {
131 return ITypesUtil::Unmarshal(parcel, changeNode.ownerBundleName_, changeNode.datas_);
132 }
133
134 template<>
Marshalling(const OperationResult & operationResult,MessageParcel & parcel)135 bool Marshalling(const OperationResult &operationResult, MessageParcel &parcel)
136 {
137 return ITypesUtil::Marshal(parcel, operationResult.key_, operationResult.errCode_);
138 }
139
140 template<>
Unmarshalling(OperationResult & predicateTemplateNode,MessageParcel & parcel)141 bool Unmarshalling(OperationResult &predicateTemplateNode, MessageParcel &parcel)
142 {
143 return ITypesUtil::Unmarshal(parcel, predicateTemplateNode.key_, predicateTemplateNode.errCode_);
144 }
145
146 template<>
Unmarshalling(AshmemNode & node,MessageParcel & parcel)147 bool Unmarshalling(AshmemNode &node, MessageParcel &parcel)
148 {
149 node.isManaged = true;
150 node.ashmem = parcel.ReadAshmem();
151 return true;
152 }
153
154 template<>
Marshalling(const AshmemNode & node,MessageParcel & parcel)155 bool Marshalling(const AshmemNode &node, MessageParcel &parcel)
156 {
157 return parcel.WriteAshmem(node.ashmem);
158 }
159
160 template<>
Marshalling(const Uri & node,MessageParcel & parcel)161 bool Marshalling(const Uri &node, MessageParcel &parcel)
162 {
163 return parcel.WriteParcelable(&node);
164 }
165
166 template<>
Unmarshalling(Uri & node,MessageParcel & parcel)167 bool Unmarshalling(Uri &node, MessageParcel &parcel)
168 {
169 auto uri = std::shared_ptr<Uri>(parcel.ReadParcelable<Uri>());
170 if (uri == nullptr) {
171 return false;
172 }
173 node = *uri;
174 return true;
175 }
176
177 template<>
Marshalling(const DataShareValuesBucket & bucket,MessageParcel & parcel)178 bool Marshalling(const DataShareValuesBucket &bucket, MessageParcel &parcel)
179 {
180 return ITypesUtil::Marshal(parcel, bucket.valuesMap);
181 }
182
183 template<>
Unmarshalling(DataShareValuesBucket & bucket,MessageParcel & parcel)184 bool Unmarshalling(DataShareValuesBucket &bucket, MessageParcel &parcel)
185 {
186 return ITypesUtil::Unmarshal(parcel, bucket.valuesMap);
187 }
188
189 template<>
Marshalling(const OperationStatement & operationStatement,MessageParcel & parcel)190 bool Marshalling(const OperationStatement &operationStatement, MessageParcel &parcel)
191 {
192 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(operationStatement.operationType),
193 operationStatement.uri, operationStatement.valuesBucket, operationStatement.predicates);
194 }
195
196 template<>
Unmarshalling(OperationStatement & operationStatement,MessageParcel & parcel)197 bool Unmarshalling(OperationStatement &operationStatement, MessageParcel &parcel)
198 {
199 int type;
200 if (!ITypesUtil::Unmarshalling(type, parcel)) {
201 return false;
202 }
203 operationStatement.operationType = static_cast<DataShare::Operation>(type);
204 return ITypesUtil::Unmarshal(parcel, operationStatement.uri,
205 operationStatement.valuesBucket, operationStatement.predicates);
206 }
207
208 template<>
Marshalling(const ExecResult & execResult,MessageParcel & parcel)209 bool Marshalling(const ExecResult &execResult, MessageParcel &parcel)
210 {
211 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(execResult.operationType), execResult.code,
212 execResult.message);
213 }
214
215 template<>
Unmarshalling(ExecResult & execResult,MessageParcel & parcel)216 bool Unmarshalling(ExecResult &execResult, MessageParcel &parcel)
217 {
218 int type;
219 if (!ITypesUtil::Unmarshalling(type, parcel)) {
220 return false;
221 }
222 execResult.operationType = static_cast<DataShare::Operation>(type);
223 return ITypesUtil::Unmarshal(parcel, execResult.code, execResult.message);
224 }
225
226 template<>
Marshalling(const ExecResultSet & execResultSet,MessageParcel & parcel)227 bool Marshalling(const ExecResultSet &execResultSet, MessageParcel &parcel)
228 {
229 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(execResultSet.errorCode), execResultSet.results);
230 }
231
232 template<>
Unmarshalling(ExecResultSet & execResultSet,MessageParcel & parcel)233 bool Unmarshalling(ExecResultSet &execResultSet, MessageParcel &parcel)
234 {
235 int errorCode;
236 if (!ITypesUtil::Unmarshalling(errorCode, parcel)) {
237 return false;
238 }
239 execResultSet.errorCode = static_cast<DataShare::ExecErrorCode>(errorCode);
240 return ITypesUtil::Unmarshal(parcel, execResultSet.results);
241 }
242 } // namespace OHOS::ITypesUtil
243