1 /*
2 * Copyright (c) 2021 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 "ishared_result_set_proxy.h"
17 #include "rdb_errno.h"
18 #include "iremote_proxy.h"
19 #include "logger.h"
20 namespace OHOS::NativeRdb {
21 std::function<std::shared_ptr<AbsSharedResultSet>(
22 MessageParcel &parcel)> ISharedResultSet::consumerCreator_ = ISharedResultSetProxy::CreateProxy;
23 BrokerDelegator<ISharedResultSetProxy> ISharedResultSetProxy::delegator_;
ISharedResultSetProxy(const sptr<OHOS::IRemoteObject> & impl)24 ISharedResultSetProxy::ISharedResultSetProxy(const sptr<OHOS::IRemoteObject> &impl)
25 : IRemoteProxy<ISharedResultSet>(impl)
26 {
27 }
28
CreateProxy(MessageParcel & parcel)29 std::shared_ptr<AbsSharedResultSet> ISharedResultSetProxy::CreateProxy(MessageParcel &parcel)
30 {
31 sptr<IRemoteObject> remoter = parcel.ReadRemoteObject();
32 if (remoter == nullptr) {
33 return nullptr;
34 }
35 sptr<ISharedResultSet> result = iface_cast<ISharedResultSet>(remoter);
36 result->Unmarshalling(parcel);
37 return std::shared_ptr<AbsSharedResultSet>(result.GetRefPtr(), [keep = result] (AbsSharedResultSet *) {});
38 }
39
GetAllColumnNames(std::vector<std::string> & columnNames)40 int ISharedResultSetProxy::GetAllColumnNames(std::vector<std::string> &columnNames)
41 {
42 LOG_DEBUG("GetAllColumnNames Begin");
43 if (!columnNames_.empty()) {
44 columnNames = columnNames_;
45 return E_OK;
46 }
47 MessageParcel request;
48 request.WriteInterfaceToken(GetDescriptor());
49 MessageParcel reply;
50 MessageOption msgOption;
51 int errCode = Remote()->SendRequest(FUNC_GET_ALL_COLUMN_NAMES, request, reply, msgOption);
52 if (errCode != 0) {
53 LOG_ERROR("GetAllColumnNames IPC Error %{public}x", errCode);
54 return -errCode;
55 }
56 errCode = reply.ReadInt32();
57 if (errCode != E_OK) {
58 LOG_ERROR("GetAllColumnNames Reply Error %{public}d", errCode);
59 return errCode;
60 }
61 if (!reply.ReadStringVector(&columnNames)) {
62 return E_INVALID_PARCEL;
63 }
64 columnNames_ = columnNames;
65 for (size_t i = 0; i < columnNames_.size(); i++) {
66 columnMap_[columnNames_[i]] = i;
67 }
68 return E_OK;
69 }
70
GetRowCount(int & count)71 int ISharedResultSetProxy::GetRowCount(int &count)
72 {
73 LOG_DEBUG("GetRowCount Begin");
74 if (rowCount_ >= 0) {
75 count = rowCount_;
76 return E_OK;
77 }
78 MessageParcel request;
79 request.WriteInterfaceToken(GetDescriptor());
80 MessageParcel reply;
81 MessageOption msgOption;
82 int errCode = Remote()->SendRequest(FUNC_GET_ROW_COUNT, request, reply, msgOption);
83 if (errCode != 0) {
84 LOG_ERROR("GetRowCount IPC Error %{public}x", errCode);
85 return -errCode;
86 }
87 errCode = reply.ReadInt32();
88 if (errCode != E_OK) {
89 LOG_ERROR("GetRowCount Reply Error %{public}d", errCode);
90 return errCode;
91 }
92 count = reply.ReadInt32();
93 LOG_DEBUG("GetRowCount count %{public}d", count);
94 rowCount_ = count;
95 return E_OK;
96 }
97
OnGo(int oldRowIndex,int newRowIndex)98 bool ISharedResultSetProxy::OnGo(int oldRowIndex, int newRowIndex)
99 {
100 LOG_DEBUG("OnGo Begin");
101 MessageParcel request;
102 request.WriteInterfaceToken(GetDescriptor());
103 request.WriteInt32(oldRowIndex);
104 request.WriteInt32(newRowIndex);
105 MessageParcel reply;
106 MessageOption msgOption;
107 int errCode = Remote()->SendRequest(FUNC_ON_GO, request, reply, msgOption);
108 if (errCode != 0) {
109 LOG_ERROR("OnGo IPC Error %{public}x", errCode);
110 return -errCode;
111 }
112 return reply.ReadBool();
113 }
114
Close()115 int ISharedResultSetProxy::Close()
116 {
117 LOG_DEBUG("Close Begin");
118 AbsSharedResultSet::Close();
119 MessageParcel request;
120 request.WriteInterfaceToken(GetDescriptor());
121 MessageParcel reply;
122 MessageOption msgOption;
123 int errCode = Remote()->SendRequest(FUNC_CLOSE, request, reply, msgOption);
124 if (errCode != 0) {
125 LOG_ERROR("Close IPC Error %{public}x", errCode);
126 return -errCode;
127 }
128 return reply.ReadInt32();
129 }
130 } // namespace OHOS::NativeRdb