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