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 "RdbResultSetStub"
17
18 #include <ipc_skeleton.h>
19 #include "itypes_util.h"
20 #include "log_print.h"
21 #include "rdb_result_set_stub.h"
22
23 namespace OHOS::DistributedRdb {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int RdbResultSetStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
25 {
26 ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
27 if (!CheckInterfaceToken(data)) {
28 return -1;
29 }
30 if (code >= 0 && code < CMD_MAX) {
31 return (this->*HANDLERS[code])(data, reply);
32 }
33 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
34 }
35
CheckInterfaceToken(MessageParcel & data)36 bool RdbResultSetStub::CheckInterfaceToken(MessageParcel& data)
37 {
38 auto localDescriptor = RdbResultSetStub::GetDescriptor();
39 auto remoteDescriptor = data.ReadInterfaceToken();
40 if (remoteDescriptor != localDescriptor) {
41 ZLOGE("interface token is not equal");
42 return false;
43 }
44 return true;
45 }
46
OnGetAllColumnNames(MessageParcel & data,MessageParcel & reply)47 int32_t RdbResultSetStub::OnGetAllColumnNames(MessageParcel &data, MessageParcel &reply)
48 {
49 std::vector<std::string> columnNames;
50 int status = GetAllColumnNames(columnNames);
51 if (status != 0) {
52 ZLOGE("failed, status: %{public}d", status);
53 if (!ITypesUtil::Marshal(reply, status)) {
54 ZLOGE("Write status failed.");
55 return -1;
56 }
57 return 0;
58 }
59 if (!ITypesUtil::Marshal(reply, status, columnNames)) {
60 ZLOGE("Write status or columnNames failed.");
61 return -1;
62 }
63 return 0;
64 }
65
OnGetColumnCount(MessageParcel & data,MessageParcel & reply)66 int32_t RdbResultSetStub::OnGetColumnCount(MessageParcel &data, MessageParcel &reply)
67 {
68 int columnCount = 0;
69 int status = GetColumnCount(columnCount);
70 if (status != 0) {
71 ZLOGE("failed, status: %{public}d", status);
72 if (!ITypesUtil::Marshal(reply, status)) {
73 ZLOGE("Write status failed.");
74 return -1;
75 }
76 return 0;
77 }
78 if (!ITypesUtil::Marshal(reply, status, columnCount)) {
79 ZLOGE("Write status or columnCount failed.");
80 return -1;
81 }
82 return 0;
83 }
84
OnGetColumnType(MessageParcel & data,MessageParcel & reply)85 int32_t RdbResultSetStub::OnGetColumnType(MessageParcel &data, MessageParcel &reply)
86 {
87 int columnIndex;
88 ITypesUtil::Unmarshal(data, columnIndex);
89 NativeRdb::ColumnType columnType;
90 int status = GetColumnType(columnIndex, columnType);
91 if (status != 0) {
92 ZLOGE("failed, status: %{public}d columnIndex: %{public}d", status, columnIndex);
93 if (!ITypesUtil::Marshal(reply, status)) {
94 ZLOGE("Write status failed.");
95 return -1;
96 }
97 return 0;
98 }
99 if (!ITypesUtil::Marshal(reply, status, static_cast<int32_t>(columnType))) {
100 ZLOGE("Write status or columnType failed.");
101 return -1;
102 }
103 return 0;
104 }
105
OnGetColumnIndex(MessageParcel & data,MessageParcel & reply)106 int32_t RdbResultSetStub::OnGetColumnIndex(MessageParcel &data, MessageParcel &reply)
107 {
108 std::string columnName;
109 ITypesUtil::Unmarshal(data, columnName);
110 int columnIndex;
111 int status = GetColumnIndex(columnName, columnIndex);
112 if (status != 0) {
113 ZLOGE("failed, status: %{public}d columnName: %{public}s.", status, columnName.c_str());
114 if (!ITypesUtil::Marshal(reply, status)) {
115 ZLOGE("Write status failed.");
116 return -1;
117 }
118 return 0;
119 }
120 if (!ITypesUtil::Marshal(reply, status, columnIndex)) {
121 ZLOGE("Write status or columnIndex failed.");
122 return -1;
123 }
124 return 0;
125 }
126
OnGetColumnName(MessageParcel & data,MessageParcel & reply)127 int32_t RdbResultSetStub::OnGetColumnName(MessageParcel &data, MessageParcel &reply)
128 {
129 int columnIndex;
130 ITypesUtil::Unmarshal(data, columnIndex);
131 std::string columnName;
132 int status = GetColumnName(columnIndex, columnName);
133 if (status != 0) {
134 ZLOGE("failed, status: %{public}d columnIndex: %{public}d.", status, columnIndex);
135 if (!ITypesUtil::Marshal(reply, status)) {
136 ZLOGE("Write status failed.");
137 return -1;
138 }
139 return 0;
140 }
141 if (!ITypesUtil::Marshal(reply, status, columnName)) {
142 ZLOGE("Write status or columnName failed.");
143 return -1;
144 }
145 return 0;
146 }
147
OnGetRowCount(MessageParcel & data,MessageParcel & reply)148 int32_t RdbResultSetStub::OnGetRowCount(MessageParcel &data, MessageParcel &reply)
149 {
150 int rowCount = 0;
151 int status = GetRowCount(rowCount);
152 if (status != 0) {
153 ZLOGE("failed, status: %{public}d", status);
154 if (!ITypesUtil::Marshal(reply, status)) {
155 ZLOGE("Write status failed.");
156 return -1;
157 }
158 return 0;
159 }
160 if (!ITypesUtil::Marshal(reply, status, rowCount)) {
161 ZLOGE("Write status or rowCount failed.");
162 return -1;
163 }
164 return 0;
165 }
166
OnGetRowIndex(MessageParcel & data,MessageParcel & reply)167 int32_t RdbResultSetStub::OnGetRowIndex(MessageParcel &data, MessageParcel &reply)
168 {
169 int rowIndex = 0;
170 int status = GetRowIndex(rowIndex);
171 if (status != 0) {
172 ZLOGE("failed, status: %{public}d", status);
173 if (!ITypesUtil::Marshal(reply, status)) {
174 ZLOGE("Write status failed.");
175 return -1;
176 }
177 return 0;
178 }
179 if (!ITypesUtil::Marshal(reply, status, rowIndex)) {
180 ZLOGE("Write status or rowIndex failed.");
181 return -1;
182 }
183 return 0;
184 }
185
OnGoTo(MessageParcel & data,MessageParcel & reply)186 int32_t RdbResultSetStub::OnGoTo(MessageParcel &data, MessageParcel &reply)
187 {
188 int offSet;
189 ITypesUtil::Unmarshal(data, offSet);
190 int status = GoTo(offSet);
191 if (status != 0) {
192 ZLOGE("failed, status: %{public}d offset: %{public}d.", status, offSet);
193 }
194
195 if (!ITypesUtil::Marshal(reply, status)) {
196 ZLOGE("Write status failed.");
197 return -1;
198 }
199 return 0;
200 }
201
OnGoToRow(MessageParcel & data,MessageParcel & reply)202 int32_t RdbResultSetStub::OnGoToRow(MessageParcel &data, MessageParcel &reply)
203 {
204 int position;
205 ITypesUtil::Unmarshal(data, position);
206 int status = GoToRow(position);
207 if (status != 0) {
208 ZLOGE("failed, status: %{public}d position: %{public}d.", status, position);
209 }
210
211 if (!ITypesUtil::Marshal(reply, status)) {
212 ZLOGE("Write status failed.");
213 return -1;
214 }
215 return 0;
216 }
217
OnGoToFirstRow(MessageParcel & data,MessageParcel & reply)218 int32_t RdbResultSetStub::OnGoToFirstRow(MessageParcel &data, MessageParcel &reply)
219 {
220 int status = GoToFirstRow();
221 if (status != 0) {
222 ZLOGE("failed, status: %{public}d", status);
223 }
224 if (!ITypesUtil::Marshal(reply, status)) {
225 ZLOGE("Write status failed.");
226 return -1;
227 }
228 return 0;
229 }
230
OnGoToLastRow(MessageParcel & data,MessageParcel & reply)231 int32_t RdbResultSetStub::OnGoToLastRow(MessageParcel &data, MessageParcel &reply)
232 {
233 int status = GoToLastRow();
234 if (status != 0) {
235 ZLOGE("failed, status: %{public}d", status);
236 }
237 if (!ITypesUtil::Marshal(reply, status)) {
238 ZLOGE("Write status failed.");
239 return -1;
240 }
241 return 0;
242 }
243
OnGoToNextRow(MessageParcel & data,MessageParcel & reply)244 int32_t RdbResultSetStub::OnGoToNextRow(MessageParcel &data, MessageParcel &reply)
245 {
246 int status = GoToNextRow();
247 if (status != 0) {
248 ZLOGE("failed, status: %{public}d", status);
249 }
250 if (!ITypesUtil::Marshal(reply, status)) {
251 ZLOGE("Write status failed.");
252 return -1;
253 }
254 return 0;
255 }
256
OnGoToPreviousRow(MessageParcel & data,MessageParcel & reply)257 int32_t RdbResultSetStub::OnGoToPreviousRow(MessageParcel &data, MessageParcel &reply)
258 {
259 int status = GoToPreviousRow();
260 if (status != 0) {
261 ZLOGE("failed, status: %{public}d", status);
262 }
263 if (!ITypesUtil::Marshal(reply, status)) {
264 ZLOGE("Write status failed.");
265 return -1;
266 }
267 return 0;
268 }
269
OnIsEnded(MessageParcel & data,MessageParcel & reply)270 int32_t RdbResultSetStub::OnIsEnded(MessageParcel &data, MessageParcel &reply)
271 {
272 bool isEnded = false;
273 int status = IsEnded(isEnded);
274 if (status != 0) {
275 ZLOGE("failed, status: %{public}d", status);
276 if (!ITypesUtil::Marshal(reply, status)) {
277 ZLOGE("Write status failed.");
278 return -1;
279 }
280 return 0;
281 }
282 if (!ITypesUtil::Marshal(reply, status, isEnded)) {
283 ZLOGE("Write status or isEnded failed.");
284 return -1;
285 }
286 return 0;
287 }
288
OnIsStarted(MessageParcel & data,MessageParcel & reply)289 int32_t RdbResultSetStub::OnIsStarted(MessageParcel &data, MessageParcel &reply)
290 {
291 bool isStarted = false;
292 int status = IsStarted(isStarted);
293 if (status != 0) {
294 ZLOGE("failed, status: %{public}d", status);
295 if (!ITypesUtil::Marshal(reply, status)) {
296 ZLOGE("Write status failed.");
297 return -1;
298 }
299 return 0;
300 }
301 if (!ITypesUtil::Marshal(reply, status, isStarted)) {
302 ZLOGE("Write status or isStarted failed.");
303 return -1;
304 }
305 return 0;
306 }
307
OnIsAtFirstRow(MessageParcel & data,MessageParcel & reply)308 int32_t RdbResultSetStub::OnIsAtFirstRow(MessageParcel &data, MessageParcel &reply)
309 {
310 bool isAtFirstRow = false;
311 int status = IsAtFirstRow(isAtFirstRow);
312 if (status != 0) {
313 ZLOGE("failed, status: %{public}d", status);
314 if (!ITypesUtil::Marshal(reply, status)) {
315 ZLOGE("Write status failed.");
316 return -1;
317 }
318 return 0;
319 }
320 if (!ITypesUtil::Marshal(reply, status, isAtFirstRow)) {
321 ZLOGE("Write status or isAtFirstRow failed.");
322 return -1;
323 }
324 return 0;
325 }
326
OnIsAtLastRow(MessageParcel & data,MessageParcel & reply)327 int32_t RdbResultSetStub::OnIsAtLastRow(MessageParcel &data, MessageParcel &reply)
328 {
329 bool isAtLastRow = false;
330 int status = IsAtLastRow(isAtLastRow);
331 if (status != 0) {
332 ZLOGE("failed, status: %{public}d", status);
333 if (!ITypesUtil::Marshal(reply, status)) {
334 ZLOGE("Write status failed.");
335 return -1;
336 }
337 return 0;
338 }
339 if (!ITypesUtil::Marshal(reply, status, isAtLastRow)) {
340 ZLOGE("Write status or isAtLastRow failed.");
341 return -1;
342 }
343 return 0;
344 }
345
OnGetBlob(MessageParcel & data,MessageParcel & reply)346 int32_t RdbResultSetStub::OnGetBlob(MessageParcel &data, MessageParcel &reply)
347 {
348 int columnIndex;
349 ITypesUtil::Unmarshal(data, columnIndex);
350 std::vector<uint8_t> blob;
351 int status = GetBlob(columnIndex, blob);
352 if (status != 0) {
353 ZLOGE("failed, status: %{public}d columnIndex: %{public}d.", status, columnIndex);
354 if (!ITypesUtil::Marshal(reply, status)) {
355 ZLOGE("Write status failed.");
356 return -1;
357 }
358 return 0;
359 }
360 if (!ITypesUtil::Marshal(reply, status, blob)) {
361 ZLOGE("Write status or blob failed.");
362 return -1;
363 }
364 return 0;
365 }
366
OnGetString(MessageParcel & data,MessageParcel & reply)367 int32_t RdbResultSetStub::OnGetString(MessageParcel &data, MessageParcel &reply)
368 {
369 int columnIndex;
370 ITypesUtil::Unmarshal(data, columnIndex);
371 std::string value;
372 int status = GetString(columnIndex, value);
373 if (status != 0) {
374 ZLOGE("failed, status: %{public}d columnIndex: %{public}d.", status, columnIndex);
375 if (!ITypesUtil::Marshal(reply, status)) {
376 ZLOGE("Write status failed.");
377 return -1;
378 }
379 return 0;
380 }
381 if (!ITypesUtil::Marshal(reply, status, value)) {
382 ZLOGE("Write status or string value failed.");
383 return -1;
384 }
385 return 0;
386 }
387
OnGetInt(MessageParcel & data,MessageParcel & reply)388 int32_t RdbResultSetStub::OnGetInt(MessageParcel &data, MessageParcel &reply)
389 {
390 int columnIndex;
391 ITypesUtil::Unmarshal(data, columnIndex);
392 int value;
393 int status = GetInt(columnIndex, value);
394 if (status != 0) {
395 ZLOGE("failed, status: %{public}d columnIndex: %{public}d.", status, columnIndex);
396 if (!ITypesUtil::Marshal(reply, status)) {
397 ZLOGE("Write status failed.");
398 return -1;
399 }
400 return 0;
401 }
402 if (!ITypesUtil::Marshal(reply, status, value)) {
403 ZLOGE("Write status or int value failed.");
404 return -1;
405 }
406 return 0;
407 }
408
OnGetLong(MessageParcel & data,MessageParcel & reply)409 int32_t RdbResultSetStub::OnGetLong(MessageParcel &data, MessageParcel &reply)
410 {
411 int columnIndex;
412 ITypesUtil::Unmarshal(data, columnIndex);
413 int64_t value;
414 int status = GetLong(columnIndex, value);
415 if (status != 0) {
416 ZLOGE("failed, status: %{public}d columnIndex: %{public}d.", status, columnIndex);
417 if (!ITypesUtil::Marshal(reply, status)) {
418 ZLOGE("Write status failed.");
419 return -1;
420 }
421 return 0;
422 }
423 if (!ITypesUtil::Marshal(reply, status, value)) {
424 ZLOGE("Write status or long value failed.");
425 return -1;
426 }
427 return 0;
428 }
429
OnGetDouble(MessageParcel & data,MessageParcel & reply)430 int32_t RdbResultSetStub::OnGetDouble(MessageParcel &data, MessageParcel &reply)
431 {
432 int columnIndex;
433 ITypesUtil::Unmarshal(data, columnIndex);
434 double value;
435 int status = GetDouble(columnIndex, value);
436 if (status != 0) {
437 ZLOGE("failed, status: %{public}d columnIndex: %{public}d.", status, columnIndex);
438 if (!ITypesUtil::Marshal(reply, status)) {
439 ZLOGE("Write status failed.");
440 return -1;
441 }
442 return 0;
443 }
444 if (!ITypesUtil::Marshal(reply, status, value)) {
445 ZLOGE("Write status or double value failed.");
446 return -1;
447 }
448 return 0;
449 }
450
OnIsColumnNull(MessageParcel & data,MessageParcel & reply)451 int32_t RdbResultSetStub::OnIsColumnNull(MessageParcel &data, MessageParcel &reply)
452 {
453 int columnIndex;
454 ITypesUtil::Unmarshal(data, columnIndex);
455 bool isColumnNull;
456 int status = IsColumnNull(columnIndex, isColumnNull);
457 if (status != 0) {
458 ZLOGE("failed, status: %{public}d columnIndex: %{public}d.", status, columnIndex);
459 if (!ITypesUtil::Marshal(reply, status)) {
460 ZLOGE("Write status failed.");
461 return -1;
462 }
463 return 0;
464 }
465 if (!ITypesUtil::Marshal(reply, status, isColumnNull)) {
466 ZLOGE("Write status or isColumnNull failed.");
467 return -1;
468 }
469 return 0;
470 }
471
OnIsClosed(MessageParcel & data,MessageParcel & reply)472 int32_t RdbResultSetStub::OnIsClosed(MessageParcel &data, MessageParcel &reply)
473 {
474 bool isClosed = IsClosed();
475 if (!ITypesUtil::Marshal(reply, isClosed)) {
476 ZLOGE("Write isClosed failed.");
477 return -1;
478 }
479 return 0;
480 }
481
OnClose(MessageParcel & data,MessageParcel & reply)482 int32_t RdbResultSetStub::OnClose(MessageParcel &data, MessageParcel &reply)
483 {
484 int status = Close();
485 if (!ITypesUtil::Marshal(reply, status)) {
486 ZLOGE("Write status failed.");
487 return -1;
488 }
489 return 0;
490 }
491 } // namespace OHOS::DistributedRdb